From 89ebd5d1ba92a4106e1685ef3d52ab16cf12cf0c Mon Sep 17 00:00:00 2001 From: Mikhail <99481254+DCFApixels@users.noreply.github.com> Date: Sat, 24 Aug 2024 13:10:50 +0800 Subject: [PATCH] update queries cashe --- src/EcsAspect.cs | 1 - src/EcsMask.cs | 4 +- src/EcsWorld.cs | 5 ++ src/Executors/EcsQueryExecutor.cs | 21 +++++--- src/Executors/EcsWhereExecutor.cs | 62 +++++------------------- src/Executors/EcsWhereToGroupExecutor.cs | 57 +++++++--------------- 6 files changed, 51 insertions(+), 99 deletions(-) diff --git a/src/EcsAspect.cs b/src/EcsAspect.cs index b96e091..594f1a4 100644 --- a/src/EcsAspect.cs +++ b/src/EcsAspect.cs @@ -2,7 +2,6 @@ using DCFApixels.DragonECS.PoolsCore; using System; using System.Collections.Generic; -using static DCFApixels.DragonECS.EcsAspect; namespace DCFApixels.DragonECS { diff --git a/src/EcsMask.cs b/src/EcsMask.cs index af5a6db..eb9ed19 100644 --- a/src/EcsMask.cs +++ b/src/EcsMask.cs @@ -222,7 +222,7 @@ namespace DCFApixels.DragonECS #region Other public EcsMaskIterator GetIterator() { - if(_iterator == null) + if (_iterator == null) { _iterator = new EcsMaskIterator(EcsWorld.GetWorld(_worldID), this); } @@ -748,7 +748,7 @@ namespace DCFApixels.DragonECS #region Enumerable public Enumerable Iterate(EcsSpan span) { - return new Enumerable(); + return new Enumerable(this, span); } public readonly ref struct Enumerable { diff --git a/src/EcsWorld.cs b/src/EcsWorld.cs index 398c05e..3c65caa 100644 --- a/src/EcsWorld.cs +++ b/src/EcsWorld.cs @@ -202,6 +202,11 @@ namespace DCFApixels.DragonECS _isDestroyed = true; _poolTypeCode_2_CmpTypeIDs = null; _cmpTypeCode_2_CmpTypeIDs = null; + + foreach (var item in _executorCoures) + { + item.Value.Destroy(); + } //_entities - не обнуляется для работы entlong.IsAlive } //public void Clear() { } diff --git a/src/Executors/EcsQueryExecutor.cs b/src/Executors/EcsQueryExecutor.cs index 4e3470f..c7fff67 100644 --- a/src/Executors/EcsQueryExecutor.cs +++ b/src/Executors/EcsQueryExecutor.cs @@ -6,7 +6,7 @@ namespace DCFApixels.DragonECS { 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; public readonly struct ExecutorMediator { @@ -19,15 +19,15 @@ namespace DCFApixels.DragonECS } World = world; } - public TExecutorCore GetCore(Type aspectType) + public TExecutorCore GetCore(EcsMask mask) where TExecutorCore : EcsQueryExecutorCore, new() { - var coreType = typeof(EcsQueryExecutorCore); - if(World._executorCoures.TryGetValue((aspectType, coreType), out EcsQueryExecutorCore core)) + var coreType = typeof(TExecutorCore); + if (World._executorCoures.TryGetValue((coreType, mask), out EcsQueryExecutorCore core) == false) { core = new TExecutorCore(); - core.Initialize(World); - World._executorCoures.Add((aspectType, coreType), core); + core.Initialize(World, mask); + World._executorCoures.Add((coreType, mask), core); } return (TExecutorCore)core; } @@ -36,6 +36,7 @@ namespace DCFApixels.DragonECS public abstract class EcsQueryExecutorCore { private EcsWorld _source; + private EcsMask _mask; public short WorldID { [MethodImpl(MethodImplOptions.AggressiveInlining)] @@ -46,9 +47,15 @@ namespace DCFApixels.DragonECS [MethodImpl(MethodImplOptions.AggressiveInlining)] 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; + _mask = mask; OnInitialize(); } internal void Destroy() diff --git a/src/Executors/EcsWhereExecutor.cs b/src/Executors/EcsWhereExecutor.cs index 0d20c42..22d03c1 100644 --- a/src/Executors/EcsWhereExecutor.cs +++ b/src/Executors/EcsWhereExecutor.cs @@ -1,5 +1,4 @@ using DCFApixels.DragonECS.Internal; -using System; using System.Runtime.CompilerServices; #if ENABLE_IL2CPP using Unity.IL2CPP.CompilerServices; @@ -11,31 +10,8 @@ namespace DCFApixels.DragonECS.Internal [Il2CppSetOption(Option.NullChecks, false)] [Il2CppSetOption(Option.ArrayBoundsChecks, false)] #endif - internal readonly struct EcsWhereExecutorCoreList : IEcsWorldComponent + 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 int[] _filteredEntities = new int[32]; private int _filteredEntitiesCount = 0; @@ -51,12 +27,13 @@ namespace DCFApixels.DragonECS.Internal } #endregion - #region Constructors - public EcsWhereExecutorCore(EcsWorld source, EcsAspect aspect) + #region OnInitialize/OnDestroy + protected sealed override void OnInitialize() { - _source = source; - _versionsChecker = new PoolVersionsChecker(aspect.Mask); + _versionsChecker = new PoolVersionsChecker(Mask); + _iterator = Mask.GetIterator(); } + protected sealed override void OnDestroy() { } #endregion #region Methods @@ -64,26 +41,26 @@ namespace DCFApixels.DragonECS.Internal private void Filter(EcsSpan span) { _filteredEntitiesCount = _iterator.Iterate(span).CopyTo(ref _filteredEntities); - _lastWorldVersion = _source.Version; + _lastWorldVersion = World.Version; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public EcsSpan Execute() { - return ExecuteFor(_source.Entities); + return ExecuteFor(World.Entities); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public EcsSpan ExecuteFor(EcsSpan span) { #if (DEBUG && !DISABLE_DEBUG) || ENABLE_DRAGONECS_ASSERT_CHEKS if (span.IsNull) { Throw.ArgumentNull(nameof(span)); } - if (span.WorldID != _source.id) { Throw.Quiery_ArgumentDifferentWorldsException(); } + if (span.WorldID != World.id) { Throw.Quiery_ArgumentDifferentWorldsException(); } #endif - _source.ReleaseDelEntityBufferAllAuto(); - if (_lastWorldVersion != _source.Version || _versionsChecker.NextEquals() == false) + World.ReleaseDelEntityBufferAllAuto(); + if (_lastWorldVersion != World.Version || _versionsChecker.NextEquals() == false) { Filter(span); } - return new EcsSpan(_source.id, _filteredEntities, _filteredEntitiesCount); + return new EcsSpan(World.id, _filteredEntities, _filteredEntitiesCount); } // [MethodImpl(MethodImplOptions.AggressiveInlining)] @@ -137,20 +114,7 @@ namespace DCFApixels.DragonECS protected sealed override void OnInitialize() { _aspect = World.GetAspect(); - int maskID = _aspect.Mask.ID; - ref var list = ref World.Get(); - 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; + _core = Mediator.GetCore(_aspect.Mask); } protected sealed override void OnDestroy() { } #endregion diff --git a/src/Executors/EcsWhereToGroupExecutor.cs b/src/Executors/EcsWhereToGroupExecutor.cs index 79ff3e0..492853d 100644 --- a/src/Executors/EcsWhereToGroupExecutor.cs +++ b/src/Executors/EcsWhereToGroupExecutor.cs @@ -10,31 +10,8 @@ namespace DCFApixels.DragonECS.Internal [Il2CppSetOption(Option.NullChecks, false)] [Il2CppSetOption(Option.ArrayBoundsChecks, false)] #endif - internal readonly struct EcsWhereToGroupExecutorCoreList : IEcsWorldComponent + 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 EcsGroup _filteredGroup; @@ -49,13 +26,14 @@ namespace DCFApixels.DragonECS.Internal } #endregion - #region Constructors/Destroy - public EcsWhereToGroupExecutorCore(EcsWorld source, EcsAspect aspect) + #region OnInitialize/OnDestroy + protected sealed override void OnInitialize() { - _source = source; - _versionsChecker = new PoolVersionsChecker(aspect.Mask); + _versionsChecker = new PoolVersionsChecker(Mask); + _filteredGroup = EcsGroup.New(World); + _iterator = Mask.GetIterator(); } - public void Destroy() + protected sealed override void OnDestroy() { _filteredGroup.Dispose(); } @@ -66,22 +44,22 @@ namespace DCFApixels.DragonECS.Internal private void Filter(EcsSpan span) { _iterator.Iterate(span).CopyTo(_filteredGroup); - _lastWorldVersion = _source.Version; + _lastWorldVersion = World.Version; } [MethodImpl(MethodImplOptions.AggressiveInlining)] - public EcsSpan Execute() + public EcsReadonlyGroup Execute() { - return ExecuteFor(_source.Entities); + return ExecuteFor(World.Entities); } [MethodImpl(MethodImplOptions.AggressiveInlining)] - public EcsSpan ExecuteFor(EcsSpan span) + public EcsReadonlyGroup ExecuteFor(EcsSpan span) { #if (DEBUG && !DISABLE_DEBUG) || ENABLE_DRAGONECS_ASSERT_CHEKS if (span.IsNull) { Throw.ArgumentNull(nameof(span)); } - if (span.WorldID != _source.id) { Throw.Quiery_ArgumentDifferentWorldsException(); } + if (span.WorldID != World.id) { Throw.Quiery_ArgumentDifferentWorldsException(); } #endif - _source.ReleaseDelEntityBufferAllAuto(); - if (_lastWorldVersion != _source.Version || _versionsChecker.NextEquals() == false) + World.ReleaseDelEntityBufferAllAuto(); + if (_lastWorldVersion != World.Version || _versionsChecker.NextEquals() == false) { Filter(span); } @@ -119,8 +97,7 @@ namespace DCFApixels.DragonECS protected sealed override void OnInitialize() { _aspect = World.GetAspect(); - _filteredGroup = EcsGroup.New(World); - _versionsChecker = new PoolVersionsChecker(_aspect._mask); + _core = Mediator.GetCore(_aspect.Mask); } protected sealed override void OnDestroy() { @@ -130,12 +107,12 @@ namespace DCFApixels.DragonECS #region Methods [MethodImpl(MethodImplOptions.AggressiveInlining)] - public EcsSpan Execute() + public EcsReadonlyGroup Execute() { return _core.Execute(); } [MethodImpl(MethodImplOptions.AggressiveInlining)] - public EcsSpan ExecuteFor(EcsSpan span) + public EcsReadonlyGroup ExecuteFor(EcsSpan span) { return _core.ExecuteFor(span); }