update queries cashe

This commit is contained in:
Mikhail 2024-08-24 13:10:50 +08:00
parent 07c122d406
commit 89ebd5d1ba
6 changed files with 51 additions and 99 deletions

View File

@ -2,7 +2,6 @@
using DCFApixels.DragonECS.PoolsCore; using DCFApixels.DragonECS.PoolsCore;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using static DCFApixels.DragonECS.EcsAspect;
namespace DCFApixels.DragonECS namespace DCFApixels.DragonECS
{ {

View File

@ -222,7 +222,7 @@ namespace DCFApixels.DragonECS
#region Other #region Other
public EcsMaskIterator GetIterator() public EcsMaskIterator GetIterator()
{ {
if(_iterator == null) if (_iterator == null)
{ {
_iterator = new EcsMaskIterator(EcsWorld.GetWorld(_worldID), this); _iterator = new EcsMaskIterator(EcsWorld.GetWorld(_worldID), this);
} }
@ -748,7 +748,7 @@ namespace DCFApixels.DragonECS
#region Enumerable #region Enumerable
public Enumerable Iterate(EcsSpan span) public Enumerable Iterate(EcsSpan span)
{ {
return new Enumerable(); return new Enumerable(this, span);
} }
public readonly ref struct Enumerable public readonly ref struct Enumerable
{ {

View File

@ -202,6 +202,11 @@ namespace DCFApixels.DragonECS
_isDestroyed = true; _isDestroyed = true;
_poolTypeCode_2_CmpTypeIDs = null; _poolTypeCode_2_CmpTypeIDs = null;
_cmpTypeCode_2_CmpTypeIDs = null; _cmpTypeCode_2_CmpTypeIDs = null;
foreach (var item in _executorCoures)
{
item.Value.Destroy();
}
//_entities - не обнуляется для работы entlong.IsAlive //_entities - не обнуляется для работы entlong.IsAlive
} }
//public void Clear() { } //public void Clear() { }

View File

@ -6,7 +6,7 @@ namespace DCFApixels.DragonECS
{ {
public partial class EcsWorld public partial class EcsWorld
{ {
private readonly Dictionary<(Type, Type), EcsQueryExecutorCore> _executorCoures = new Dictionary<(Type, Type), EcsQueryExecutorCore>(); private readonly Dictionary<(Type, EcsMask), EcsQueryExecutorCore> _executorCoures = new Dictionary<(Type, EcsMask), EcsQueryExecutorCore>();
private readonly ExecutorMediator _executorsMediator; private readonly ExecutorMediator _executorsMediator;
public readonly struct ExecutorMediator public readonly struct ExecutorMediator
{ {
@ -19,15 +19,15 @@ namespace DCFApixels.DragonECS
} }
World = world; World = world;
} }
public TExecutorCore GetCore<TExecutorCore>(Type aspectType) public TExecutorCore GetCore<TExecutorCore>(EcsMask mask)
where TExecutorCore : EcsQueryExecutorCore, new() where TExecutorCore : EcsQueryExecutorCore, new()
{ {
var coreType = typeof(EcsQueryExecutorCore); var coreType = typeof(TExecutorCore);
if(World._executorCoures.TryGetValue((aspectType, coreType), out EcsQueryExecutorCore core)) if (World._executorCoures.TryGetValue((coreType, mask), out EcsQueryExecutorCore core) == false)
{ {
core = new TExecutorCore(); core = new TExecutorCore();
core.Initialize(World); core.Initialize(World, mask);
World._executorCoures.Add((aspectType, coreType), core); World._executorCoures.Add((coreType, mask), core);
} }
return (TExecutorCore)core; return (TExecutorCore)core;
} }
@ -36,6 +36,7 @@ namespace DCFApixels.DragonECS
public abstract class EcsQueryExecutorCore public abstract class EcsQueryExecutorCore
{ {
private EcsWorld _source; private EcsWorld _source;
private EcsMask _mask;
public short WorldID public short WorldID
{ {
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
@ -46,9 +47,15 @@ namespace DCFApixels.DragonECS
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
get { return _source; } get { return _source; }
} }
internal void Initialize(EcsWorld world) protected EcsMask Mask
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get { return _mask; }
}
internal void Initialize(EcsWorld world, EcsMask mask)
{ {
_source = world; _source = world;
_mask = mask;
OnInitialize(); OnInitialize();
} }
internal void Destroy() internal void Destroy()

View File

@ -1,5 +1,4 @@
using DCFApixels.DragonECS.Internal; using DCFApixels.DragonECS.Internal;
using System;
using System.Runtime.CompilerServices; using System.Runtime.CompilerServices;
#if ENABLE_IL2CPP #if ENABLE_IL2CPP
using Unity.IL2CPP.CompilerServices; using Unity.IL2CPP.CompilerServices;
@ -11,31 +10,8 @@ namespace DCFApixels.DragonECS.Internal
[Il2CppSetOption(Option.NullChecks, false)] [Il2CppSetOption(Option.NullChecks, false)]
[Il2CppSetOption(Option.ArrayBoundsChecks, false)] [Il2CppSetOption(Option.ArrayBoundsChecks, false)]
#endif #endif
internal readonly struct EcsWhereExecutorCoreList : IEcsWorldComponent<EcsWhereExecutorCoreList> internal class EcsWhereExecutorCore : EcsQueryExecutorCore
{ {
internal readonly EcsWhereExecutorCore[] _cores;
public EcsWhereExecutorCoreList(EcsWhereExecutorCore[] cores)
{
_cores = cores;
}
public void Init(ref EcsWhereExecutorCoreList component, EcsWorld world)
{
component = new EcsWhereExecutorCoreList(new EcsWhereExecutorCore[64]);
}
public void OnDestroy(ref EcsWhereExecutorCoreList component, EcsWorld world)
{
component = default;
}
}
#if ENABLE_IL2CPP
[Il2CppSetOption(Option.NullChecks, false)]
[Il2CppSetOption(Option.ArrayBoundsChecks, false)]
#endif
internal class EcsWhereExecutorCore
{
private EcsWorld _source;
private EcsMaskIterator _iterator; private EcsMaskIterator _iterator;
private int[] _filteredEntities = new int[32]; private int[] _filteredEntities = new int[32];
private int _filteredEntitiesCount = 0; private int _filteredEntitiesCount = 0;
@ -51,12 +27,13 @@ namespace DCFApixels.DragonECS.Internal
} }
#endregion #endregion
#region Constructors #region OnInitialize/OnDestroy
public EcsWhereExecutorCore(EcsWorld source, EcsAspect aspect) protected sealed override void OnInitialize()
{ {
_source = source; _versionsChecker = new PoolVersionsChecker(Mask);
_versionsChecker = new PoolVersionsChecker(aspect.Mask); _iterator = Mask.GetIterator();
} }
protected sealed override void OnDestroy() { }
#endregion #endregion
#region Methods #region Methods
@ -64,26 +41,26 @@ namespace DCFApixels.DragonECS.Internal
private void Filter(EcsSpan span) private void Filter(EcsSpan span)
{ {
_filteredEntitiesCount = _iterator.Iterate(span).CopyTo(ref _filteredEntities); _filteredEntitiesCount = _iterator.Iterate(span).CopyTo(ref _filteredEntities);
_lastWorldVersion = _source.Version; _lastWorldVersion = World.Version;
} }
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public EcsSpan Execute() public EcsSpan Execute()
{ {
return ExecuteFor(_source.Entities); return ExecuteFor(World.Entities);
} }
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public EcsSpan ExecuteFor(EcsSpan span) public EcsSpan ExecuteFor(EcsSpan span)
{ {
#if (DEBUG && !DISABLE_DEBUG) || ENABLE_DRAGONECS_ASSERT_CHEKS #if (DEBUG && !DISABLE_DEBUG) || ENABLE_DRAGONECS_ASSERT_CHEKS
if (span.IsNull) { Throw.ArgumentNull(nameof(span)); } if (span.IsNull) { Throw.ArgumentNull(nameof(span)); }
if (span.WorldID != _source.id) { Throw.Quiery_ArgumentDifferentWorldsException(); } if (span.WorldID != World.id) { Throw.Quiery_ArgumentDifferentWorldsException(); }
#endif #endif
_source.ReleaseDelEntityBufferAllAuto(); World.ReleaseDelEntityBufferAllAuto();
if (_lastWorldVersion != _source.Version || _versionsChecker.NextEquals() == false) if (_lastWorldVersion != World.Version || _versionsChecker.NextEquals() == false)
{ {
Filter(span); Filter(span);
} }
return new EcsSpan(_source.id, _filteredEntities, _filteredEntitiesCount); return new EcsSpan(World.id, _filteredEntities, _filteredEntitiesCount);
} }
// [MethodImpl(MethodImplOptions.AggressiveInlining)] // [MethodImpl(MethodImplOptions.AggressiveInlining)]
@ -137,20 +114,7 @@ namespace DCFApixels.DragonECS
protected sealed override void OnInitialize() protected sealed override void OnInitialize()
{ {
_aspect = World.GetAspect<TAspect>(); _aspect = World.GetAspect<TAspect>();
int maskID = _aspect.Mask.ID; _core = Mediator.GetCore<EcsWhereExecutorCore>(_aspect.Mask);
ref var list = ref World.Get<EcsWhereExecutorCoreList>();
var cores = list._cores;
if (maskID >= list._cores.Length)
{
Array.Resize(ref cores, cores.Length << 1);
list = new EcsWhereExecutorCoreList(cores);
}
ref var coreRef = ref cores[maskID];
if (coreRef == null)
{
coreRef = new EcsWhereExecutorCore(World, _aspect);
}
_core = coreRef;
} }
protected sealed override void OnDestroy() { } protected sealed override void OnDestroy() { }
#endregion #endregion

View File

@ -10,31 +10,8 @@ namespace DCFApixels.DragonECS.Internal
[Il2CppSetOption(Option.NullChecks, false)] [Il2CppSetOption(Option.NullChecks, false)]
[Il2CppSetOption(Option.ArrayBoundsChecks, false)] [Il2CppSetOption(Option.ArrayBoundsChecks, false)]
#endif #endif
internal readonly struct EcsWhereToGroupExecutorCoreList : IEcsWorldComponent<EcsWhereToGroupExecutorCoreList> internal class EcsWhereToGroupExecutorCore : EcsQueryExecutorCore
{ {
internal readonly EcsWhereToGroupExecutorCore[] _cores;
public EcsWhereToGroupExecutorCoreList(EcsWhereToGroupExecutorCore[] cores)
{
_cores = cores;
}
public void Init(ref EcsWhereToGroupExecutorCoreList component, EcsWorld world)
{
component = new EcsWhereToGroupExecutorCoreList(new EcsWhereToGroupExecutorCore[64]);
}
public void OnDestroy(ref EcsWhereToGroupExecutorCoreList component, EcsWorld world)
{
component = default;
}
}
#if ENABLE_IL2CPP
[Il2CppSetOption(Option.NullChecks, false)]
[Il2CppSetOption(Option.ArrayBoundsChecks, false)]
#endif
internal class EcsWhereToGroupExecutorCore
{
private EcsWorld _source;
private EcsMaskIterator _iterator; private EcsMaskIterator _iterator;
private EcsGroup _filteredGroup; private EcsGroup _filteredGroup;
@ -49,13 +26,14 @@ namespace DCFApixels.DragonECS.Internal
} }
#endregion #endregion
#region Constructors/Destroy #region OnInitialize/OnDestroy
public EcsWhereToGroupExecutorCore(EcsWorld source, EcsAspect aspect) protected sealed override void OnInitialize()
{ {
_source = source; _versionsChecker = new PoolVersionsChecker(Mask);
_versionsChecker = new PoolVersionsChecker(aspect.Mask); _filteredGroup = EcsGroup.New(World);
_iterator = Mask.GetIterator();
} }
public void Destroy() protected sealed override void OnDestroy()
{ {
_filteredGroup.Dispose(); _filteredGroup.Dispose();
} }
@ -66,22 +44,22 @@ namespace DCFApixels.DragonECS.Internal
private void Filter(EcsSpan span) private void Filter(EcsSpan span)
{ {
_iterator.Iterate(span).CopyTo(_filteredGroup); _iterator.Iterate(span).CopyTo(_filteredGroup);
_lastWorldVersion = _source.Version; _lastWorldVersion = World.Version;
} }
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public EcsSpan Execute() public EcsReadonlyGroup Execute()
{ {
return ExecuteFor(_source.Entities); return ExecuteFor(World.Entities);
} }
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public EcsSpan ExecuteFor(EcsSpan span) public EcsReadonlyGroup ExecuteFor(EcsSpan span)
{ {
#if (DEBUG && !DISABLE_DEBUG) || ENABLE_DRAGONECS_ASSERT_CHEKS #if (DEBUG && !DISABLE_DEBUG) || ENABLE_DRAGONECS_ASSERT_CHEKS
if (span.IsNull) { Throw.ArgumentNull(nameof(span)); } if (span.IsNull) { Throw.ArgumentNull(nameof(span)); }
if (span.WorldID != _source.id) { Throw.Quiery_ArgumentDifferentWorldsException(); } if (span.WorldID != World.id) { Throw.Quiery_ArgumentDifferentWorldsException(); }
#endif #endif
_source.ReleaseDelEntityBufferAllAuto(); World.ReleaseDelEntityBufferAllAuto();
if (_lastWorldVersion != _source.Version || _versionsChecker.NextEquals() == false) if (_lastWorldVersion != World.Version || _versionsChecker.NextEquals() == false)
{ {
Filter(span); Filter(span);
} }
@ -119,8 +97,7 @@ namespace DCFApixels.DragonECS
protected sealed override void OnInitialize() protected sealed override void OnInitialize()
{ {
_aspect = World.GetAspect<TAspect>(); _aspect = World.GetAspect<TAspect>();
_filteredGroup = EcsGroup.New(World); _core = Mediator.GetCore<EcsWhereToGroupExecutorCore>(_aspect.Mask);
_versionsChecker = new PoolVersionsChecker(_aspect._mask);
} }
protected sealed override void OnDestroy() protected sealed override void OnDestroy()
{ {
@ -130,12 +107,12 @@ namespace DCFApixels.DragonECS
#region Methods #region Methods
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public EcsSpan Execute() public EcsReadonlyGroup Execute()
{ {
return _core.Execute(); return _core.Execute();
} }
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public EcsSpan ExecuteFor(EcsSpan span) public EcsReadonlyGroup ExecuteFor(EcsSpan span)
{ {
return _core.ExecuteFor(span); return _core.ExecuteFor(span);
} }