diff --git a/src/ECS/Entity.cs b/src/ECS/Entity.cs index c9789925..0ff0abd9 100644 --- a/src/ECS/Entity.cs +++ b/src/ECS/Entity.cs @@ -682,6 +682,12 @@ internal Entity(EntityStore entityStore, int id) { Id = id; Revision = store.nodes[id].revision; } + + internal Entity(EntityStore entityStore, int id, short revision) { + store = entityStore; + Id = id; + Revision = revision; + } /// /// Returns an to add/remove components or tags to/from this entity using the batch.
diff --git a/src/ECS/EntityStore.cs b/src/ECS/EntityStore.cs index e25cb7e0..1b28634c 100644 --- a/src/ECS/EntityStore.cs +++ b/src/ECS/EntityStore.cs @@ -204,8 +204,9 @@ public ref readonly EntityNode GetEntityNode(int id) { ///
/// In case passed invalid (id >= ). public Entity GetEntityById(int id) { - if (0 <= id && id < nodes.Length) { - return new Entity(this, id); + var localNodes = nodes; + if (0 <= id && id < localNodes.Length) { + return new Entity(this, id, localNodes[id].revision); } throw IdOutOfRangeException(this, id); } diff --git a/src/Tests/ECS/Entity/Test_Entity.cs b/src/Tests/ECS/Entity/Test_Entity.cs index fbca9f15..9e31e57e 100644 --- a/src/Tests/ECS/Entity/Test_Entity.cs +++ b/src/Tests/ECS/Entity/Test_Entity.cs @@ -161,6 +161,20 @@ public static void Assert_GetEntityById() AreEqual("id: 4. expect in [0, current max id: 3]", e!.Message); } + [Test] + public static void Assert_GetEntityById_Perf() { + var count = 10; // 10_000_000_000L; + // Assert_GetEntityById_Perf() - count: 10000000000, duration: 3238 + var store = new EntityStore(); + store.CreateEntity(2); + var sw = new Stopwatch(); + sw.Start(); + for (long n = 0; n < count; n++) { + store.GetEntityById(2); + } + Console.WriteLine($"Assert_GetEntityById_Perf() - count: {count}, duration: {sw.ElapsedMilliseconds}"); + } + [Test] public static void Assert_TryGetEntityById() { @@ -187,7 +201,7 @@ public static void Assert_TryGetEntityById() IsFalse(store.TryGetEntityById(4, out entity)); IsTrue (entity.IsNull); } - + [Test] public static void Test_EntityStore_CloneEntity() {