|
| 1 | +using System; |
| 2 | +using System.Runtime.CompilerServices; |
| 3 | +using EasyEcs.Core.Components; |
| 4 | + |
| 5 | +namespace EasyEcs.Core; |
| 6 | + |
| 7 | +/// <summary> |
| 8 | +/// Archetype stores entities with the same component configuration. |
| 9 | +/// Uses tombstone pattern (-1) for safe iteration during modifications. |
| 10 | +/// </summary> |
| 11 | +internal class Archetype |
| 12 | +{ |
| 13 | + public Tag ComponentMask; |
| 14 | + public int[] EntityIds; |
| 15 | + public int Count; // Total slots (including tombstones) |
| 16 | + public int AliveCount; // Living entities only |
| 17 | + |
| 18 | + private const int Tombstone = -1; |
| 19 | + |
| 20 | + public Archetype(in Tag componentMask, int initialCapacity = 1024) |
| 21 | + { |
| 22 | + ComponentMask = componentMask; |
| 23 | + EntityIds = new int[initialCapacity]; |
| 24 | + Count = 0; |
| 25 | + AliveCount = 0; |
| 26 | + } |
| 27 | + |
| 28 | + /// <summary> |
| 29 | + /// Add an entity to this archetype. |
| 30 | + /// Reuses tombstone slots first, then appends. |
| 31 | + /// </summary> |
| 32 | + [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] |
| 33 | + public void Add(int entityId) |
| 34 | + { |
| 35 | + // Try to find a tombstone slot for reuse |
| 36 | + var span = EntityIds.AsSpan(0, Count); |
| 37 | + int emptySlot = span.IndexOf(Tombstone); |
| 38 | + |
| 39 | + if (emptySlot >= 0) |
| 40 | + { |
| 41 | + // Reuse tombstone slot |
| 42 | + EntityIds[emptySlot] = entityId; |
| 43 | + AliveCount++; |
| 44 | + } |
| 45 | + else |
| 46 | + { |
| 47 | + // No tombstones available, append to end |
| 48 | + if (Count >= EntityIds.Length) |
| 49 | + { |
| 50 | + // Grow array (doubling strategy) |
| 51 | + Array.Resize(ref EntityIds, EntityIds.Length * 2); |
| 52 | + } |
| 53 | + |
| 54 | + EntityIds[Count++] = entityId; |
| 55 | + AliveCount++; |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + /// <summary> |
| 60 | + /// Remove an entity from this archetype. |
| 61 | + /// Marks the slot as tombstone (-1) for safe iteration. |
| 62 | + /// </summary> |
| 63 | + [MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)] |
| 64 | + public void Remove(int entityId) |
| 65 | + { |
| 66 | + // Find and tombstone the entity |
| 67 | + var span = EntityIds.AsSpan(0, Count); |
| 68 | + int idx = span.IndexOf(entityId); |
| 69 | + |
| 70 | + if (idx >= 0) |
| 71 | + { |
| 72 | + EntityIds[idx] = Tombstone; |
| 73 | + AliveCount--; |
| 74 | + |
| 75 | + // Optional: Compact if heavily fragmented (>50% tombstones) |
| 76 | + // This prevents memory waste while keeping iteration safe |
| 77 | + if (AliveCount > 0 && AliveCount < Count / 2) |
| 78 | + { |
| 79 | + Compact(); |
| 80 | + } |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + /// <summary> |
| 85 | + /// Compact the array by removing all tombstones. |
| 86 | + /// Only called when heavily fragmented (>50% dead slots). |
| 87 | + /// </summary> |
| 88 | + [MethodImpl(MethodImplOptions.AggressiveOptimization)] |
| 89 | + public void Compact() |
| 90 | + { |
| 91 | + int writeIdx = 0; |
| 92 | + var span = EntityIds.AsSpan(0, Count); |
| 93 | + |
| 94 | + for (int readIdx = 0; readIdx < Count; readIdx++) |
| 95 | + { |
| 96 | + if (span[readIdx] != Tombstone) |
| 97 | + { |
| 98 | + EntityIds[writeIdx++] = EntityIds[readIdx]; |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + Count = writeIdx; |
| 103 | + // AliveCount should already match Count after compaction |
| 104 | + } |
| 105 | + |
| 106 | + /// <summary> |
| 107 | + /// Get a read-only span of entity IDs for iteration. |
| 108 | + /// Includes tombstones - iterator must skip -1 values. |
| 109 | + /// </summary> |
| 110 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 111 | + public ReadOnlySpan<int> GetEntitySpan() => new ReadOnlySpan<int>(EntityIds, 0, Count); |
| 112 | + |
| 113 | + /// <summary> |
| 114 | + /// Get fragmentation ratio (percentage of tombstones). |
| 115 | + /// </summary> |
| 116 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 117 | + public float GetFragmentation() |
| 118 | + { |
| 119 | + return Count > 0 ? 1.0f - (float)AliveCount / Count : 0f; |
| 120 | + } |
| 121 | +} |
0 commit comments