| | | 1 | | //======================================================================= |
| | | 2 | | // RetainedPartitionLifecycle.cs |
| | | 3 | | //======================================================================= |
| | | 4 | | // MIT License, Copyright (c) 2026-present David Oravsky (mrdav30) |
| | | 5 | | // See LICENSE file in the project root for full license information. |
| | | 6 | | //======================================================================= |
| | | 7 | | |
| | | 8 | | using System; |
| | | 9 | | using GridForge.Grids; |
| | | 10 | | using SwiftCollections; |
| | | 11 | | |
| | | 12 | | namespace Gravitas; |
| | | 13 | | |
| | | 14 | | /// <summary> |
| | | 15 | | /// Tracks retained partitions and releases empty payloads through expiry, reuse, or context teardown. |
| | | 16 | | /// </summary> |
| | | 17 | | internal static class RetainedPartitionLifecycle |
| | | 18 | | { |
| | | 19 | | internal static void DetachAll<TPartition, TOwner>( |
| | | 20 | | SwiftList<TPartition> retainedPartitions, |
| | | 21 | | SwiftList<TPartition> emptyPartitions, |
| | | 22 | | GridWorld world, |
| | | 23 | | TOwner owner, |
| | | 24 | | Action<TPartition> releasePartition, |
| | | 25 | | string partitionName, |
| | | 26 | | string detachError) |
| | | 27 | | where TPartition : class, IRetainedPhysicsPartition<TOwner> |
| | | 28 | | { |
| | | 29 | | // Reset is a context boundary; retained GridForge payloads are a runtime cache, not replay state. |
| | 13482 | 30 | | while (retainedPartitions.Count > 0) |
| | | 31 | | { |
| | 13093 | 32 | | TPartition partition = retainedPartitions[retainedPartitions.Count - 1]; |
| | 13093 | 33 | | if (!partition.IsOwnedBy(owner)) |
| | | 34 | | { |
| | 1 | 35 | | int disabledCursor = -1; |
| | 1 | 36 | | Untrack(retainedPartitions, emptyPartitions, owner, partition, ref disabledCursor); |
| | 1 | 37 | | continue; |
| | | 38 | | } |
| | | 39 | | |
| | 13092 | 40 | | if (world.TryGetVoxel(partition.WorldIndex, out Voxel? voxel) |
| | 13092 | 41 | | && voxel!.TryGetPartition(out TPartition? attachedPartition) |
| | 13092 | 42 | | && ReferenceEquals(attachedPartition, partition)) |
| | | 43 | | { |
| | 13091 | 44 | | bool removed = voxel.TryRemovePartition<TPartition>(); |
| | 13091 | 45 | | ChronicleGuard.ThrowIfTrue(!removed, partitionName, detachError); |
| | | 46 | | |
| | 13091 | 47 | | if (partition.IsOwnedBy(owner)) |
| | 1 | 48 | | releasePartition(partition); |
| | | 49 | | |
| | 1 | 50 | | continue; |
| | | 51 | | } |
| | | 52 | | |
| | 1 | 53 | | releasePartition(partition); |
| | | 54 | | } |
| | 389 | 55 | | } |
| | | 56 | | |
| | | 57 | | internal static void Track<TPartition, TOwner>( |
| | | 58 | | SwiftList<TPartition> retainedPartitions, |
| | | 59 | | SwiftList<TPartition> emptyPartitions, |
| | | 60 | | TOwner owner, |
| | | 61 | | TPartition partition, |
| | | 62 | | string partitionName) |
| | | 63 | | where TPartition : class, IRetainedPhysicsPartition<TOwner> |
| | | 64 | | { |
| | 1718025 | 65 | | ChronicleGuard.ThrowIfNull(partition, nameof(partition)); |
| | | 66 | | // Tracking runs once per newly occupied voxel. Format validation details |
| | | 67 | | // only on failure, rather than allocating a message for every payload. |
| | 1718025 | 68 | | if (partition.RetainedIndex >= 0) |
| | 1 | 69 | | throw new ArgumentException($"{partitionName} is already tracked as retained.", nameof(partition)); |
| | | 70 | | |
| | 1718024 | 71 | | partition.SetRetainedIndex(retainedPartitions.Count); |
| | 1718024 | 72 | | retainedPartitions.Add(partition); |
| | | 73 | | // Every retained payload can become empty later. Reserve those slots |
| | | 74 | | // during registration so ordinary membership removal cannot allocate. |
| | 1718024 | 75 | | emptyPartitions.EnsureCapacity(retainedPartitions.Count); |
| | 1718024 | 76 | | RefreshEmptyEligibility(emptyPartitions, owner, partition); |
| | 1718024 | 77 | | } |
| | | 78 | | |
| | | 79 | | internal static void Untrack<TPartition, TOwner>( |
| | | 80 | | SwiftList<TPartition> retainedPartitions, |
| | | 81 | | SwiftList<TPartition> emptyPartitions, |
| | | 82 | | TOwner owner, |
| | | 83 | | TPartition partition, |
| | | 84 | | ref int retirementCursor) |
| | | 85 | | where TPartition : class, IRetainedPhysicsPartition<TOwner> |
| | | 86 | | { |
| | 1717965 | 87 | | UntrackEmpty<TPartition, TOwner>(emptyPartitions, partition); |
| | 1717965 | 88 | | int index = FindIndex(retainedPartitions, owner, partition); |
| | 1717965 | 89 | | if (index < 0) |
| | | 90 | | { |
| | 22 | 91 | | partition.ClearRetainedIndex(); |
| | 22 | 92 | | return; |
| | | 93 | | } |
| | | 94 | | |
| | 1717943 | 95 | | int lastIndex = retainedPartitions.Count - 1; |
| | 1717943 | 96 | | if (index != lastIndex) |
| | | 97 | | { |
| | 1181756 | 98 | | TPartition movedPartition = retainedPartitions[lastIndex]; |
| | 1181756 | 99 | | retainedPartitions[index] = movedPartition; |
| | 1181756 | 100 | | movedPartition.SetRetainedIndex(index); |
| | | 101 | | } |
| | | 102 | | |
| | 1717943 | 103 | | retainedPartitions.RemoveAt(lastIndex); |
| | 1717943 | 104 | | partition.ClearRetainedIndex(); |
| | | 105 | | |
| | 1717943 | 106 | | if (retirementCursor < 0) |
| | 2 | 107 | | return; |
| | | 108 | | |
| | 1717941 | 109 | | if (retirementCursor > index) |
| | 155998 | 110 | | retirementCursor--; |
| | 1717941 | 111 | | if (retirementCursor >= retainedPartitions.Count) |
| | 15735 | 112 | | retirementCursor = 0; |
| | 1717941 | 113 | | } |
| | | 114 | | |
| | | 115 | | internal static void RetireExpired<TPartition, TOwner>( |
| | | 116 | | SwiftList<TPartition> retainedPartitions, |
| | | 117 | | GridWorld world, |
| | | 118 | | TOwner owner, |
| | | 119 | | int budget, |
| | | 120 | | long currentFrame, |
| | | 121 | | int timeToKillFrames, |
| | | 122 | | Action<TPartition> releasePartition, |
| | | 123 | | ref int retirementCursor) |
| | | 124 | | where TPartition : class, IRetainedPhysicsPartition<TOwner> |
| | | 125 | | { |
| | 103361 | 126 | | if (budget <= 0 || retainedPartitions.Count == 0) |
| | 2886 | 127 | | return; |
| | | 128 | | |
| | 100475 | 129 | | int inspected = 0; |
| | 6555442 | 130 | | while (inspected < budget && retainedPartitions.Count > 0) |
| | | 131 | | { |
| | 6454967 | 132 | | if (retirementCursor >= retainedPartitions.Count) |
| | 131854 | 133 | | retirementCursor = 0; |
| | | 134 | | |
| | 6454967 | 135 | | TPartition partition = retainedPartitions[retirementCursor]; |
| | 6454967 | 136 | | inspected++; |
| | | 137 | | |
| | 6454967 | 138 | | if (!ShouldRetire(partition, owner, currentFrame, timeToKillFrames)) |
| | | 139 | | { |
| | 6454081 | 140 | | retirementCursor++; |
| | 6454081 | 141 | | continue; |
| | | 142 | | } |
| | | 143 | | |
| | 886 | 144 | | Retire(world, owner, partition, releasePartition); |
| | | 145 | | } |
| | 100475 | 146 | | } |
| | | 147 | | |
| | | 148 | | internal static bool TryRetireEmptyForReuse<TPartition, TOwner>( |
| | | 149 | | SwiftList<TPartition> emptyPartitions, |
| | | 150 | | SwiftStack<TPartition> inactivePartitionPool, |
| | | 151 | | GridWorld world, |
| | | 152 | | TOwner owner, |
| | | 153 | | Action<TPartition> releasePartition) |
| | | 154 | | where TPartition : class, IRetainedPhysicsPartition<TOwner> |
| | | 155 | | { |
| | | 156 | | // Membership transitions maintain this dense list. Occupied cold rentals |
| | | 157 | | // never scan retained payloads; reuse takes the last dense eligible entry. |
| | 1717694 | 158 | | while (emptyPartitions.Count > 0) |
| | | 159 | | { |
| | 87609 | 160 | | int lastIndex = emptyPartitions.Count - 1; |
| | 87609 | 161 | | TPartition partition = emptyPartitions[lastIndex]; |
| | 87609 | 162 | | RemoveEmptyAt<TPartition, TOwner>(emptyPartitions, lastIndex); |
| | | 163 | | |
| | | 164 | | // Recheck ownership and eligibility before detaching a selected payload, |
| | | 165 | | // without rediscovering occupied payloads or trusting stale cache entries. |
| | 87609 | 166 | | if (partition.RetainedIndex < 0 || !partition.IsOwnedBy(owner) || !partition.IsEmpty || partition.IsAllocate |
| | | 167 | | continue; |
| | | 168 | | |
| | 87605 | 169 | | int poolCount = inactivePartitionPool.Count; |
| | 87605 | 170 | | Retire(world, owner, partition, releasePartition); |
| | 87605 | 171 | | if (inactivePartitionPool.Count > poolCount) |
| | 87604 | 172 | | return true; |
| | | 173 | | } |
| | | 174 | | |
| | 1630085 | 175 | | return false; |
| | | 176 | | } |
| | | 177 | | |
| | | 178 | | internal static void RefreshEmptyEligibility<TPartition, TOwner>( |
| | | 179 | | SwiftList<TPartition> emptyPartitions, |
| | | 180 | | TOwner owner, |
| | | 181 | | TPartition partition) |
| | | 182 | | where TPartition : class, IRetainedPhysicsPartition<TOwner> |
| | | 183 | | { |
| | 4240070 | 184 | | bool eligible = partition.RetainedIndex >= 0 && partition.IsOwnedBy(owner) |
| | 4240070 | 185 | | && partition.IsEmpty && !partition.IsAllocated; |
| | 4240070 | 186 | | if (!eligible) |
| | | 187 | | { |
| | 2253631 | 188 | | UntrackEmpty<TPartition, TOwner>(emptyPartitions, partition); |
| | 2253631 | 189 | | return; |
| | | 190 | | } |
| | | 191 | | |
| | 1986439 | 192 | | if ((uint)partition.EmptyIndex < (uint)emptyPartitions.Count |
| | 1986439 | 193 | | && ReferenceEquals(emptyPartitions[partition.EmptyIndex], partition)) |
| | 4 | 194 | | return; |
| | | 195 | | |
| | 1986435 | 196 | | partition.EmptyIndex = emptyPartitions.Count; |
| | 1986435 | 197 | | emptyPartitions.Add(partition); |
| | 1986435 | 198 | | } |
| | | 199 | | |
| | | 200 | | private static void UntrackEmpty<TPartition, TOwner>( |
| | | 201 | | SwiftList<TPartition> emptyPartitions, |
| | | 202 | | TPartition partition) |
| | | 203 | | where TPartition : class, IRetainedPhysicsPartition<TOwner> |
| | | 204 | | { |
| | 3971596 | 205 | | int index = partition.EmptyIndex; |
| | 3971596 | 206 | | if ((uint)index < (uint)emptyPartitions.Count && ReferenceEquals(emptyPartitions[index], partition)) |
| | 1898784 | 207 | | RemoveEmptyAt<TPartition, TOwner>(emptyPartitions, index); |
| | | 208 | | else |
| | 2072812 | 209 | | partition.EmptyIndex = -1; |
| | 2072812 | 210 | | } |
| | | 211 | | |
| | | 212 | | private static void RemoveEmptyAt<TPartition, TOwner>(SwiftList<TPartition> emptyPartitions, int index) |
| | | 213 | | where TPartition : class, IRetainedPhysicsPartition<TOwner> |
| | | 214 | | { |
| | | 215 | | // Swap-back keeps transitions constant-time; update the moved payload's |
| | | 216 | | // slot so a later reoccupation removes it without searching the list. |
| | 1986393 | 217 | | TPartition partition = emptyPartitions[index]; |
| | 1986393 | 218 | | int lastIndex = emptyPartitions.Count - 1; |
| | 1986393 | 219 | | if (index != lastIndex) |
| | | 220 | | { |
| | 98561 | 221 | | TPartition movedPartition = emptyPartitions[lastIndex]; |
| | 98561 | 222 | | emptyPartitions[index] = movedPartition; |
| | 98561 | 223 | | movedPartition.EmptyIndex = index; |
| | | 224 | | } |
| | | 225 | | |
| | 1986393 | 226 | | emptyPartitions.RemoveAt(lastIndex); |
| | 1986393 | 227 | | partition.EmptyIndex = -1; |
| | 1986393 | 228 | | } |
| | | 229 | | |
| | | 230 | | private static int FindIndex<TPartition, TOwner>( |
| | | 231 | | SwiftList<TPartition> retainedPartitions, |
| | | 232 | | TOwner owner, |
| | | 233 | | TPartition partition) |
| | | 234 | | where TPartition : class, IRetainedPhysicsPartition<TOwner> |
| | | 235 | | { |
| | | 236 | | _ = owner; |
| | 1717965 | 237 | | int index = partition.RetainedIndex; |
| | 1717965 | 238 | | return (uint)index < (uint)retainedPartitions.Count && ReferenceEquals(retainedPartitions[index], partition) |
| | 1717965 | 239 | | ? index |
| | 1717965 | 240 | | : -1; |
| | | 241 | | } |
| | | 242 | | |
| | | 243 | | private static bool ShouldRetire<TPartition, TOwner>( |
| | | 244 | | TPartition partition, |
| | | 245 | | TOwner owner, |
| | | 246 | | long currentFrame, |
| | | 247 | | int timeToKillFrames) |
| | | 248 | | where TPartition : class, IRetainedPhysicsPartition<TOwner> |
| | | 249 | | { |
| | 6454967 | 250 | | if (!partition.IsOwnedBy(owner) || !partition.IsEmpty || partition.IsAllocated || partition.EmptySinceFrame < 0) |
| | 6182000 | 251 | | return false; |
| | | 252 | | |
| | 272967 | 253 | | long idleFrames = currentFrame - partition.EmptySinceFrame; |
| | 272967 | 254 | | return idleFrames >= timeToKillFrames; |
| | | 255 | | } |
| | | 256 | | |
| | | 257 | | private static void Retire<TPartition, TOwner>( |
| | | 258 | | GridWorld world, |
| | | 259 | | TOwner owner, |
| | | 260 | | TPartition partition, |
| | | 261 | | Action<TPartition> releasePartition) |
| | | 262 | | where TPartition : class, IRetainedPhysicsPartition<TOwner> |
| | | 263 | | { |
| | | 264 | | _ = owner; |
| | 88491 | 265 | | if (!world.TryGetVoxel(partition.WorldIndex, out Voxel? voxel)) |
| | | 266 | | { |
| | 4 | 267 | | releasePartition(partition); |
| | 4 | 268 | | return; |
| | | 269 | | } |
| | | 270 | | |
| | 88487 | 271 | | if (!voxel!.TryGetPartition(out TPartition? attachedPartition) |
| | 88487 | 272 | | || !ReferenceEquals(attachedPartition, partition)) |
| | | 273 | | { |
| | 2 | 274 | | releasePartition(partition); |
| | 2 | 275 | | return; |
| | | 276 | | } |
| | | 277 | | |
| | 88485 | 278 | | _ = voxel.TryRemovePartition<TPartition>(); |
| | 88485 | 279 | | } |
| | | 280 | | } |