| | | 1 | | //======================================================================= |
| | | 2 | | // NavigationFlowFieldPayloadCache.cs |
| | | 3 | | //======================================================================= |
| | | 4 | | // MIT License, Copyright (c) 2024-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 | | |
| | | 11 | | namespace Trailblazer.Pathing; |
| | | 12 | | |
| | | 13 | | internal readonly struct NavigationFlowFieldReservation |
| | | 14 | | { |
| | | 15 | | internal NavigationFlowFieldReservation( |
| | | 16 | | NavigationFlowFieldPayloadCache owner, |
| | | 17 | | int slot, |
| | | 18 | | ulong generation) |
| | | 19 | | { |
| | | 20 | | Owner = owner; |
| | | 21 | | Slot = slot; |
| | | 22 | | Generation = generation; |
| | | 23 | | } |
| | | 24 | | |
| | | 25 | | internal readonly NavigationFlowFieldPayloadCache? Owner; |
| | | 26 | | internal readonly int Slot; |
| | | 27 | | internal readonly ulong Generation; |
| | | 28 | | } |
| | | 29 | | |
| | | 30 | | /// <summary>Stores bounded destination-centric canonical flow-field prefixes.</summary> |
| | | 31 | | internal sealed class NavigationFlowFieldPayloadCache : IDisposable |
| | | 32 | | { |
| | | 33 | | internal enum LeaseSlotState : byte |
| | | 34 | | { |
| | | 35 | | Free = 0, |
| | | 36 | | Reserved = 1, |
| | | 37 | | ActivePayload = 2, |
| | | 38 | | ActiveGuide = 3, |
| | | 39 | | Retired = 4 |
| | | 40 | | } |
| | | 41 | | |
| | | 42 | | private enum PrefixRelation : byte |
| | | 43 | | { |
| | | 44 | | Equal = 0, |
| | | 45 | | CandidateLonger = 1, |
| | | 46 | | ExistingLonger = 2 |
| | | 47 | | } |
| | | 48 | | |
| | 1191 | 49 | | private readonly object _sync = new(); |
| | | 50 | | private readonly GridWorld _world; |
| | | 51 | | private readonly NavigationFlowFieldPayloadKey[] _keys; |
| | | 52 | | private readonly CacheEntry?[] _entries; |
| | | 53 | | private readonly byte[] _states; |
| | | 54 | | private readonly int[] _previous; |
| | | 55 | | private readonly int[] _next; |
| | | 56 | | private readonly LeaseSlot[] _leaseSlots; |
| | | 57 | | private readonly int[] _freeLeaseSlots; |
| | | 58 | | private readonly int _mask; |
| | | 59 | | private readonly int _maximumEntries; |
| | | 60 | | private readonly long _maximumReusableBytes; |
| | | 61 | | private readonly long _maximumSinglePayloadBytes; |
| | | 62 | | private readonly long _maximumActivePayloadBytes; |
| | | 63 | | private int _freeLeaseCount; |
| | 1191 | 64 | | private int _leastRecent = -1; |
| | 1191 | 65 | | private int _mostRecent = -1; |
| | | 66 | | private int _count; |
| | | 67 | | private int _activeLeaseCount; |
| | | 68 | | private int _reservedLeaseCount; |
| | | 69 | | private long _cachedBytes; |
| | | 70 | | private long _leasedBytes; |
| | | 71 | | private long _detachedBytes; |
| | | 72 | | private long _reservedPayloadBytes; |
| | | 73 | | private bool _disposed; |
| | | 74 | | private NavigationFlowFieldGuideLease?[] _freeGuides; |
| | | 75 | | private int _freeGuideCount; |
| | | 76 | | private readonly int _guideMapCapacity; |
| | | 77 | | private readonly NavigationImmediateRayWorkspace _immediateRayWorkspace; |
| | | 78 | | |
| | 1191 | 79 | | internal NavigationFlowFieldPayloadCache( |
| | 1191 | 80 | | GridWorld world, |
| | 1191 | 81 | | int maxEntries, |
| | 1191 | 82 | | long maxReusableBytes, |
| | 1191 | 83 | | long maxSinglePayloadBytes, |
| | 1191 | 84 | | long maxActivePayloadBytes, |
| | 1191 | 85 | | int maxActiveLeases, |
| | 1191 | 86 | | int guideMapCapacity, |
| | 1191 | 87 | | NavigationImmediateRayWorkspace immediateRayWorkspace) |
| | | 88 | | { |
| | 1191 | 89 | | SwiftThrowHelper.ThrowIfNull(world, nameof(world)); |
| | 1191 | 90 | | SwiftThrowHelper.ThrowIfNegative(maxEntries, nameof(maxEntries)); |
| | 1191 | 91 | | if (maxReusableBytes < 0) |
| | 1 | 92 | | throw new ArgumentOutOfRangeException(nameof(maxReusableBytes)); |
| | 1190 | 93 | | if (maxSinglePayloadBytes < 0) |
| | 1 | 94 | | throw new ArgumentOutOfRangeException(nameof(maxSinglePayloadBytes)); |
| | 1189 | 95 | | if (maxActivePayloadBytes < 0) |
| | 1 | 96 | | throw new ArgumentOutOfRangeException(nameof(maxActivePayloadBytes)); |
| | 1188 | 97 | | SwiftThrowHelper.ThrowIfNegative(maxActiveLeases, nameof(maxActiveLeases)); |
| | 1188 | 98 | | SwiftThrowHelper.ThrowIfNegative(guideMapCapacity, nameof(guideMapCapacity)); |
| | 1188 | 99 | | SwiftThrowHelper.ThrowIfNull(immediateRayWorkspace, nameof(immediateRayWorkspace)); |
| | 1188 | 100 | | SwiftThrowHelper.ThrowIfArgument( |
| | 1188 | 101 | | maxSinglePayloadBytes > maxActivePayloadBytes, |
| | 1188 | 102 | | nameof(maxSinglePayloadBytes), |
| | 1188 | 103 | | "A single payload cannot exceed the complete active-payload byte ceiling."); |
| | | 104 | | |
| | 1188 | 105 | | int tableSize = 1; |
| | 1188 | 106 | | int required = checked(Math.Max(1, maxEntries * 2)); |
| | 9343 | 107 | | while (tableSize < required) |
| | 8155 | 108 | | tableSize = checked(tableSize * 2); |
| | 1188 | 109 | | _world = world; |
| | 1188 | 110 | | _keys = new NavigationFlowFieldPayloadKey[tableSize]; |
| | 1188 | 111 | | _entries = new CacheEntry[tableSize]; |
| | 1188 | 112 | | _states = new byte[tableSize]; |
| | 1188 | 113 | | _previous = new int[tableSize]; |
| | 1188 | 114 | | _next = new int[tableSize]; |
| | 1188 | 115 | | Array.Fill(_previous, -1); |
| | 1188 | 116 | | Array.Fill(_next, -1); |
| | 1188 | 117 | | _leaseSlots = maxActiveLeases == 0 |
| | 1188 | 118 | | ? Array.Empty<LeaseSlot>() |
| | 1188 | 119 | | : new LeaseSlot[maxActiveLeases]; |
| | 1188 | 120 | | _freeLeaseSlots = maxActiveLeases == 0 |
| | 1188 | 121 | | ? Array.Empty<int>() |
| | 1188 | 122 | | : new int[maxActiveLeases]; |
| | 18818 | 123 | | for (int i = 0; i < _freeLeaseSlots.Length; i++) |
| | 8221 | 124 | | _freeLeaseSlots[i] = _freeLeaseSlots.Length - i - 1; |
| | 1188 | 125 | | _freeLeaseCount = _freeLeaseSlots.Length; |
| | 1188 | 126 | | _freeGuides = maxActiveLeases == 0 |
| | 1188 | 127 | | ? Array.Empty<NavigationFlowFieldGuideLease?>() |
| | 1188 | 128 | | : new NavigationFlowFieldGuideLease[maxActiveLeases]; |
| | 18818 | 129 | | for (int i = 0; i < _freeGuides.Length; i++) |
| | 8221 | 130 | | _freeGuides[i] = new NavigationFlowFieldGuideLease(guideMapCapacity); |
| | 1188 | 131 | | _freeGuideCount = _freeGuides.Length; |
| | 1188 | 132 | | _guideMapCapacity = guideMapCapacity; |
| | 1188 | 133 | | _immediateRayWorkspace = immediateRayWorkspace; |
| | 1188 | 134 | | _mask = tableSize - 1; |
| | 1188 | 135 | | _maximumEntries = maxEntries; |
| | 1188 | 136 | | _maximumReusableBytes = maxReusableBytes; |
| | 1188 | 137 | | _maximumSinglePayloadBytes = maxSinglePayloadBytes; |
| | 1188 | 138 | | _maximumActivePayloadBytes = maxActivePayloadBytes; |
| | 1188 | 139 | | } |
| | | 140 | | |
| | | 141 | | internal int Count |
| | | 142 | | { |
| | 213 | 143 | | get { lock (_sync) return _count; } |
| | | 144 | | } |
| | | 145 | | |
| | | 146 | | internal NavigationImmediateRayWorkspace ImmediateRayWorkspace => |
| | 1379 | 147 | | _immediateRayWorkspace; |
| | | 148 | | |
| | 9202 | 149 | | internal GridWorld World => _world; |
| | | 150 | | |
| | | 151 | | internal long CachedBytes |
| | | 152 | | { |
| | 81 | 153 | | get { lock (_sync) return _cachedBytes; } |
| | | 154 | | } |
| | | 155 | | |
| | | 156 | | internal long LeasedBytes |
| | | 157 | | { |
| | 129 | 158 | | get { lock (_sync) return _leasedBytes; } |
| | | 159 | | } |
| | | 160 | | |
| | | 161 | | internal long DetachedBytes |
| | | 162 | | { |
| | 75 | 163 | | get { lock (_sync) return _detachedBytes; } |
| | | 164 | | } |
| | | 165 | | |
| | | 166 | | internal long ReservedPayloadBytes |
| | | 167 | | { |
| | 63 | 168 | | get { lock (_sync) return _reservedPayloadBytes; } |
| | | 169 | | } |
| | | 170 | | |
| | | 171 | | internal int ActiveLeaseCount |
| | | 172 | | { |
| | 354 | 173 | | get { lock (_sync) return _activeLeaseCount; } |
| | | 174 | | } |
| | | 175 | | |
| | | 176 | | internal int ReservedLeaseCount |
| | | 177 | | { |
| | 165 | 178 | | get { lock (_sync) return _reservedLeaseCount; } |
| | | 179 | | } |
| | | 180 | | |
| | 39 | 181 | | internal long MaximumSinglePayloadBytes => _maximumSinglePayloadBytes; |
| | | 182 | | |
| | | 183 | | internal NavigationGuideStatus TryCreateGuide( |
| | | 184 | | NavigationWorldGraphStore store, |
| | | 185 | | NavigationFlowQueryResult result, |
| | | 186 | | out NavigationFlowFieldLease guide) |
| | | 187 | | { |
| | 1185 | 188 | | SwiftThrowHelper.ThrowIfNull(store, nameof(store)); |
| | 1185 | 189 | | guide = default; |
| | 1185 | 190 | | NavigationFlowFieldPayloadLease payloadLease = result.PayloadLease; |
| | 1185 | 191 | | NavigationFlowFieldGuideLease? inner = null; |
| | 1185 | 192 | | NavigationFlowFieldPayload? payload = null; |
| | 1185 | 193 | | NavigationGuideStatus status = NavigationGuideStatus.Stale; |
| | 1185 | 194 | | int slot = -1; |
| | 1185 | 195 | | ulong generation = 0; |
| | 1185 | 196 | | FixedMathSharp.Fixed64 originIntegrationCost = FixedMathSharp.Fixed64.Zero; |
| | 1185 | 197 | | lock (_sync) |
| | | 198 | | { |
| | 1185 | 199 | | if (!_disposed |
| | 1185 | 200 | | && payloadLease.IsOwnedBy(this, out slot, out generation)) |
| | | 201 | | { |
| | 1184 | 202 | | ref LeaseSlot leaseSlot = ref _leaseSlots[slot]; |
| | 1184 | 203 | | if (leaseSlot.Generation == generation |
| | 1184 | 204 | | && leaseSlot.State == LeaseSlotState.ActivePayload |
| | 1184 | 205 | | && leaseSlot.Entry != null |
| | 1184 | 206 | | && !leaseSlot.Entry.IsInvalidated) |
| | | 207 | | { |
| | 1184 | 208 | | payload = leaseSlot.Entry.Payload; |
| | 1184 | 209 | | NavigationWorldGraph current = store.Current; |
| | 1184 | 210 | | if (current.MapCount > _guideMapCapacity) |
| | | 211 | | { |
| | 3 | 212 | | status = NavigationGuideStatus.CapacityExceeded; |
| | | 213 | | } |
| | 1181 | 214 | | else if (!IsPayloadCurrent(current, payload)) |
| | | 215 | | { |
| | 1 | 216 | | leaseSlot.Entry.IsInvalidated = true; |
| | | 217 | | } |
| | 1180 | 218 | | else if (!payload.TryGetNode( |
| | 1180 | 219 | | result.ResolvedOrigin, |
| | 1180 | 220 | | payload.Key.StartMedium, |
| | 1180 | 221 | | out NavigationFlowFieldNode origin)) |
| | | 222 | | { |
| | 1 | 223 | | status = NavigationGuideStatus.Stale; |
| | | 224 | | } |
| | | 225 | | else |
| | | 226 | | { |
| | 1179 | 227 | | inner = RentGuide( |
| | 1179 | 228 | | _freeGuides, |
| | 1179 | 229 | | ref _freeGuideCount, |
| | 1179 | 230 | | _guideMapCapacity); |
| | 1179 | 231 | | leaseSlot.State = LeaseSlotState.ActiveGuide; |
| | 1179 | 232 | | originIntegrationCost = origin.IntegrationCost; |
| | 1179 | 233 | | status = NavigationGuideStatus.Success; |
| | | 234 | | } |
| | | 235 | | } |
| | | 236 | | } |
| | 1180 | 237 | | } |
| | 1185 | 238 | | if (status != NavigationGuideStatus.Success) |
| | | 239 | | { |
| | 6 | 240 | | payloadLease.Dispose(); |
| | 6 | 241 | | return status; |
| | | 242 | | } |
| | 1179 | 243 | | NavigationFlowFieldGuideLease activeGuide = inner!; |
| | 1179 | 244 | | NavigationFlowFieldPayload activePayload = payload!; |
| | 1179 | 245 | | activeGuide.Bind( |
| | 1179 | 246 | | this, |
| | 1179 | 247 | | store, |
| | 1179 | 248 | | slot, |
| | 1179 | 249 | | generation, |
| | 1179 | 250 | | result.ResolvedOrigin, |
| | 1179 | 251 | | activePayload.Key.StartMedium, |
| | 1179 | 252 | | originIntegrationCost); |
| | 1179 | 253 | | guide = new NavigationFlowFieldLease(activeGuide); |
| | 1179 | 254 | | bool currentAfterBind = IsBoundGuideCurrent( |
| | 1179 | 255 | | TryGetGuidePayload(slot, generation, out _), |
| | 1179 | 256 | | IsPayloadCurrent(store.Current, activePayload)); |
| | 1179 | 257 | | return ResolveBoundGuideValidation(currentAfterBind, ref guide); |
| | | 258 | | } |
| | | 259 | | |
| | | 260 | | internal static bool IsBoundGuideCurrent( |
| | | 261 | | NavigationFlowFieldStatus leaseStatus, |
| | | 262 | | bool payloadCurrent) => |
| | 1183 | 263 | | leaseStatus == NavigationFlowFieldStatus.Success && payloadCurrent; |
| | | 264 | | |
| | | 265 | | internal static NavigationGuideStatus ResolveBoundGuideValidation( |
| | | 266 | | bool current, |
| | | 267 | | ref NavigationFlowFieldLease guide) |
| | | 268 | | { |
| | 1181 | 269 | | if (current) |
| | 1180 | 270 | | return NavigationGuideStatus.Success; |
| | 1 | 271 | | guide.Dispose(); |
| | 1 | 272 | | guide = default; |
| | 1 | 273 | | return NavigationGuideStatus.Stale; |
| | | 274 | | } |
| | | 275 | | |
| | | 276 | | internal bool TryReservePayload( |
| | | 277 | | long maximumBytes, |
| | | 278 | | out NavigationFlowFieldReservation reservation) |
| | | 279 | | { |
| | 555 | 280 | | if (maximumBytes < 0) |
| | 1 | 281 | | throw new ArgumentOutOfRangeException(nameof(maximumBytes)); |
| | 554 | 282 | | reservation = default; |
| | 554 | 283 | | if (maximumBytes > _maximumSinglePayloadBytes) |
| | 3 | 284 | | return false; |
| | 551 | 285 | | lock (_sync) |
| | | 286 | | { |
| | 551 | 287 | | if (_disposed |
| | 551 | 288 | | || maximumBytes > _maximumActivePayloadBytes |
| | 551 | 289 | | - _leasedBytes |
| | 551 | 290 | | - _reservedPayloadBytes |
| | 551 | 291 | | || !TryIssueLeaseSlot(out int slot, out ulong generation)) |
| | | 292 | | { |
| | 4 | 293 | | return false; |
| | | 294 | | } |
| | 547 | 295 | | ref LeaseSlot leaseSlot = ref _leaseSlots[slot]; |
| | 547 | 296 | | leaseSlot.State = LeaseSlotState.Reserved; |
| | 547 | 297 | | leaseSlot.MaximumBytes = maximumBytes; |
| | 547 | 298 | | _reservedLeaseCount++; |
| | 547 | 299 | | _reservedPayloadBytes = checked(_reservedPayloadBytes + maximumBytes); |
| | 547 | 300 | | reservation = new NavigationFlowFieldReservation(this, slot, generation); |
| | 547 | 301 | | return true; |
| | | 302 | | } |
| | 551 | 303 | | } |
| | | 304 | | |
| | | 305 | | internal void ReleasePayloadReservation( |
| | | 306 | | ref NavigationFlowFieldReservation reservation) |
| | | 307 | | { |
| | 442 | 308 | | if (reservation.Owner == null) |
| | | 309 | | { |
| | 131 | 310 | | reservation = default; |
| | 131 | 311 | | return; |
| | | 312 | | } |
| | 311 | 313 | | if (!ReferenceEquals(reservation.Owner, this)) |
| | 1 | 314 | | return; |
| | 310 | 315 | | NavigationFlowFieldReservation released = reservation; |
| | 310 | 316 | | reservation = default; |
| | 310 | 317 | | lock (_sync) |
| | | 318 | | { |
| | 310 | 319 | | if (TryGetReservationSlot(released, out int slot)) |
| | 307 | 320 | | ReleaseReservationSlot(slot); |
| | 310 | 321 | | } |
| | 310 | 322 | | } |
| | | 323 | | |
| | | 324 | | internal NavigationFlowFieldStatus TryCheckout( |
| | | 325 | | NavigationWorldGraphStore store, |
| | | 326 | | NavigationWorldGraph expectedGraph, |
| | | 327 | | NavigationFlowFieldPayloadKey key, |
| | | 328 | | NavigationCellAddress requiredOrigin, |
| | | 329 | | out NavigationFlowFieldPayloadLease lease, |
| | | 330 | | out NavigationFlowFieldPayload? proof) |
| | | 331 | | { |
| | 1425 | 332 | | SwiftThrowHelper.ThrowIfNull(store, nameof(store)); |
| | 1425 | 333 | | SwiftThrowHelper.ThrowIfNull(expectedGraph, nameof(expectedGraph)); |
| | 1425 | 334 | | lease = default; |
| | 1425 | 335 | | proof = null; |
| | 1425 | 336 | | NavigationFlowFieldPayload? checkedPayload = null; |
| | | 337 | | NavigationFlowFieldStatus status; |
| | 1425 | 338 | | lock (_sync) |
| | | 339 | | { |
| | 1425 | 340 | | if (_disposed) |
| | 1 | 341 | | return NavigationFlowFieldStatus.Stale; |
| | 1424 | 342 | | FindSlot(key, out int slot, out bool found); |
| | 1424 | 343 | | if (!found) |
| | 57 | 344 | | return NavigationFlowFieldStatus.Pending; |
| | 1367 | 345 | | CacheEntry entry = _entries[slot]!; |
| | 1367 | 346 | | NavigationWorldGraph graph = store.Current; |
| | 1367 | 347 | | if (!IsPayloadCurrent(graph, entry.Payload)) |
| | | 348 | | { |
| | 3 | 349 | | RemoveAt(slot, invalidate: true); |
| | 3 | 350 | | return NavigationFlowFieldStatus.Stale; |
| | | 351 | | } |
| | 1364 | 352 | | if (!IsPayloadCurrent(expectedGraph, entry.Payload)) |
| | 1 | 353 | | return NavigationFlowFieldStatus.Pending; |
| | 1363 | 354 | | checkedPayload = entry.Payload; |
| | 1363 | 355 | | status = ClassifyCoverage(checkedPayload, requiredOrigin); |
| | 1363 | 356 | | if (status == NavigationFlowFieldStatus.Success) |
| | | 357 | | { |
| | 1356 | 358 | | if (!TryCheckout(entry, out lease)) |
| | 5 | 359 | | return NavigationFlowFieldStatus.CapacityExceeded; |
| | 1351 | 360 | | Touch(slot); |
| | | 361 | | } |
| | 7 | 362 | | else if (status is NavigationFlowFieldStatus.NoPath |
| | 7 | 363 | | or NavigationFlowFieldStatus.CostOverflow) |
| | | 364 | | { |
| | 6 | 365 | | Touch(slot); |
| | | 366 | | } |
| | 7 | 367 | | } |
| | 1358 | 368 | | if (status != NavigationFlowFieldStatus.Pending) |
| | | 369 | | { |
| | 1357 | 370 | | status = CompletePostLockValidation( |
| | 1357 | 371 | | store, |
| | 1357 | 372 | | checkedPayload!, |
| | 1357 | 373 | | status, |
| | 1357 | 374 | | ref lease); |
| | 1357 | 375 | | proof = ResolveValidatedProof(status, checkedPayload); |
| | | 376 | | } |
| | 1358 | 377 | | return status; |
| | 67 | 378 | | } |
| | | 379 | | |
| | | 380 | | internal static NavigationFlowFieldPayload? ResolveValidatedProof( |
| | | 381 | | NavigationFlowFieldStatus status, |
| | 1361 | 382 | | NavigationFlowFieldPayload payload) => status == NavigationFlowFieldStatus.Stale |
| | 1361 | 383 | | ? null |
| | 1361 | 384 | | : payload; |
| | | 385 | | |
| | | 386 | | internal bool IsExactProofCurrent( |
| | | 387 | | NavigationWorldGraphStore store, |
| | | 388 | | NavigationFlowFieldPayload proof, |
| | | 389 | | NavigationCellAddress requiredOrigin, |
| | | 390 | | NavigationFlowFieldStatus expectedStatus) |
| | | 391 | | { |
| | 5 | 392 | | SwiftThrowHelper.ThrowIfNull(store, nameof(store)); |
| | 5 | 393 | | SwiftThrowHelper.ThrowIfNull(proof, nameof(proof)); |
| | | 394 | | bool current; |
| | 5 | 395 | | lock (_sync) |
| | | 396 | | { |
| | 5 | 397 | | FindSlot(proof.Key, out int slot, out bool found); |
| | 5 | 398 | | current = !_disposed |
| | 5 | 399 | | && found |
| | 5 | 400 | | && ReferenceEquals(_entries[slot]!.Payload, proof) |
| | 5 | 401 | | && IsPayloadCurrent(store.Current, proof) |
| | 5 | 402 | | && ClassifyCoverage(proof, requiredOrigin) == expectedStatus; |
| | 5 | 403 | | } |
| | 5 | 404 | | if (current && IsPayloadCurrent(store.Current, proof)) |
| | | 405 | | { |
| | 2 | 406 | | return true; |
| | | 407 | | } |
| | 3 | 408 | | InvalidateExact(proof); |
| | 3 | 409 | | return false; |
| | | 410 | | } |
| | | 411 | | |
| | | 412 | | internal NavigationFlowFieldStatus TryPublishOrPromote( |
| | | 413 | | NavigationWorldGraphStore store, |
| | | 414 | | NavigationFlowFieldPayload payload, |
| | | 415 | | NavigationCellAddress requiredOrigin, |
| | | 416 | | ref NavigationFlowFieldReservation reservation, |
| | | 417 | | out NavigationFlowFieldPayloadLease lease) |
| | | 418 | | { |
| | 270 | 419 | | SwiftThrowHelper.ThrowIfNull(store, nameof(store)); |
| | 270 | 420 | | SwiftThrowHelper.ThrowIfNull(payload, nameof(payload)); |
| | 270 | 421 | | lease = default; |
| | 270 | 422 | | NavigationFlowFieldPayload? canonical = null; |
| | | 423 | | NavigationFlowFieldStatus status; |
| | 270 | 424 | | lock (_sync) |
| | | 425 | | { |
| | 270 | 426 | | if (_disposed || !TryGetReservationSlot(reservation, out int reservationSlot)) |
| | 1 | 427 | | return NavigationFlowFieldStatus.Stale; |
| | 269 | 428 | | ref LeaseSlot reserved = ref _leaseSlots[reservationSlot]; |
| | 269 | 429 | | if (payload.RetainedBytes > reserved.MaximumBytes) |
| | 1 | 430 | | return NavigationFlowFieldStatus.CapacityExceeded; |
| | 268 | 431 | | NavigationWorldGraph graph = store.Current; |
| | 268 | 432 | | if (!IsPayloadCurrent(graph, payload)) |
| | 2 | 433 | | return NavigationFlowFieldStatus.Stale; |
| | | 434 | | |
| | 266 | 435 | | FindSlot(payload.Key, out int currentSlot, out bool found); |
| | 266 | 436 | | if (found |
| | 266 | 437 | | && !IsPayloadCurrent(graph, _entries[currentSlot]!.Payload)) |
| | | 438 | | { |
| | 1 | 439 | | RemoveAt(currentSlot, invalidate: true); |
| | 1 | 440 | | found = false; |
| | | 441 | | } |
| | | 442 | | |
| | 266 | 443 | | status = ClassifyCoverage(payload, requiredOrigin); |
| | | 444 | | |
| | 266 | 445 | | CacheEntry? incumbent = found ? _entries[currentSlot] : null; |
| | 266 | 446 | | if (incumbent != null) |
| | | 447 | | { |
| | 42 | 448 | | PrefixRelation relation = ComparePrefixes(incumbent.Payload, payload); |
| | 14 | 449 | | if (relation is PrefixRelation.Equal or PrefixRelation.ExistingLonger) |
| | | 450 | | { |
| | 5 | 451 | | canonical = incumbent.Payload; |
| | 5 | 452 | | status = ClassifyCoverage(canonical, requiredOrigin); |
| | 5 | 453 | | if (!TryFinishExisting( |
| | 5 | 454 | | currentSlot, |
| | 5 | 455 | | incumbent, |
| | 5 | 456 | | status, |
| | 5 | 457 | | reservationSlot, |
| | 5 | 458 | | ref reservation, |
| | 5 | 459 | | out lease)) |
| | | 460 | | { |
| | 1 | 461 | | return NavigationFlowFieldStatus.CapacityExceeded; |
| | | 462 | | } |
| | | 463 | | } |
| | | 464 | | else |
| | | 465 | | { |
| | 9 | 466 | | canonical = payload; |
| | 9 | 467 | | PublishCandidate( |
| | 9 | 468 | | payload, |
| | 9 | 469 | | status, |
| | 9 | 470 | | currentSlot, |
| | 9 | 471 | | incumbent, |
| | 9 | 472 | | reservationSlot, |
| | 9 | 473 | | ref reservation, |
| | 9 | 474 | | out lease); |
| | | 475 | | } |
| | | 476 | | } |
| | | 477 | | else |
| | | 478 | | { |
| | 224 | 479 | | canonical = payload; |
| | 224 | 480 | | PublishCandidate( |
| | 224 | 481 | | payload, |
| | 224 | 482 | | status, |
| | 224 | 483 | | currentSlot: -1, |
| | 224 | 484 | | incumbent: null, |
| | 224 | 485 | | reservationSlot, |
| | 224 | 486 | | ref reservation, |
| | 224 | 487 | | out lease); |
| | | 488 | | } |
| | 228 | 489 | | } |
| | | 490 | | |
| | 237 | 491 | | return CompletePostLockValidation( |
| | 237 | 492 | | store, |
| | 237 | 493 | | canonical!, |
| | 237 | 494 | | status, |
| | 237 | 495 | | ref lease); |
| | 5 | 496 | | } |
| | | 497 | | |
| | | 498 | | internal NavigationFlowFieldStatus TryGetPayload( |
| | | 499 | | int slot, |
| | | 500 | | ulong generation, |
| | | 501 | | out NavigationFlowFieldPayload payload) |
| | | 502 | | { |
| | 302 | 503 | | lock (_sync) |
| | | 504 | | { |
| | 302 | 505 | | ref LeaseSlot leaseSlot = ref _leaseSlots[slot]; |
| | 302 | 506 | | CacheEntry? entry = leaseSlot.Entry; |
| | 302 | 507 | | if (leaseSlot.Generation == generation |
| | 302 | 508 | | && leaseSlot.State == LeaseSlotState.ActivePayload |
| | 302 | 509 | | && entry != null |
| | 302 | 510 | | && !entry!.IsInvalidated) |
| | | 511 | | { |
| | 292 | 512 | | payload = entry!.Payload; |
| | 292 | 513 | | return NavigationFlowFieldStatus.Success; |
| | | 514 | | } |
| | 10 | 515 | | } |
| | 10 | 516 | | payload = null!; |
| | 10 | 517 | | return NavigationFlowFieldStatus.Stale; |
| | 292 | 518 | | } |
| | | 519 | | |
| | | 520 | | internal NavigationFlowFieldStatus TryGetGuidePayload( |
| | | 521 | | int slot, |
| | | 522 | | ulong generation, |
| | | 523 | | out NavigationFlowFieldPayload payload) |
| | | 524 | | { |
| | 3952 | 525 | | lock (_sync) |
| | | 526 | | { |
| | 3952 | 527 | | ref LeaseSlot leaseSlot = ref _leaseSlots[slot]; |
| | 3952 | 528 | | CacheEntry? entry = leaseSlot.Entry; |
| | 3952 | 529 | | if (leaseSlot.Generation == generation |
| | 3952 | 530 | | && leaseSlot.State == LeaseSlotState.ActiveGuide |
| | 3952 | 531 | | && entry != null |
| | 3952 | 532 | | && !entry!.IsInvalidated) |
| | | 533 | | { |
| | 3950 | 534 | | payload = entry!.Payload; |
| | 3950 | 535 | | return NavigationFlowFieldStatus.Success; |
| | | 536 | | } |
| | 2 | 537 | | } |
| | 2 | 538 | | payload = null!; |
| | 2 | 539 | | return NavigationFlowFieldStatus.Stale; |
| | 3950 | 540 | | } |
| | | 541 | | |
| | | 542 | | internal void Return(int slot, ulong generation) |
| | | 543 | | { |
| | 429 | 544 | | lock (_sync) |
| | | 545 | | { |
| | 429 | 546 | | ref LeaseSlot leaseSlot = ref _leaseSlots[slot]; |
| | 429 | 547 | | bool active = leaseSlot.Generation == generation |
| | 429 | 548 | | && leaseSlot.State == LeaseSlotState.ActivePayload |
| | 429 | 549 | | && leaseSlot.Entry != null; |
| | 429 | 550 | | if (!active) |
| | | 551 | | { |
| | 35 | 552 | | return; |
| | | 553 | | } |
| | 394 | 554 | | CacheEntry entry = leaseSlot.Entry!; |
| | 394 | 555 | | entry.LeaseCount--; |
| | 394 | 556 | | _activeLeaseCount--; |
| | 394 | 557 | | if (entry.LeaseCount == 0) |
| | | 558 | | { |
| | 390 | 559 | | _leasedBytes = checked(_leasedBytes - entry.Payload.RetainedBytes); |
| | 390 | 560 | | if (!entry.IsCached) |
| | 14 | 561 | | _detachedBytes = checked(_detachedBytes - entry.Payload.RetainedBytes); |
| | | 562 | | } |
| | 394 | 563 | | RecycleLeaseSlot(slot); |
| | 394 | 564 | | } |
| | 429 | 565 | | } |
| | | 566 | | |
| | | 567 | | internal void ReturnDetachedGuide( |
| | | 568 | | NavigationFlowFieldGuideLease guide, |
| | | 569 | | ulong generation, |
| | | 570 | | int slot) |
| | | 571 | | { |
| | 1178 | 572 | | lock (_sync) |
| | | 573 | | { |
| | 1178 | 574 | | ref LeaseSlot leaseSlot = ref _leaseSlots[slot]; |
| | 1178 | 575 | | CacheEntry entry = leaseSlot.Entry!; |
| | 1178 | 576 | | entry.LeaseCount--; |
| | 1178 | 577 | | _activeLeaseCount--; |
| | 1178 | 578 | | if (entry.LeaseCount == 0) |
| | | 579 | | { |
| | 1171 | 580 | | _leasedBytes = checked(_leasedBytes - entry.Payload.RetainedBytes); |
| | 1171 | 581 | | if (!entry.IsCached) |
| | | 582 | | { |
| | 2 | 583 | | _detachedBytes = checked( |
| | 2 | 584 | | _detachedBytes - entry.Payload.RetainedBytes); |
| | | 585 | | } |
| | | 586 | | } |
| | 1178 | 587 | | RecycleLeaseSlot(slot); |
| | 1178 | 588 | | _freeGuideCount = RetainGuideIfReusable( |
| | 1178 | 589 | | generation, |
| | 1178 | 590 | | _disposed, |
| | 1178 | 591 | | guide, |
| | 1178 | 592 | | _freeGuides, |
| | 1178 | 593 | | _freeGuideCount); |
| | 1178 | 594 | | } |
| | 1178 | 595 | | } |
| | | 596 | | |
| | | 597 | | internal static int RetainGuideIfReusable( |
| | | 598 | | ulong generation, |
| | | 599 | | bool disposed, |
| | | 600 | | NavigationFlowFieldGuideLease guide, |
| | | 601 | | NavigationFlowFieldGuideLease?[] freeGuides, |
| | | 602 | | int freeGuideCount) |
| | | 603 | | { |
| | 1182 | 604 | | if (disposed |
| | 1182 | 605 | | || !NavigationGenerationCounter.CanAdvance(generation) |
| | 1182 | 606 | | || freeGuideCount >= freeGuides.Length) |
| | | 607 | | { |
| | 4 | 608 | | return freeGuideCount; |
| | | 609 | | } |
| | 1178 | 610 | | freeGuides[freeGuideCount] = guide; |
| | 1178 | 611 | | return freeGuideCount + 1; |
| | | 612 | | } |
| | | 613 | | |
| | | 614 | | internal void RemoveExact(NavigationFlowFieldPayload payload) |
| | | 615 | | { |
| | 3 | 616 | | SwiftThrowHelper.ThrowIfNull(payload, nameof(payload)); |
| | 3 | 617 | | InvalidateExact(payload); |
| | 3 | 618 | | } |
| | | 619 | | |
| | | 620 | | internal void Reset() |
| | | 621 | | { |
| | 15 | 622 | | lock (_sync) |
| | | 623 | | { |
| | 15 | 624 | | if (_disposed) |
| | 1 | 625 | | return; |
| | 14 | 626 | | ResetUnderLock(); |
| | 14 | 627 | | } |
| | 15 | 628 | | } |
| | | 629 | | |
| | | 630 | | public void Dispose() |
| | | 631 | | { |
| | 1189 | 632 | | lock (_sync) |
| | | 633 | | { |
| | 1189 | 634 | | if (_disposed) |
| | 2 | 635 | | return; |
| | 1187 | 636 | | ResetUnderLock(); |
| | 1187 | 637 | | _freeGuides = Array.Empty<NavigationFlowFieldGuideLease?>(); |
| | 1187 | 638 | | _freeGuideCount = 0; |
| | 1187 | 639 | | _disposed = true; |
| | 1187 | 640 | | } |
| | 1189 | 641 | | } |
| | | 642 | | |
| | | 643 | | private bool TryFinishExisting( |
| | | 644 | | int currentSlot, |
| | | 645 | | CacheEntry incumbent, |
| | | 646 | | NavigationFlowFieldStatus status, |
| | | 647 | | int reservationSlot, |
| | | 648 | | ref NavigationFlowFieldReservation reservation, |
| | | 649 | | out NavigationFlowFieldPayloadLease lease) |
| | | 650 | | { |
| | 5 | 651 | | lease = default; |
| | 5 | 652 | | if (status == NavigationFlowFieldStatus.Success) |
| | | 653 | | { |
| | 4 | 654 | | if (!CanActivate(incumbent, reservationSlot)) |
| | 1 | 655 | | return false; |
| | 3 | 656 | | lease = ConvertReservation( |
| | 3 | 657 | | incumbent, |
| | 3 | 658 | | reservationSlot, |
| | 3 | 659 | | ref reservation); |
| | | 660 | | } |
| | | 661 | | else |
| | | 662 | | { |
| | 1 | 663 | | ReleaseReservationSlot(reservationSlot); |
| | 1 | 664 | | reservation = default; |
| | | 665 | | } |
| | 4 | 666 | | Touch(currentSlot); |
| | 4 | 667 | | return true; |
| | | 668 | | } |
| | | 669 | | |
| | | 670 | | private void PublishCandidate( |
| | | 671 | | NavigationFlowFieldPayload payload, |
| | | 672 | | NavigationFlowFieldStatus status, |
| | | 673 | | int currentSlot, |
| | | 674 | | CacheEntry? incumbent, |
| | | 675 | | int reservationSlot, |
| | | 676 | | ref NavigationFlowFieldReservation reservation, |
| | | 677 | | out NavigationFlowFieldPayloadLease lease) |
| | | 678 | | { |
| | 233 | 679 | | lease = default; |
| | 233 | 680 | | bool reusable = _maximumEntries > 0 |
| | 233 | 681 | | && payload.RetainedBytes <= _maximumReusableBytes; |
| | 233 | 682 | | if (!reusable && status != NavigationFlowFieldStatus.Success) |
| | | 683 | | { |
| | 2 | 684 | | ReleaseReservationSlot(reservationSlot); |
| | 2 | 685 | | reservation = default; |
| | 2 | 686 | | return; |
| | | 687 | | } |
| | | 688 | | |
| | | 689 | | CacheEntry candidate; |
| | 231 | 690 | | if (reusable) |
| | | 691 | | { |
| | 229 | 692 | | EvictFor(payload.RetainedBytes, currentSlot); |
| | 229 | 693 | | if (incumbent != null) |
| | 9 | 694 | | RemoveAt(currentSlot, invalidate: false); |
| | 229 | 695 | | candidate = new CacheEntry(payload, isCached: true); |
| | 229 | 696 | | Insert(candidate); |
| | | 697 | | } |
| | | 698 | | else |
| | | 699 | | { |
| | 2 | 700 | | candidate = new CacheEntry(payload, isCached: false); |
| | | 701 | | } |
| | | 702 | | |
| | 231 | 703 | | if (status == NavigationFlowFieldStatus.Success) |
| | | 704 | | { |
| | 221 | 705 | | lease = ConvertReservation(candidate, reservationSlot, ref reservation); |
| | | 706 | | } |
| | | 707 | | else |
| | | 708 | | { |
| | 10 | 709 | | ReleaseReservationSlot(reservationSlot); |
| | 10 | 710 | | reservation = default; |
| | | 711 | | } |
| | 10 | 712 | | } |
| | | 713 | | |
| | | 714 | | private bool TryCheckout( |
| | | 715 | | CacheEntry entry, |
| | | 716 | | out NavigationFlowFieldPayloadLease lease) |
| | | 717 | | { |
| | 1356 | 718 | | lease = default; |
| | 1356 | 719 | | if (entry.LeaseCount == 0 |
| | 1356 | 720 | | && entry.Payload.RetainedBytes > _maximumActivePayloadBytes |
| | 1356 | 721 | | - _leasedBytes |
| | 1356 | 722 | | - _reservedPayloadBytes) |
| | | 723 | | { |
| | 2 | 724 | | return false; |
| | | 725 | | } |
| | 1354 | 726 | | if (!TryIssueLeaseSlot(out int slot, out ulong generation)) |
| | 3 | 727 | | return false; |
| | 1351 | 728 | | ActivateEntry(entry); |
| | 1351 | 729 | | ref LeaseSlot leaseSlot = ref _leaseSlots[slot]; |
| | 1351 | 730 | | leaseSlot.State = LeaseSlotState.ActivePayload; |
| | 1351 | 731 | | leaseSlot.Entry = entry; |
| | 1351 | 732 | | _activeLeaseCount++; |
| | 1351 | 733 | | lease = new NavigationFlowFieldPayloadLease(this, slot, generation); |
| | 1351 | 734 | | return true; |
| | | 735 | | } |
| | | 736 | | |
| | | 737 | | private NavigationFlowFieldPayloadLease ConvertReservation( |
| | | 738 | | CacheEntry entry, |
| | | 739 | | int reservationSlot, |
| | | 740 | | ref NavigationFlowFieldReservation reservation) |
| | | 741 | | { |
| | 224 | 742 | | ref LeaseSlot leaseSlot = ref _leaseSlots[reservationSlot]; |
| | 224 | 743 | | _reservedPayloadBytes -= leaseSlot.MaximumBytes; |
| | 224 | 744 | | _reservedLeaseCount--; |
| | 224 | 745 | | leaseSlot.MaximumBytes = 0; |
| | 224 | 746 | | leaseSlot.State = LeaseSlotState.ActivePayload; |
| | 224 | 747 | | leaseSlot.Entry = entry; |
| | 224 | 748 | | ActivateEntry(entry); |
| | 224 | 749 | | _activeLeaseCount++; |
| | 224 | 750 | | var lease = new NavigationFlowFieldPayloadLease( |
| | 224 | 751 | | this, |
| | 224 | 752 | | reservationSlot, |
| | 224 | 753 | | leaseSlot.Generation); |
| | 224 | 754 | | reservation = default; |
| | 224 | 755 | | return lease; |
| | | 756 | | } |
| | | 757 | | |
| | | 758 | | private void ActivateEntry(CacheEntry entry) |
| | | 759 | | { |
| | 1575 | 760 | | if (entry.LeaseCount == 0) |
| | | 761 | | { |
| | 1564 | 762 | | _leasedBytes = checked(_leasedBytes + entry.Payload.RetainedBytes); |
| | 1564 | 763 | | if (!entry.IsCached) |
| | 2 | 764 | | _detachedBytes = checked(_detachedBytes + entry.Payload.RetainedBytes); |
| | | 765 | | } |
| | 1575 | 766 | | entry.LeaseCount++; |
| | 1575 | 767 | | } |
| | | 768 | | |
| | | 769 | | internal static NavigationFlowFieldGuideLease RentGuide( |
| | | 770 | | NavigationFlowFieldGuideLease?[] freeGuides, |
| | | 771 | | ref int freeGuideCount, |
| | | 772 | | int coveredAddressGenerationCapacity) |
| | | 773 | | { |
| | 1181 | 774 | | if (freeGuideCount == 0) |
| | 1 | 775 | | return new NavigationFlowFieldGuideLease(coveredAddressGenerationCapacity); |
| | 1180 | 776 | | int index = --freeGuideCount; |
| | 1180 | 777 | | NavigationFlowFieldGuideLease guide = freeGuides[index]!; |
| | 1180 | 778 | | freeGuides[index] = null; |
| | 1180 | 779 | | return guide; |
| | | 780 | | } |
| | | 781 | | |
| | | 782 | | private bool CanActivate(CacheEntry entry, int reservationSlot) => |
| | 4 | 783 | | entry.LeaseCount != 0 |
| | 4 | 784 | | || CanActivateNew(entry.Payload.RetainedBytes, reservationSlot); |
| | | 785 | | |
| | | 786 | | private bool CanActivateNew(long retainedBytes, int reservationSlot) |
| | | 787 | | { |
| | 1 | 788 | | long ownReservation = _leaseSlots[reservationSlot].MaximumBytes; |
| | 1 | 789 | | long otherReservations = _reservedPayloadBytes - ownReservation; |
| | 1 | 790 | | return retainedBytes <= _maximumActivePayloadBytes |
| | 1 | 791 | | - _leasedBytes |
| | 1 | 792 | | - otherReservations; |
| | | 793 | | } |
| | | 794 | | |
| | | 795 | | private static NavigationFlowFieldStatus ClassifyCoverage( |
| | | 796 | | NavigationFlowFieldPayload payload, |
| | | 797 | | NavigationCellAddress requiredOrigin) |
| | | 798 | | { |
| | 1636 | 799 | | if (!payload.TryGetNode( |
| | 1636 | 800 | | requiredOrigin, |
| | 1636 | 801 | | payload.Key.StartMedium, |
| | 1636 | 802 | | out NavigationFlowFieldNode origin)) |
| | | 803 | | { |
| | 14 | 804 | | return payload.IsComplete |
| | 14 | 805 | | ? NavigationFlowFieldStatus.NoPath |
| | 14 | 806 | | : NavigationFlowFieldStatus.Pending; |
| | | 807 | | } |
| | 1622 | 808 | | if (!FixedMathSharp.Fixed64.TryAdd( |
| | 1622 | 809 | | origin.IntegrationCost, |
| | 1622 | 810 | | payload.Key.FlowField.ExtraIntegrationCost, |
| | 1622 | 811 | | out FixedMathSharp.Fixed64 requiredCost)) |
| | | 812 | | { |
| | 8 | 813 | | return NavigationFlowFieldStatus.CostOverflow; |
| | | 814 | | } |
| | 1614 | 815 | | return payload.IsComplete || payload.LastSettledCost >= requiredCost |
| | 1614 | 816 | | ? NavigationFlowFieldStatus.Success |
| | 1614 | 817 | | : NavigationFlowFieldStatus.Pending; |
| | | 818 | | } |
| | | 819 | | |
| | | 820 | | private static PrefixRelation ComparePrefixes( |
| | | 821 | | NavigationFlowFieldPayload existing, |
| | | 822 | | NavigationFlowFieldPayload candidate) |
| | | 823 | | { |
| | 42 | 824 | | int common = Math.Min(existing.Nodes.Length, candidate.Nodes.Length); |
| | 288 | 825 | | for (int i = 0; i < common; i++) |
| | | 826 | | { |
| | 118 | 827 | | NavigationFlowFieldNode left = existing.Nodes[i]; |
| | 118 | 828 | | NavigationFlowFieldNode right = candidate.Nodes[i]; |
| | 118 | 829 | | bool nodesEqual = left.Address == right.Address |
| | 118 | 830 | | && left.Medium == right.Medium |
| | 118 | 831 | | && left.IntegrationCost == right.IntegrationCost |
| | 118 | 832 | | && left.SelectedEdge.Equals(right.SelectedEdge) |
| | 118 | 833 | | && left.TransitionInstructionOrdinal |
| | 118 | 834 | | == right.TransitionInstructionOrdinal |
| | 118 | 835 | | && NodeTransitionInstructionsAreEqual( |
| | 118 | 836 | | existing, |
| | 118 | 837 | | left, |
| | 118 | 838 | | candidate, |
| | 118 | 839 | | right); |
| | 118 | 840 | | SwiftThrowHelper.ThrowIfTrue( |
| | 118 | 841 | | !nodesEqual, |
| | 118 | 842 | | message: "Same-key flow payloads do not share one canonical node prefix."); |
| | | 843 | | } |
| | | 844 | | |
| | 26 | 845 | | if (existing.Nodes.Length == candidate.Nodes.Length) |
| | | 846 | | { |
| | 8 | 847 | | bool completionEqual = existing.IsComplete == candidate.IsComplete |
| | 8 | 848 | | && existing.WorldChangeSequence == candidate.WorldChangeSequence |
| | 8 | 849 | | && existing.TransitionInstructions.Length |
| | 8 | 850 | | == candidate.TransitionInstructions.Length |
| | 8 | 851 | | && DependenciesAreEqual(existing.Dependencies, candidate.Dependencies); |
| | 8 | 852 | | SwiftThrowHelper.ThrowIfTrue( |
| | 8 | 853 | | !completionEqual, |
| | 8 | 854 | | message: "Equal flow prefixes do not share exact completion dependencies."); |
| | 3 | 855 | | return PrefixRelation.Equal; |
| | | 856 | | } |
| | | 857 | | |
| | 18 | 858 | | NavigationFlowFieldPayload shorter = existing.Nodes.Length < candidate.Nodes.Length |
| | 18 | 859 | | ? existing |
| | 18 | 860 | | : candidate; |
| | 18 | 861 | | NavigationFlowFieldPayload longer = ReferenceEquals(shorter, existing) |
| | 18 | 862 | | ? candidate |
| | 18 | 863 | | : existing; |
| | 18 | 864 | | SwiftThrowHelper.ThrowIfTrue( |
| | 18 | 865 | | shorter.IsComplete |
| | 18 | 866 | | || !WorldDependencyIsSubset(shorter, longer) |
| | 18 | 867 | | || longer.Nodes[shorter.Nodes.Length].IntegrationCost |
| | 18 | 868 | | <= shorter.LastSettledCost |
| | 18 | 869 | | || !DependenciesAreSubset(shorter.Dependencies, longer.Dependencies), |
| | 18 | 870 | | message: "A complete or dependency-incompatible flow payload cannot be a strict prefix."); |
| | 11 | 871 | | return ReferenceEquals(longer, candidate) |
| | 11 | 872 | | ? PrefixRelation.CandidateLonger |
| | 11 | 873 | | : PrefixRelation.ExistingLonger; |
| | | 874 | | } |
| | | 875 | | |
| | | 876 | | internal bool IsWorldCurrent(NavigationFlowFieldPayload payload) => |
| | 9769 | 877 | | !payload.WorldChangeSequence.HasValue |
| | 9769 | 878 | | || _world.ChangeSequence == payload.WorldChangeSequence.GetValueOrDefault(); |
| | | 879 | | |
| | | 880 | | internal bool IsPayloadCurrent( |
| | | 881 | | NavigationWorldGraph graph, |
| | | 882 | | NavigationFlowFieldPayload payload) => |
| | 9774 | 883 | | graph.IsDependencyCurrent(payload.Dependencies) |
| | 9774 | 884 | | && IsWorldCurrent(payload); |
| | | 885 | | |
| | | 886 | | internal NavigationFlowFieldStatus CompletePostLockValidation( |
| | | 887 | | NavigationWorldGraphStore store, |
| | | 888 | | NavigationFlowFieldPayload payload, |
| | | 889 | | NavigationFlowFieldStatus status, |
| | | 890 | | ref NavigationFlowFieldPayloadLease lease) |
| | | 891 | | { |
| | 1595 | 892 | | if (IsPayloadCurrent(store.Current, payload)) |
| | 1594 | 893 | | return status; |
| | 1 | 894 | | InvalidateExact(payload); |
| | 1 | 895 | | lease.Dispose(); |
| | 1 | 896 | | lease = default; |
| | 1 | 897 | | return NavigationFlowFieldStatus.Stale; |
| | | 898 | | } |
| | | 899 | | |
| | | 900 | | // Both operands are current against the same graph before prefix comparison, |
| | | 901 | | // so two active transition-rule dependencies necessarily share its one version. |
| | | 902 | | private static bool DependenciesAreEqual( |
| | | 903 | | GraphDependencyStamp left, |
| | | 904 | | GraphDependencyStamp right) => |
| | 6 | 905 | | left.Components.Length == right.Components.Length |
| | 6 | 906 | | && left.Pages.Length == right.Pages.Length |
| | 6 | 907 | | && left.HasTransitionRuleDependency == right.HasTransitionRuleDependency |
| | 6 | 908 | | && DependenciesAreSubset(left, right); |
| | | 909 | | |
| | | 910 | | internal static bool DependenciesAreSubset( |
| | | 911 | | GraphDependencyStamp shorter, |
| | | 912 | | GraphDependencyStamp longer) => |
| | | 913 | | // Prefix comparison only runs for payloads with the same key, and that |
| | | 914 | | // key already includes the exact area-policy revision. |
| | 21 | 915 | | (!shorter.HasTransitionRuleDependency |
| | 21 | 916 | | || longer.HasTransitionRuleDependency) |
| | 21 | 917 | | && ComponentDependenciesAreSubset(shorter.Components, longer.Components) |
| | 21 | 918 | | && PageDependenciesAreSubset(shorter.Pages, longer.Pages); |
| | | 919 | | |
| | | 920 | | private static bool WorldDependencyIsSubset( |
| | | 921 | | NavigationFlowFieldPayload shorter, |
| | | 922 | | NavigationFlowFieldPayload longer) => |
| | 17 | 923 | | !shorter.WorldChangeSequence.HasValue |
| | 17 | 924 | | || longer.WorldChangeSequence == shorter.WorldChangeSequence; |
| | | 925 | | |
| | | 926 | | private static bool NodeTransitionInstructionsAreEqual( |
| | | 927 | | NavigationFlowFieldPayload leftPayload, |
| | | 928 | | NavigationFlowFieldNode left, |
| | | 929 | | NavigationFlowFieldPayload rightPayload, |
| | | 930 | | NavigationFlowFieldNode right) |
| | | 931 | | { |
| | 114 | 932 | | int ordinal = left.TransitionInstructionOrdinal; |
| | 114 | 933 | | if (ordinal < 0) |
| | 102 | 934 | | return true; |
| | 12 | 935 | | return TransitionInstructionsAreEqual( |
| | 12 | 936 | | leftPayload.TransitionInstructions[ordinal], |
| | 12 | 937 | | rightPayload.TransitionInstructions[ordinal]); |
| | | 938 | | } |
| | | 939 | | |
| | | 940 | | private static bool TransitionInstructionsAreEqual( |
| | | 941 | | in NavigationTransitionInstruction left, |
| | | 942 | | in NavigationTransitionInstruction right) => |
| | 12 | 943 | | left.IdentityKind == right.IdentityKind |
| | 12 | 944 | | && string.Equals(left.OwnerMapId, right.OwnerMapId, StringComparison.Ordinal) |
| | 12 | 945 | | && string.Equals(left.Id, right.Id, StringComparison.Ordinal) |
| | 12 | 946 | | && left.Type == right.Type |
| | 12 | 947 | | && left.SourceAddress == right.SourceAddress |
| | 12 | 948 | | && left.DestinationAddress == right.DestinationAddress |
| | 12 | 949 | | && left.SourceMedium == right.SourceMedium |
| | 12 | 950 | | && left.DestinationMedium == right.DestinationMedium |
| | 12 | 951 | | && left.SourcePosition == right.SourcePosition |
| | 12 | 952 | | && left.DestinationPosition == right.DestinationPosition |
| | 12 | 953 | | && left.LocomotionHints == right.LocomotionHints; |
| | | 954 | | |
| | | 955 | | private static bool ComponentDependenciesAreSubset( |
| | | 956 | | GraphComponentDependency[] shorter, |
| | | 957 | | GraphComponentDependency[] longer) |
| | | 958 | | { |
| | 19 | 959 | | int longerIndex = 0; |
| | 68 | 960 | | for (int i = 0; i < shorter.Length; i++) |
| | | 961 | | { |
| | 16 | 962 | | GraphComponentDependency expected = shorter[i]; |
| | 17 | 963 | | while (longerIndex < longer.Length |
| | 17 | 964 | | && longer[longerIndex].Key.CompareTo(expected.Key) < 0) |
| | | 965 | | { |
| | 1 | 966 | | longerIndex++; |
| | | 967 | | } |
| | 16 | 968 | | if (longerIndex >= longer.Length |
| | 16 | 969 | | || !longer[longerIndex].Equals(expected)) |
| | | 970 | | { |
| | 1 | 971 | | return false; |
| | | 972 | | } |
| | 15 | 973 | | longerIndex++; |
| | | 974 | | } |
| | 18 | 975 | | return true; |
| | | 976 | | } |
| | | 977 | | |
| | | 978 | | private static bool PageDependenciesAreSubset( |
| | | 979 | | GraphPageDependency[] shorter, |
| | | 980 | | GraphPageDependency[] longer) |
| | | 981 | | { |
| | 18 | 982 | | int longerIndex = 0; |
| | 66 | 983 | | for (int i = 0; i < shorter.Length; i++) |
| | | 984 | | { |
| | 16 | 985 | | GraphPageDependency expected = shorter[i]; |
| | 18 | 986 | | while (longerIndex < longer.Length |
| | 18 | 987 | | && ComparePageAddress(longer[longerIndex], expected) < 0) |
| | | 988 | | { |
| | 2 | 989 | | longerIndex++; |
| | | 990 | | } |
| | 16 | 991 | | if (longerIndex >= longer.Length |
| | 16 | 992 | | || !longer[longerIndex].Equals(expected)) |
| | | 993 | | { |
| | 1 | 994 | | return false; |
| | | 995 | | } |
| | 15 | 996 | | longerIndex++; |
| | | 997 | | } |
| | 17 | 998 | | return true; |
| | | 999 | | } |
| | | 1000 | | |
| | | 1001 | | private static int ComparePageAddress( |
| | | 1002 | | GraphPageDependency left, |
| | | 1003 | | GraphPageDependency right) |
| | | 1004 | | { |
| | 17 | 1005 | | int mapComparison = string.CompareOrdinal(left.MapId, right.MapId); |
| | 17 | 1006 | | return mapComparison != 0 |
| | 17 | 1007 | | ? mapComparison |
| | 17 | 1008 | | : left.PageIndex.CompareTo(right.PageIndex); |
| | | 1009 | | } |
| | | 1010 | | |
| | | 1011 | | private void EvictFor(long candidateBytes, int replacedSlot) |
| | | 1012 | | { |
| | 229 | 1013 | | int prospectiveCount = _count + (replacedSlot >= 0 ? 0 : 1); |
| | 229 | 1014 | | long prospectiveBytes = checked( |
| | 229 | 1015 | | _cachedBytes |
| | 229 | 1016 | | - (replacedSlot >= 0 ? _entries[replacedSlot]!.Payload.RetainedBytes : 0) |
| | 229 | 1017 | | + candidateBytes); |
| | 234 | 1018 | | while (prospectiveCount > _maximumEntries |
| | 234 | 1019 | | || prospectiveBytes > _maximumReusableBytes) |
| | | 1020 | | { |
| | 5 | 1021 | | int eviction = _leastRecent; |
| | 5 | 1022 | | if (eviction == replacedSlot) |
| | 1 | 1023 | | eviction = _next[eviction]; |
| | 5 | 1024 | | long bytes = _entries[eviction]!.Payload.RetainedBytes; |
| | 5 | 1025 | | RemoveAt(eviction, invalidate: false); |
| | 5 | 1026 | | prospectiveCount--; |
| | 5 | 1027 | | prospectiveBytes -= bytes; |
| | | 1028 | | } |
| | 229 | 1029 | | } |
| | | 1030 | | |
| | | 1031 | | private void Insert(CacheEntry entry) |
| | | 1032 | | { |
| | 229 | 1033 | | FindSlot(entry.Payload.Key, out int slot, out _); |
| | 229 | 1034 | | _states[slot] = 1; |
| | 229 | 1035 | | _keys[slot] = entry.Payload.Key; |
| | 229 | 1036 | | _entries[slot] = entry; |
| | 229 | 1037 | | LinkMostRecent(slot); |
| | 229 | 1038 | | _count++; |
| | 229 | 1039 | | _cachedBytes = checked(_cachedBytes + entry.Payload.RetainedBytes); |
| | 229 | 1040 | | } |
| | | 1041 | | |
| | | 1042 | | private bool TryIssueLeaseSlot(out int slot, out ulong generation) |
| | | 1043 | | { |
| | 1902 | 1044 | | if (_freeLeaseCount > 0) |
| | | 1045 | | { |
| | 1898 | 1046 | | slot = _freeLeaseSlots[--_freeLeaseCount]; |
| | 1898 | 1047 | | ref LeaseSlot leaseSlot = ref _leaseSlots[slot]; |
| | | 1048 | | // RecycleLeaseSlot never returns retired generations to the free stack. |
| | 1898 | 1049 | | leaseSlot.Generation++; |
| | 1898 | 1050 | | generation = leaseSlot.Generation; |
| | 1898 | 1051 | | return true; |
| | | 1052 | | } |
| | 4 | 1053 | | slot = -1; |
| | 4 | 1054 | | generation = 0; |
| | 4 | 1055 | | return false; |
| | | 1056 | | } |
| | | 1057 | | |
| | | 1058 | | private bool TryGetReservationSlot( |
| | | 1059 | | NavigationFlowFieldReservation reservation, |
| | | 1060 | | out int slot) |
| | | 1061 | | { |
| | 580 | 1062 | | slot = reservation.Slot; |
| | 580 | 1063 | | return ReferenceEquals(reservation.Owner, this) |
| | 580 | 1064 | | && (uint)slot < (uint)_leaseSlots.Length |
| | 580 | 1065 | | && _leaseSlots[slot].Generation == reservation.Generation |
| | 580 | 1066 | | && _leaseSlots[slot].State == LeaseSlotState.Reserved; |
| | | 1067 | | } |
| | | 1068 | | |
| | | 1069 | | private void ReleaseReservationSlot(int slot) |
| | | 1070 | | { |
| | 320 | 1071 | | ref LeaseSlot leaseSlot = ref _leaseSlots[slot]; |
| | 320 | 1072 | | _reservedPayloadBytes -= leaseSlot.MaximumBytes; |
| | 320 | 1073 | | _reservedLeaseCount--; |
| | 320 | 1074 | | RecycleLeaseSlot(slot); |
| | 320 | 1075 | | } |
| | | 1076 | | |
| | | 1077 | | private void RecycleLeaseSlot(int slot) |
| | | 1078 | | { |
| | 1895 | 1079 | | ref LeaseSlot leaseSlot = ref _leaseSlots[slot]; |
| | 1895 | 1080 | | leaseSlot.Entry = null; |
| | 1895 | 1081 | | leaseSlot.MaximumBytes = 0; |
| | 1895 | 1082 | | leaseSlot.State = RecycleLeaseSlotIdentity( |
| | 1895 | 1083 | | leaseSlot.Generation, |
| | 1895 | 1084 | | slot, |
| | 1895 | 1085 | | _freeLeaseSlots, |
| | 1895 | 1086 | | ref _freeLeaseCount); |
| | 1895 | 1087 | | } |
| | | 1088 | | |
| | | 1089 | | internal static LeaseSlotState RecycleLeaseSlotIdentity( |
| | | 1090 | | ulong generation, |
| | | 1091 | | int slot, |
| | | 1092 | | int[] freeLeaseSlots, |
| | | 1093 | | ref int freeLeaseCount) |
| | | 1094 | | { |
| | 1897 | 1095 | | if (!NavigationGenerationCounter.CanAdvance(generation)) |
| | 1 | 1096 | | return LeaseSlotState.Retired; |
| | 1896 | 1097 | | freeLeaseSlots[freeLeaseCount++] = slot; |
| | 1896 | 1098 | | return LeaseSlotState.Free; |
| | | 1099 | | } |
| | | 1100 | | |
| | | 1101 | | private void InvalidateExact(NavigationFlowFieldPayload payload) |
| | | 1102 | | { |
| | 7 | 1103 | | lock (_sync) |
| | | 1104 | | { |
| | 7 | 1105 | | FindSlot(payload.Key, out int slot, out bool found); |
| | 7 | 1106 | | if (found && ReferenceEquals(_entries[slot]!.Payload, payload)) |
| | 5 | 1107 | | RemoveAt(slot, invalidate: true); |
| | 46 | 1108 | | for (int i = 0; i < _leaseSlots.Length; i++) |
| | | 1109 | | { |
| | 16 | 1110 | | ref LeaseSlot leaseSlot = ref _leaseSlots[i]; |
| | 16 | 1111 | | if ((leaseSlot.State is LeaseSlotState.ActivePayload |
| | 16 | 1112 | | or LeaseSlotState.ActiveGuide) |
| | 16 | 1113 | | && leaseSlot.Entry != null |
| | 16 | 1114 | | && ReferenceEquals(leaseSlot.Entry.Payload, payload)) |
| | | 1115 | | { |
| | 4 | 1116 | | leaseSlot.Entry.IsInvalidated = true; |
| | | 1117 | | } |
| | | 1118 | | } |
| | 7 | 1119 | | } |
| | 7 | 1120 | | } |
| | | 1121 | | |
| | | 1122 | | private void ResetUnderLock() |
| | | 1123 | | { |
| | 1407 | 1124 | | while (_leastRecent >= 0) |
| | 206 | 1125 | | RemoveAt(_leastRecent, invalidate: true); |
| | 1201 | 1126 | | Array.Clear(_states, 0, _states.Length); |
| | 18912 | 1127 | | for (int i = 0; i < _leaseSlots.Length; i++) |
| | | 1128 | | { |
| | 8255 | 1129 | | ref LeaseSlot leaseSlot = ref _leaseSlots[i]; |
| | 8255 | 1130 | | if (leaseSlot.State == LeaseSlotState.Reserved) |
| | | 1131 | | { |
| | 3 | 1132 | | _reservedPayloadBytes -= leaseSlot.MaximumBytes; |
| | 3 | 1133 | | _reservedLeaseCount--; |
| | 3 | 1134 | | RecycleLeaseSlot(i); |
| | | 1135 | | } |
| | 8252 | 1136 | | else if (leaseSlot.State is LeaseSlotState.ActivePayload |
| | 8252 | 1137 | | or LeaseSlotState.ActiveGuide) |
| | | 1138 | | { |
| | 9 | 1139 | | leaseSlot.Entry!.IsInvalidated = true; |
| | | 1140 | | } |
| | | 1141 | | } |
| | 1201 | 1142 | | } |
| | | 1143 | | |
| | | 1144 | | private void FindSlot( |
| | | 1145 | | NavigationFlowFieldPayloadKey key, |
| | | 1146 | | out int slot, |
| | | 1147 | | out bool found) |
| | | 1148 | | { |
| | 1931 | 1149 | | slot = key.GetHashCode() & _mask; |
| | 1931 | 1150 | | int firstRemoved = -1; |
| | 3922 | 1151 | | for (int probe = 0; probe < _states.Length; probe++) |
| | | 1152 | | { |
| | 1958 | 1153 | | byte state = _states[slot]; |
| | 1958 | 1154 | | if (state == 0) |
| | | 1155 | | { |
| | 508 | 1156 | | found = false; |
| | 508 | 1157 | | if (firstRemoved >= 0) |
| | 16 | 1158 | | slot = firstRemoved; |
| | 508 | 1159 | | return; |
| | | 1160 | | } |
| | 1450 | 1161 | | if (state == 1 && _keys[slot].Equals(key)) |
| | | 1162 | | { |
| | 1420 | 1163 | | found = true; |
| | 1420 | 1164 | | return; |
| | | 1165 | | } |
| | 30 | 1166 | | if (state == 2 && firstRemoved < 0) |
| | 19 | 1167 | | firstRemoved = slot; |
| | 30 | 1168 | | slot = (slot + 1) & _mask; |
| | | 1169 | | } |
| | 3 | 1170 | | found = false; |
| | 3 | 1171 | | slot = firstRemoved; |
| | 3 | 1172 | | } |
| | | 1173 | | |
| | | 1174 | | private void Touch(int slot) |
| | | 1175 | | { |
| | 1361 | 1176 | | if (_mostRecent == slot) |
| | 1353 | 1177 | | return; |
| | 8 | 1178 | | Unlink(slot); |
| | 8 | 1179 | | LinkMostRecent(slot); |
| | 8 | 1180 | | } |
| | | 1181 | | |
| | | 1182 | | private void LinkMostRecent(int slot) |
| | | 1183 | | { |
| | 237 | 1184 | | _previous[slot] = _mostRecent; |
| | 237 | 1185 | | _next[slot] = -1; |
| | 237 | 1186 | | if (_mostRecent >= 0) |
| | 21 | 1187 | | _next[_mostRecent] = slot; |
| | | 1188 | | else |
| | 216 | 1189 | | _leastRecent = slot; |
| | 237 | 1190 | | _mostRecent = slot; |
| | 237 | 1191 | | } |
| | | 1192 | | |
| | | 1193 | | private void Unlink(int slot) |
| | | 1194 | | { |
| | 237 | 1195 | | int previous = _previous[slot]; |
| | 237 | 1196 | | int next = _next[slot]; |
| | 237 | 1197 | | if (previous >= 0) |
| | 3 | 1198 | | _next[previous] = next; |
| | | 1199 | | else |
| | 234 | 1200 | | _leastRecent = next; |
| | 237 | 1201 | | if (next >= 0) |
| | 20 | 1202 | | _previous[next] = previous; |
| | | 1203 | | else |
| | 217 | 1204 | | _mostRecent = previous; |
| | 237 | 1205 | | _previous[slot] = -1; |
| | 237 | 1206 | | _next[slot] = -1; |
| | 237 | 1207 | | } |
| | | 1208 | | |
| | | 1209 | | private void RemoveAt(int slot, bool invalidate) |
| | | 1210 | | { |
| | 229 | 1211 | | CacheEntry entry = _entries[slot]!; |
| | 229 | 1212 | | _cachedBytes = checked(_cachedBytes - entry.Payload.RetainedBytes); |
| | 229 | 1213 | | entry.IsCached = false; |
| | 229 | 1214 | | entry.IsInvalidated = entry.IsInvalidated || invalidate; |
| | 229 | 1215 | | if (entry.LeaseCount != 0) |
| | 17 | 1216 | | _detachedBytes = checked(_detachedBytes + entry.Payload.RetainedBytes); |
| | 229 | 1217 | | Unlink(slot); |
| | 229 | 1218 | | _keys[slot] = default; |
| | 229 | 1219 | | _entries[slot] = null; |
| | 229 | 1220 | | _states[slot] = 2; |
| | 229 | 1221 | | _count--; |
| | 229 | 1222 | | } |
| | | 1223 | | |
| | | 1224 | | private sealed class CacheEntry |
| | | 1225 | | { |
| | 231 | 1226 | | internal CacheEntry(NavigationFlowFieldPayload payload, bool isCached) |
| | | 1227 | | { |
| | 231 | 1228 | | Payload = payload; |
| | 231 | 1229 | | IsCached = isCached; |
| | 231 | 1230 | | } |
| | | 1231 | | |
| | | 1232 | | internal NavigationFlowFieldPayload Payload { get; } |
| | | 1233 | | internal int LeaseCount { get; set; } |
| | | 1234 | | internal bool IsCached { get; set; } |
| | | 1235 | | internal bool IsInvalidated { get; set; } |
| | | 1236 | | } |
| | | 1237 | | |
| | | 1238 | | private struct LeaseSlot |
| | | 1239 | | { |
| | | 1240 | | internal CacheEntry? Entry; |
| | | 1241 | | internal long MaximumBytes; |
| | | 1242 | | internal ulong Generation; |
| | | 1243 | | internal LeaseSlotState State; |
| | | 1244 | | } |
| | | 1245 | | } |