rework world

to remove abstraction, to improve performance.
This commit is contained in:
Mikhail 2023-04-20 20:03:26 +08:00
parent df833a5b39
commit f2d43146d6
9 changed files with 60 additions and 55 deletions

View File

@ -63,23 +63,23 @@
public interface IEcsWorldCreate : IEcsSystem public interface IEcsWorldCreate : IEcsSystem
{ {
public void OnWorldCreate(IEcsWorld world); public void OnWorldCreate(EcsWorld world);
} }
public interface IEcsWorldDestroy : IEcsSystem public interface IEcsWorldDestroy : IEcsSystem
{ {
public void OnWorldDestroy(IEcsWorld world); public void OnWorldDestroy(EcsWorld world);
} }
public interface IEcsWorldLifecycle : IEcsWorldCreate, IEcsWorldDestroy { } public interface IEcsWorldLifecycle : IEcsWorldCreate, IEcsWorldDestroy { }
public sealed class EcsWorldCreateRunner : EcsRunner<IEcsWorldCreate>, IEcsWorldCreate public sealed class EcsWorldCreateRunner : EcsRunner<IEcsWorldCreate>, IEcsWorldCreate
{ {
public void OnWorldCreate(IEcsWorld world) public void OnWorldCreate(EcsWorld world)
{ {
foreach (var item in targets) item.OnWorldCreate(world); foreach (var item in targets) item.OnWorldCreate(world);
} }
} }
public sealed class EcsWorldDestryRunner : EcsRunner<IEcsWorldDestroy>, IEcsWorldDestroy public sealed class EcsWorldDestryRunner : EcsRunner<IEcsWorldDestroy>, IEcsWorldDestroy
{ {
public void OnWorldDestroy(IEcsWorld world) public void OnWorldDestroy(EcsWorld world)
{ {
foreach (var item in targets) item.OnWorldDestroy(world); foreach (var item in targets) item.OnWorldDestroy(world);
} }

View File

@ -13,6 +13,6 @@
} }
public sealed class EcsEventWrold : EcsEdgeWorld<EcsEventWrold> public sealed class EcsEventWrold : EcsEdgeWorld<EcsEventWrold>
{ {
public EcsEventWrold(IEcsWorld firstTarget, IEcsWorld secondTarget, EcsPipeline pipeline = null) : base(firstTarget, secondTarget, pipeline) { } public EcsEventWrold(EcsWorld firstTarget, EcsWorld secondTarget, EcsPipeline pipeline = null) : base(firstTarget, secondTarget, pipeline) { }
} }
} }

View File

