| | | 1 | | //======================================================================= |
| | | 2 | | // NavigationTransitionRefreshWork.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 | | |
| | | 10 | | namespace Trailblazer.Pathing; |
| | | 11 | | |
| | | 12 | | /// <summary>Refreshes affected explicit-transition pages and the bounded rule table.</summary> |
| | | 13 | | internal sealed class NavigationTransitionRefreshWork |
| | | 14 | | { |
| | | 15 | | private const long FixedRetainedBytes = 536L; |
| | | 16 | | private readonly NavigationWorldGraph _source; |
| | | 17 | | private readonly NavigationWorldGraph _candidate; |
| | | 18 | | private readonly NavigationOperationCandidate? _operationCandidate; |
| | | 19 | | private readonly PersistentStringMap<bool> _changedMaps; |
| | | 20 | | private readonly bool _rebuildRules; |
| | | 21 | | private readonly long _version; |
| | 2998 | 22 | | private PersistentStringMap<bool> _affectedSources = PersistentStringMap<bool>.Empty; |
| | 2998 | 23 | | private PersistentStringMap<PersistentIntMap<PageBuildState>> _pageBuilds = |
| | 2998 | 24 | | PersistentStringMap<PersistentIntMap<PageBuildState>>.Empty; |
| | | 25 | | private long _pageBuildInnerBytes; |
| | | 26 | | private int _pageBuildInnerPages; |
| | | 27 | | private int _pageBuildStateCount; |
| | | 28 | | private NavigationTransitionPageRoot _pages; |
| | | 29 | | private NavigationTransitionRuleTable _rules; |
| | | 30 | | private int _changedMapOrdinal; |
| | 2998 | 31 | | private int _incomingOrdinal = -1; |
| | | 32 | | private int _definitionPhase; |
| | | 33 | | private int _definitionSourceOrdinal; |
| | | 34 | | private EffectiveTransitionCursor _definitionCursor; |
| | | 35 | | private bool _definitionCursorActive; |
| | | 36 | | private string? _pendingOwnerMapId; |
| | | 37 | | private TraversalTransitionDefinition _pendingDefinition; |
| | | 38 | | private NavigationPublishedTransition _pendingTransition; |
| | 2998 | 39 | | private int _pendingPlacementStage = -1; |
| | | 40 | | private int _pageMapOrdinal; |
| | | 41 | | private int _pageOrdinal; |
| | | 42 | | private long _workingBufferBytes; |
| | | 43 | | private int _workingBufferPages; |
| | | 44 | | private long _publicationOwnedBytes; |
| | | 45 | | private int _publicationOwnedPages; |
| | | 46 | | private bool _publicationRootOwned; |
| | | 47 | | private string? _publicationRootMapId; |
| | | 48 | | private int _ruleCountMapOrdinal; |
| | | 49 | | private int _ruleCount; |
| | | 50 | | private TraversalTransitionRule[]? _ruleBuffer; |
| | | 51 | | private int[]? _ruleOrdinals; |
| | | 52 | | private int _ruleOutputOrdinal; |
| | | 53 | | private int _ruleScanMapOrdinal; |
| | 2998 | 54 | | private int _ruleBestMapOrdinal = -1; |
| | | 55 | | private bool _ruleScanActive; |
| | | 56 | | private bool _ruleContentEqual; |
| | | 57 | | |
| | 2998 | 58 | | internal NavigationTransitionRefreshWork( |
| | 2998 | 59 | | NavigationWorldGraph source, |
| | 2998 | 60 | | NavigationWorldGraph candidate, |
| | 2998 | 61 | | NavigationOperationCandidate? operationCandidate, |
| | 2998 | 62 | | PersistentStringMap<bool> changedMaps, |
| | 2998 | 63 | | bool rebuildRules, |
| | 2998 | 64 | | long version) |
| | | 65 | | { |
| | 2998 | 66 | | _source = source; |
| | 2998 | 67 | | _candidate = candidate; |
| | 2998 | 68 | | _operationCandidate = operationCandidate; |
| | 2998 | 69 | | _changedMaps = changedMaps; |
| | 2998 | 70 | | _rebuildRules = rebuildRules; |
| | 2998 | 71 | | _version = version; |
| | 2998 | 72 | | _pages = source.TransitionPages; |
| | 2998 | 73 | | _rules = source.TransitionRules; |
| | 2998 | 74 | | } |
| | | 75 | | |
| | | 76 | | internal bool IsComplete { get; private set; } |
| | | 77 | | |
| | 3003 | 78 | | internal NavigationTransitionPageRoot Pages => _pages; |
| | | 79 | | |
| | 2992 | 80 | | internal NavigationTransitionRuleTable Rules => _rules; |
| | | 81 | | |
| | 272058 | 82 | | internal long RetainedBytes => checked( |
| | 272058 | 83 | | FixedRetainedBytes |
| | 272058 | 84 | | + _affectedSources.RetainedBytes |
| | 272058 | 85 | | + _pageBuilds.RetainedBytes |
| | 272058 | 86 | | + _pageBuildInnerBytes |
| | 272058 | 87 | | + ((long)_pageBuildStateCount * PageBuildState.BaseRetainedBytes) |
| | 272058 | 88 | | + _workingBufferBytes |
| | 272058 | 89 | | + (_ruleOrdinals == null ? 0L : GetArrayBytes(_ruleOrdinals.Length, sizeof(int))) |
| | 272058 | 90 | | + _publicationOwnedBytes); |
| | | 91 | | |
| | 272407 | 92 | | internal int PersistentPageCount => checked( |
| | 272407 | 93 | | 1 |
| | 272407 | 94 | | + _affectedSources.PersistentNodeCount |
| | 272407 | 95 | | + _pageBuilds.PersistentNodeCount |
| | 272407 | 96 | | + _pageBuildInnerPages |
| | 272407 | 97 | | + _pageBuildStateCount |
| | 272407 | 98 | | + _workingBufferPages |
| | 272407 | 99 | | + (_ruleOrdinals == null || _ruleOrdinals.Length == 0 ? 0 : 1) |
| | 272407 | 100 | | + _publicationOwnedPages); |
| | | 101 | | |
| | | 102 | | internal bool Advance(MaintenanceWorkMeter meter) |
| | | 103 | | { |
| | 8225 | 104 | | if (IsComplete) |
| | 2738 | 105 | | return true; |
| | 5487 | 106 | | if (!CaptureAffectedSources(meter)) |
| | 509 | 107 | | return false; |
| | 4978 | 108 | | if (!RefreshDefinitions(meter)) |
| | 1504 | 109 | | return false; |
| | 3474 | 110 | | if (_rebuildRules && !RefreshRules(meter)) |
| | 480 | 111 | | return false; |
| | 2994 | 112 | | IsComplete = true; |
| | 2994 | 113 | | return true; |
| | | 114 | | } |
| | | 115 | | |
| | | 116 | | private bool CaptureAffectedSources(MaintenanceWorkMeter meter) |
| | | 117 | | { |
| | 8274 | 118 | | while (_changedMapOrdinal < _changedMaps.Count) |
| | | 119 | | { |
| | 3296 | 120 | | string mapId = _changedMaps.GetKeyAt(_changedMapOrdinal); |
| | 3296 | 121 | | if (_incomingOrdinal < 0) |
| | | 122 | | { |
| | 3295 | 123 | | if (!meter.TryConsumeComponentNodes(1)) |
| | 508 | 124 | | return false; |
| | 2787 | 125 | | _affectedSources = _affectedSources.Set(mapId, true); |
| | 2787 | 126 | | _incomingOrdinal = 0; |
| | | 127 | | } |
| | 2788 | 128 | | int incomingCount = _operationCandidate?.GetIncomingSourceCount(mapId) ?? 0; |
| | 2928 | 129 | | while (_incomingOrdinal < incomingCount) |
| | | 130 | | { |
| | 141 | 131 | | if (!meter.TryConsumeDependencyEntries(1)) |
| | 1 | 132 | | return false; |
| | 140 | 133 | | _affectedSources = _affectedSources.Set( |
| | 140 | 134 | | _operationCandidate!.GetIncomingSource(mapId, _incomingOrdinal++), |
| | 140 | 135 | | true); |
| | | 136 | | } |
| | 2787 | 137 | | _changedMapOrdinal++; |
| | 2787 | 138 | | _incomingOrdinal = -1; |
| | | 139 | | } |
| | 4978 | 140 | | return true; |
| | | 141 | | } |
| | | 142 | | |
| | | 143 | | private bool RefreshDefinitions(MaintenanceWorkMeter meter) |
| | | 144 | | { |
| | 19954 | 145 | | while (_definitionPhase < 5) |
| | | 146 | | { |
| | 16480 | 147 | | switch (_definitionPhase) |
| | | 148 | | { |
| | | 149 | | case 0: |
| | 3137 | 150 | | if (!AdvancePriorPageDiscovery(meter)) |
| | 141 | 151 | | return false; |
| | 2996 | 152 | | CompleteDefinitionPhase(); |
| | 2996 | 153 | | break; |
| | | 154 | | case 1: |
| | 3422 | 155 | | if (!AdvanceNextPageCount(meter)) |
| | 426 | 156 | | return false; |
| | 2996 | 157 | | CompleteDefinitionPhase(); |
| | 2996 | 158 | | break; |
| | | 159 | | case 2: |
| | 3037 | 160 | | if (!AdvancePagePreparation(meter)) |
| | 42 | 161 | | return false; |
| | 2995 | 162 | | ResetPageCursor(); |
| | 2995 | 163 | | _definitionPhase++; |
| | 2995 | 164 | | break; |
| | | 165 | | case 3: |
| | 3475 | 166 | | if (!AdvanceNextPageFill(meter)) |
| | 480 | 167 | | return false; |
| | 2995 | 168 | | CompleteDefinitionPhase(); |
| | 2995 | 169 | | break; |
| | | 170 | | default: |
| | 3409 | 171 | | if (!AdvancePageSeal(meter)) |
| | 415 | 172 | | return false; |
| | 2994 | 173 | | ClearPageBuildScratch(); |
| | 2994 | 174 | | _definitionPhase++; |
| | | 175 | | break; |
| | | 176 | | } |
| | | 177 | | } |
| | 3474 | 178 | | return true; |
| | | 179 | | } |
| | | 180 | | |
| | | 181 | | private bool AdvancePriorPageDiscovery(MaintenanceWorkMeter meter) |
| | | 182 | | { |
| | 4203 | 183 | | while (true) |
| | | 184 | | { |
| | 7342 | 185 | | if (_pendingPlacementStage < 0) |
| | | 186 | | { |
| | 7340 | 187 | | if (!TryAdvanceEffectiveDefinition(_source, meter)) |
| | | 188 | | { |
| | 3135 | 189 | | return !_definitionCursorActive |
| | 3135 | 190 | | && _definitionSourceOrdinal >= _affectedSources.Count; |
| | | 191 | | } |
| | 4205 | 192 | | if (!TryGetRetainedPrior( |
| | 4205 | 193 | | _pendingOwnerMapId!, |
| | 4205 | 194 | | _pendingDefinition, |
| | 4205 | 195 | | out _pendingTransition)) |
| | | 196 | | { |
| | 2 | 197 | | ClearPendingDefinition(); |
| | 2 | 198 | | continue; |
| | | 199 | | } |
| | 4203 | 200 | | _pendingPlacementStage = 0; |
| | | 201 | | } |
| | 4205 | 202 | | if (_pendingPlacementStage == 0) |
| | | 203 | | { |
| | 4204 | 204 | | if (!meter.TryConsumeDependencyEntries(1)) |
| | 1 | 205 | | return false; |
| | 4203 | 206 | | GetOrAddPage(_pendingTransition.SourcePage); |
| | 4203 | 207 | | _pendingPlacementStage = 1; |
| | | 208 | | } |
| | 4204 | 209 | | if (_pendingTransition.DestinationPage.HasValue) |
| | | 210 | | { |
| | 4199 | 211 | | if (!meter.TryConsumeDependencyEntries(1)) |
| | 1 | 212 | | return false; |
| | 4198 | 213 | | GetOrAddPage(_pendingTransition.DestinationPage.Value); |
| | | 214 | | } |
| | 4203 | 215 | | ClearPendingDefinition(); |
| | | 216 | | } |
| | | 217 | | } |
| | | 218 | | |
| | | 219 | | private bool AdvanceNextPageCount(MaintenanceWorkMeter meter) |
| | | 220 | | { |
| | 8414 | 221 | | while (true) |
| | | 222 | | { |
| | 11836 | 223 | | if (_pendingPlacementStage < 0) |
| | | 224 | | { |
| | 11773 | 225 | | if (!TryAdvanceEffectiveDefinition(_candidate, meter)) |
| | | 226 | | { |
| | 3359 | 227 | | return !_definitionCursorActive |
| | 3359 | 228 | | && _definitionSourceOrdinal >= _affectedSources.Count; |
| | | 229 | | } |
| | 8414 | 230 | | _pendingTransition = CreatePublished( |
| | 8414 | 231 | | _candidate, |
| | 8414 | 232 | | _pendingOwnerMapId!, |
| | 8414 | 233 | | _pendingDefinition); |
| | 8414 | 234 | | _pendingPlacementStage = 0; |
| | | 235 | | } |
| | 8477 | 236 | | if (_pendingPlacementStage == 0) |
| | | 237 | | { |
| | 8445 | 238 | | if (!meter.TryConsumeDependencyEntries(1)) |
| | 31 | 239 | | return false; |
| | 8414 | 240 | | GetOrAddPage(_pendingTransition.SourcePage).NewOutgoingCount++; |
| | 8414 | 241 | | _pendingPlacementStage = 1; |
| | | 242 | | } |
| | 8446 | 243 | | if (_pendingTransition.DestinationPage.HasValue) |
| | | 244 | | { |
| | 8440 | 245 | | if (!meter.TryConsumeDependencyEntries(1)) |
| | 32 | 246 | | return false; |
| | 8408 | 247 | | GetOrAddPage(_pendingTransition.DestinationPage.Value).NewIncomingCount++; |
| | | 248 | | } |
| | 8414 | 249 | | ClearPendingDefinition(); |
| | | 250 | | } |
| | | 251 | | } |
| | | 252 | | |
| | | 253 | | private bool AdvancePagePreparation(MaintenanceWorkMeter meter) |
| | | 254 | | { |
| | 3347 | 255 | | while (TryGetCurrentPageState(out PageBuildState state)) |
| | | 256 | | { |
| | 352 | 257 | | bool complete = state.AdvancePreparation( |
| | 352 | 258 | | _affectedSources, |
| | 352 | 259 | | meter, |
| | 352 | 260 | | out long addedBytes, |
| | 352 | 261 | | out int addedPages); |
| | 352 | 262 | | AddWorkingBuffers(addedBytes, addedPages); |
| | 352 | 263 | | if (!complete) |
| | 42 | 264 | | return false; |
| | 310 | 265 | | AdvancePageCursor(); |
| | 310 | 266 | | } |
| | 2995 | 267 | | return true; |
| | | 268 | | } |
| | | 269 | | |
| | | 270 | | private bool AdvanceNextPageFill(MaintenanceWorkMeter meter) |
| | | 271 | | { |
| | 8413 | 272 | | while (true) |
| | | 273 | | { |
| | 11888 | 274 | | if (_pendingPlacementStage < 0) |
| | | 275 | | { |
| | 11810 | 276 | | if (!TryAdvanceEffectiveDefinition(_candidate, meter)) |
| | | 277 | | { |
| | 3397 | 278 | | return !_definitionCursorActive |
| | 3397 | 279 | | && _definitionSourceOrdinal >= _affectedSources.Count; |
| | | 280 | | } |
| | 8413 | 281 | | _pendingTransition = CreatePublished( |
| | 8413 | 282 | | _candidate, |
| | 8413 | 283 | | _pendingOwnerMapId!, |
| | 8413 | 284 | | _pendingDefinition); |
| | 8413 | 285 | | _pendingPlacementStage = 0; |
| | | 286 | | } |
| | 8491 | 287 | | if (_pendingPlacementStage == 0) |
| | | 288 | | { |
| | 8452 | 289 | | if (!meter.TryConsumeExplicitEdges(1)) |
| | 39 | 290 | | return false; |
| | 8413 | 291 | | GetPage(_pendingTransition.SourcePage).AppendOutgoing(_pendingTransition); |
| | 8413 | 292 | | _pendingPlacementStage = 1; |
| | | 293 | | } |
| | 8452 | 294 | | if (_pendingTransition.DestinationPage.HasValue) |
| | | 295 | | { |
| | 8446 | 296 | | if (!meter.TryConsumeExplicitEdges(1)) |
| | 39 | 297 | | return false; |
| | 8407 | 298 | | GetPage(_pendingTransition.DestinationPage.Value).AppendIncoming( |
| | 8407 | 299 | | new NavigationIncomingTransitionRef(_pendingTransition)); |
| | | 300 | | } |
| | 8413 | 301 | | ClearPendingDefinition(); |
| | | 302 | | } |
| | | 303 | | } |
| | | 304 | | |
| | | 305 | | private bool AdvancePageSeal(MaintenanceWorkMeter meter) |
| | | 306 | | { |
| | 3718 | 307 | | while (TryGetCurrentPageState(out PageBuildState state)) |
| | | 308 | | { |
| | 724 | 309 | | bool complete = state.AdvanceSeal( |
| | 724 | 310 | | meter, |
| | 724 | 311 | | out bool changed, |
| | 724 | 312 | | out long releasedWorkingBytes, |
| | 724 | 313 | | out int releasedWorkingPages); |
| | 724 | 314 | | ReleaseWorkingBuffers(releasedWorkingBytes, releasedWorkingPages); |
| | 724 | 315 | | if (!complete) |
| | 412 | 316 | | return false; |
| | 312 | 317 | | if (changed) |
| | | 318 | | { |
| | 169 | 319 | | if (!meter.TryConsumeComponentNodes(1)) |
| | 3 | 320 | | return false; |
| | 166 | 321 | | NavigationTransitionPage page = state.CreatePage(_version); |
| | 166 | 322 | | _pages = _pages.Set(page, out int copiedNodes, out long copiedBytes); |
| | 166 | 323 | | OwnPublishedPageRoot(state.Address.MapId, page, copiedNodes, copiedBytes); |
| | | 324 | | } |
| | 309 | 325 | | state.ReleaseFinalBuffers( |
| | 309 | 326 | | out releasedWorkingBytes, |
| | 309 | 327 | | out releasedWorkingPages); |
| | 309 | 328 | | ReleaseWorkingBuffers(releasedWorkingBytes, releasedWorkingPages); |
| | 309 | 329 | | AdvancePageCursor(); |
| | 309 | 330 | | } |
| | 2994 | 331 | | return true; |
| | | 332 | | } |
| | | 333 | | |
| | | 334 | | private bool TryAdvanceEffectiveDefinition( |
| | | 335 | | NavigationWorldGraph graph, |
| | | 336 | | MaintenanceWorkMeter meter) |
| | | 337 | | { |
| | 39406 | 338 | | while (_definitionSourceOrdinal < _affectedSources.Count) |
| | | 339 | | { |
| | 30419 | 340 | | string ownerMapId = _affectedSources.GetKeyAt(_definitionSourceOrdinal); |
| | 30419 | 341 | | if (!_definitionCursorActive) |
| | | 342 | | { |
| | 8374 | 343 | | graph.TryGetMap(ownerMapId, out NavigationMapInstance? instance); |
| | 8374 | 344 | | _definitionCursor = new EffectiveTransitionCursor(instance); |
| | 8374 | 345 | | _definitionCursorActive = true; |
| | | 346 | | } |
| | 30419 | 347 | | if (!meter.TryConsumeExplicitEdges(1)) |
| | 904 | 348 | | return false; |
| | 29515 | 349 | | bool hasValue = _definitionCursor.AdvanceOne( |
| | 29515 | 350 | | out TraversalTransitionDefinition definition, |
| | 29515 | 351 | | out bool complete); |
| | 29515 | 352 | | if (complete) |
| | | 353 | | { |
| | 8374 | 354 | | _definitionCursorActive = false; |
| | 8374 | 355 | | _definitionSourceOrdinal++; |
| | 8374 | 356 | | continue; |
| | | 357 | | } |
| | 21141 | 358 | | if (hasValue) |
| | | 359 | | { |
| | 21032 | 360 | | _pendingOwnerMapId = ownerMapId; |
| | 21032 | 361 | | _pendingDefinition = definition; |
| | 21032 | 362 | | return true; |
| | | 363 | | } |
| | | 364 | | } |
| | 8987 | 365 | | return false; |
| | | 366 | | } |
| | | 367 | | |
| | | 368 | | private bool TryGetRetainedPrior( |
| | | 369 | | string ownerMapId, |
| | | 370 | | TraversalTransitionDefinition definition, |
| | | 371 | | out NavigationPublishedTransition transition) |
| | | 372 | | { |
| | 4205 | 373 | | NavigationPublishedTransition located = CreatePublished( |
| | 4205 | 374 | | _source, |
| | 4205 | 375 | | ownerMapId, |
| | 4205 | 376 | | definition); |
| | 4205 | 377 | | if (_pages.TryGet(located.SourcePage, out NavigationTransitionPage page) |
| | 4205 | 378 | | && page.TryGetOutgoing(located.Owner, out transition)) |
| | | 379 | | { |
| | 4203 | 380 | | return true; |
| | | 381 | | } |
| | 2 | 382 | | transition = default; |
| | 2 | 383 | | return false; |
| | | 384 | | } |
| | | 385 | | |
| | | 386 | | private PageBuildState GetOrAddPage(NavigationTransitionPageAddress address) |
| | | 387 | | { |
| | 25223 | 388 | | bool hadMap = _pageBuilds.TryGetValue( |
| | 25223 | 389 | | address.MapId, |
| | 25223 | 390 | | out PersistentIntMap<PageBuildState> map); |
| | 25223 | 391 | | map ??= PersistentIntMap<PageBuildState>.Empty; |
| | 25223 | 392 | | if (map.TryGetValue(address.PageIndex, out PageBuildState state)) |
| | 24911 | 393 | | return state; |
| | 312 | 394 | | _pages.TryGet(address, out NavigationTransitionPage prior); |
| | 312 | 395 | | state = new PageBuildState(address, prior); |
| | 312 | 396 | | long priorInnerBytes = hadMap ? map.RetainedBytes : 0L; |
| | 312 | 397 | | int priorInnerPages = hadMap ? map.PersistentNodeCount : 0; |
| | 312 | 398 | | map = map.Set(address.PageIndex, state); |
| | 312 | 399 | | _pageBuilds = _pageBuilds.Set(address.MapId, map); |
| | 312 | 400 | | _pageBuildInnerBytes = checked( |
| | 312 | 401 | | _pageBuildInnerBytes - priorInnerBytes + map.RetainedBytes); |
| | 312 | 402 | | _pageBuildInnerPages = checked( |
| | 312 | 403 | | _pageBuildInnerPages - priorInnerPages + map.PersistentNodeCount); |
| | 312 | 404 | | _pageBuildStateCount++; |
| | 312 | 405 | | return state; |
| | | 406 | | } |
| | | 407 | | |
| | | 408 | | private PageBuildState GetPage(NavigationTransitionPageAddress address) |
| | | 409 | | { |
| | 16820 | 410 | | _pageBuilds.TryGetValue(address.MapId, out PersistentIntMap<PageBuildState> map); |
| | 16820 | 411 | | map.TryGetValue(address.PageIndex, out PageBuildState state); |
| | 16820 | 412 | | return state; |
| | | 413 | | } |
| | | 414 | | |
| | | 415 | | private bool TryGetCurrentPageState(out PageBuildState state) |
| | | 416 | | { |
| | 7422 | 417 | | while (_pageMapOrdinal < _pageBuilds.Count) |
| | | 418 | | { |
| | 1433 | 419 | | PersistentIntMap<PageBuildState> map = _pageBuilds.GetValueAt(_pageMapOrdinal); |
| | 1433 | 420 | | if (_pageOrdinal < map.Count) |
| | | 421 | | { |
| | 1076 | 422 | | state = map.GetValueAt(_pageOrdinal); |
| | 1076 | 423 | | return true; |
| | | 424 | | } |
| | 357 | 425 | | _pageMapOrdinal++; |
| | 357 | 426 | | _pageOrdinal = 0; |
| | | 427 | | } |
| | 5989 | 428 | | state = null!; |
| | 5989 | 429 | | return false; |
| | | 430 | | } |
| | | 431 | | |
| | 619 | 432 | | private void AdvancePageCursor() => _pageOrdinal++; |
| | | 433 | | |
| | | 434 | | private void ResetPageCursor() |
| | | 435 | | { |
| | 5989 | 436 | | _pageMapOrdinal = 0; |
| | 5989 | 437 | | _pageOrdinal = 0; |
| | 5989 | 438 | | } |
| | | 439 | | |
| | | 440 | | private void CompleteDefinitionPhase() |
| | | 441 | | { |
| | 8987 | 442 | | _definitionPhase++; |
| | 8987 | 443 | | _definitionSourceOrdinal = 0; |
| | 8987 | 444 | | _definitionCursorActive = false; |
| | 8987 | 445 | | ClearPendingDefinition(); |
| | 8987 | 446 | | } |
| | | 447 | | |
| | | 448 | | private void ClearPendingDefinition() |
| | | 449 | | { |
| | 30019 | 450 | | _pendingOwnerMapId = null; |
| | 30019 | 451 | | _pendingDefinition = default; |
| | 30019 | 452 | | _pendingTransition = default; |
| | 30019 | 453 | | _pendingPlacementStage = -1; |
| | 30019 | 454 | | } |
| | | 455 | | |
| | | 456 | | private void ClearPageBuildScratch() |
| | | 457 | | { |
| | 2994 | 458 | | _pageBuilds = PersistentStringMap<PersistentIntMap<PageBuildState>>.Empty; |
| | 2994 | 459 | | _pageBuildInnerBytes = 0; |
| | 2994 | 460 | | _pageBuildInnerPages = 0; |
| | 2994 | 461 | | _pageBuildStateCount = 0; |
| | 2994 | 462 | | ResetPageCursor(); |
| | 2994 | 463 | | } |
| | | 464 | | |
| | | 465 | | private void AddWorkingBuffers(long bytes, int pages) |
| | | 466 | | { |
| | 1301 | 467 | | _workingBufferBytes = checked(_workingBufferBytes + bytes); |
| | 1301 | 468 | | _workingBufferPages = checked(_workingBufferPages + pages); |
| | 1301 | 469 | | } |
| | | 470 | | |
| | | 471 | | private void ReleaseWorkingBuffers(long bytes, int pages) |
| | | 472 | | { |
| | 1982 | 473 | | _workingBufferBytes = checked(_workingBufferBytes - bytes); |
| | 1982 | 474 | | _workingBufferPages = checked(_workingBufferPages - pages); |
| | 1982 | 475 | | } |
| | | 476 | | |
| | | 477 | | private void OwnPublishedPageRoot( |
| | | 478 | | string mapId, |
| | | 479 | | NavigationTransitionPage page, |
| | | 480 | | int copiedNodes, |
| | | 481 | | long copiedBytes) |
| | | 482 | | { |
| | 166 | 483 | | if (!_publicationRootOwned) |
| | | 484 | | { |
| | 91 | 485 | | _publicationRootOwned = true; |
| | 91 | 486 | | _publicationOwnedBytes = checked(_publicationOwnedBytes + 88L); |
| | 91 | 487 | | _publicationOwnedPages++; |
| | | 488 | | } |
| | 166 | 489 | | if (!string.Equals(_publicationRootMapId, mapId, StringComparison.Ordinal)) |
| | | 490 | | { |
| | 101 | 491 | | _publicationRootMapId = mapId; |
| | 101 | 492 | | _publicationOwnedBytes = checked(_publicationOwnedBytes + 32L); |
| | | 493 | | } |
| | 166 | 494 | | _publicationOwnedBytes = checked( |
| | 166 | 495 | | _publicationOwnedBytes |
| | 166 | 496 | | + copiedBytes |
| | 166 | 497 | | + (page.IsEmpty ? 0L : page.RetainedBytes)); |
| | 166 | 498 | | _publicationOwnedPages = checked( |
| | 166 | 499 | | _publicationOwnedPages |
| | 166 | 500 | | + copiedNodes |
| | 166 | 501 | | + (page.IsEmpty ? 0 : page.PersistentPageCount)); |
| | 166 | 502 | | } |
| | | 503 | | |
| | | 504 | | private bool RefreshRules(MaintenanceWorkMeter meter) |
| | | 505 | | { |
| | 11243 | 506 | | while (_ruleCountMapOrdinal < _candidate.MapCount) |
| | | 507 | | { |
| | 10286 | 508 | | if (!meter.TryConsumeComponentNodes(1)) |
| | 472 | 509 | | return false; |
| | 9814 | 510 | | _ruleCount = checked( |
| | 9814 | 511 | | _ruleCount |
| | 9814 | 512 | | + _candidate.GetInstance(_ruleCountMapOrdinal++).Map.TransitionRuleSpan.Length); |
| | | 513 | | } |
| | 957 | 514 | | if (_ruleBuffer == null) |
| | | 515 | | { |
| | 949 | 516 | | _ruleBuffer = new TraversalTransitionRule[_ruleCount]; |
| | 949 | 517 | | _ruleOrdinals = new int[_candidate.MapCount]; |
| | 949 | 518 | | _ruleContentEqual = _rules.Count == _ruleCount; |
| | 949 | 519 | | AddWorkingBuffers( |
| | 949 | 520 | | GetArrayBytes(_ruleBuffer.Length, NavigationTransitionRuleTable.RecordRetainedBytes), |
| | 949 | 521 | | _ruleBuffer.Length == 0 ? 0 : 1); |
| | | 522 | | } |
| | 1012 | 523 | | while (_ruleOutputOrdinal < _ruleBuffer.Length) |
| | | 524 | | { |
| | 63 | 525 | | if (!_ruleScanActive) |
| | | 526 | | { |
| | 55 | 527 | | _ruleScanMapOrdinal = 0; |
| | 55 | 528 | | _ruleBestMapOrdinal = -1; |
| | 55 | 529 | | _ruleScanActive = true; |
| | | 530 | | } |
| | 138 | 531 | | while (_ruleScanMapOrdinal < _candidate.MapCount) |
| | | 532 | | { |
| | 83 | 533 | | if (!meter.TryConsumeDependencyEntries(1)) |
| | 8 | 534 | | return false; |
| | 75 | 535 | | int mapOrdinal = _ruleScanMapOrdinal++; |
| | 75 | 536 | | ReadOnlySpan<TraversalTransitionRule> rules = |
| | 75 | 537 | | _candidate.GetInstance(mapOrdinal).Map.TransitionRuleSpan; |
| | 75 | 538 | | if (_ruleOrdinals![mapOrdinal] >= rules.Length) |
| | | 539 | | continue; |
| | 61 | 540 | | if (_ruleBestMapOrdinal < 0) |
| | | 541 | | { |
| | 55 | 542 | | _ruleBestMapOrdinal = mapOrdinal; |
| | 55 | 543 | | continue; |
| | | 544 | | } |
| | 6 | 545 | | TraversalTransitionRule candidate = rules[_ruleOrdinals[mapOrdinal]]; |
| | 6 | 546 | | ReadOnlySpan<TraversalTransitionRule> bestRules = |
| | 6 | 547 | | _candidate.GetInstance(_ruleBestMapOrdinal).Map.TransitionRuleSpan; |
| | 6 | 548 | | TraversalTransitionRule best = bestRules[_ruleOrdinals[_ruleBestMapOrdinal]]; |
| | 6 | 549 | | int comparison = string.CompareOrdinal(candidate.Id, best.Id); |
| | | 550 | | System.Diagnostics.Debug.Assert(comparison != 0, |
| | | 551 | | "Map folding rejects duplicate global transition-rule ownership."); |
| | 6 | 552 | | if (comparison < 0) |
| | 4 | 553 | | _ruleBestMapOrdinal = mapOrdinal; |
| | | 554 | | } |
| | 55 | 555 | | TraversalTransitionRule rule = _candidate.GetInstance(_ruleBestMapOrdinal) |
| | 55 | 556 | | .Map.TransitionRuleSpan[_ruleOrdinals![_ruleBestMapOrdinal]++]; |
| | 55 | 557 | | _ruleBuffer[_ruleOutputOrdinal] = rule; |
| | 55 | 558 | | if (_ruleContentEqual && !_rules[_ruleOutputOrdinal].Equals(rule)) |
| | 3 | 559 | | _ruleContentEqual = false; |
| | 55 | 560 | | _ruleOutputOrdinal++; |
| | 55 | 561 | | _ruleScanActive = false; |
| | | 562 | | } |
| | 949 | 563 | | long bufferBytes = GetArrayBytes( |
| | 949 | 564 | | _ruleBuffer.Length, |
| | 949 | 565 | | NavigationTransitionRuleTable.RecordRetainedBytes); |
| | 949 | 566 | | int bufferPages = _ruleBuffer.Length == 0 ? 0 : 1; |
| | 949 | 567 | | ReleaseWorkingBuffers(bufferBytes, bufferPages); |
| | 949 | 568 | | if (_ruleContentEqual) |
| | 905 | 569 | | _ruleBuffer = null; |
| | | 570 | | else |
| | | 571 | | { |
| | 44 | 572 | | _rules = new NavigationTransitionRuleTable(_ruleBuffer, _version); |
| | 44 | 573 | | _publicationOwnedBytes = checked( |
| | 44 | 574 | | _publicationOwnedBytes |
| | 44 | 575 | | + NavigationTransitionRuleTable.BaseRetainedBytes |
| | 44 | 576 | | + bufferBytes); |
| | 44 | 577 | | _publicationOwnedPages = checked( |
| | 44 | 578 | | _publicationOwnedPages + _rules.PersistentPageCount); |
| | | 579 | | } |
| | 949 | 580 | | _ruleOrdinals = null; |
| | 949 | 581 | | return true; |
| | | 582 | | } |
| | | 583 | | |
| | | 584 | | private static NavigationPublishedTransition CreatePublished( |
| | | 585 | | NavigationWorldGraph graph, |
| | | 586 | | string ownerMapId, |
| | | 587 | | TraversalTransitionDefinition definition) |
| | | 588 | | { |
| | 21032 | 589 | | bool foundOwner = graph.TryGetMap(ownerMapId, out NavigationMapInstance source); |
| | | 590 | | System.Diagnostics.Debug.Assert(foundOwner, |
| | | 591 | | "Effective transition cursors enumerate definitions from an owned map."); |
| | 21032 | 592 | | bool foundSource = source.TryGetSlot(definition.SourceIndex, out int sourceSlot); |
| | | 593 | | System.Diagnostics.Debug.Assert(foundSource, |
| | | 594 | | "Published transition definitions retain their validated local source cell."); |
| | 21032 | 595 | | var sourcePage = new NavigationTransitionPageAddress( |
| | 21032 | 596 | | ownerMapId, |
| | 21032 | 597 | | sourceSlot / NavigationSemanticPage.SlotCount); |
| | 21032 | 598 | | NavigationTransitionPageAddress? destinationPage = null; |
| | 21032 | 599 | | if (graph.TryGetMap(definition.Destination.MapId, out NavigationMapInstance destination) |
| | 21032 | 600 | | && destination.TryGetSlot(definition.Destination.Index, out int destinationSlot)) |
| | | 601 | | { |
| | 21016 | 602 | | destinationPage = new NavigationTransitionPageAddress( |
| | 21016 | 603 | | definition.Destination.MapId, |
| | 21016 | 604 | | destinationSlot / NavigationSemanticPage.SlotCount); |
| | | 605 | | } |
| | 21032 | 606 | | return new NavigationPublishedTransition( |
| | 21032 | 607 | | ownerMapId, |
| | 21032 | 608 | | definition, |
| | 21032 | 609 | | sourcePage, |
| | 21032 | 610 | | destinationPage); |
| | | 611 | | } |
| | | 612 | | |
| | 1906 | 613 | | private static long GetArrayBytes(int count, long elementBytes) => count == 0 |
| | 1906 | 614 | | ? 0L |
| | 1906 | 615 | | : checked(24L + ((long)count * elementBytes)); |
| | | 616 | | |
| | | 617 | | private sealed class PageBuildState |
| | | 618 | | { |
| | | 619 | | internal const long BaseRetainedBytes = 240L; |
| | | 620 | | private readonly NavigationTransitionPage? _prior; |
| | 312 | 621 | | private NavigationPublishedTransition[] _outgoing = |
| | 312 | 622 | | Array.Empty<NavigationPublishedTransition>(); |
| | 312 | 623 | | private NavigationIncomingTransitionRef[] _incoming = |
| | 312 | 624 | | Array.Empty<NavigationIncomingTransitionRef>(); |
| | | 625 | | private NavigationPublishedTransition[]? _outgoingScratch; |
| | | 626 | | private NavigationIncomingTransitionRef[]? _incomingScratch; |
| | | 627 | | private MergeSortWork<NavigationPublishedTransition, OutgoingComparer> _outgoingSort; |
| | | 628 | | private MergeSortWork<NavigationIncomingTransitionRef, IncomingComparer> _incomingSort; |
| | | 629 | | private int _prepareStage; |
| | | 630 | | private int _prepareOrdinal; |
| | | 631 | | private int _retainedOutgoingCount; |
| | | 632 | | private int _retainedIncomingCount; |
| | | 633 | | private int _outgoingFill; |
| | | 634 | | private int _incomingFill; |
| | | 635 | | private int _sealStage; |
| | | 636 | | private int _compareOrdinal; |
| | | 637 | | private bool _sameContent; |
| | | 638 | | |
| | 312 | 639 | | internal PageBuildState( |
| | 312 | 640 | | NavigationTransitionPageAddress address, |
| | 312 | 641 | | NavigationTransitionPage? prior) |
| | | 642 | | { |
| | 312 | 643 | | Address = address; |
| | 312 | 644 | | _prior = prior; |
| | 312 | 645 | | } |
| | | 646 | | |
| | | 647 | | internal NavigationTransitionPageAddress Address { get; } |
| | | 648 | | |
| | | 649 | | internal int NewOutgoingCount { get; set; } |
| | | 650 | | |
| | | 651 | | internal int NewIncomingCount { get; set; } |
| | | 652 | | |
| | | 653 | | internal void AppendOutgoing(NavigationPublishedTransition transition) => |
| | 8413 | 654 | | _outgoing[_outgoingFill++] = transition; |
| | | 655 | | |
| | | 656 | | internal void AppendIncoming(NavigationIncomingTransitionRef incoming) => |
| | 8407 | 657 | | _incoming[_incomingFill++] = incoming; |
| | | 658 | | |
| | | 659 | | internal bool AdvancePreparation( |
| | | 660 | | PersistentStringMap<bool> affectedSources, |
| | | 661 | | MaintenanceWorkMeter meter, |
| | | 662 | | out long addedBytes, |
| | | 663 | | out int addedPages) |
| | | 664 | | { |
| | 352 | 665 | | addedBytes = 0; |
| | 352 | 666 | | addedPages = 0; |
| | 1904 | 667 | | while (_prepareStage < 5) |
| | | 668 | | { |
| | 1594 | 669 | | if (_prepareStage == 0) |
| | | 670 | | { |
| | 320 | 671 | | int count = _prior?.OutgoingCount ?? 0; |
| | 4524 | 672 | | while (_prepareOrdinal < count) |
| | | 673 | | { |
| | 4213 | 674 | | if (!meter.TryConsumeExplicitEdges(1)) |
| | 9 | 675 | | return false; |
| | 4204 | 676 | | if (!affectedSources.ContainsKey( |
| | 4204 | 677 | | _prior!.GetOutgoingAt(_prepareOrdinal++).Owner.MapId)) |
| | | 678 | | { |
| | 1 | 679 | | _retainedOutgoingCount++; |
| | | 680 | | } |
| | | 681 | | } |
| | 311 | 682 | | _prepareStage++; |
| | 311 | 683 | | _prepareOrdinal = 0; |
| | | 684 | | } |
| | 1274 | 685 | | else if (_prepareStage == 1) |
| | | 686 | | { |
| | 321 | 687 | | int count = _prior?.IncomingCount ?? 0; |
| | 4521 | 688 | | while (_prepareOrdinal < count) |
| | | 689 | | { |
| | 4210 | 690 | | if (!meter.TryConsumeExplicitEdges(1)) |
| | 10 | 691 | | return false; |
| | 4200 | 692 | | if (!affectedSources.ContainsKey( |
| | 4200 | 693 | | _prior!.GetIncomingAt(_prepareOrdinal++).Owner.MapId)) |
| | | 694 | | { |
| | 2 | 695 | | _retainedIncomingCount++; |
| | | 696 | | } |
| | | 697 | | } |
| | 311 | 698 | | _prepareStage++; |
| | 311 | 699 | | _prepareOrdinal = 0; |
| | | 700 | | } |
| | 953 | 701 | | else if (_prepareStage == 2) |
| | | 702 | | { |
| | 314 | 703 | | if (!meter.TryConsumeComponentNodes(1)) |
| | 4 | 704 | | return false; |
| | 310 | 705 | | int outgoingLength = checked(_retainedOutgoingCount + NewOutgoingCount); |
| | 310 | 706 | | int incomingLength = checked(_retainedIncomingCount + NewIncomingCount); |
| | 310 | 707 | | _outgoing = outgoingLength == 0 |
| | 310 | 708 | | ? Array.Empty<NavigationPublishedTransition>() |
| | 310 | 709 | | : new NavigationPublishedTransition[outgoingLength]; |
| | 310 | 710 | | _incoming = incomingLength == 0 |
| | 310 | 711 | | ? Array.Empty<NavigationIncomingTransitionRef>() |
| | 310 | 712 | | : new NavigationIncomingTransitionRef[incomingLength]; |
| | 310 | 713 | | _outgoingScratch = outgoingLength > 1 |
| | 310 | 714 | | ? new NavigationPublishedTransition[outgoingLength] |
| | 310 | 715 | | : null; |
| | 310 | 716 | | _incomingScratch = incomingLength > 1 |
| | 310 | 717 | | ? new NavigationIncomingTransitionRef[incomingLength] |
| | 310 | 718 | | : null; |
| | 310 | 719 | | _outgoingSort.Initialize(_outgoing, _outgoingScratch); |
| | 310 | 720 | | _incomingSort.Initialize(_incoming, _incomingScratch); |
| | 310 | 721 | | addedBytes = WorkingBufferBytes; |
| | 310 | 722 | | addedPages = WorkingBufferPages; |
| | 310 | 723 | | _prepareStage++; |
| | | 724 | | } |
| | 639 | 725 | | else if (_prepareStage == 3) |
| | | 726 | | { |
| | 319 | 727 | | int count = _prior?.OutgoingCount ?? 0; |
| | 4522 | 728 | | while (_prepareOrdinal < count) |
| | | 729 | | { |
| | 4212 | 730 | | if (!meter.TryConsumeExplicitEdges(1)) |
| | 9 | 731 | | return false; |
| | 4203 | 732 | | NavigationPublishedTransition transition = |
| | 4203 | 733 | | _prior!.GetOutgoingAt(_prepareOrdinal++); |
| | 4203 | 734 | | if (!affectedSources.ContainsKey(transition.Owner.MapId)) |
| | 1 | 735 | | _outgoing[_outgoingFill++] = transition; |
| | | 736 | | } |
| | 310 | 737 | | _prepareStage++; |
| | 310 | 738 | | _prepareOrdinal = 0; |
| | | 739 | | } |
| | | 740 | | else |
| | | 741 | | { |
| | 320 | 742 | | int count = _prior?.IncomingCount ?? 0; |
| | 4520 | 743 | | while (_prepareOrdinal < count) |
| | | 744 | | { |
| | 4210 | 745 | | if (!meter.TryConsumeExplicitEdges(1)) |
| | 10 | 746 | | return false; |
| | 4200 | 747 | | NavigationIncomingTransitionRef incoming = |
| | 4200 | 748 | | _prior!.GetIncomingAt(_prepareOrdinal++); |
| | 4200 | 749 | | if (!affectedSources.ContainsKey(incoming.Owner.MapId)) |
| | 2 | 750 | | _incoming[_incomingFill++] = incoming; |
| | | 751 | | } |
| | 310 | 752 | | _prepareStage++; |
| | | 753 | | } |
| | | 754 | | } |
| | 310 | 755 | | return true; |
| | | 756 | | } |
| | | 757 | | |
| | | 758 | | internal bool AdvanceSeal( |
| | | 759 | | MaintenanceWorkMeter meter, |
| | | 760 | | out bool changed, |
| | | 761 | | out long releasedWorkingBytes, |
| | | 762 | | out int releasedWorkingPages) |
| | | 763 | | { |
| | 724 | 764 | | changed = false; |
| | 724 | 765 | | releasedWorkingBytes = 0; |
| | 724 | 766 | | releasedWorkingPages = 0; |
| | 724 | 767 | | if (_sealStage == 0) |
| | | 768 | | { |
| | 714 | 769 | | if (!_outgoingSort.Advance(meter) || !_incomingSort.Advance(meter)) |
| | 404 | 770 | | return false; |
| | 310 | 771 | | releasedWorkingBytes = SortScratchBytes; |
| | 310 | 772 | | releasedWorkingPages = SortScratchPages; |
| | 310 | 773 | | _outgoingScratch = null; |
| | 310 | 774 | | _incomingScratch = null; |
| | 310 | 775 | | _outgoingSort.Release(); |
| | 310 | 776 | | _incomingSort.Release(); |
| | 310 | 777 | | _sameContent = _prior != null |
| | 310 | 778 | | && _prior.OutgoingCount == _outgoing.Length |
| | 310 | 779 | | && _prior.IncomingCount == _incoming.Length; |
| | 310 | 780 | | _sealStage++; |
| | | 781 | | } |
| | 320 | 782 | | if (_sealStage == 1 && _sameContent) |
| | | 783 | | { |
| | 158 | 784 | | int total = checked(_outgoing.Length + _incoming.Length); |
| | 8522 | 785 | | while (_compareOrdinal < total) |
| | | 786 | | { |
| | 8379 | 787 | | if (!meter.TryConsumeExplicitEdges(1)) |
| | 8 | 788 | | return false; |
| | 8371 | 789 | | bool equal = _compareOrdinal < _outgoing.Length |
| | 8371 | 790 | | ? _prior!.GetOutgoingAt(_compareOrdinal).Equals( |
| | 8371 | 791 | | _outgoing[_compareOrdinal]) |
| | 8371 | 792 | | : _prior!.GetIncomingAt(_compareOrdinal - _outgoing.Length).Equals( |
| | 8371 | 793 | | _incoming[_compareOrdinal - _outgoing.Length]); |
| | 8371 | 794 | | _compareOrdinal++; |
| | 8371 | 795 | | if (!equal) |
| | | 796 | | { |
| | 7 | 797 | | _sameContent = false; |
| | 7 | 798 | | break; |
| | | 799 | | } |
| | | 800 | | } |
| | 150 | 801 | | _sealStage++; |
| | | 802 | | } |
| | 162 | 803 | | else if (_sealStage == 1) |
| | 160 | 804 | | _sealStage++; |
| | 312 | 805 | | changed = !_sameContent; |
| | 312 | 806 | | return true; |
| | | 807 | | } |
| | | 808 | | |
| | | 809 | | internal NavigationTransitionPage CreatePage(long version) => |
| | 166 | 810 | | new(Address, version, _outgoing, _incoming); |
| | | 811 | | |
| | | 812 | | internal void ReleaseFinalBuffers(out long bytes, out int pages) |
| | | 813 | | { |
| | 309 | 814 | | bytes = FinalArrayBytes; |
| | 309 | 815 | | pages = FinalArrayPages; |
| | 309 | 816 | | _outgoing = Array.Empty<NavigationPublishedTransition>(); |
| | 309 | 817 | | _incoming = Array.Empty<NavigationIncomingTransitionRef>(); |
| | 309 | 818 | | } |
| | | 819 | | |
| | 310 | 820 | | private long WorkingBufferBytes => checked(FinalArrayBytes + SortScratchBytes); |
| | | 821 | | |
| | 310 | 822 | | private int WorkingBufferPages => checked(FinalArrayPages + SortScratchPages); |
| | | 823 | | |
| | 619 | 824 | | private long FinalArrayBytes => checked( |
| | 619 | 825 | | NavigationTransitionPage.GetArrayBytes( |
| | 619 | 826 | | _outgoing.Length, |
| | 619 | 827 | | NavigationTransitionPage.OutgoingRecordBytes) |
| | 619 | 828 | | + NavigationTransitionPage.GetArrayBytes( |
| | 619 | 829 | | _incoming.Length, |
| | 619 | 830 | | NavigationTransitionPage.IncomingRecordBytes)); |
| | | 831 | | |
| | | 832 | | private int FinalArrayPages => |
| | 619 | 833 | | (_outgoing.Length == 0 ? 0 : 1) + (_incoming.Length == 0 ? 0 : 1); |
| | | 834 | | |
| | 620 | 835 | | private long SortScratchBytes => checked( |
| | 620 | 836 | | NavigationTransitionPage.GetArrayBytes( |
| | 620 | 837 | | _outgoingScratch?.Length ?? 0, |
| | 620 | 838 | | NavigationTransitionPage.OutgoingRecordBytes) |
| | 620 | 839 | | + NavigationTransitionPage.GetArrayBytes( |
| | 620 | 840 | | _incomingScratch?.Length ?? 0, |
| | 620 | 841 | | NavigationTransitionPage.IncomingRecordBytes)); |
| | | 842 | | |
| | | 843 | | private int SortScratchPages => |
| | 620 | 844 | | (_outgoingScratch == null ? 0 : 1) + (_incomingScratch == null ? 0 : 1); |
| | | 845 | | } |
| | | 846 | | |
| | | 847 | | private interface ITransitionRecordComparer<T> |
| | | 848 | | { |
| | | 849 | | int Compare(T left, T right); |
| | | 850 | | } |
| | | 851 | | |
| | | 852 | | private readonly struct OutgoingComparer : ITransitionRecordComparer<NavigationPublishedTransition> |
| | | 853 | | { |
| | | 854 | | public int Compare( |
| | | 855 | | NavigationPublishedTransition left, |
| | | 856 | | NavigationPublishedTransition right) => |
| | 24702 | 857 | | NavigationTransitionPage.CompareOutgoing(left, right); |
| | | 858 | | } |
| | | 859 | | |
| | | 860 | | private readonly struct IncomingComparer : ITransitionRecordComparer<NavigationIncomingTransitionRef> |
| | | 861 | | { |
| | | 862 | | public int Compare( |
| | | 863 | | NavigationIncomingTransitionRef left, |
| | | 864 | | NavigationIncomingTransitionRef right) => |
| | 24694 | 865 | | NavigationTransitionPage.CompareIncoming(left, right); |
| | | 866 | | } |
| | | 867 | | |
| | | 868 | | private struct MergeSortWork<T, TComparer> |
| | | 869 | | where TComparer : struct, ITransitionRecordComparer<T> |
| | | 870 | | { |
| | | 871 | | private T[]? _values; |
| | | 872 | | private T[]? _source; |
| | | 873 | | private T[]? _destination; |
| | | 874 | | private int _width; |
| | | 875 | | private int _left; |
| | | 876 | | private int _middle; |
| | | 877 | | private int _right; |
| | | 878 | | private int _sourceLeft; |
| | | 879 | | private int _sourceRight; |
| | | 880 | | private int _destinationOrdinal; |
| | | 881 | | private bool _runActive; |
| | | 882 | | |
| | | 883 | | internal void Initialize(T[] values, T[]? scratch) |
| | | 884 | | { |
| | 620 | 885 | | _values = values; |
| | 620 | 886 | | _source = values; |
| | 620 | 887 | | _destination = scratch; |
| | 620 | 888 | | _width = 1; |
| | 620 | 889 | | _left = 0; |
| | 620 | 890 | | } |
| | | 891 | | |
| | | 892 | | internal bool Advance(MaintenanceWorkMeter meter) |
| | | 893 | | { |
| | | 894 | | System.Diagnostics.Debug.Assert(_values != null, |
| | | 895 | | "Transition page sorting is initialized during page preparation."); |
| | 1228 | 896 | | if (_values!.Length < 2) |
| | 324 | 897 | | return true; |
| | 904 | 898 | | var comparer = default(TComparer); |
| | 17190 | 899 | | while (_width < _values.Length) |
| | | 900 | | { |
| | 16616 | 901 | | if (!_runActive) |
| | | 902 | | { |
| | 16286 | 903 | | _middle = Math.Min(_left + _width, _values.Length); |
| | 16286 | 904 | | _right = Math.Min(_left + (_width * 2), _values.Length); |
| | 16286 | 905 | | _sourceLeft = _left; |
| | 16286 | 906 | | _sourceRight = _middle; |
| | 16286 | 907 | | _destinationOrdinal = _left; |
| | 16286 | 908 | | _runActive = true; |
| | | 909 | | } |
| | 115396 | 910 | | while (_destinationOrdinal < _right) |
| | | 911 | | { |
| | 99110 | 912 | | if (!meter.TryConsumeExplicitEdges(1)) |
| | 330 | 913 | | return false; |
| | 98780 | 914 | | T[] source = _source!; |
| | 98780 | 915 | | T[] destination = _destination!; |
| | 98780 | 916 | | bool takeLeft = _sourceRight >= _right |
| | 98780 | 917 | | || (_sourceLeft < _middle |
| | 98780 | 918 | | && comparer.Compare( |
| | 98780 | 919 | | source[_sourceLeft], |
| | 98780 | 920 | | source[_sourceRight]) <= 0); |
| | 98780 | 921 | | destination[_destinationOrdinal++] = takeLeft |
| | 98780 | 922 | | ? source[_sourceLeft++] |
| | 98780 | 923 | | : source[_sourceRight++]; |
| | | 924 | | } |
| | 16286 | 925 | | _runActive = false; |
| | 16286 | 926 | | _left = _right; |
| | 16286 | 927 | | if (_left < _values.Length) |
| | | 928 | | continue; |
| | 1617 | 929 | | T[] priorSource = _source!; |
| | 1617 | 930 | | _source = _destination; |
| | 1617 | 931 | | _destination = priorSource; |
| | 1617 | 932 | | _width = _width > _values.Length / 2 |
| | 1617 | 933 | | ? _values.Length |
| | 1617 | 934 | | : _width * 2; |
| | 1617 | 935 | | _left = 0; |
| | | 936 | | } |
| | 574 | 937 | | if (!ReferenceEquals(_source, _values)) |
| | | 938 | | { |
| | 455 | 939 | | while (_left < _values.Length) |
| | | 940 | | { |
| | 224 | 941 | | if (!meter.TryConsumeExplicitEdges(1)) |
| | 74 | 942 | | return false; |
| | 150 | 943 | | _values[_left] = _source![_left]; |
| | 150 | 944 | | _left++; |
| | | 945 | | } |
| | | 946 | | } |
| | 500 | 947 | | return true; |
| | | 948 | | } |
| | | 949 | | |
| | | 950 | | internal void Release() |
| | | 951 | | { |
| | 620 | 952 | | _values = null; |
| | 620 | 953 | | _source = null; |
| | 620 | 954 | | _destination = null; |
| | 620 | 955 | | } |
| | | 956 | | } |
| | | 957 | | |
| | | 958 | | private struct EffectiveTransitionCursor |
| | | 959 | | { |
| | | 960 | | private readonly NavigationMap? _map; |
| | | 961 | | private readonly NavigationMapOverlayState? _overlay; |
| | | 962 | | private int _bakedOrdinal; |
| | | 963 | | private int _overlayOrdinal; |
| | | 964 | | |
| | | 965 | | internal EffectiveTransitionCursor(NavigationMapInstance? instance) |
| | | 966 | | { |
| | 8374 | 967 | | _map = instance?.Map; |
| | 8374 | 968 | | _overlay = instance?.Overlay; |
| | 8374 | 969 | | _bakedOrdinal = 0; |
| | 8374 | 970 | | _overlayOrdinal = 0; |
| | 8374 | 971 | | } |
| | | 972 | | |
| | | 973 | | internal bool AdvanceOne( |
| | | 974 | | out TraversalTransitionDefinition definition, |
| | | 975 | | out bool complete) |
| | | 976 | | { |
| | 29515 | 977 | | ReadOnlySpan<TraversalTransitionDefinition> baked = _map == null |
| | 29515 | 978 | | ? ReadOnlySpan<TraversalTransitionDefinition>.Empty |
| | 29515 | 979 | | : _map.TransitionSpan; |
| | 29515 | 980 | | int overlayCount = _overlay?.TransitionCount ?? 0; |
| | 29515 | 981 | | if (_bakedOrdinal >= baked.Length && _overlayOrdinal >= overlayCount) |
| | | 982 | | { |
| | 8374 | 983 | | definition = default; |
| | 8374 | 984 | | complete = true; |
| | 8374 | 985 | | return false; |
| | | 986 | | } |
| | 21141 | 987 | | TraversalTransitionOverlayOperation overlay = _overlayOrdinal < overlayCount |
| | 21141 | 988 | | ? _overlay!.GetTransitionAt(_overlayOrdinal) |
| | 21141 | 989 | | : default; |
| | 21141 | 990 | | int comparison = _bakedOrdinal >= baked.Length |
| | 21141 | 991 | | ? 1 |
| | 21141 | 992 | | : _overlayOrdinal >= overlayCount |
| | 21141 | 993 | | ? -1 |
| | 21141 | 994 | | : string.CompareOrdinal(baked[_bakedOrdinal].Id, overlay.Id); |
| | 21141 | 995 | | if (comparison < 0) |
| | | 996 | | { |
| | 499 | 997 | | definition = baked[_bakedOrdinal++]; |
| | 499 | 998 | | complete = false; |
| | 499 | 999 | | return true; |
| | | 1000 | | } |
| | 20642 | 1001 | | if (comparison == 0) |
| | 12 | 1002 | | _bakedOrdinal++; |
| | 20642 | 1003 | | _overlayOrdinal++; |
| | 20642 | 1004 | | complete = false; |
| | 20642 | 1005 | | if (overlay.Kind == TraversalTransitionOverlayOperationKind.Upsert) |
| | | 1006 | | { |
| | 20533 | 1007 | | definition = overlay.Transition; |
| | 20533 | 1008 | | return true; |
| | | 1009 | | } |
| | 109 | 1010 | | definition = default; |
| | 109 | 1011 | | return false; |
| | | 1012 | | } |
| | | 1013 | | } |
| | | 1014 | | } |