Skip to content

A fast 2D and 3D game engine — TypeScript SDK, C++/WebAssembly core, visual editor. Ship one project to web, desktop, WeChat MiniGames, playable ads, and native Android / iOS.

perf(ecs): iterate transforms without the per-frame entity-list snapshot

esengineb15fe6d5476a
diff --git a/src/esengine/ecs/Registry.hpp b/src/esengine/ecs/Registry.hpp
index 7c8c8ffca..1c0ca396a 100644
--- a/src/esengine/ecs/Registry.hpp
+++ b/src/esengine/ecs/Registry.hpp
@@ -493,6 +493,15 @@ public:
view<Components...>().each(std::forward<Func>(func));
}
+ /**
+ * @brief each() without the per-call snapshot copy, for read-only passes.
+ * @warning The callback must not emplace/remove the iterated component(s).
+ */
+ template<typename... Components, typename Func>
+ void eachLive(Func&& func) {
+ view<Components...>().eachLive(std::forward<Func>(func));
+ }
+
/**
* @brief Sorts components by a comparison function
* @tparam T The component type to sort
diff --git a/src/esengine/ecs/TransformSystem.hpp b/src/esengine/ecs/TransformSystem.hpp
index ae77bf68b..ac987cf70 100644
--- a/src/esengine/ecs/TransformSystem.hpp
+++ b/src/esengine/ecs/TransformSystem.hpp
@@ -46,7 +46,10 @@ private:
void updateDirtyTransforms(Registry& registry) {
dirty_to_clear_.clear();
- registry.each<Transform>([&registry, this](Entity entity, Transform& transform) {
+ // eachLive (no per-frame snapshot copy of the entity list): the callback
+ // only reads/writes Transform fields and defers TransformDirty removal to
+ // dirty_to_clear_, so it never structurally mutates the Transform pool.
+ registry.eachLive<Transform>([&registry, this](Entity entity, Transform& transform) {
if (!registry.has<Parent>(entity)) {
bool isStatic = registry.has<TransformStatic>(entity);
bool isDirty = registry.has<TransformDirty>(entity);
diff --git a/src/esengine/ecs/View.hpp b/src/esengine/ecs/View.hpp
index 1aeb7a1f3..6b7bb9aa2 100644
--- a/src/esengine/ecs/View.hpp
+++ b/src/esengine/ecs/View.hpp
@@ -446,6 +446,29 @@ public:
}
}
+ /**
+ * @brief Like each(), but iterates the live dense array with no defensive copy.
+ * @warning The callback MUST NOT emplace/remove T during iteration — that would
+ * invalidate the dense array. Use for read-only per-frame system passes
+ * (the common hot path); use each() when the callback may mutate the pool.
+ */
+ template<typename Func>
+ void eachLive(Func&& func) {
+ if (!pool_) return;
+
+ const std::vector<Entity>& dense = pool_->entities();
+ for (usize i = 0; i < dense.size(); ++i) {
+ Entity entity = dense[i];
+ if constexpr (std::is_invocable_v<Func, Entity, T&>) {
+ func(entity, pool_->getUnchecked(entity));
+ } else if constexpr (std::is_invocable_v<Func, T&>) {
+ func(pool_->getUnchecked(entity));
+ } else {
+ func(entity);
+ }
+ }
+ }
+
private:
/** @brief Pointer to the component pool */
SparseSet<T>* pool_;
diff --git a/tests/ecs/test_registry.cpp b/tests/ecs/test_registry.cpp
index 78582256d..eabc5ef09 100644
--- a/tests/ecs/test_registry.cpp
+++ b/tests/ecs/test_registry.cpp
@@ -204,6 +204,30 @@ TEST_CASE("view_each") {
CHECK_EQ(totalX, 44.0f);
}
+TEST_CASE("eachLive visits every entity and allows in-place mutation") {
+ esengine::ecs::Registry registry;
+
+ esengine::Entity e1 = registry.create();
+ esengine::Entity e2 = registry.create();
+ esengine::Entity e3 = registry.create();
+ registry.emplace<test::Position>(e1, 1.0f, 0.0f);
+ registry.emplace<test::Position>(e2, 2.0f, 0.0f);
+ registry.emplace<test::Position>(e3, 3.0f, 0.0f);
+
+ int count = 0;
+ float sum = 0.0f;
+ registry.eachLive<test::Position>([&](esengine::Entity, test::Position& p) {
+ count++;
+ sum += p.x;
+ p.x += 10.0f; // read + in-place write is fine (not a structural change)
+ });
+
+ CHECK_EQ(count, 3);
+ CHECK_EQ(sum, 6.0f);
+ CHECK_EQ(registry.get<test::Position>(e1).x, 11.0f);
+ CHECK_EQ(registry.get<test::Position>(e3).x, 13.0f);
+}
+
TEST_CASE("has_all_any") {
esengine::ecs::Registry registry;
esengine::Entity entity = registry.create();