@ -8,14 +8,14 @@ namespace DCFApixels.DragonECS
{ {
public class EcsEdgeWorld<TWorldArchetype> : EcsWorld<TWorldArchetype> where TWorldArchetype : EcsWorld<TWorldArchetype> public class EcsEdgeWorld<TWorldArchetype> : EcsWorld<TWorldArchetype> where TWorldArchetype : EcsWorld<TWorldArchetype>
{ {
private IEcsWorld _firstTarget; private EcsWorld _firstTarget;
private IEcsWorld _secondTarget; private EcsWorld _secondTarget;
public EcsEdgeWorld(IEcsWorld firstTarget, IEcsWorld secondTarget, EcsPipeline pipeline) : base(pipeline) public EcsEdgeWorld(EcsWorld firstTarget, EcsWorld secondTarget, EcsPipeline pipeline) : base(pipeline)
{ {
_firstTarget = firstTarget; _firstTarget = firstTarget;
_secondTarget = secondTarget; _secondTarget = secondTarget;
} }
public EcsEdgeWorld(IEcsWorld firstTarget, EcsPipeline pipeline) : base(pipeline) public EcsEdgeWorld(EcsWorld firstTarget, EcsPipeline pipeline) : base(pipeline)
{ {
_firstTarget = firstTarget; _firstTarget = firstTarget;
} }

View File

@ -86,7 +86,7 @@ namespace DCFApixels.DragonECS
private const int DEALAYED_ADD = 0; private const int DEALAYED_ADD = 0;
private const int DEALAYED_REMOVE = int.MinValue; private const int DEALAYED_REMOVE = int.MinValue;
private IEcsWorld _source; private EcsWorld _source;
private int[] _dense; private int[] _dense;
private int[] _sparse; private int[] _sparse;
@ -101,7 +101,7 @@ namespace DCFApixels.DragonECS
private bool _isReleazed = true; private bool _isReleazed = true;
#region Properties #region Properties
public IEcsWorld World => _source; public EcsWorld World => _source;
public int Count public int Count
{ {
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
@ -131,12 +131,12 @@ namespace DCFApixels.DragonECS
#region Constrcutors/Finalizer #region Constrcutors/Finalizer
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public static EcsGroup New(IEcsWorld world) public static EcsGroup New(EcsWorld world)
{ {
return world.GetGroupFromPool(); return world.GetGroupFromPool();
} }
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
internal EcsGroup(IEcsWorld world, int denseCapacity = 64, int delayedOpsCapacity = 128) internal EcsGroup(EcsWorld world, int denseCapacity = 64, int delayedOpsCapacity = 128)
{ {
_source = world; _source = world;
_source.RegisterGroup(this); _source.RegisterGroup(this);

View File

@ -8,7 +8,7 @@ namespace DCFApixels.DragonECS
{ {
#region Properties #region Properties
public Type ComponentType { get; } public Type ComponentType { get; }
public IEcsWorld World { get; } public EcsWorld World { get; }
public int Count { get; } public int Count { get; }
public int Capacity { get; } public int Capacity { get; }
#endregion #endregion
@ -34,14 +34,14 @@ namespace DCFApixels.DragonECS
public sealed class EcsNullPool : IEcsPool<NullComponent> public sealed class EcsNullPool : IEcsPool<NullComponent>
{ {
public static EcsNullPool instance => new EcsNullPool(null); public static EcsNullPool instance => new EcsNullPool(null);
private IEcsWorld _source; private EcsWorld _source;
private NullComponent fakeComponent; private NullComponent fakeComponent;
private EcsNullPool(IEcsWorld source) => _source = source; private EcsNullPool(EcsWorld source) => _source = source;
#region Properties #region Properties
public Type ComponentType => typeof(NullComponent); public Type ComponentType => typeof(NullComponent);
public int ComponentID => -1; public int ComponentID => -1;
public IEcsWorld World => _source; public EcsWorld World => _source;
public int Count => 0; public int Count => 0;
public int Capacity => 1; public int Capacity => 1;
#endregion #endregion
@ -61,12 +61,12 @@ namespace DCFApixels.DragonECS
public sealed class EcsPool<T> : IEcsPool<T> public sealed class EcsPool<T> : IEcsPool<T>
where T : struct where T : struct
{ {
public static EcsPool<T> Builder(IEcsWorld source) public static EcsPool<T> Builder(EcsWorld source)
{ {
return new EcsPool<T>(source, 512, default); return new EcsPool<T>(source, 512, default);
} }
private readonly IEcsWorld _source; private readonly EcsWorld _source;
private int[] _mapping;// index = entityID / value = itemIndex;/ value = 0 = no entityID private int[] _mapping;// index = entityID / value = itemIndex;/ value = 0 = no entityID
private T[] _items; //dense private T[] _items; //dense
@ -80,12 +80,12 @@ namespace DCFApixels.DragonECS
#region Properites #region Properites
public int Count => _itemsCount; public int Count => _itemsCount;
public int Capacity => _items.Length; public int Capacity => _items.Length;
public IEcsWorld World => _source; public EcsWorld World => _source;
public Type ComponentType => typeof(T); public Type ComponentType => typeof(T);
#endregion #endregion
#region Constructors #region Constructors
internal EcsPool(IEcsWorld source, int capacity, PoolRunners poolRunnres) internal EcsPool(EcsWorld source, int capacity, PoolRunners poolRunnres)
{ {
_source = source; _source = source;

View File

@ -7,10 +7,10 @@ namespace DCFApixels.DragonECS
{ {
public abstract class EcsQueryBase public abstract class EcsQueryBase
{ {
internal IEcsWorld source; internal EcsWorld source;
internal EcsGroup groupFilter; internal EcsGroup groupFilter;
internal EcsQueryMask mask; internal EcsQueryMask mask;
public IEcsWorld World => source; public EcsWorld World => source;
#region Builder #region Builder
protected virtual void Init(Builder b) { } protected virtual void Init(Builder b) { }
@ -18,17 +18,17 @@ namespace DCFApixels.DragonECS
public abstract void Execute(); public abstract void Execute();
public sealed class Builder : EcsQueryBuilderBase public sealed class Builder : EcsQueryBuilderBase
{ {
private IEcsWorld _world; private EcsWorld _world;
private List<int> _inc; private List<int> _inc;
private List<int> _exc; private List<int> _exc;
private Builder(IEcsWorld world) private Builder(EcsWorld world)
{ {
_world = world; _world = world;
_inc = new List<int>(8); _inc = new List<int>(8);
_exc = new List<int>(4); _exc = new List<int>(4);
} }
internal static TQuery Build<TQuery>(IEcsWorld world) where TQuery : EcsQueryBase internal static TQuery Build<TQuery>(EcsWorld world) where TQuery : EcsQueryBase
{ {
Builder builder = new Builder(world); Builder builder = new Builder(world);
Type queryType = typeof(TQuery); Type queryType = typeof(TQuery);

View File

@ -20,30 +20,14 @@ namespace DCFApixels.DragonECS
#endregion #endregion
} }
public abstract class EcsWorld public abstract class EcsWorld : IEcsWorld
{ {
public static IEcsWorld[] Worlds = new IEcsWorld[8]; public static EcsWorld[] Worlds = new EcsWorld[8];
private static IntDispenser _worldIdDispenser = new IntDispenser(0); private static IntDispenser _worldIdDispenser = new IntDispenser(0);
public readonly short uniqueID; public readonly short uniqueID;
protected EcsWorld()
{
uniqueID = (short)_worldIdDispenser.GetFree();
if (uniqueID >= Worlds.Length)
Array.Resize(ref Worlds, Worlds.Length << 1);
Worlds[uniqueID] = (IEcsWorld)this;
}
protected void Realeze()
{
Worlds[uniqueID] = null;
_worldIdDispenser.Release(uniqueID);
}
}
public abstract class EcsWorld<TWorldArchetype> : EcsWorld, IEcsWorld
where TWorldArchetype : EcsWorld<TWorldArchetype>
{
private const int DEL_ENT_BUFFER_SIZE_OFFSET = 2; private const int DEL_ENT_BUFFER_SIZE_OFFSET = 2;
private readonly int _worldArchetypeID = WorldMetaStorage.GetWorldId<TWorldArchetype>(); private int _worldArchetypeID;
private IntDispenser _entityDispenser; private IntDispenser _entityDispenser;
private int _entitiesCount; private int _entitiesCount;
@ -78,7 +62,7 @@ namespace DCFApixels.DragonECS
#endregion #endregion
#region Properties #region Properties
public Type Archetype => typeof(TWorldArchetype); public abstract Type Archetype { get; }
public int UniqueID => uniqueID; public int UniqueID => uniqueID;
public int Count => _entitiesCount; public int Count => _entitiesCount;
public int Capacity => _entitesCapacity; //_denseEntities.Length; public int Capacity => _entitesCapacity; //_denseEntities.Length;
@ -89,6 +73,13 @@ namespace DCFApixels.DragonECS
#region Constructors #region Constructors
public EcsWorld(EcsPipeline pipline) public EcsWorld(EcsPipeline pipline)
{ {
uniqueID = (short)_worldIdDispenser.GetFree();
if (uniqueID >= Worlds.Length)
Array.Resize(ref Worlds, Worlds.Length << 1);
Worlds[uniqueID] = this;
_worldArchetypeID = WorldMetaStorage.GetWorldId(Archetype);
_pipeline = pipline ?? EcsPipeline.Empty; _pipeline = pipline ?? EcsPipeline.Empty;
if (!_pipeline.IsInit) pipline.Init(); if (!_pipeline.IsInit) pipline.Init();
_entityDispenser = new IntDispenser(0); _entityDispenser = new IntDispenser(0);
@ -109,9 +100,14 @@ namespace DCFApixels.DragonECS
_poolRunners = new PoolRunners(_pipeline); _poolRunners = new PoolRunners(_pipeline);
_entityCreate = _pipeline.GetRunner<IEcsEntityCreate>(); _entityCreate = _pipeline.GetRunner<IEcsEntityCreate>();
_entityDestry = _pipeline.GetRunner<IEcsEntityDestroy>(); _entityDestry = _pipeline.GetRunner<IEcsEntityDestroy>();
_pipeline.GetRunner<IEcsInject<IEcsWorld>>().Inject(this); _pipeline.GetRunner<IEcsInject<EcsWorld>>().Inject(this);
_pipeline.GetRunner<IEcsWorldCreate>().OnWorldCreate(this); _pipeline.GetRunner<IEcsWorldCreate>().OnWorldCreate(this);
} }
protected void Realeze()
{
Worlds[uniqueID] = null;
_worldIdDispenser.Release(uniqueID);
}
#endregion #endregion
#region GetPool #region GetPool
@ -254,7 +250,8 @@ namespace DCFApixels.DragonECS
#endregion #endregion
#region Groups #region Groups
void IEcsTable.RegisterGroup(EcsGroup group) void IEcsTable.RegisterGroup(EcsGroup group) => RegisterGroup(group);
internal void RegisterGroup(EcsGroup group)
{ {
_groups.Add(new WeakReference<EcsGroup>(group)); _groups.Add(new WeakReference<EcsGroup>(group));
} }
@ -265,7 +262,8 @@ namespace DCFApixels.DragonECS
return new EcsGroup(this); return new EcsGroup(this);
return _groupsPool.Pop(); return _groupsPool.Pop();
} }
void IEcsTable.ReleaseGroup(EcsGroup group) void IEcsTable.ReleaseGroup(EcsGroup group) => ReleaseGroup(group);
internal void ReleaseGroup(EcsGroup group)
{ {
#if (DEBUG && !DISABLE_DEBUG) || !DRAGONECS_NO_SANITIZE_CHECKS #if (DEBUG && !DISABLE_DEBUG) || !DRAGONECS_NO_SANITIZE_CHECKS
if (group.World != this) if (group.World != this)
@ -277,6 +275,13 @@ namespace DCFApixels.DragonECS
#endregion #endregion
} }
public abstract class EcsWorld<TWorldArchetype> : EcsWorld
where TWorldArchetype : EcsWorld<TWorldArchetype>
{
public override Type Archetype => typeof(TWorldArchetype);
public EcsWorld(EcsPipeline pipline) : base(pipline) { }
}
#region Utils #region Utils
[StructLayout(LayoutKind.Sequential, Pack = 8, Size = 24)] [StructLayout(LayoutKind.Sequential, Pack = 8, Size = 24)]
internal readonly struct PoolRunners internal readonly struct PoolRunners

View File

@ -27,6 +27,6 @@ namespace DCFApixels.DragonECS
public struct Null { } public struct Null { }
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public EcsEntity ToStrongID(IEcsWorld world) => world.GetEcsEntity(id); public EcsEntity ToStrongID(EcsWorld world) => world.GetEcsEntity(id);
} }
} }

View File

@ -45,7 +45,7 @@ namespace DCFApixels.DragonECS.Test
{ {
public Type ComponentType => throw new NotImplementedException(); public Type ComponentType => throw new NotImplementedException();
public IEcsWorld World => throw new NotImplementedException(); public EcsWorld World => throw new NotImplementedException();
public int Count => throw new NotImplementedException(); public int Count => throw new NotImplementedException();
@ -86,7 +86,7 @@ namespace DCFApixels.DragonECS.Test
throw new NotImplementedException(); throw new NotImplementedException();
} }
void IEcsPool.Write(int entityID) void IEcsPool.Write(int entityID, object data)
{ {
throw new NotImplementedException(); throw new NotImplementedException();
} }