From 6c1e999a31e4b4e485c369cd2dade0d4ddc85867 Mon Sep 17 00:00:00 2001 From: Mikhail <99481254+DCFApixels@users.noreply.github.com> Date: Sat, 24 Aug 2024 21:02:18 +0800 Subject: [PATCH] fix executor logic --- src/Executors/EcsWhereExecutor.cs | 73 +++++++++++++----------- src/Executors/EcsWhereToGroupExecutor.cs | 45 ++++++++++----- 2 files changed, 71 insertions(+), 47 deletions(-) diff --git a/src/Executors/EcsWhereExecutor.cs b/src/Executors/EcsWhereExecutor.cs index 22d03c1..3919948 100644 --- a/src/Executors/EcsWhereExecutor.cs +++ b/src/Executors/EcsWhereExecutor.cs @@ -13,7 +13,11 @@ namespace DCFApixels.DragonECS.Internal internal class EcsWhereExecutorCore : EcsQueryExecutorCore { private EcsMaskIterator _iterator; - private int[] _filteredEntities = new int[32]; + private int[] _filteredAllEntities = new int[32]; + private int _filteredAllEntitiesCount = 0; + private long _version; + + private int[] _filteredEntities = null; private int _filteredEntitiesCount = 0; private long _lastWorldVersion = 0; @@ -23,7 +27,7 @@ namespace DCFApixels.DragonECS.Internal public long Version { [MethodImpl(MethodImplOptions.AggressiveInlining)] - get { return _lastWorldVersion; } + get { return _version; } } #endregion @@ -38,50 +42,55 @@ namespace DCFApixels.DragonECS.Internal #region Methods [MethodImpl(MethodImplOptions.AggressiveInlining)] - private void Filter(EcsSpan span) + private void Execute_Iternal() { - _filteredEntitiesCount = _iterator.Iterate(span).CopyTo(ref _filteredEntities); + World.ReleaseDelEntityBufferAllAuto(); + if (_lastWorldVersion != World.Version || _versionsChecker.NextEquals() == false) + { + _version++; + _filteredAllEntitiesCount = _iterator.Iterate(World.Entities).CopyTo(ref _filteredAllEntities); + } _lastWorldVersion = World.Version; } [MethodImpl(MethodImplOptions.AggressiveInlining)] - public EcsSpan Execute() - { - return ExecuteFor(World.Entities); - } - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public EcsSpan ExecuteFor(EcsSpan span) + private void ExecuteFor_Iternal(EcsSpan span) { #if (DEBUG && !DISABLE_DEBUG) || ENABLE_DRAGONECS_ASSERT_CHEKS if (span.IsNull) { Throw.ArgumentNull(nameof(span)); } if (span.WorldID != World.id) { Throw.Quiery_ArgumentDifferentWorldsException(); } #endif - World.ReleaseDelEntityBufferAllAuto(); - if (_lastWorldVersion != World.Version || _versionsChecker.NextEquals() == false) + if (_filteredEntities == null) { - Filter(span); + _filteredEntities = new int[32]; } + _filteredEntitiesCount = _iterator.Iterate(span).CopyTo(ref _filteredEntities); + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public EcsSpan Execute() + { + Execute_Iternal(); + return new EcsSpan(World.id, _filteredAllEntities, _filteredAllEntitiesCount); + } + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public EcsSpan ExecuteFor(EcsSpan span) + { + ExecuteFor_Iternal(span); return new EcsSpan(World.id, _filteredEntities, _filteredEntitiesCount); } - // [MethodImpl(MethodImplOptions.AggressiveInlining)] - // public EcsSpan Execute(Comparison comparison) - // { - // return ExecuteFor(_aspect.World.Entities, comparison); - // } - // [MethodImpl(MethodImplOptions.AggressiveInlining)] - // public EcsSpan ExecuteFor(EcsSpan span, Comparison comparison) - // { - //#if (DEBUG && !DISABLE_DEBUG) || ENABLE_DRAGONECS_ASSERT_CHEKS - // if (span.IsNull) { Throw.ArgumentNull(nameof(span)); } - // if (span.WorldID != WorldID) { Throw.Quiery_ArgumentDifferentWorldsException(); } - //#endif - // World.ReleaseDelEntityBufferAllAuto(); - // if (_lastWorldVersion != World.Version) - // { - // Filter(span); - // } - // Array.Sort(_filteredEntities, 0, _filteredEntitiesCount, comparison); - // } + //[MethodImpl(MethodImplOptions.AggressiveInlining)] + //public EcsSpan Execute(Comparison comparison) + //{ + // Execute_Iternal(); + // return new EcsSpan(World.id, _filteredAllEntities, _filteredAllEntitiesCount); + //} + //[MethodImpl(MethodImplOptions.AggressiveInlining)] + //public EcsSpan ExecuteFor(EcsSpan span, Comparison comparison) + //{ + // ExecuteFor_Iternal(span); + // return new EcsSpan(World.id, _filteredEntities, _filteredEntitiesCount); + //} #endregion } } diff --git a/src/Executors/EcsWhereToGroupExecutor.cs b/src/Executors/EcsWhereToGroupExecutor.cs index 492853d..0389ef7 100644 --- a/src/Executors/EcsWhereToGroupExecutor.cs +++ b/src/Executors/EcsWhereToGroupExecutor.cs @@ -13,6 +13,9 @@ namespace DCFApixels.DragonECS.Internal internal class EcsWhereToGroupExecutorCore : EcsQueryExecutorCore { private EcsMaskIterator _iterator; + private EcsGroup _filteredAllGroup; + private long _version; + private EcsGroup _filteredGroup; private long _lastWorldVersion; @@ -22,7 +25,7 @@ namespace DCFApixels.DragonECS.Internal public long Version { [MethodImpl(MethodImplOptions.AggressiveInlining)] - get { return _lastWorldVersion; } + get { return _version; } } #endregion @@ -30,40 +33,52 @@ namespace DCFApixels.DragonECS.Internal protected sealed override void OnInitialize() { _versionsChecker = new PoolVersionsChecker(Mask); - _filteredGroup = EcsGroup.New(World); + _filteredAllGroup = EcsGroup.New(World); _iterator = Mask.GetIterator(); } protected sealed override void OnDestroy() { - _filteredGroup.Dispose(); + _filteredAllGroup.Dispose(); } #endregion #region Methods [MethodImpl(MethodImplOptions.AggressiveInlining)] - private void Filter(EcsSpan span) + private void Execute_Iternal() { - _iterator.Iterate(span).CopyTo(_filteredGroup); + World.ReleaseDelEntityBufferAllAuto(); + if (_lastWorldVersion != World.Version || _versionsChecker.NextEquals() == false) + { + _version++; + _iterator.Iterate(World.Entities).CopyTo(_filteredAllGroup); + } _lastWorldVersion = World.Version; } [MethodImpl(MethodImplOptions.AggressiveInlining)] - public EcsReadonlyGroup Execute() - { - return ExecuteFor(World.Entities); - } - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public EcsReadonlyGroup ExecuteFor(EcsSpan span) + private void ExecuteFor_Iternal(EcsSpan span) { #if (DEBUG && !DISABLE_DEBUG) || ENABLE_DRAGONECS_ASSERT_CHEKS if (span.IsNull) { Throw.ArgumentNull(nameof(span)); } if (span.WorldID != World.id) { Throw.Quiery_ArgumentDifferentWorldsException(); } #endif - World.ReleaseDelEntityBufferAllAuto(); - if (_lastWorldVersion != World.Version || _versionsChecker.NextEquals() == false) + if (_filteredGroup == null) { - Filter(span); + _filteredGroup = EcsGroup.New(World); } - return _filteredGroup.Readonly; + _iterator.Iterate(span).CopyTo(_filteredGroup); + } + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public EcsReadonlyGroup Execute() + { + Execute_Iternal(); + return _filteredAllGroup; + } + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public EcsReadonlyGroup ExecuteFor(EcsSpan span) + { + ExecuteFor_Iternal(span); + return _filteredGroup; } #endregion }