| | | 1 | | //======================================================================= |
| | | 2 | | // NavigationOperationProcessor.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 FixedMathSharp; |
| | | 10 | | using GridForge.Grids.Topology; |
| | | 11 | | using SwiftCollections; |
| | | 12 | | using SwiftCollections.Utility; |
| | | 13 | | |
| | | 14 | | namespace Trailblazer.Pathing; |
| | | 15 | | |
| | | 16 | | /// <summary> |
| | | 17 | | /// Executes map and overlay candidates against persistent graph roots. |
| | | 18 | | /// </summary> |
| | | 19 | | internal sealed class NavigationOperationProcessor |
| | | 20 | | { |
| | | 21 | | private const int BaseCoalescingScratchBytes = 2_048; |
| | | 22 | | private const int FixedScratchBytesPerOperation = 8; |
| | | 23 | | private const int MapCoverageScratchBytes = 64; |
| | | 24 | | |
| | | 25 | | private readonly NavigationOperationLimits _limits; |
| | | 26 | | private readonly int _maxBakedCellsPerMap; |
| | | 27 | | private readonly int _navigationAreaCount; |
| | 1302 | 28 | | private readonly SwiftList<PendingOperation> _pending = new(); |
| | | 29 | | private NavigationOperationCandidate _candidate; |
| | | 30 | | private readonly GridCellPrism[] _corridorPrisms; |
| | | 31 | | private readonly Vector3d[] _corridorWaypoints; |
| | | 32 | | private readonly NavigationCellAddress[] _corridorAddresses; |
| | | 33 | | private readonly NavigationAddressStampSet _corridorAddressSet; |
| | 1302 | 34 | | private readonly MaintenanceWorkMeter _unboundedMaintenanceMeter = new( |
| | 1302 | 35 | | new MaintenanceWorkBudget( |
| | 1302 | 36 | | int.MaxValue, |
| | 1302 | 37 | | int.MaxValue, |
| | 1302 | 38 | | int.MaxValue, |
| | 1302 | 39 | | int.MaxValue, |
| | 1302 | 40 | | int.MaxValue, |
| | 1302 | 41 | | int.MaxValue, |
| | 1302 | 42 | | int.MaxValue, |
| | 1302 | 43 | | int.MaxValue)); |
| | | 44 | | private readonly NavigationOperationRejection[] _outcomes; |
| | | 45 | | private readonly bool[] _superseded; |
| | | 46 | | private readonly NavigationOperationFrameChange[] _changes; |
| | | 47 | | private readonly SwiftHashSet<string> _mapOverwriters; |
| | | 48 | | private readonly SwiftHashSet<NavigationCellAddress> _coveredCells; |
| | | 49 | | private readonly SwiftHashSet<OverlayIdKey> _coveredConnections; |
| | | 50 | | private readonly SwiftHashSet<OverlayIdKey> _coveredTransitions; |
| | | 51 | | private readonly long _coverageScratchBytes; |
| | | 52 | | |
| | | 53 | | private long _pendingDescriptorBytes; |
| | | 54 | | private long _pendingPreparedMapBytes; |
| | | 55 | | private long _sequenceHighWater; |
| | 1302 | 56 | | private int _effectiveFrameHighWater = -1; |
| | 1302 | 57 | | private int _lastProcessedFrame = -1; |
| | | 58 | | private int _deferredPrefixCount; |
| | | 59 | | private int _foldOperationIndex; |
| | | 60 | | private NavigationMapFoldWork? _mapFoldWork; |
| | | 61 | | private NavigationOverlayFoldWork? _overlayFoldWork; |
| | | 62 | | private NavigationOperationCandidate? _sourceCandidateForDeferred; |
| | | 63 | | private NavigationOperationCandidate? _deferredCandidate; |
| | | 64 | | private NavigationOperationCandidate? _activeFoldSourceCandidate; |
| | | 65 | | private bool _supersedenceActive; |
| | | 66 | | private int _supersedenceIndex; |
| | | 67 | | private int _supersedenceMapIndex; |
| | | 68 | | private int _supersedenceKind; |
| | | 69 | | private int _supersedenceItemIndex; |
| | | 70 | | private bool _supersedenceCovered; |
| | | 71 | | private bool _supersedenceComplete; |
| | | 72 | | |
| | 1302 | 73 | | internal NavigationOperationProcessor( |
| | 1302 | 74 | | NavigationOperationLimits limits, |
| | 1302 | 75 | | int maxBakedCellsPerMap = int.MaxValue, |
| | 1302 | 76 | | int navigationAreaCount = ushort.MaxValue + 1) |
| | | 77 | | { |
| | 1302 | 78 | | SwiftThrowHelper.ThrowIfArgument( |
| | 1302 | 79 | | GetFixedScratchBytes(limits.MaxBatchItems) > limits.MaxBatchSortScratchBytes, |
| | 1302 | 80 | | nameof(limits), |
| | 1302 | 81 | | "Batch scratch capacity is smaller than the processor's fixed batch storage."); |
| | 1302 | 82 | | _limits = limits; |
| | 1302 | 83 | | _maxBakedCellsPerMap = maxBakedCellsPerMap; |
| | 1302 | 84 | | _navigationAreaCount = navigationAreaCount; |
| | 1302 | 85 | | _candidate = new NavigationOperationCandidate(navigationAreaCount); |
| | 1302 | 86 | | _corridorPrisms = new GridCellPrism[limits.MaxCorridorCells]; |
| | 1302 | 87 | | _corridorWaypoints = new Vector3d[checked((limits.MaxCorridorCells - 1) * 2)]; |
| | 1302 | 88 | | _corridorAddresses = new NavigationCellAddress[limits.MaxCorridorCells]; |
| | 1302 | 89 | | _corridorAddressSet = new NavigationAddressStampSet(limits.MaxCorridorCells); |
| | 1302 | 90 | | _outcomes = new NavigationOperationRejection[limits.MaxBatchItems]; |
| | 1302 | 91 | | _superseded = new bool[limits.MaxBatchItems]; |
| | 1302 | 92 | | _changes = new NavigationOperationFrameChange[limits.MaxBatchItems]; |
| | 1302 | 93 | | _mapOverwriters = new SwiftHashSet<string>( |
| | 1302 | 94 | | limits.MaxMaps, |
| | 1302 | 95 | | SwiftHashTools.GetDeterministicStringEqualityComparer()); |
| | 1302 | 96 | | _coveredCells = new SwiftHashSet<NavigationCellAddress>( |
| | 1302 | 97 | | Math.Max(SwiftHashSet<NavigationCellAddress>.DefaultCapacity, limits.MaxOverlayCells)); |
| | 1302 | 98 | | _coveredConnections = new SwiftHashSet<OverlayIdKey>( |
| | 1302 | 99 | | Math.Max(SwiftHashSet<OverlayIdKey>.DefaultCapacity, limits.MaxOverlayConnections)); |
| | 1302 | 100 | | _coveredTransitions = new SwiftHashSet<OverlayIdKey>( |
| | 1302 | 101 | | Math.Max(SwiftHashSet<OverlayIdKey>.DefaultCapacity, limits.MaxOverlayTransitions)); |
| | 1302 | 102 | | _coverageScratchBytes = checked( |
| | 1302 | 103 | | ((long)SwiftHashTools.NextPowerOfTwo(Math.Max(8, limits.MaxMaps)) * 32L) |
| | 1302 | 104 | | + ((long)SwiftHashTools.NextPowerOfTwo(Math.Max(8, limits.MaxOverlayCells)) * 40L) |
| | 1302 | 105 | | + ((long)SwiftHashTools.NextPowerOfTwo(Math.Max(8, limits.MaxOverlayConnections)) * 48L) |
| | 1302 | 106 | | + ((long)SwiftHashTools.NextPowerOfTwo(Math.Max(8, limits.MaxOverlayTransitions)) * 48L)); |
| | 1302 | 107 | | } |
| | | 108 | | |
| | 2049 | 109 | | internal NavigationOperationCandidate Candidate => _candidate; |
| | | 110 | | |
| | 5 | 111 | | internal long CoverageScratchBytes => _coverageScratchBytes; |
| | | 112 | | |
| | 53815 | 113 | | internal long RetainedOperationWorkBytes => checked( |
| | 53815 | 114 | | GetActiveAdditionalRetainedBytes() |
| | 53815 | 115 | | + (_supersedenceActive ? _coverageScratchBytes : 0)); |
| | | 116 | | |
| | 54157 | 117 | | internal int RetainedOperationWorkPageCount => GetActiveAdditionalPersistentPages(); |
| | | 118 | | |
| | | 119 | | internal int RetainedOperationWorkCount => |
| | 54369 | 120 | | _mapFoldWork != null |
| | 54369 | 121 | | || _overlayFoldWork != null |
| | 54369 | 122 | | || _deferredCandidate != null |
| | 54369 | 123 | | ? 1 |
| | 54369 | 124 | | : 0; |
| | | 125 | | |
| | | 126 | | internal void RejectDeferredCapacity() |
| | | 127 | | { |
| | 3 | 128 | | int count = Math.Min(_deferredPrefixCount, _pending.Count); |
| | | 129 | | System.Diagnostics.Debug.Assert(_sourceCandidateForDeferred != null); |
| | 3 | 130 | | _candidate = _sourceCandidateForDeferred!; |
| | 12 | 131 | | for (int i = 0; i < count; i++) |
| | 3 | 132 | | _pending[i].Receipt.CompleteRejected(NavigationOperationRejection.CapacityExceeded); |
| | 3 | 133 | | RemovePrefix(count); |
| | 3 | 134 | | _deferredPrefixCount = 0; |
| | 3 | 135 | | _foldOperationIndex = 0; |
| | 3 | 136 | | _mapFoldWork = null; |
| | 3 | 137 | | _overlayFoldWork = null; |
| | 3 | 138 | | _sourceCandidateForDeferred = null; |
| | 3 | 139 | | _deferredCandidate = null; |
| | 3 | 140 | | _activeFoldSourceCandidate = null; |
| | 3 | 141 | | ResetSupersedence(); |
| | 3 | 142 | | } |
| | | 143 | | |
| | | 144 | | internal void Reset() |
| | | 145 | | { |
| | 50 | 146 | | for (int i = 0; i < _pending.Count; i++) |
| | 2 | 147 | | _pending[i].Receipt.CompleteSuperseded(); |
| | 23 | 148 | | _pending.Clear(); |
| | 23 | 149 | | _candidate = new NavigationOperationCandidate(_navigationAreaCount); |
| | 23 | 150 | | _pendingDescriptorBytes = 0; |
| | 23 | 151 | | _pendingPreparedMapBytes = 0; |
| | 23 | 152 | | _sequenceHighWater = 0; |
| | 23 | 153 | | _effectiveFrameHighWater = -1; |
| | 23 | 154 | | _lastProcessedFrame = -1; |
| | 23 | 155 | | _deferredPrefixCount = 0; |
| | 23 | 156 | | _foldOperationIndex = 0; |
| | 23 | 157 | | _mapFoldWork = null; |
| | 23 | 158 | | _overlayFoldWork = null; |
| | 23 | 159 | | _sourceCandidateForDeferred = null; |
| | 23 | 160 | | _deferredCandidate = null; |
| | 23 | 161 | | _activeFoldSourceCandidate = null; |
| | 23 | 162 | | ResetSupersedence(); |
| | 23 | 163 | | _mapOverwriters.Clear(); |
| | 23 | 164 | | _coveredCells.Clear(); |
| | 23 | 165 | | _coveredConnections.Clear(); |
| | 23 | 166 | | _coveredTransitions.Clear(); |
| | 23 | 167 | | } |
| | | 168 | | |
| | | 169 | | internal bool Admit(NavigationMapCommitOperation operation) |
| | | 170 | | { |
| | 1311 | 171 | | SwiftThrowHelper.ThrowIfNull(operation.PreparedMap, nameof(operation)); |
| | 1311 | 172 | | return Admit(PendingOperation.ForMapCommit(operation)); |
| | | 173 | | } |
| | | 174 | | |
| | | 175 | | internal bool Admit(NavigationMapRemoveOperation operation) |
| | | 176 | | { |
| | 22 | 177 | | SwiftThrowHelper.ThrowIfNull(operation.MapId, nameof(operation)); |
| | 22 | 178 | | return Admit(PendingOperation.ForMapRemove(operation)); |
| | | 179 | | } |
| | | 180 | | |
| | | 181 | | internal bool Admit(NavigationOverlayCommitOperation operation) |
| | | 182 | | { |
| | 286 | 183 | | SwiftThrowHelper.ThrowIfNull(operation.PreparedOverlay, nameof(operation)); |
| | 286 | 184 | | return Admit(PendingOperation.ForOverlay(operation)); |
| | | 185 | | } |
| | | 186 | | |
| | | 187 | | internal void ProcessFrame(int frame) |
| | | 188 | | { |
| | 184 | 189 | | ProcessFrame(frame, static (_, _, _, _) => NavigationCandidatePublication.Published); |
| | 184 | 190 | | } |
| | | 191 | | |
| | | 192 | | internal NavigationOperationFrameResult ProcessFrame( |
| | | 193 | | int frame, |
| | | 194 | | NavigationCandidatePublisher publishCandidate, |
| | | 195 | | MaintenanceWorkMeter? maintenanceMeter = null, |
| | | 196 | | NavigationRetainedWorkGuard? retainedWorkGuard = null) |
| | | 197 | | { |
| | 17189 | 198 | | SwiftThrowHelper.ThrowIfNull(publishCandidate, nameof(publishCandidate)); |
| | 17189 | 199 | | SwiftThrowHelper.ThrowIfArgument(frame <= _lastProcessedFrame, nameof(frame)); |
| | 17189 | 200 | | _lastProcessedFrame = frame; |
| | 17189 | 201 | | if (maintenanceMeter == null) |
| | | 202 | | { |
| | 186 | 203 | | _unboundedMaintenanceMeter.Reset(); |
| | 186 | 204 | | maintenanceMeter = _unboundedMaintenanceMeter; |
| | | 205 | | } |
| | 17189 | 206 | | int eligibleCount = 0; |
| | 17189 | 207 | | long batchDescriptorBytes = 0; |
| | 17189 | 208 | | long batchScratchBytes = GetFixedScratchBytes(_limits.MaxBatchItems); |
| | 17189 | 209 | | int maximumEligible = _deferredPrefixCount == 0 |
| | 17189 | 210 | | ? _pending.Count |
| | 17189 | 211 | | : Math.Min(_deferredPrefixCount, _pending.Count); |
| | 59910 | 212 | | while (eligibleCount < maximumEligible && _pending[eligibleCount].EffectiveFrame <= frame) |
| | | 213 | | { |
| | 42724 | 214 | | PendingOperation operation = _pending[eligibleCount]; |
| | 42724 | 215 | | if (eligibleCount >= _limits.MaxBatchItems |
| | 42724 | 216 | | || WouldExceed(batchDescriptorBytes, operation.DescriptorBytes, _limits.MaxBatchDescriptorBytes) |
| | 42724 | 217 | | || WouldExceed(batchScratchBytes, operation.CoalescingScratchBytes, _limits.MaxBatchSortScratchBytes)) |
| | | 218 | | { |
| | | 219 | | break; |
| | | 220 | | } |
| | | 221 | | |
| | 42721 | 222 | | batchDescriptorBytes += operation.DescriptorBytes; |
| | 42721 | 223 | | batchScratchBytes += operation.CoalescingScratchBytes; |
| | 42721 | 224 | | eligibleCount++; |
| | | 225 | | } |
| | | 226 | | |
| | 17189 | 227 | | NavigationOperationCandidate published = _sourceCandidateForDeferred ?? _candidate; |
| | 17189 | 228 | | NavigationOperationCandidate working = _deferredCandidate |
| | 17189 | 229 | | ?? (eligibleCount > 0 ? _candidate.Clone() : _candidate); |
| | 17189 | 230 | | _candidate = working; |
| | 17189 | 231 | | int foldStart = _deferredPrefixCount == 0 ? 0 : _foldOperationIndex; |
| | 17189 | 232 | | if (_mapFoldWork != null) |
| | 2346 | 233 | | _candidate = _mapFoldWork.Candidate; |
| | 14843 | 234 | | else if (_overlayFoldWork != null) |
| | 185 | 235 | | _candidate = _overlayFoldWork.Candidate; |
| | 37570 | 236 | | for (int i = foldStart; i < eligibleCount; i++) |
| | | 237 | | { |
| | 4130 | 238 | | if (_pending[i].Kind is PendingOperationKind.MapCommit or PendingOperationKind.MapRemove) |
| | | 239 | | { |
| | 3661 | 240 | | _mapFoldWork ??= _pending[i].Kind == PendingOperationKind.MapCommit |
| | 3661 | 241 | | ? new NavigationMapFoldWork( |
| | 3661 | 242 | | _candidate, |
| | 3661 | 243 | | _pending[i].PreparedMap!, |
| | 3661 | 244 | | _pending[i].ReplacementPolicy, |
| | 3661 | 245 | | _limits, |
| | 3661 | 246 | | _corridorPrisms, |
| | 3661 | 247 | | _corridorWaypoints, |
| | 3661 | 248 | | _corridorAddresses, |
| | 3661 | 249 | | _corridorAddressSet) |
| | 3661 | 250 | | : new NavigationMapFoldWork( |
| | 3661 | 251 | | _candidate, |
| | 3661 | 252 | | _pending[i].MapId!, |
| | 3661 | 253 | | _corridorPrisms, |
| | 3661 | 254 | | _corridorWaypoints, |
| | 3661 | 255 | | _corridorAddresses, |
| | 3661 | 256 | | _corridorAddressSet); |
| | 3661 | 257 | | _activeFoldSourceCandidate ??= _candidate; |
| | 3661 | 258 | | _foldOperationIndex = i; |
| | 3661 | 259 | | if (retainedWorkGuard != null |
| | 3661 | 260 | | && !retainedWorkGuard( |
| | 3661 | 261 | | GetAdditionalRetainedBytes(_mapFoldWork, published), |
| | 3661 | 262 | | GetAdditionalPersistentPages(_mapFoldWork, published))) |
| | | 263 | | { |
| | 2 | 264 | | _outcomes[i] = NavigationOperationRejection.CapacityExceeded; |
| | 2 | 265 | | _candidate = _activeFoldSourceCandidate; |
| | 2 | 266 | | _mapFoldWork = null; |
| | 2 | 267 | | _activeFoldSourceCandidate = null; |
| | 2 | 268 | | _foldOperationIndex = i + 1; |
| | 2 | 269 | | _superseded[i] = false; |
| | 2 | 270 | | continue; |
| | | 271 | | } |
| | 3659 | 272 | | if (!_mapFoldWork.Advance(maintenanceMeter, out _outcomes[i])) |
| | | 273 | | { |
| | 2346 | 274 | | _sourceCandidateForDeferred ??= published; |
| | 2346 | 275 | | _deferredCandidate = _mapFoldWork.Candidate; |
| | 2346 | 276 | | _candidate = published; |
| | 2346 | 277 | | _deferredPrefixCount = eligibleCount; |
| | 2346 | 278 | | return NavigationOperationFrameResult.Deferred; |
| | | 279 | | } |
| | 1313 | 280 | | if (_outcomes[i] == NavigationOperationRejection.None |
| | 1313 | 281 | | && retainedWorkGuard != null |
| | 1313 | 282 | | && !retainedWorkGuard( |
| | 1313 | 283 | | GetAdditionalRetainedBytes(_mapFoldWork, published), |
| | 1313 | 284 | | GetAdditionalPersistentPages(_mapFoldWork, published))) |
| | | 285 | | { |
| | 2 | 286 | | _outcomes[i] = NavigationOperationRejection.CapacityExceeded; |
| | | 287 | | } |
| | 1313 | 288 | | if (_outcomes[i] == NavigationOperationRejection.None) |
| | 1282 | 289 | | _candidate = _mapFoldWork.Candidate; |
| | | 290 | | else |
| | 31 | 291 | | _candidate = _activeFoldSourceCandidate!; |
| | 1313 | 292 | | _mapFoldWork = null; |
| | 1313 | 293 | | _activeFoldSourceCandidate = null; |
| | 1313 | 294 | | _foldOperationIndex = i + 1; |
| | | 295 | | } |
| | | 296 | | else |
| | | 297 | | { |
| | 469 | 298 | | _overlayFoldWork ??= new NavigationOverlayFoldWork( |
| | 469 | 299 | | _candidate, |
| | 469 | 300 | | _pending[i].PreparedOverlay!.Transaction, |
| | 469 | 301 | | _pending[i].OperationSequence, |
| | 469 | 302 | | _limits, |
| | 469 | 303 | | _corridorPrisms, |
| | 469 | 304 | | _corridorWaypoints, |
| | 469 | 305 | | _corridorAddresses, |
| | 469 | 306 | | _corridorAddressSet); |
| | 469 | 307 | | _activeFoldSourceCandidate ??= _candidate; |
| | 469 | 308 | | _foldOperationIndex = i; |
| | 469 | 309 | | if (retainedWorkGuard != null |
| | 469 | 310 | | && !retainedWorkGuard( |
| | 469 | 311 | | GetAdditionalRetainedBytes(_overlayFoldWork, published), |
| | 469 | 312 | | GetAdditionalPersistentPages(_overlayFoldWork, published))) |
| | | 313 | | { |
| | 1 | 314 | | _outcomes[i] = NavigationOperationRejection.CapacityExceeded; |
| | 1 | 315 | | _candidate = _activeFoldSourceCandidate; |
| | 1 | 316 | | _overlayFoldWork = null; |
| | 1 | 317 | | _activeFoldSourceCandidate = null; |
| | 1 | 318 | | _foldOperationIndex = i + 1; |
| | 1 | 319 | | _superseded[i] = false; |
| | 1 | 320 | | continue; |
| | | 321 | | } |
| | 468 | 322 | | if (!_overlayFoldWork.Advance(maintenanceMeter, out _outcomes[i])) |
| | | 323 | | { |
| | 188 | 324 | | _sourceCandidateForDeferred ??= published; |
| | 188 | 325 | | _deferredCandidate = _overlayFoldWork.Candidate; |
| | 188 | 326 | | _candidate = published; |
| | 188 | 327 | | _deferredPrefixCount = eligibleCount; |
| | 188 | 328 | | return NavigationOperationFrameResult.Deferred; |
| | | 329 | | } |
| | 280 | 330 | | if (_outcomes[i] == NavigationOperationRejection.None |
| | 280 | 331 | | && retainedWorkGuard != null |
| | 280 | 332 | | && !retainedWorkGuard( |
| | 280 | 333 | | GetAdditionalRetainedBytes(_overlayFoldWork, published), |
| | 280 | 334 | | GetAdditionalPersistentPages(_overlayFoldWork, published))) |
| | | 335 | | { |
| | 1 | 336 | | _outcomes[i] = NavigationOperationRejection.CapacityExceeded; |
| | | 337 | | } |
| | 280 | 338 | | if (_outcomes[i] == NavigationOperationRejection.None) |
| | 262 | 339 | | _candidate = _overlayFoldWork.Candidate; |
| | | 340 | | else |
| | 18 | 341 | | _candidate = _activeFoldSourceCandidate!; |
| | 280 | 342 | | _overlayFoldWork = null; |
| | 280 | 343 | | _activeFoldSourceCandidate = null; |
| | 280 | 344 | | _foldOperationIndex = i + 1; |
| | | 345 | | } |
| | 1593 | 346 | | _foldOperationIndex = i + 1; |
| | 1593 | 347 | | _superseded[i] = false; |
| | | 348 | | } |
| | | 349 | | |
| | 14655 | 350 | | if (retainedWorkGuard != null |
| | 14655 | 351 | | && !retainedWorkGuard( |
| | 14655 | 352 | | checked(GetPositiveRetainedDelta(_candidate, published) + _coverageScratchBytes), |
| | 14655 | 353 | | GetPositivePersistentPageDelta(_candidate, published))) |
| | | 354 | | { |
| | 21 | 355 | | _candidate = published; |
| | 86 | 356 | | for (int i = 0; i < eligibleCount; i++) |
| | | 357 | | { |
| | 22 | 358 | | if (_outcomes[i] == NavigationOperationRejection.None) |
| | 19 | 359 | | _outcomes[i] = NavigationOperationRejection.CapacityExceeded; |
| | | 360 | | } |
| | 21 | 361 | | ResetSupersedence(); |
| | | 362 | | } |
| | 14634 | 363 | | else if (!AdvanceSupersedence(eligibleCount, maintenanceMeter)) |
| | | 364 | | { |
| | 116 | 365 | | _sourceCandidateForDeferred ??= published; |
| | 116 | 366 | | _deferredCandidate = _candidate; |
| | 116 | 367 | | _candidate = published; |
| | 116 | 368 | | _deferredPrefixCount = eligibleCount; |
| | 116 | 369 | | return NavigationOperationFrameResult.Deferred; |
| | | 370 | | } |
| | | 371 | | |
| | 14539 | 372 | | int changeCount = 0; |
| | 105958 | 373 | | for (int i = 0; i < eligibleCount; i++) |
| | | 374 | | { |
| | 38440 | 375 | | if (_outcomes[i] == NavigationOperationRejection.None) |
| | 38369 | 376 | | _changes[changeCount++] = _pending[i].ToFrameChange(); |
| | | 377 | | } |
| | 14539 | 378 | | NavigationCandidatePublication publication = eligibleCount > 0 |
| | 14539 | 379 | | ? publishCandidate(_candidate, frame, _changes, changeCount) |
| | 14539 | 380 | | : NavigationCandidatePublication.Published; |
| | 14539 | 381 | | if (publication == NavigationCandidatePublication.Deferred) |
| | | 382 | | { |
| | 10861 | 383 | | _sourceCandidateForDeferred ??= published; |
| | 10861 | 384 | | _deferredCandidate = _candidate; |
| | 10861 | 385 | | if (_deferredPrefixCount == 0) |
| | 114 | 386 | | _deferredPrefixCount = eligibleCount; |
| | 10861 | 387 | | _candidate = published; |
| | 10861 | 388 | | return NavigationOperationFrameResult.Deferred; |
| | | 389 | | } |
| | 3678 | 390 | | if (publication == NavigationCandidatePublication.PermanentCapacity) |
| | | 391 | | { |
| | 176 | 392 | | _candidate = published; |
| | 706 | 393 | | for (int i = 0; i < eligibleCount; i++) |
| | | 394 | | { |
| | 177 | 395 | | if (_outcomes[i] == NavigationOperationRejection.None) |
| | 176 | 396 | | _outcomes[i] = NavigationOperationRejection.CapacityExceeded; |
| | | 397 | | } |
| | | 398 | | } |
| | | 399 | | |
| | 10536 | 400 | | for (int i = 0; i < eligibleCount; i++) |
| | | 401 | | { |
| | 1590 | 402 | | PendingOperation operation = _pending[i]; |
| | 1590 | 403 | | if (_outcomes[i] != NavigationOperationRejection.None) |
| | 247 | 404 | | operation.Receipt.CompleteRejected(_outcomes[i]); |
| | 1343 | 405 | | else if (_superseded[i]) |
| | 14 | 406 | | operation.Receipt.CompleteSuperseded(); |
| | | 407 | | else |
| | 1329 | 408 | | operation.Receipt.CompleteApplied(frame); |
| | | 409 | | } |
| | | 410 | | |
| | 3678 | 411 | | RemovePrefix(eligibleCount); |
| | 3678 | 412 | | if (publication == NavigationCandidatePublication.Published) |
| | 3502 | 413 | | _candidate.ResetWorkCopiedPersistentOwnership(); |
| | 3678 | 414 | | _deferredPrefixCount = 0; |
| | 3678 | 415 | | _foldOperationIndex = 0; |
| | 3678 | 416 | | _mapFoldWork = null; |
| | 3678 | 417 | | _overlayFoldWork = null; |
| | 3678 | 418 | | _sourceCandidateForDeferred = null; |
| | 3678 | 419 | | _deferredCandidate = null; |
| | 3678 | 420 | | _activeFoldSourceCandidate = null; |
| | 3678 | 421 | | ResetSupersedence(); |
| | 3678 | 422 | | if (eligibleCount == 0) |
| | 2337 | 423 | | return NavigationOperationFrameResult.None; |
| | 1341 | 424 | | return publication == NavigationCandidatePublication.Published |
| | 1341 | 425 | | ? NavigationOperationFrameResult.Published |
| | 1341 | 426 | | : NavigationOperationFrameResult.Rejected; |
| | | 427 | | } |
| | | 428 | | |
| | | 429 | | private bool Admit(PendingOperation operation) |
| | | 430 | | { |
| | 1619 | 431 | | if (!operation.Receipt.TryClaimAdmission()) |
| | 2 | 432 | | return false; |
| | | 433 | | |
| | 1617 | 434 | | NavigationOperationRejection rejection = GetAdmissionRejection(operation); |
| | 1617 | 435 | | bool advancesSequence = operation.OperationSequence > _sequenceHighWater; |
| | 1617 | 436 | | if (advancesSequence) |
| | | 437 | | { |
| | 1614 | 438 | | _sequenceHighWater = operation.OperationSequence; |
| | 1614 | 439 | | if (operation.EffectiveFrame > _effectiveFrameHighWater) |
| | 1358 | 440 | | _effectiveFrameHighWater = operation.EffectiveFrame; |
| | | 441 | | } |
| | | 442 | | |
| | 1617 | 443 | | if (rejection != NavigationOperationRejection.None) |
| | | 444 | | { |
| | 14 | 445 | | operation.Receipt.CompleteRejected(rejection); |
| | 14 | 446 | | return false; |
| | | 447 | | } |
| | | 448 | | |
| | 1603 | 449 | | _pending.Add(operation); |
| | 1603 | 450 | | _pendingDescriptorBytes += operation.DescriptorBytes; |
| | 1603 | 451 | | _pendingPreparedMapBytes += operation.PreparedMapBytes; |
| | 1603 | 452 | | return true; |
| | | 453 | | } |
| | | 454 | | |
| | | 455 | | private NavigationOperationRejection GetAdmissionRejection(PendingOperation operation) |
| | | 456 | | { |
| | 1617 | 457 | | if (operation.OperationSequence == _sequenceHighWater) |
| | 2 | 458 | | return NavigationOperationRejection.DuplicateSequence; |
| | 1615 | 459 | | if (operation.OperationSequence < _sequenceHighWater) |
| | 1 | 460 | | return NavigationOperationRejection.RegressingSequence; |
| | 1614 | 461 | | if (operation.EffectiveFrame < _effectiveFrameHighWater) |
| | 1 | 462 | | return NavigationOperationRejection.RegressingEffectiveFrame; |
| | 1613 | 463 | | if (operation.EffectiveFrame <= _lastProcessedFrame) |
| | 1 | 464 | | return NavigationOperationRejection.LateEffectiveFrame; |
| | 1612 | 465 | | long fixedScratchBytes = GetFixedScratchBytes(_limits.MaxBatchItems); |
| | 1612 | 466 | | if (operation.DescriptorBytes > _limits.MaxBatchDescriptorBytes |
| | 1612 | 467 | | || (operation.Kind == PendingOperationKind.MapCommit |
| | 1612 | 468 | | && operation.PreparedMap!.Map.Cells.Count > _maxBakedCellsPerMap) |
| | 1612 | 469 | | || (operation.Kind == PendingOperationKind.MapCommit |
| | 1612 | 470 | | && operation.PreparedMap!.Map.TransitionRuleSpan.Length |
| | 1612 | 471 | | > _limits.MaxTransitionRulesPerMap) |
| | 1612 | 472 | | || WouldExceed(fixedScratchBytes, operation.CoalescingScratchBytes, _limits.MaxBatchSortScratchBytes) |
| | 1612 | 473 | | || _pending.Count >= _limits.MaxPendingOperations |
| | 1612 | 474 | | || WouldExceed(_pendingDescriptorBytes, operation.DescriptorBytes, _limits.MaxPendingDescriptorBytes) |
| | 1612 | 475 | | || WouldExceed(_pendingPreparedMapBytes, operation.PreparedMapBytes, _limits.MaxPreparedMapBytes)) |
| | | 476 | | { |
| | 9 | 477 | | return NavigationOperationRejection.CapacityExceeded; |
| | | 478 | | } |
| | | 479 | | |
| | 1603 | 480 | | return NavigationOperationRejection.None; |
| | | 481 | | } |
| | | 482 | | |
| | | 483 | | private bool AdvanceSupersedence(int eligibleCount, MaintenanceWorkMeter meter) |
| | | 484 | | { |
| | 14634 | 485 | | if (_supersedenceComplete) |
| | 10854 | 486 | | return true; |
| | 3780 | 487 | | if (!_supersedenceActive) |
| | | 488 | | { |
| | 3664 | 489 | | _mapOverwriters.Clear(); |
| | 3664 | 490 | | _coveredCells.Clear(); |
| | 3664 | 491 | | _coveredConnections.Clear(); |
| | 3664 | 492 | | _coveredTransitions.Clear(); |
| | 3664 | 493 | | _supersedenceIndex = eligibleCount - 1; |
| | 3664 | 494 | | _supersedenceMapIndex = 0; |
| | 3664 | 495 | | _supersedenceKind = 0; |
| | 3664 | 496 | | _supersedenceItemIndex = 0; |
| | 3664 | 497 | | _supersedenceCovered = true; |
| | 3664 | 498 | | _supersedenceActive = true; |
| | | 499 | | } |
| | 5355 | 500 | | while (_supersedenceIndex >= 0) |
| | | 501 | | { |
| | 1691 | 502 | | if (_outcomes[_supersedenceIndex] != NavigationOperationRejection.None) |
| | | 503 | | { |
| | 49 | 504 | | CompleteSupersedenceOperation(); |
| | 49 | 505 | | continue; |
| | | 506 | | } |
| | 1642 | 507 | | PendingOperation operation = _pending[_supersedenceIndex]; |
| | 1642 | 508 | | if (operation.IsMapOperation) |
| | | 509 | | { |
| | 1317 | 510 | | if (!meter.TryConsumeComponentNodes(1)) |
| | 45 | 511 | | return false; |
| | 1272 | 512 | | _superseded[_supersedenceIndex] = _mapOverwriters.Contains(operation.MapId!); |
| | 1272 | 513 | | if (operation.Kind == PendingOperationKind.MapRemove |
| | 1272 | 514 | | || operation.ReplacementPolicy == OverlayReplacementPolicy.Clear) |
| | | 515 | | { |
| | 1264 | 516 | | _mapOverwriters.Add(operation.MapId!); |
| | | 517 | | } |
| | 1272 | 518 | | CompleteSupersedenceOperation(); |
| | 1272 | 519 | | continue; |
| | | 520 | | } |
| | | 521 | | |
| | 325 | 522 | | ReadOnlySpan<NavigationMapOverlayDelta> maps = |
| | 325 | 523 | | operation.PreparedOverlay!.Transaction.MapSpan; |
| | 596 | 524 | | while (_supersedenceMapIndex < maps.Length) |
| | | 525 | | { |
| | 342 | 526 | | NavigationMapOverlayDelta map = maps[_supersedenceMapIndex]; |
| | 342 | 527 | | if (_mapOverwriters.Contains(map.MapId)) |
| | | 528 | | { |
| | 1 | 529 | | _supersedenceMapIndex++; |
| | 1 | 530 | | _supersedenceKind = 0; |
| | 1 | 531 | | _supersedenceItemIndex = 0; |
| | 1 | 532 | | continue; |
| | | 533 | | } |
| | 1961 | 534 | | while (_supersedenceKind < 6) |
| | | 535 | | { |
| | 1691 | 536 | | int count = _supersedenceKind switch |
| | 1691 | 537 | | { |
| | 606 | 538 | | 0 or 3 => map.Cells.Count, |
| | 542 | 539 | | 1 or 4 => map.Connections.Count, |
| | 543 | 540 | | _ => map.Transitions.Count |
| | 1691 | 541 | | }; |
| | 52451 | 542 | | while (_supersedenceItemIndex < count) |
| | | 543 | | { |
| | 50831 | 544 | | if (!meter.TryConsumeOverlaySlots(1)) |
| | 71 | 545 | | return false; |
| | 50760 | 546 | | int index = _supersedenceItemIndex++; |
| | 50760 | 547 | | if (_supersedenceKind < 3) |
| | | 548 | | { |
| | 25380 | 549 | | bool covered = _supersedenceKind switch |
| | 25380 | 550 | | { |
| | 17128 | 551 | | 0 => _coveredCells.Contains( |
| | 17128 | 552 | | new NavigationCellAddress(map.MapId, map.Cells[index].Index)), |
| | 4139 | 553 | | 1 => _coveredConnections.Contains( |
| | 4139 | 554 | | new OverlayIdKey(map.MapId, map.Connections[index].Id)), |
| | 4113 | 555 | | _ => _coveredTransitions.Contains( |
| | 4113 | 556 | | new OverlayIdKey(map.MapId, map.Transitions[index].Id)) |
| | 25380 | 557 | | }; |
| | 25380 | 558 | | _supersedenceCovered &= covered; |
| | | 559 | | } |
| | 25380 | 560 | | else if (_supersedenceKind == 3) |
| | | 561 | | { |
| | 17128 | 562 | | _coveredCells.Add( |
| | 17128 | 563 | | new NavigationCellAddress(map.MapId, map.Cells[index].Index)); |
| | | 564 | | } |
| | 8252 | 565 | | else if (_supersedenceKind == 4) |
| | 4139 | 566 | | _coveredConnections.Add(new OverlayIdKey(map.MapId, map.Connections[index].Id)); |
| | | 567 | | else |
| | 4113 | 568 | | _coveredTransitions.Add(new OverlayIdKey(map.MapId, map.Transitions[index].Id)); |
| | | 569 | | } |
| | 1620 | 570 | | _supersedenceKind++; |
| | 1620 | 571 | | _supersedenceItemIndex = 0; |
| | | 572 | | } |
| | 270 | 573 | | _supersedenceMapIndex++; |
| | 270 | 574 | | _supersedenceKind = 0; |
| | | 575 | | } |
| | 254 | 576 | | _superseded[_supersedenceIndex] = _supersedenceCovered; |
| | 254 | 577 | | CompleteSupersedenceOperation(); |
| | | 578 | | } |
| | 3664 | 579 | | _supersedenceActive = false; |
| | 3664 | 580 | | _supersedenceComplete = true; |
| | 3664 | 581 | | return true; |
| | | 582 | | } |
| | | 583 | | |
| | | 584 | | private void CompleteSupersedenceOperation() |
| | | 585 | | { |
| | 1575 | 586 | | _supersedenceIndex--; |
| | 1575 | 587 | | _supersedenceMapIndex = 0; |
| | 1575 | 588 | | _supersedenceKind = 0; |
| | 1575 | 589 | | _supersedenceItemIndex = 0; |
| | 1575 | 590 | | _supersedenceCovered = true; |
| | 1575 | 591 | | _supersedenceComplete = false; |
| | 1575 | 592 | | } |
| | | 593 | | |
| | | 594 | | private void ResetSupersedence() |
| | | 595 | | { |
| | 3725 | 596 | | _supersedenceActive = false; |
| | 3725 | 597 | | _supersedenceComplete = false; |
| | 3725 | 598 | | _supersedenceIndex = -1; |
| | 3725 | 599 | | _supersedenceMapIndex = 0; |
| | 3725 | 600 | | _supersedenceKind = 0; |
| | 3725 | 601 | | _supersedenceItemIndex = 0; |
| | 3725 | 602 | | _supersedenceCovered = true; |
| | 3725 | 603 | | } |
| | | 604 | | |
| | | 605 | | private static bool WouldExceed(long current, long increment, long maximum) => |
| | 90262 | 606 | | increment > maximum - current; |
| | | 607 | | |
| | | 608 | | private static long GetFixedScratchBytes(int maxBatchItems) => |
| | 20103 | 609 | | BaseCoalescingScratchBytes + ((long)maxBatchItems * FixedScratchBytesPerOperation); |
| | | 610 | | |
| | | 611 | | private long GetActiveAdditionalRetainedBytes() |
| | | 612 | | { |
| | 53815 | 613 | | NavigationOperationCandidate published = _sourceCandidateForDeferred ?? _candidate; |
| | 53815 | 614 | | if (_mapFoldWork != null) |
| | 4641 | 615 | | return GetAdditionalRetainedBytes(_mapFoldWork, published); |
| | 49174 | 616 | | if (_overlayFoldWork != null) |
| | 284 | 617 | | return GetAdditionalRetainedBytes(_overlayFoldWork, published); |
| | 48890 | 618 | | return _deferredCandidate == null |
| | 48890 | 619 | | ? 0 |
| | 48890 | 620 | | : GetPositiveRetainedDelta(_deferredCandidate, published); |
| | | 621 | | } |
| | | 622 | | |
| | | 623 | | private int GetActiveAdditionalPersistentPages() |
| | | 624 | | { |
| | 54157 | 625 | | NavigationOperationCandidate published = _sourceCandidateForDeferred ?? _candidate; |
| | 54157 | 626 | | if (_mapFoldWork != null) |
| | 4643 | 627 | | return GetAdditionalPersistentPages(_mapFoldWork, published); |
| | 49514 | 628 | | if (_overlayFoldWork != null) |
| | 284 | 629 | | return GetAdditionalPersistentPages(_overlayFoldWork, published); |
| | 49230 | 630 | | return _deferredCandidate == null |
| | 49230 | 631 | | ? 0 |
| | 49230 | 632 | | : GetPositivePersistentPageDelta(_deferredCandidate, published); |
| | | 633 | | } |
| | | 634 | | |
| | | 635 | | private static long GetAdditionalRetainedBytes( |
| | | 636 | | NavigationMapFoldWork work, |
| | 9257 | 637 | | NavigationOperationCandidate published) => checked( |
| | 9257 | 638 | | work.RetainedBytes |
| | 9257 | 639 | | + GetPositiveNonPayloadRetainedDelta(work.Candidate, published) |
| | 9257 | 640 | | + GetAdditionalExplicitPayloadBytes(work.Candidate, published) |
| | 9257 | 641 | | + GetAdditionalMapStatePayloadBytes(work.Candidate, published) |
| | 9257 | 642 | | + work.DisplacedExplicitPayloadBytes |
| | 9257 | 643 | | + work.DisplacedMapStatePayloadBytes); |
| | | 644 | | |
| | | 645 | | private static int GetAdditionalPersistentPages( |
| | | 646 | | NavigationMapFoldWork work, |
| | 9259 | 647 | | NavigationOperationCandidate published) => checked( |
| | 9259 | 648 | | work.PersistentPageCount |
| | 9259 | 649 | | + GetPositiveNonPayloadPageDelta(work.Candidate, published) |
| | 9259 | 650 | | + GetAdditionalExplicitPayloadPages(work.Candidate, published) |
| | 9259 | 651 | | + GetAdditionalMapStatePayloadPages(work.Candidate, published) |
| | 9259 | 652 | | + work.DisplacedExplicitPayloadPages |
| | 9259 | 653 | | + work.DisplacedMapStatePayloadPages); |
| | | 654 | | |
| | | 655 | | private static long GetAdditionalRetainedBytes( |
| | | 656 | | NavigationOverlayFoldWork work, |
| | 870 | 657 | | NavigationOperationCandidate published) => checked( |
| | 870 | 658 | | work.RetainedBytes |
| | 870 | 659 | | + GetPositiveNonPayloadRetainedDelta(work.Candidate, published) |
| | 870 | 660 | | + GetAdditionalExplicitPayloadBytes(work.Candidate, published) |
| | 870 | 661 | | + GetAdditionalMapStatePayloadBytes(work.Candidate, published) |
| | 870 | 662 | | + work.DisplacedExplicitPayloadBytes |
| | 870 | 663 | | + work.DisplacedMapStatePayloadBytes); |
| | | 664 | | |
| | | 665 | | private static int GetAdditionalPersistentPages( |
| | | 666 | | NavigationOverlayFoldWork work, |
| | 870 | 667 | | NavigationOperationCandidate published) => checked( |
| | 870 | 668 | | work.PersistentPageCount |
| | 870 | 669 | | + GetPositiveNonPayloadPageDelta(work.Candidate, published) |
| | 870 | 670 | | + GetAdditionalExplicitPayloadPages(work.Candidate, published) |
| | 870 | 671 | | + GetAdditionalMapStatePayloadPages(work.Candidate, published) |
| | 870 | 672 | | + work.DisplacedExplicitPayloadPages |
| | 870 | 673 | | + work.DisplacedMapStatePayloadPages); |
| | | 674 | | |
| | | 675 | | private static long GetAdditionalExplicitPayloadBytes( |
| | | 676 | | NavigationOperationCandidate candidate, |
| | 62816 | 677 | | NavigationOperationCandidate published) => Math.Max( |
| | 62816 | 678 | | 0L, |
| | 62816 | 679 | | candidate.WorkOwnedExplicitPayloadBytes |
| | 62816 | 680 | | - published.WorkOwnedExplicitPayloadBytes); |
| | | 681 | | |
| | | 682 | | private static int GetAdditionalExplicitPayloadPages( |
| | | 683 | | NavigationOperationCandidate candidate, |
| | 63185 | 684 | | NavigationOperationCandidate published) => Math.Max( |
| | 63185 | 685 | | 0, |
| | 63185 | 686 | | candidate.WorkOwnedExplicitPayloadPages |
| | 63185 | 687 | | - published.WorkOwnedExplicitPayloadPages); |
| | | 688 | | |
| | | 689 | | private static long GetAdditionalMapStatePayloadBytes( |
| | | 690 | | NavigationOperationCandidate candidate, |
| | 62816 | 691 | | NavigationOperationCandidate published) => Math.Max( |
| | 62816 | 692 | | 0L, |
| | 62816 | 693 | | candidate.WorkOwnedMapStatePayloadBytes |
| | 62816 | 694 | | - published.WorkOwnedMapStatePayloadBytes); |
| | | 695 | | |
| | | 696 | | private static int GetAdditionalMapStatePayloadPages( |
| | | 697 | | NavigationOperationCandidate candidate, |
| | 63185 | 698 | | NavigationOperationCandidate published) => Math.Max( |
| | 63185 | 699 | | 0, |
| | 63185 | 700 | | candidate.WorkOwnedMapStatePayloadPages |
| | 63185 | 701 | | - published.WorkOwnedMapStatePayloadPages); |
| | | 702 | | |
| | | 703 | | private static long GetPositiveNonPayloadRetainedDelta( |
| | | 704 | | NavigationOperationCandidate candidate, |
| | 62816 | 705 | | NavigationOperationCandidate source) => Math.Max( |
| | 62816 | 706 | | 0L, |
| | 62816 | 707 | | candidate.NonPayloadRetainedBytes - source.NonPayloadRetainedBytes); |
| | | 708 | | |
| | | 709 | | private static int GetPositiveNonPayloadPageDelta( |
| | | 710 | | NavigationOperationCandidate candidate, |
| | 63185 | 711 | | NavigationOperationCandidate source) => Math.Max( |
| | 63185 | 712 | | 0, |
| | 63185 | 713 | | candidate.NonPayloadPersistentPageCount - source.NonPayloadPersistentPageCount); |
| | | 714 | | |
| | | 715 | | private static long GetPositiveRetainedDelta( |
| | | 716 | | NavigationOperationCandidate candidate, |
| | | 717 | | NavigationOperationCandidate source) => |
| | 52689 | 718 | | checked( |
| | 52689 | 719 | | GetPositiveNonPayloadRetainedDelta(candidate, source) |
| | 52689 | 720 | | + GetAdditionalExplicitPayloadBytes(candidate, source) |
| | 52689 | 721 | | + GetAdditionalMapStatePayloadBytes(candidate, source) |
| | 52689 | 722 | | + candidate.WorkCopiedPersistentBytes); |
| | | 723 | | |
| | | 724 | | private static int GetPositivePersistentPageDelta( |
| | | 725 | | NavigationOperationCandidate candidate, |
| | | 726 | | NavigationOperationCandidate source) => |
| | 53056 | 727 | | checked( |
| | 53056 | 728 | | GetPositiveNonPayloadPageDelta(candidate, source) |
| | 53056 | 729 | | + GetAdditionalExplicitPayloadPages(candidate, source) |
| | 53056 | 730 | | + GetAdditionalMapStatePayloadPages(candidate, source) |
| | 53056 | 731 | | + candidate.WorkCopiedPersistentPages); |
| | | 732 | | |
| | 3681 | 733 | | private void RemovePrefix(int count) => RemoveRange(0, count); |
| | | 734 | | |
| | | 735 | | private void RemoveRange(int index, int count) |
| | | 736 | | { |
| | 3681 | 737 | | if (count == 0) |
| | 2337 | 738 | | return; |
| | | 739 | | |
| | 5874 | 740 | | for (int i = 0; i < count; i++) |
| | | 741 | | { |
| | 1593 | 742 | | PendingOperation operation = _pending[index + i]; |
| | 1593 | 743 | | _pendingDescriptorBytes -= operation.DescriptorBytes; |
| | 1593 | 744 | | _pendingPreparedMapBytes -= operation.PreparedMapBytes; |
| | | 745 | | } |
| | | 746 | | |
| | 5874 | 747 | | for (int i = 0; i < count; i++) |
| | 1593 | 748 | | _pending.RemoveAt(index); |
| | 1344 | 749 | | } |
| | | 750 | | |
| | | 751 | | private enum PendingOperationKind |
| | | 752 | | { |
| | | 753 | | MapCommit, |
| | | 754 | | MapRemove, |
| | | 755 | | Overlay |
| | | 756 | | } |
| | | 757 | | |
| | | 758 | | private readonly struct OverlayIdKey : IEquatable<OverlayIdKey> |
| | | 759 | | { |
| | | 760 | | internal OverlayIdKey(string mapId, string id) |
| | | 761 | | { |
| | 16504 | 762 | | MapId = mapId; |
| | 16504 | 763 | | Id = id; |
| | 16504 | 764 | | } |
| | | 765 | | |
| | | 766 | | private string MapId { get; } |
| | | 767 | | private string Id { get; } |
| | | 768 | | |
| | | 769 | | public bool Equals(OverlayIdKey other) => |
| | 8 | 770 | | string.Equals(MapId, other.MapId, StringComparison.Ordinal) |
| | 8 | 771 | | & string.Equals(Id, other.Id, StringComparison.Ordinal); |
| | | 772 | | |
| | | 773 | | public override int GetHashCode() |
| | | 774 | | { |
| | 16504 | 775 | | var comparer = SwiftHashTools.GetDeterministicStringEqualityComparer(); |
| | 16504 | 776 | | return SwiftHashTools.CombineHashCodes( |
| | 16504 | 777 | | comparer.GetHashCode(MapId), |
| | 16504 | 778 | | comparer.GetHashCode(Id)); |
| | | 779 | | } |
| | | 780 | | } |
| | | 781 | | |
| | | 782 | | private readonly struct PendingOperation |
| | | 783 | | { |
| | | 784 | | private PendingOperation( |
| | | 785 | | PendingOperationKind kind, |
| | | 786 | | string? mapId, |
| | | 787 | | PreparedNavigationMap? preparedMap, |
| | | 788 | | PreparedNavigationOverlay? preparedOverlay, |
| | | 789 | | OverlayReplacementPolicy replacementPolicy, |
| | | 790 | | long operationSequence, |
| | | 791 | | int effectiveFrame, |
| | | 792 | | NavigationOperationReceipt receipt, |
| | | 793 | | long descriptorBytes, |
| | | 794 | | long preparedMapBytes, |
| | | 795 | | long coalescingScratchBytes) |
| | | 796 | | { |
| | 1619 | 797 | | Kind = kind; |
| | 1619 | 798 | | MapId = mapId; |
| | 1619 | 799 | | PreparedMap = preparedMap; |
| | 1619 | 800 | | PreparedOverlay = preparedOverlay; |
| | 1619 | 801 | | ReplacementPolicy = replacementPolicy; |
| | 1619 | 802 | | OperationSequence = operationSequence; |
| | 1619 | 803 | | EffectiveFrame = effectiveFrame; |
| | 1619 | 804 | | Receipt = receipt; |
| | 1619 | 805 | | DescriptorBytes = descriptorBytes; |
| | 1619 | 806 | | PreparedMapBytes = preparedMapBytes; |
| | 1619 | 807 | | CoalescingScratchBytes = coalescingScratchBytes; |
| | 1619 | 808 | | } |
| | | 809 | | |
| | | 810 | | internal PendingOperationKind Kind { get; } |
| | | 811 | | internal string? MapId { get; } |
| | | 812 | | internal PreparedNavigationMap? PreparedMap { get; } |
| | | 813 | | internal PreparedNavigationOverlay? PreparedOverlay { get; } |
| | | 814 | | internal OverlayReplacementPolicy ReplacementPolicy { get; } |
| | | 815 | | internal long OperationSequence { get; } |
| | | 816 | | internal int EffectiveFrame { get; } |
| | | 817 | | internal NavigationOperationReceipt Receipt { get; } |
| | | 818 | | internal long DescriptorBytes { get; } |
| | | 819 | | internal long PreparedMapBytes { get; } |
| | | 820 | | internal long CoalescingScratchBytes { get; } |
| | 1642 | 821 | | internal bool IsMapOperation => Kind is PendingOperationKind.MapCommit or PendingOperationKind.MapRemove; |
| | | 822 | | |
| | 38369 | 823 | | internal NavigationOperationFrameChange ToFrameChange() => Kind switch |
| | 38369 | 824 | | { |
| | 37408 | 825 | | PendingOperationKind.MapCommit => NavigationOperationFrameChange.MapCommit( |
| | 37408 | 826 | | PreparedMap!, |
| | 37408 | 827 | | ReplacementPolicy, |
| | 37408 | 828 | | OperationSequence), |
| | 15 | 829 | | PendingOperationKind.MapRemove => NavigationOperationFrameChange.MapRemove( |
| | 15 | 830 | | MapId!, |
| | 15 | 831 | | OperationSequence), |
| | 946 | 832 | | _ => NavigationOperationFrameChange.Overlay( |
| | 946 | 833 | | PreparedOverlay!, |
| | 946 | 834 | | OperationSequence) |
| | 38369 | 835 | | }; |
| | | 836 | | |
| | | 837 | | internal static PendingOperation ForMapCommit(NavigationMapCommitOperation operation) => |
| | 1311 | 838 | | new( |
| | 1311 | 839 | | PendingOperationKind.MapCommit, |
| | 1311 | 840 | | operation.PreparedMap.Map.MapId, |
| | 1311 | 841 | | operation.PreparedMap, |
| | 1311 | 842 | | preparedOverlay: null, |
| | 1311 | 843 | | operation.OverlayReplacementPolicy, |
| | 1311 | 844 | | operation.OperationSequence, |
| | 1311 | 845 | | operation.EffectiveFrame, |
| | 1311 | 846 | | operation.Receipt, |
| | 1311 | 847 | | descriptorBytes: 64L + (operation.PreparedMap.Map.MapId.Length * sizeof(char)), |
| | 1311 | 848 | | preparedMapBytes: operation.PreparedMap.RetainedBytes, |
| | 1311 | 849 | | coalescingScratchBytes: MapCoverageScratchBytes); |
| | | 850 | | |
| | | 851 | | internal static PendingOperation ForMapRemove(NavigationMapRemoveOperation operation) => |
| | 22 | 852 | | new( |
| | 22 | 853 | | PendingOperationKind.MapRemove, |
| | 22 | 854 | | operation.MapId, |
| | 22 | 855 | | preparedMap: null, |
| | 22 | 856 | | preparedOverlay: null, |
| | 22 | 857 | | OverlayReplacementPolicy.PreserveAndRevalidate, |
| | 22 | 858 | | operation.OperationSequence, |
| | 22 | 859 | | operation.EffectiveFrame, |
| | 22 | 860 | | operation.Receipt, |
| | 22 | 861 | | descriptorBytes: 48L + (operation.MapId.Length * sizeof(char)), |
| | 22 | 862 | | preparedMapBytes: 0, |
| | 22 | 863 | | coalescingScratchBytes: MapCoverageScratchBytes); |
| | | 864 | | |
| | | 865 | | internal static PendingOperation ForOverlay(NavigationOverlayCommitOperation operation) => |
| | 286 | 866 | | new( |
| | 286 | 867 | | PendingOperationKind.Overlay, |
| | 286 | 868 | | mapId: null, |
| | 286 | 869 | | preparedMap: null, |
| | 286 | 870 | | operation.PreparedOverlay, |
| | 286 | 871 | | OverlayReplacementPolicy.PreserveAndRevalidate, |
| | 286 | 872 | | operation.OperationSequence, |
| | 286 | 873 | | operation.EffectiveFrame, |
| | 286 | 874 | | operation.Receipt, |
| | 286 | 875 | | operation.PreparedOverlay.DescriptorBytes, |
| | 286 | 876 | | preparedMapBytes: 0, |
| | 286 | 877 | | coalescingScratchBytes: GetOverlayCoverageScratchBytes(operation.PreparedOverlay)); |
| | | 878 | | |
| | | 879 | | private static long GetOverlayCoverageScratchBytes(PreparedNavigationOverlay overlay) => |
| | 286 | 880 | | NavigationByteCount.SaturatingAdd(overlay.DescriptorBytes, overlay.DescriptorBytes); |
| | | 881 | | |
| | | 882 | | } |
| | | 883 | | } |