Improve ecs query performance;

This commit is contained in:
2025-10-12 19:49:05 +09:00
parent 682200cbf1
commit 6d1b510ac1
27 changed files with 1546 additions and 992 deletions

View File

@@ -1,4 +1,5 @@
using Ghost.Entities.Components;
using Ghost.Entities.Query;
using Ghost.Entities.Systems;
using Ghost.Test.Core;
using System.Numerics;
@@ -7,50 +8,62 @@ namespace Ghost.Entities.Test;
public partial class EntityTest : ITest
{
private World _world = null!;
public void Setup()
{
_world = World.Create();
}
public void Run()
{
var world = World.Create();
var entity1 = _world.EntityManager.CreateEntity();
var entity2 = _world.EntityManager.CreateEntity();
var entity3 = _world.EntityManager.CreateEntity();
var entity1 = world.EntityManager.CreateEntity();
var entity2 = world.EntityManager.CreateEntity();
var entity3 = world.EntityManager.CreateEntity();
_world.EntityManager.AddComponent(entity1, new Transform { position = new Vector3(1, 2, 3) });
_world.EntityManager.AddComponent(entity1, new Mesh { index = 42 });
_world.EntityManager.AddScript<UIManager>(entity1);
_world.EntityManager.AddScript<EventManager>(entity1);
world.EntityManager.AddComponent(entity1, new Transform { position = new Vector3(1, 2, 3) });
world.EntityManager.AddComponent(entity1, new Mesh { index = 42 });
world.EntityManager.AddScript<UIManager>(entity1);
world.EntityManager.AddScript<EventManager>(entity1);
_world.EntityManager.AddComponent(entity2, new Transform { position = new Vector3(4, 5, 6) });
_world.EntityManager.AddComponent(entity2, new Mesh { index = 43 });
_world.EntityManager.AddScript<UserScript>(entity2);
world.EntityManager.AddComponent(entity2, new Transform { position = new Vector3(4, 5, 6) });
world.EntityManager.AddComponent(entity2, new Mesh { index = 43 });
world.EntityManager.AddScript<UserScript>(entity2);
_world.EntityManager.AddComponent(entity3, new Transform { position = new Vector3(7, 8, 9) });
_world.EntityManager.AddScript<EventManager>(entity3);
world.EntityManager.AddComponent(entity3, new Transform { position = new Vector3(7, 8, 9) });
world.EntityManager.AddScript<EventManager>(entity3);
foreach (var (_, transform) in world.Query<Transform>())
foreach (var (_, transform) in _world.Query<Transform>())
{
transform.ValueRW.position += new Vector3(1, 1, 1);
}
foreach (var (_, mesh) in world.Query<Mesh>())
var filter = new QueryBuilder()
.WithAll<Mesh>()
.Build();
foreach (var (_, mesh) in _world.QueryFilter<Mesh>(in filter))
{
mesh.ValueRW.index += 1;
}
world.EntityManager.RemoveEntity(ref entity2);
_world.EntityManager.RemoveEntity(ref entity2);
var entity4 = world.EntityManager.CreateEntity();
world.EntityManager.AddComponent(entity4, new Transform { position = new Vector3(10, 11, 12) });
world.EntityManager.AddComponent(entity4, new Mesh { index = 44 });
world.EntityManager.AddScript<UserScript>(entity4);
var entity4 = _world.EntityManager.CreateEntity();
_world.EntityManager.AddComponent(entity4, new Transform { position = new Vector3(10, 11, 12) });
_world.EntityManager.AddComponent(entity4, new Mesh { index = 45 });
_world.EntityManager.AddScript<UserScript>(entity4);
world.SystemStorage.AddSystem<TestSystem2>();
world.SystemStorage.AddSystem<TestSystem>();
_world.SystemStorage.AddSystem<TestSystem2>();
_world.SystemStorage.AddSystem<TestSystem>();
world.SystemStorage.CreateSystems();
world.SystemStorage.UpdateSystems();
_world.SystemStorage.CreateSystems();
_world.SystemStorage.UpdateSystems();
}
world.Dispose();
public void Cleanup()
{
_world.Dispose();
}
}
@@ -62,9 +75,9 @@ public class TestSystem : ISystem
public void OnUpdate(in SystemState state)
{
foreach (var (entity, transform) in state.World.Query<Transform>().WithAbsent<Mesh>())
foreach (var (entity, transform) in state.World.Query<Transform>())
{
Console.WriteLine($"Entity {entity.ID}: Transform Position = {transform.ValueRO.position}");
Console.WriteLine($"Entity {entity}: Transform Position = {transform.ValueRO.position}");
}
}
@@ -84,7 +97,7 @@ public class TestSystem2 : ISystem
{
foreach (var (entity, mesh) in state.World.Query<Mesh>())
{
Console.WriteLine($"Entity {entity.ID}: Mesh Index = {mesh.ValueRO.index}");
Console.WriteLine($"Entity {entity}: Mesh Index = {mesh.ValueRO.index}");
}
}
@@ -109,19 +122,35 @@ public class UserScript : ScriptComponent
{
public override int ExecutionOrder => -1;
public override void OnEnable()
{
Console.WriteLine("UserScript enabled for entity: " + Owner);
EntityManager.GetComponent<Transform>(Owner).ValueRW.position += new Vector3(10, 10, 10);
}
override public void OnDisable()
{
Console.WriteLine("UserScript disabled for entity: " + Owner);
}
public override void Initialize()
{
Console.WriteLine("UserScript initialized for entity: " + Owner);
}
public override void Start()
{
Console.WriteLine("UserScript started for entity: " + Owner.ID);
Console.WriteLine("UserScript started for entity: " + Owner);
}
public override void Update()
{
Console.WriteLine("UserScript updating for entity: " + Owner.ID);
Console.WriteLine("UserScript updating for entity: " + Owner);
}
public override void OnDestroy()
{
Console.WriteLine("UserScript destroyed for entity: " + Owner.ID);
Console.WriteLine("UserScript destroyed for entity: " + Owner);
}
}
@@ -129,17 +158,17 @@ public class UIManager : ScriptComponent
{
public override void Start()
{
Console.WriteLine("UIManager started for entity: " + Owner.ID);
Console.WriteLine("UIManager started for entity: " + Owner);
}
public override void Update()
{
Console.WriteLine("UIManager updating for entity: " + Owner.ID);
Console.WriteLine("UIManager updating for entity: " + Owner);
}
public override void OnDestroy()
{
Console.WriteLine("UIManager destroyed for entity: " + Owner.ID);
Console.WriteLine("UIManager destroyed for entity: " + Owner);
}
}
@@ -147,16 +176,16 @@ public class EventManager : ScriptComponent
{
public override void Start()
{
Console.WriteLine("EventManager started for entity: " + Owner.ID);
Console.WriteLine("EventManager started for entity: " + Owner);
}
public override void Update()
{
Console.WriteLine("EventManager updating for entity: " + Owner.ID);
Console.WriteLine("EventManager updating for entity: " + Owner);
}
public override void OnDestroy()
{
Console.WriteLine("EventManager destroyed for entity: " + Owner.ID);
Console.WriteLine("EventManager destroyed for entity: " + Owner);
}
}