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 void OnWorldCreate(IEcsWorld world);
public void OnWorldCreate(EcsWorld world);
}
public interface IEcsWorldDestroy : IEcsSystem
{
public void OnWorldDestroy(IEcsWorld world);
public void OnWorldDestroy(EcsWorld world);
}
public interface IEcsWorldLifecycle : IEcsWorldCreate, IEcsWorldDestroy { }
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);
}
}
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);
}

View File

@ -13,6 +13,6 @@
}
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>
{
private IEcsWorld _firstTarget;
private IEcsWorld _secondTarget;
public EcsEdgeWorld(IEcsWorld firstTarget, IEcsWorld secondTarget, EcsPipeline pipeline) : base(pipeline)
private EcsWorld _firstTarget;
private EcsWorld _secondTarget;
public EcsEdgeWorld(EcsWorld firstTarget, EcsWorld secondTarget, EcsPipeline pipeline) : base(pipeline)
{
_firstTarget = firstTarget;
_secondTarget = secondTarget;
}
public EcsEdgeWorld(IEcsWorld firstTarget, EcsPipeline pipeline) : base(pipeline)
public EcsEdgeWorld(EcsWorld firstTarget, EcsPipeline pipeline) : base(pipeline)
{
_firstTarget = firstTarget;
}

View File

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

View File

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

View File

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

View File

@ -20,30 +20,14 @@ namespace DCFApixels.DragonECS
#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);
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 readonly int _worldArchetypeID = WorldMetaStorage.GetWorldId<TWorldArchetype>();
private int _worldArchetypeID;
private IntDispenser _entityDispenser;
private int _entitiesCount;
@ -78,7 +62,7 @@ namespace DCFApixels.DragonECS
#endregion
#region Properties
public Type Archetype => typeof(TWorldArchetype);
public abstract Type Archetype { get; }
public int UniqueID => uniqueID;
public int Count => _entitiesCount;
public int Capacity => _entitesCapacity; //_denseEntities.Length;
@ -88,7 +72,14 @@ namespace DCFApixels.DragonECS
#region Constructors
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;
if (!_pipeline.IsInit) pipline.Init();
_entityDispenser = new IntDispenser(0);
@ -109,9 +100,14 @@ namespace DCFApixels.DragonECS
_poolRunners = new PoolRunners(_pipeline);
_entityCreate = _pipeline.GetRunner<IEcsEntityCreate>();
_entityDestry = _pipeline.GetRunner<IEcsEntityDestroy>();
_pipeline.GetRunner<IEcsInject<IEcsWorld>>().Inject(this);
_pipeline.GetRunner<IEcsInject<EcsWorld>>().Inject(this);
_pipeline.GetRunner<IEcsWorldCreate>().OnWorldCreate(this);
}
protected void Realeze()
{
Worlds[uniqueID] = null;
_worldIdDispenser.Release(uniqueID);
}
#endregion
#region GetPool
@ -212,7 +208,7 @@ namespace DCFApixels.DragonECS
_entitiesCount--;
_entityDestry.OnEntityDestroy(entity);
if(_delEntBufferCount >= _delEntBuffer.Length)
if (_delEntBufferCount >= _delEntBuffer.Length)
ReleaseDelEntBuffer();
}
@ -254,7 +250,8 @@ namespace DCFApixels.DragonECS
#endregion
#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));
}
@ -265,7 +262,8 @@ namespace DCFApixels.DragonECS
return new EcsGroup(this);
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 (group.World != this)
@ -277,6 +275,13 @@ namespace DCFApixels.DragonECS
#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
[StructLayout(LayoutKind.Sequential, Pack = 8, Size = 24)]
internal readonly struct PoolRunners

View File

@ -27,6 +27,6 @@ namespace DCFApixels.DragonECS
public struct Null { }
[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 IEcsWorld World => throw new NotImplementedException();
public EcsWorld World => throw new NotImplementedException();
public int Count => throw new NotImplementedException();
@ -86,7 +86,7 @@ namespace DCFApixels.DragonECS.Test
throw new NotImplementedException();
}
void IEcsPool.Write(int entityID)
void IEcsPool.Write(int entityID, object data)
{
throw new NotImplementedException();
}