optimisation

This commit is contained in:
Mikhail 2023-06-27 05:30:45 +08:00
parent 1f1fc2ca9b
commit cd1da0253a
3 changed files with 42 additions and 33 deletions

View File

@ -1,20 +1,21 @@
using DCFApixels.DragonECS.Utils;
using System;
using System.Reflection;
namespace DCFApixels.DragonECS
{
public partial class EcsWorld
{
internal readonly struct PoolCache<TPool> : IEcsWorldComponent<PoolCache<TPool>>
where TPool : IEcsPoolImplementation, new()
internal readonly struct PoolCache<T> : IEcsWorldComponent<PoolCache<T>>
where T : IEcsPoolImplementation, new()
{
public readonly TPool instance;
public PoolCache(TPool instance) => this.instance = instance;
void IEcsWorldComponent<PoolCache<TPool>>.Init(ref PoolCache<TPool> component, EcsWorld world)
public readonly T instance;
public PoolCache(T instance) => this.instance = instance;
void IEcsWorldComponent<PoolCache<T>>.Init(ref PoolCache<T> component, EcsWorld world)
{
component = new PoolCache<TPool>(world.CreatePool<TPool>());
component = new PoolCache<T>(world.CreatePool<T>());
}
void IEcsWorldComponent<PoolCache<TPool>>.OnDestroy(ref PoolCache<TPool> component, EcsWorld world)
void IEcsWorldComponent<PoolCache<T>>.OnDestroy(ref PoolCache<T> component, EcsWorld world)
{
component = default;
}
@ -36,5 +37,35 @@ namespace DCFApixels.DragonECS
}
return (TPool)_pools[index];
}
internal readonly struct AspectCache<T> : IEcsWorldComponent<AspectCache<T>>
where T : EcsAspect
{
public readonly T instance;
public AspectCache(T instance) => this.instance = instance;
void IEcsWorldComponent<AspectCache<T>>.Init(ref AspectCache<T> component, EcsWorld world)
{
component = new AspectCache<T>(EcsAspect.Builder.Build<T>(world));
}
void IEcsWorldComponent<AspectCache<T>>.OnDestroy(ref AspectCache<T> component, EcsWorld world)
{
component = default;
}
}
internal readonly struct ExcecutorCache<T> : IEcsWorldComponent<ExcecutorCache<T>>
where T : EcsQueryExecutor, new()
{
public readonly T instance;
public ExcecutorCache(T instance) => this.instance = instance;
void IEcsWorldComponent<ExcecutorCache<T>>.Init(ref ExcecutorCache<T> component, EcsWorld world)
{
T instance = new T();
instance.Initialize(world);
component = new ExcecutorCache<T>(instance);
}
void IEcsWorldComponent<ExcecutorCache<T>>.OnDestroy(ref ExcecutorCache<T> component, EcsWorld world)
{
component = default;
}
}
}
}

View File

@ -27,9 +27,6 @@ namespace DCFApixels.DragonECS
internal IEcsPoolImplementation[] _pools;
private EcsNullPool _nullPool = EcsNullPool.instance;
private EcsAspect[] _aspects;
private EcsQueryExecutor[] _executors;
private List<WeakReference<EcsGroup>> _groups = new List<WeakReference<EcsGroup>>();
private Stack<EcsGroup> _groupsPool = new Stack<EcsGroup>(64);
@ -73,9 +70,6 @@ namespace DCFApixels.DragonECS
_delEntBuffer = new int[_entitesCapacity >> DEL_ENT_BUFFER_SIZE_OFFSET];
_allEntites = GetFreeGroup();
_aspects = new EcsAspect[128];
_executors = new EcsQueryExecutor[128];
}
public void Destroy()
{
@ -83,8 +77,6 @@ namespace DCFApixels.DragonECS
_gens = null;
_pools = null;
_nullPool = null;
_aspects = null;
_executors = null;
Worlds[id] = null;
ReleaseData(id);
_worldIdDispenser.Release(id);
@ -108,28 +100,15 @@ namespace DCFApixels.DragonECS
{
return Get<PoolCache<TPool>>().instance;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public TAspect GetAspect<TAspect>() where TAspect : EcsAspect
{
int index = WorldMetaStorage.GetAspectID<TAspect>(_worldTypeID);
if (index >= _aspects.Length)
Array.Resize(ref _aspects, _aspects.Length << 1);
if (_aspects[index] == null)
_aspects[index] = EcsAspect.Builder.Build<TAspect>(this);
return (TAspect)_aspects[index];
return Get<AspectCache<TAspect>>().instance;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public TExecutor GetExecutor<TExecutor>() where TExecutor : EcsQueryExecutor, new()
{
int index = WorldMetaStorage.GetExecutorID<TExecutor>(_worldTypeID);
if (index >= _executors.Length)
Array.Resize(ref _executors, _executors.Length << 1);
var result = _executors[index];
if (result == null)
{
result = new TExecutor();
_executors[index] = result;
result.Initialize(this);
}
return (TExecutor)result;
return Get<ExcecutorCache<TExecutor>>().instance;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]

View File

@ -1,5 +1,4 @@
using DCFApixels.DragonECS.Internal;
using DCFApixels.DragonECS.Internal;
using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;