From fd653dc06fd6d09a1f00c12742bec8d16e2907f1 Mon Sep 17 00:00:00 2001 From: Mikhail <99481254+DCFApixels@users.noreply.github.com> Date: Sat, 1 Apr 2023 21:16:08 +0800 Subject: [PATCH] Optimization --- src/EcsGroup.cs | 50 +++++++++++++++++++++---------------------------- src/EcsPool.cs | 39 +++++++++++++++++++------------------- 2 files changed, 40 insertions(+), 49 deletions(-) diff --git a/src/EcsGroup.cs b/src/EcsGroup.cs index 2c5955b..e509987 100644 --- a/src/EcsGroup.cs +++ b/src/EcsGroup.cs @@ -37,7 +37,7 @@ namespace DCFApixels.DragonECS // _delayedOps это int[] для отложенных операций, хранятся отложенные операции в виде int значения, если старший бит = 0 то это опреация добавленияб если = 1 то это операция вычитания // this collection can only store numbers greater than 0 - public class EcsGroup + public class EcsGroup { public const int DEALAYED_ADD = 0; public const int DEALAYED_REMOVE = int.MinValue; @@ -198,21 +198,22 @@ namespace DCFApixels.DragonECS #region AddGroup/RemoveGroup public void AddGroup(EcsReadonlyGroup group) - { - foreach (var item in group) UncheckedAdd(item.id); - } + { foreach (var item in group) Add(item.id); } public void RemoveGroup(EcsReadonlyGroup group) - { - foreach (var item in group) UncheckedRemove(item.id); - } + { foreach (var item in group) Remove(item.id); } public void AddGroup(EcsGroup group) - { - foreach (var item in group) UncheckedAdd(item.id); - } + { foreach (var item in group) Add(item.id); } public void RemoveGroup(EcsGroup group) - { - foreach (var item in group) UncheckedRemove(item.id); - } + { foreach (var item in group) Remove(item.id); } + + public void UncheckedAddGroup(EcsReadonlyGroup group) + { foreach (var item in group) AddInternal(item.id); } + public void UncheckedRemoveGroup(EcsReadonlyGroup group) + { foreach (var item in group) RemoveInternal(item.id); } + public void UncheckedAddGroup(EcsGroup group) + { foreach (var item in group) AddInternal(item.id); } + public void UncheckedRemoveGroup(EcsGroup group) + { foreach (var item in group) RemoveInternal(item.id); } #endregion #region GetEnumerator @@ -231,13 +232,9 @@ namespace DCFApixels.DragonECS { delayedOp op = _delayedOps[i]; if (op >= 0) //delayedOp.IsAdded - { UncheckedAdd(op & int.MaxValue); //delayedOp.Entity - } else - { UncheckedRemove(op & int.MaxValue); //delayedOp.Entity - } } } } @@ -253,31 +250,29 @@ namespace DCFApixels.DragonECS public struct Enumerator : IDisposable { private readonly EcsGroup _source; + private readonly int[] _dense; + private readonly int _count; private int _pointer; [MethodImpl(MethodImplOptions.AggressiveInlining)] public Enumerator(EcsGroup group) { _source = group; + _dense = group._dense; + _count = group.Count; _pointer = 0; } - private static EcsProfilerMarker _marker = new EcsProfilerMarker("EcsGroup.Enumerator.Current"); - public ent Current { [MethodImpl(MethodImplOptions.AggressiveInlining)] - get - { - using (_marker.Auto()) - return _source.World.GetEntity(_source._dense[_pointer]); - } + get => _source.World.GetEntity(_dense[_pointer]); } [MethodImpl(MethodImplOptions.AggressiveInlining)] public bool MoveNext() { - return ++_pointer <= _source.Count; + return ++_pointer <= _count && _count < _dense.Length; //_count < _dense.Length дает среде понять что проверки на выход за границы не нужны } [MethodImpl(MethodImplOptions.AggressiveInlining)] @@ -287,10 +282,7 @@ namespace DCFApixels.DragonECS } [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void Reset() - { - _pointer = -1; - } + public void Reset() { } } #endregion } diff --git a/src/EcsPool.cs b/src/EcsPool.cs index a726cae..e82d424 100644 --- a/src/EcsPool.cs +++ b/src/EcsPool.cs @@ -14,9 +14,9 @@ namespace DCFApixels.DragonECS public IEcsWorld World { get; } public Type DataType { get; } public int ID { get; } - public bool Has(int index); - public void Write(int index); - public void Del(int index); + public bool Has(int entityID); + public void Write(int entityID); + public void Del(int entityID); internal void OnWorldResize(int newSize); } @@ -50,13 +50,13 @@ namespace DCFApixels.DragonECS void IEcsPool.OnWorldResize(int newSize) { } } - + public class EcsPool : IEcsPool where T : struct { private readonly int _id; private readonly IEcsWorld _source; - // private readonly EcsGroup _entities; + // private readonly EcsGroup _entities; private int[] _mapping;// index = entity / value = itemIndex;/ value = 0 = no entity private T[] _items; //dense @@ -86,7 +86,7 @@ namespace DCFApixels.DragonECS _mapping = new int[source.EntitesCapacity]; _recycledItems = new int[128]; _recycledItemsCount = 0; - _items =new T[capacity]; + _items = new T[capacity]; _itemsCount = 0; _componentResetHandler = ComponentResetHandler.New(); @@ -102,8 +102,8 @@ namespace DCFApixels.DragonECS private ProfilerMarker _delMark = new ProfilerMarker("EcsPoo.Del"); public ref T Write(int entityID) { - //using (_writeMark.Auto()) - //{ + // using (_writeMark.Auto()) + // { ref int itemIndex = ref _mapping[entityID]; if (itemIndex <= 0) // 0 { @@ -115,7 +115,7 @@ namespace DCFApixels.DragonECS else { itemIndex = _itemsCount++; - if (itemIndex >= _items.Length) + if (itemIndex >= _items.Length) Array.Resize(ref _items, _items.Length << 1); } _mapping[entityID] = itemIndex; @@ -131,35 +131,34 @@ namespace DCFApixels.DragonECS [MethodImpl(MethodImplOptions.AggressiveInlining)] public ref readonly T Read(int entityID) { - //using (_readMark.Auto()) - return ref _items[_mapping[entityID]]; + // using (_readMark.Auto()) + return ref _items[_mapping[entityID]]; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public bool Has(int entityID) { - //using (_hasMark.Auto()) - return _mapping[entityID] > 0; + // using (_hasMark.Auto()) + return _mapping[entityID] > 0; } public void Del(int entityID) { //using (_delMark.Auto()) - //{ - if(_recycledItemsCount >= _recycledItems.Length) + // { + if (_recycledItemsCount >= _recycledItems.Length) Array.Resize(ref _recycledItems, _recycledItems.Length << 1); _recycledItems[_recycledItemsCount++] = _mapping[entityID]; _mapping[entityID] = 0; _itemsCount--; - //_entities.UncheckedRemove(entityID); _source.OnEntityComponentRemoved(entityID, _id); _poolRunnres.del.OnComponentDel(entityID); - //} - } + // } + } #endregion #region IEcsPool - void IEcsPool.Write(int index) + void IEcsPool.Write(int entityID) { - Write(index); + Write(entityID); } #endregion