From db863bf8bffbf7b7d8f20bb3335ada29f4a3d51b Mon Sep 17 00:00:00 2001 From: Mikhail <99481254+DCFApixels@users.noreply.github.com> Date: Thu, 23 Nov 2023 00:37:03 +0800 Subject: [PATCH] tmp commit --- src/EcsAspect.cs | 13 ++++++++----- src/EcsWorld.cs | 4 ++-- src/Pools/EcsPool.cs | 2 +- src/Pools/EcsTestPool.cs | 9 ++++++++- src/Utils/SparseArray64.cs | 12 ++++++------ 5 files changed, 25 insertions(+), 15 deletions(-) diff --git a/src/EcsAspect.cs b/src/EcsAspect.cs index 7291cb4..e3b69a6 100644 --- a/src/EcsAspect.cs +++ b/src/EcsAspect.cs @@ -142,7 +142,7 @@ namespace DCFApixels.DragonECS Dictionary r = new Dictionary(); foreach (var id in maskInc) { - var bit = EcsMaskBit.FromPoolID(id); + var bit = EcsMaskBit.FromID(id); if(!r.TryAdd(bit.chankIndex, bit.mask)) r[bit.chankIndex] = r[bit.chankIndex] | bit.mask; } @@ -150,7 +150,7 @@ namespace DCFApixels.DragonECS r.Clear(); foreach (var id in maskExc) { - var bit = EcsMaskBit.FromPoolID(id); + var bit = EcsMaskBit.FromID(id); if (!r.TryAdd(bit.chankIndex, bit.mask)) r[bit.chankIndex] = r[bit.chankIndex] | bit.mask; } @@ -218,6 +218,10 @@ namespace DCFApixels.DragonECS #region Mask public readonly struct EcsMaskBit { + private const int BITS = 32; + private const int DIV_SHIFT = 5; + private const int MOD_MASK = BITS - 1; + public readonly int chankIndex; public readonly int mask; public EcsMaskBit(int chankIndex, int mask) @@ -225,11 +229,10 @@ namespace DCFApixels.DragonECS this.chankIndex = chankIndex; this.mask = mask; } - public static EcsMaskBit FromPoolID(int id) + public static EcsMaskBit FromID(int id) { - return new EcsMaskBit(id / 32, 1 << (id % 32)); + return new EcsMaskBit(id >> DIV_SHIFT, 1 << (id & MOD_MASK)); //аналогично new EcsMaskBit(id / BITS, 1 << (id % BITS)) но быстрее } - public override string ToString() { return $"bit({chankIndex}, {mask})"; diff --git a/src/EcsWorld.cs b/src/EcsWorld.cs index 3791d87..0d0051e 100644 --- a/src/EcsWorld.cs +++ b/src/EcsWorld.cs @@ -273,14 +273,14 @@ namespace DCFApixels.DragonECS internal void IncrementEntityComponentCount(int entityID, int componentID) { _componentCounts[entityID]++; - EcsMaskBit bit = EcsMaskBit.FromPoolID(componentID); + EcsMaskBit bit = EcsMaskBit.FromID(componentID); _entitiesComponentMasks[entityID][bit.chankIndex] |= bit.mask; } [MethodImpl(MethodImplOptions.AggressiveInlining)] internal void DecrementEntityComponentCount(int entityID, int componentID) { var count = --_componentCounts[entityID]; - EcsMaskBit bit = EcsMaskBit.FromPoolID(componentID); + EcsMaskBit bit = EcsMaskBit.FromID(componentID); _entitiesComponentMasks[entityID][bit.chankIndex] &= ~bit.mask; if (count == 0 && _allEntites.Has(entityID)) diff --git a/src/Pools/EcsPool.cs b/src/Pools/EcsPool.cs index cd7d1aa..ba41891 100644 --- a/src/Pools/EcsPool.cs +++ b/src/Pools/EcsPool.cs @@ -140,7 +140,7 @@ namespace DCFApixels.DragonECS _source = world; _componentID = componentID; - _maskBit = EcsMaskBit.FromPoolID(componentID); + _maskBit = EcsMaskBit.FromID(componentID); const int capacity = 512; diff --git a/src/Pools/EcsTestPool.cs b/src/Pools/EcsTestPool.cs index 7eaff35..3bbc04f 100644 --- a/src/Pools/EcsTestPool.cs +++ b/src/Pools/EcsTestPool.cs @@ -187,7 +187,14 @@ namespace DCFApixels.DragonECS if (!Has(key: key)) EcsPoolThrowHalper.ThrowNotHaveComponent(unchecked((int)key)); #endif _listeners.InvokeOnGet(unchecked((int)key)); - return ref _entries[FindEntry(key)].value; + //return ref _entries[FindEntry(key)].value; + + for (int i = _buckets[unchecked((int)key & _modBitMask)]; i >= 0; i = _entries[i].next) + if (_entries[i].key == key) return ref _entries[i].value; // return i; + +#pragma warning disable CS0251 // + return ref _entries[-1].value; // . +#pragma warning restore CS0251 // } [MethodImpl(MethodImplOptions.AggressiveInlining)] diff --git a/src/Utils/SparseArray64.cs b/src/Utils/SparseArray64.cs index 69053a4..946f800 100644 --- a/src/Utils/SparseArray64.cs +++ b/src/Utils/SparseArray64.cs @@ -25,15 +25,15 @@ namespace DCFApixels.DragonECS.Utils private int _modBitMask; #region Properties - public TValue this[long keyX, long keyY] + public ref TValue this[long keyX, long keyY] { - get => _entries[FindEntry(keyX + (keyY << 32))].value; - set => Insert(keyX + (keyY << 32), value); + get => ref _entries[FindEntry(keyX + (keyY << 32))].value; + //set => Insert(keyX + (keyY << 32), value); } - public TValue this[long key] + public ref TValue this[long key] { - get => _entries[FindEntry(key)].value; - set => Insert(key, value); + get => ref _entries[FindEntry(key)].value; + //set => Insert(key, value); } public int Count => _count;