| | | 1 | | //======================================================================= |
| | | 2 | | // NavigationSurfaceComponentBuildWork.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>Builds exact weak surface components with bounded node and edge progress.</summary> |
| | | 13 | | internal sealed class NavigationSurfaceComponentBuildWork |
| | | 14 | | { |
| | | 15 | | private const int NavigationCellAddressBytes = 24; |
| | | 16 | | private const int MaximumUnmeteredRootProbes = 1_024; |
| | | 17 | | |
| | | 18 | | private readonly NavigationWorldGraph _graph; |
| | | 19 | | private readonly NavigationWorldGraph _previousGraph; |
| | | 20 | | private readonly NavigationSurfaceComponentKeySet _affectedKeys; |
| | | 21 | | private readonly NavigationCellAddressSet _seedAddresses; |
| | | 22 | | private readonly NavigationAddressStampSet _visited; |
| | | 23 | | private readonly NavigationAddressStampSet _domain; |
| | | 24 | | private readonly NavigationCellAddress[] _queue; |
| | | 25 | | private readonly NavigationCellAddress[] _rootScratch; |
| | | 26 | | private NavigationPagedSequence<NavigationCellAddress>.Builder? _memberBuilder; |
| | | 27 | | private NavigationSurfaceComponent? _sealingComponent; |
| | | 28 | | private NavigationPagedSequence<NavigationCellAddress>.Enumerator _sealingMembers; |
| | 1647 | 29 | | private NavigationSurfaceComponentIndex _result = NavigationSurfaceComponentIndex.Empty; |
| | | 30 | | private NavigationSurfaceEdgeEnumerator _outgoing; |
| | | 31 | | private NavigationIncomingSurfaceEdgeEnumerator _incoming; |
| | | 32 | | private NavigationAutomaticSeamIndex.EndpointEnumerator _volumeSeams; |
| | | 33 | | private int _queueRead; |
| | | 34 | | private int _queueWrite; |
| | | 35 | | private int _memberCount; |
| | | 36 | | private NavigationCellAddress _representative; |
| | | 37 | | private bool _componentActive; |
| | | 38 | | private bool _nodeActive; |
| | | 39 | | private bool _outgoingComplete; |
| | | 40 | | private int _primaryDirectionOrdinal; |
| | | 41 | | private int _primaryDirectionCount; |
| | 1647 | 42 | | private TraversalMedium _medium = TraversalMedium.Solid; |
| | 1647 | 43 | | private bool _certified = true; |
| | | 44 | | private int _affectedKeyOrdinal; |
| | | 45 | | private NavigationSurfaceComponentKeySet.Enumerator _affectedKeyEnumerator; |
| | | 46 | | private int _seedOrdinal; |
| | | 47 | | private int _rootCount; |
| | | 48 | | private int _rootOrdinal; |
| | | 49 | | private NavigationSurfaceComponent? _removedComponent; |
| | | 50 | | private NavigationPagedSequence<NavigationCellAddress>.Enumerator _removedMembers; |
| | | 51 | | private NavigationCellAddress _pendingRemovedMember; |
| | | 52 | | private bool _hasPendingRemovedMember; |
| | | 53 | | private bool _removedRecord; |
| | | 54 | | private bool _incrementalPreparationComplete; |
| | | 55 | | private int _copiedPersistentNodes; |
| | | 56 | | private long _newComponentPayloadBytes; |
| | | 57 | | private int _newComponentPayloadPages; |
| | | 58 | | private bool _ownsResultRoot; |
| | | 59 | | private bool _sealingRecordAdded; |
| | | 60 | | private bool _hasPendingSealingMember; |
| | | 61 | | private NavigationCellAddress _pendingSealingMember; |
| | | 62 | | |
| | 1647 | 63 | | internal NavigationSurfaceComponentBuildWork( |
| | 1647 | 64 | | NavigationWorldGraph graph, |
| | 1647 | 65 | | NavigationWorldGraph previousGraph, |
| | 1647 | 66 | | NavigationSurfaceComponentKeySet affectedKeys, |
| | 1647 | 67 | | NavigationCellAddressSet seedAddresses, |
| | 1647 | 68 | | int affectedAddressCapacity) |
| | | 69 | | { |
| | 1647 | 70 | | SwiftThrowHelper.ThrowIfNull(graph, nameof(graph)); |
| | 1647 | 71 | | SwiftThrowHelper.ThrowIfNull(previousGraph, nameof(previousGraph)); |
| | 1647 | 72 | | SwiftThrowHelper.ThrowIfNull(affectedKeys, nameof(affectedKeys)); |
| | 1647 | 73 | | SwiftThrowHelper.ThrowIfNull(seedAddresses, nameof(seedAddresses)); |
| | 1647 | 74 | | _graph = graph; |
| | 1647 | 75 | | _previousGraph = previousGraph; |
| | 1647 | 76 | | _affectedKeys = affectedKeys; |
| | 1647 | 77 | | _affectedKeyEnumerator = affectedKeys.GetEnumerator(); |
| | 1647 | 78 | | _seedAddresses = seedAddresses; |
| | 1647 | 79 | | SwiftThrowHelper.ThrowIfArgumentOutOfRange( |
| | 1647 | 80 | | affectedAddressCapacity < seedAddresses.Count, |
| | 1647 | 81 | | affectedAddressCapacity, |
| | 1647 | 82 | | nameof(affectedAddressCapacity)); |
| | 1647 | 83 | | int capacity = affectedAddressCapacity; |
| | 1647 | 84 | | int stampCapacity = Math.Max(1, capacity); |
| | 1647 | 85 | | _visited = new NavigationAddressStampSet(stampCapacity); |
| | 1647 | 86 | | _domain = new NavigationAddressStampSet(stampCapacity); |
| | 1647 | 87 | | _queue = capacity == 0 |
| | 1647 | 88 | | ? Array.Empty<NavigationCellAddress>() |
| | 1647 | 89 | | : new NavigationCellAddress[capacity]; |
| | 1647 | 90 | | _rootScratch = capacity == 0 |
| | 1647 | 91 | | ? Array.Empty<NavigationCellAddress>() |
| | 1647 | 92 | | : new NavigationCellAddress[capacity]; |
| | 1647 | 93 | | _result = previousGraph.SurfaceComponents; |
| | 1647 | 94 | | } |
| | | 95 | | |
| | | 96 | | internal bool IsComplete { get; private set; } |
| | | 97 | | |
| | 1622 | 98 | | internal NavigationSurfaceComponentIndex Result => _result; |
| | | 99 | | |
| | 8681 | 100 | | internal long RetainedBytes => checked( |
| | 8681 | 101 | | 152L |
| | 8681 | 102 | | + GetArrayBytes(_queue.Length, NavigationCellAddressBytes) |
| | 8681 | 103 | | + GetArrayBytes(_rootScratch.Length, NavigationCellAddressBytes) |
| | 8681 | 104 | | + GetStampSetBytes(Math.Max(1, _queue.Length)) |
| | 8681 | 105 | | + GetStampSetBytes(Math.Max(1, _queue.Length)) |
| | 8681 | 106 | | + (_memberBuilder?.RetainedBytes ?? 0L) |
| | 8681 | 107 | | + (_ownsResultRoot ? 64L : 0L) |
| | 8681 | 108 | | + ((long)GetRetainedCopiedPersistentNodes() * 64L) |
| | 8681 | 109 | | + _newComponentPayloadBytes); |
| | | 110 | | |
| | 8910 | 111 | | internal int PersistentPageCount => checked( |
| | 8910 | 112 | | 1 |
| | 8910 | 113 | | + (_memberBuilder?.PersistentPageCount ?? 0) |
| | 8910 | 114 | | + (_ownsResultRoot ? 1 : 0) |
| | 8910 | 115 | | + GetRetainedCopiedPersistentNodes() |
| | 8910 | 116 | | + _newComponentPayloadPages); |
| | | 117 | | |
| | | 118 | | internal bool Advance(MaintenanceWorkMeter meter) |
| | | 119 | | { |
| | 4052 | 120 | | SwiftThrowHelper.ThrowIfNull(meter, nameof(meter)); |
| | 4052 | 121 | | if (IsComplete) |
| | 140 | 122 | | return true; |
| | | 123 | | |
| | 3912 | 124 | | if (!_incrementalPreparationComplete |
| | 3912 | 125 | | && !AdvanceIncrementalPreparation(meter)) |
| | | 126 | | { |
| | 94 | 127 | | return false; |
| | | 128 | | } |
| | | 129 | | |
| | 3818 | 130 | | int unmeteredRootProbes = 0; |
| | 846687 | 131 | | while (!IsComplete) |
| | | 132 | | { |
| | 846666 | 133 | | if (_sealingComponent != null) |
| | | 134 | | { |
| | 2036 | 135 | | if (!AdvanceSealComponent(meter)) |
| | 79 | 136 | | return false; |
| | | 137 | | continue; |
| | | 138 | | } |
| | 844630 | 139 | | if (!_componentActive) |
| | | 140 | | { |
| | 3670 | 141 | | if (!TryBeginNextComponent(ref unmeteredRootProbes)) |
| | 1709 | 142 | | return IsComplete; |
| | | 143 | | } |
| | | 144 | | |
| | 842921 | 145 | | if (!_nodeActive) |
| | | 146 | | { |
| | 56419 | 147 | | if (_queueRead == _queueWrite) |
| | | 148 | | { |
| | 1958 | 149 | | BeginSealComponent(); |
| | 1958 | 150 | | continue; |
| | | 151 | | } |
| | 54461 | 152 | | if (!meter.TryConsumeComponentNodes(1)) |
| | 1490 | 153 | | return false; |
| | 52971 | 154 | | NavigationCellAddress currentAddress = _queue[_queueRead++]; |
| | 52971 | 155 | | _memberBuilder!.Append(currentAddress); |
| | 52971 | 156 | | _memberCount++; |
| | 52971 | 157 | | if (_memberCount == 1 || currentAddress.CompareTo(_representative) < 0) |
| | 1972 | 158 | | _representative = currentAddress; |
| | 52971 | 159 | | bool found = _graph.TryGetNodeRef(currentAddress, out NavigationNodeRef current); |
| | | 160 | | System.Diagnostics.Debug.Assert(found); |
| | 52971 | 161 | | if (_medium != TraversalMedium.Solid) |
| | | 162 | | { |
| | 1192 | 163 | | _primaryDirectionOrdinal = 0; |
| | 1192 | 164 | | _primaryDirectionCount = _graph.GetPrimaryDirectionCount(current); |
| | 1192 | 165 | | _volumeSeams = _graph.AutomaticSeams.GetActiveEndpointEnumerator( |
| | 1192 | 166 | | currentAddress); |
| | 1192 | 167 | | _nodeActive = true; |
| | 1192 | 168 | | continue; |
| | | 169 | | } |
| | 51779 | 170 | | _outgoing = _graph.EnumerateStructuralSurfaceEdges(current); |
| | 51779 | 171 | | _incoming = _graph.EnumerateIncomingStructuralSurfaceEdges(current); |
| | 51779 | 172 | | _outgoingComplete = false; |
| | 51779 | 173 | | _nodeActive = true; |
| | | 174 | | } |
| | | 175 | | |
| | 838281 | 176 | | if (_medium != TraversalMedium.Solid) |
| | | 177 | | { |
| | 1582 | 178 | | if (!AdvanceVolumeNode(meter)) |
| | 390 | 179 | | return false; |
| | | 180 | | continue; |
| | | 181 | | } |
| | | 182 | | |
| | 836699 | 183 | | int remainingEdges = meter.RemainingSurfaceComponentEdges; |
| | 836699 | 184 | | if (!_outgoingComplete) |
| | | 185 | | { |
| | 323503 | 186 | | NavigationSurfaceEdgeAdvanceStatus status = |
| | 323503 | 187 | | _outgoing.AdvanceOne(meter, ref remainingEdges); |
| | 323503 | 188 | | if (status == NavigationSurfaceEdgeAdvanceStatus.Blocked) |
| | 47 | 189 | | return false; |
| | 323456 | 190 | | if (status == NavigationSurfaceEdgeAdvanceStatus.Pending) |
| | | 191 | | continue; |
| | 158574 | 192 | | if (status == NavigationSurfaceEdgeAdvanceStatus.Edge) |
| | | 193 | | { |
| | 106796 | 194 | | NavigationGraphEdge edge = _outgoing.Current; |
| | 106796 | 195 | | if (edge.Kind == NavigationGraphEdgeKind.Explicit) |
| | | 196 | | { |
| | 4366 | 197 | | _certified &= edge.ExplicitConnection.IsLowerBoundCertified; |
| | | 198 | | } |
| | 102430 | 199 | | else if (edge.Kind == NavigationGraphEdgeKind.Seam) |
| | | 200 | | { |
| | 391 | 201 | | _certified &= edge.AutomaticSeam.Portal.FaceKind |
| | 391 | 202 | | == GridForge.Grids.Topology.VoxelContactFaceKind.Vertical; |
| | | 203 | | } |
| | 106796 | 204 | | AddNeighbor(edge.Target); |
| | 106795 | 205 | | continue; |
| | | 206 | | } |
| | 51778 | 207 | | _outgoingComplete = true; |
| | | 208 | | } |
| | | 209 | | |
| | 564974 | 210 | | NavigationSurfaceEdgeAdvanceStatus incomingStatus = |
| | 564974 | 211 | | _incoming.AdvanceOne(meter, ref remainingEdges); |
| | 564974 | 212 | | if (incomingStatus == NavigationSurfaceEdgeAdvanceStatus.Blocked) |
| | 80 | 213 | | return false; |
| | 564894 | 214 | | if (incomingStatus == NavigationSurfaceEdgeAdvanceStatus.Pending) |
| | | 215 | | continue; |
| | 158573 | 216 | | if (incomingStatus == NavigationSurfaceEdgeAdvanceStatus.Edge) |
| | | 217 | | { |
| | 106795 | 218 | | NavigationIncomingSurfaceEdge edge = _incoming.Current; |
| | 106795 | 219 | | if (edge.ForwardEdge.Kind == NavigationGraphEdgeKind.Explicit) |
| | | 220 | | { |
| | 4366 | 221 | | _certified &= edge.ForwardEdge.ExplicitConnection.IsLowerBoundCertified; |
| | | 222 | | } |
| | 102429 | 223 | | else if (edge.ForwardEdge.Kind == NavigationGraphEdgeKind.Seam) |
| | | 224 | | { |
| | 391 | 225 | | _certified &= edge.ForwardEdge.AutomaticSeam.Portal.FaceKind |
| | 391 | 226 | | == GridForge.Grids.Topology.VoxelContactFaceKind.Vertical; |
| | | 227 | | } |
| | 106795 | 228 | | AddNeighbor(edge.Predecessor); |
| | 106795 | 229 | | continue; |
| | | 230 | | } |
| | 51778 | 231 | | _outgoing = default; |
| | 51778 | 232 | | _incoming = default; |
| | 51778 | 233 | | _nodeActive = false; |
| | | 234 | | } |
| | 21 | 235 | | return true; |
| | | 236 | | } |
| | | 237 | | |
| | | 238 | | private bool TryBeginNextComponent(ref int unmeteredRootProbes) |
| | | 239 | | { |
| | 3243 | 240 | | while (true) |
| | | 241 | | { |
| | 164011 | 242 | | while (_rootOrdinal < _rootCount) |
| | | 243 | | { |
| | 159147 | 244 | | if (unmeteredRootProbes++ == MaximumUnmeteredRootProbes) |
| | 88 | 245 | | return false; |
| | 159059 | 246 | | NavigationCellAddress address = _rootScratch[_rootOrdinal++]; |
| | 159059 | 247 | | bool exists = _graph.TryGetStructuralMediumStateRef( |
| | 159059 | 248 | | address, |
| | 159059 | 249 | | _medium, |
| | 159059 | 250 | | out _); |
| | 159059 | 251 | | if (!exists |
| | 159059 | 252 | | || _result.TryGet(address, _medium, out _) |
| | 159059 | 253 | | || !_visited.Add(address)) |
| | | 254 | | { |
| | | 255 | | continue; |
| | | 256 | | } |
| | 1961 | 257 | | _queueRead = 0; |
| | 1961 | 258 | | _queueWrite = 1; |
| | 1961 | 259 | | _memberCount = 0; |
| | 1961 | 260 | | _memberBuilder = new NavigationPagedSequence<NavigationCellAddress>.Builder( |
| | 1961 | 261 | | NavigationCellAddressBytes); |
| | 1961 | 262 | | _certified = true; |
| | 1961 | 263 | | _queue[0] = address; |
| | 1961 | 264 | | _componentActive = true; |
| | 1961 | 265 | | return true; |
| | | 266 | | } |
| | 4864 | 267 | | if (_medium == TraversalMedium.Liquid) |
| | | 268 | | { |
| | 1621 | 269 | | IsComplete = true; |
| | 1621 | 270 | | return false; |
| | | 271 | | } |
| | 3243 | 272 | | _medium++; |
| | 3243 | 273 | | _visited.Reset(); |
| | 3243 | 274 | | _rootOrdinal = 0; |
| | 3243 | 275 | | unmeteredRootProbes = 0; |
| | | 276 | | } |
| | | 277 | | } |
| | | 278 | | |
| | | 279 | | private void AddNeighbor(NavigationNodeRef neighbor) |
| | | 280 | | { |
| | 213591 | 281 | | bool found = _graph.TryGetNodeAddress(neighbor, out NavigationCellAddress address); |
| | | 282 | | System.Diagnostics.Debug.Assert(found); |
| | 213591 | 283 | | if (!_graph.TryGetStructuralMediumStateRef( |
| | 213591 | 284 | | address, |
| | 213591 | 285 | | TraversalMedium.Solid, |
| | 213591 | 286 | | out _) |
| | 213591 | 287 | | || !_visited.Add(address)) |
| | | 288 | | { |
| | 163421 | 289 | | return; |
| | | 290 | | } |
| | 50170 | 291 | | SwiftThrowHelper.ThrowIfTrue( |
| | 50170 | 292 | | !_domain.Contains(address), |
| | 50170 | 293 | | message: "Surface-component closure omitted a structurally adjacent prior component."); |
| | 50169 | 294 | | _queue[_queueWrite++] = address; |
| | 50169 | 295 | | } |
| | | 296 | | |
| | | 297 | | private bool AdvanceVolumeNode(MaintenanceWorkMeter meter) |
| | | 298 | | { |
| | 9122 | 299 | | while (_primaryDirectionOrdinal < _primaryDirectionCount) |
| | | 300 | | { |
| | 7866 | 301 | | if (!meter.TryConsumeSurfaceComponentEdges(1)) |
| | 325 | 302 | | return false; |
| | 7541 | 303 | | int ordinal = _primaryDirectionOrdinal++; |
| | 7541 | 304 | | NavigationCellAddress current = _queue[_queueRead - 1]; |
| | 7541 | 305 | | bool found = _graph.TryGetStructuralMediumStateRef( |
| | 7541 | 306 | | current, |
| | 7541 | 307 | | _medium, |
| | 7541 | 308 | | out NavigationMediumStateRef source); |
| | | 309 | | System.Diagnostics.Debug.Assert(found); |
| | 7541 | 310 | | if (_graph.TryGetStructuralPrimaryMediumNeighbor( |
| | 7541 | 311 | | source, |
| | 7541 | 312 | | ordinal, |
| | 7541 | 313 | | out NavigationMediumStateRef neighbor)) |
| | | 314 | | { |
| | 2819 | 315 | | found = _graph.TryGetNodeAddress( |
| | 2819 | 316 | | neighbor.Node, |
| | 2819 | 317 | | out NavigationCellAddress address); |
| | | 318 | | System.Diagnostics.Debug.Assert(found); |
| | 2819 | 319 | | AddVolumeNeighbor(address); |
| | | 320 | | } |
| | | 321 | | } |
| | | 322 | | |
| | 1321 | 323 | | while (meter.RemainingSurfaceComponentEdges > 0) |
| | | 324 | | { |
| | 1256 | 325 | | if (!_volumeSeams.MoveNext()) |
| | | 326 | | { |
| | 1191 | 327 | | _volumeSeams = default; |
| | 1191 | 328 | | _nodeActive = false; |
| | 1191 | 329 | | return true; |
| | | 330 | | } |
| | 65 | 331 | | meter.TryConsumeSurfaceComponentEdges(1); |
| | 65 | 332 | | NavigationCellAddress destination = _volumeSeams.Current.Destination; |
| | 65 | 333 | | if (_graph.TryGetStructuralMediumStateRef(destination, _medium, out _)) |
| | 52 | 334 | | AddVolumeNeighbor(destination); |
| | | 335 | | } |
| | 65 | 336 | | return false; |
| | | 337 | | } |
| | | 338 | | |
| | | 339 | | private void AddVolumeNeighbor(NavigationCellAddress address) |
| | | 340 | | { |
| | 2871 | 341 | | if (!_visited.Add(address)) |
| | 2028 | 342 | | return; |
| | 843 | 343 | | SwiftThrowHelper.ThrowIfTrue( |
| | 843 | 344 | | !_domain.Contains(address), |
| | 843 | 345 | | message: "Medium-component closure omitted a positive-face neighbor."); |
| | 842 | 346 | | _queue[_queueWrite++] = address; |
| | 842 | 347 | | } |
| | | 348 | | |
| | | 349 | | private bool AdvanceIncrementalPreparation(MaintenanceWorkMeter meter) |
| | | 350 | | { |
| | 1942 | 351 | | while (_affectedKeyOrdinal < _affectedKeys.Count || _removedComponent != null) |
| | | 352 | | { |
| | 221 | 353 | | if (_removedComponent == null) |
| | | 354 | | { |
| | 203 | 355 | | bool hasNext = _affectedKeyEnumerator.MoveNext(); |
| | | 356 | | System.Diagnostics.Debug.Assert( |
| | | 357 | | hasNext, |
| | | 358 | | "The immutable affected-key count matches its enumerator."); |
| | 203 | 359 | | _affectedKeyOrdinal++; |
| | 203 | 360 | | NavigationSurfaceComponentKey key = _affectedKeyEnumerator.Current; |
| | 203 | 361 | | _previousGraph.SurfaceComponents.TryGet(key, out _removedComponent); |
| | | 362 | | System.Diagnostics.Debug.Assert( |
| | | 363 | | _removedComponent != null, |
| | | 364 | | "Affected keys originate from the previous component root."); |
| | 203 | 365 | | _removedMembers = _removedComponent.Members.GetEnumerator(); |
| | 203 | 366 | | _removedRecord = false; |
| | | 367 | | } |
| | 221 | 368 | | if (!_removedRecord) |
| | | 369 | | { |
| | 209 | 370 | | if (!meter.TryConsumeDependencyEntries(1)) |
| | 7 | 371 | | return false; |
| | 202 | 372 | | _result = _result.RemoveComponentRecord( |
| | 202 | 373 | | _removedComponent, |
| | 202 | 374 | | out int copiedPersistentNodes); |
| | 202 | 375 | | RecordPersistentCopies(copiedPersistentNodes); |
| | 202 | 376 | | _removedRecord = true; |
| | | 377 | | } |
| | 17012 | 378 | | while (true) |
| | | 379 | | { |
| | 17226 | 380 | | if (!_hasPendingRemovedMember) |
| | | 381 | | { |
| | 17214 | 382 | | if (!_removedMembers.MoveNext()) |
| | | 383 | | break; |
| | 17012 | 384 | | _pendingRemovedMember = _removedMembers.Current; |
| | 17012 | 385 | | _hasPendingRemovedMember = true; |
| | | 386 | | } |
| | 17024 | 387 | | if (!meter.TryConsumeDependencyEntries(1)) |
| | 12 | 388 | | return false; |
| | 17012 | 389 | | _result = _result.RemoveMembership( |
| | 17012 | 390 | | _pendingRemovedMember, |
| | 17012 | 391 | | _removedComponent.Key, |
| | 17012 | 392 | | out int copiedPersistentNodes); |
| | 17012 | 393 | | RecordPersistentCopies(copiedPersistentNodes); |
| | 17012 | 394 | | AddDomainRoot(_pendingRemovedMember); |
| | 17012 | 395 | | _hasPendingRemovedMember = false; |
| | | 396 | | } |
| | 202 | 397 | | _removedComponent = null; |
| | | 398 | | } |
| | 42088 | 399 | | while (_seedOrdinal < _seedAddresses.Count) |
| | | 400 | | { |
| | 40442 | 401 | | if (!meter.TryConsumeDependencyEntries(1)) |
| | 75 | 402 | | return false; |
| | 40367 | 403 | | AddDomainRoot(_seedAddresses.GetAt(_seedOrdinal++)); |
| | | 404 | | } |
| | 1646 | 405 | | _incrementalPreparationComplete = true; |
| | 1646 | 406 | | if (_rootCount == 0) |
| | 21 | 407 | | IsComplete = true; |
| | 1646 | 408 | | return true; |
| | | 409 | | } |
| | | 410 | | |
| | | 411 | | private void AddDomainRoot(NavigationCellAddress address) |
| | | 412 | | { |
| | 57379 | 413 | | if (!_domain.Add(address)) |
| | 4293 | 414 | | return; |
| | 53086 | 415 | | _rootScratch[_rootCount++] = address; |
| | 53086 | 416 | | } |
| | | 417 | | |
| | | 418 | | private void BeginSealComponent() |
| | | 419 | | { |
| | 1958 | 420 | | NavigationPagedSequence<NavigationCellAddress> sequence = _memberBuilder!.Seal(); |
| | 1958 | 421 | | _memberBuilder = null; |
| | 1958 | 422 | | var key = new NavigationSurfaceComponentKey(_representative, _medium); |
| | 1958 | 423 | | _sealingComponent = new NavigationSurfaceComponent( |
| | 1958 | 424 | | key, |
| | 1958 | 425 | | _graph.GraphVersion, |
| | 1958 | 426 | | sequence, |
| | 1958 | 427 | | _certified); |
| | 1958 | 428 | | _sealingMembers = sequence.GetEnumerator(); |
| | 1958 | 429 | | _sealingRecordAdded = false; |
| | 1958 | 430 | | _hasPendingSealingMember = false; |
| | 1958 | 431 | | _newComponentPayloadBytes = checked( |
| | 1958 | 432 | | _newComponentPayloadBytes + _sealingComponent.RetainedBytes); |
| | 1958 | 433 | | _newComponentPayloadPages = checked( |
| | 1958 | 434 | | _newComponentPayloadPages + _sealingComponent.PersistentPageCount); |
| | 1958 | 435 | | } |
| | | 436 | | |
| | | 437 | | private bool AdvanceSealComponent(MaintenanceWorkMeter meter) |
| | | 438 | | { |
| | 2036 | 439 | | if (!_sealingRecordAdded) |
| | | 440 | | { |
| | 1973 | 441 | | if (!meter.TryConsumeDependencyEntries(1)) |
| | 15 | 442 | | return false; |
| | 1958 | 443 | | _result = _result.AddComponentRecord( |
| | 1958 | 444 | | _sealingComponent!, |
| | 1958 | 445 | | out int copiedPersistentNodes); |
| | 1958 | 446 | | RecordPersistentCopies(copiedPersistentNodes); |
| | 1958 | 447 | | _sealingRecordAdded = true; |
| | | 448 | | } |
| | 52967 | 449 | | while (true) |
| | | 450 | | { |
| | 54988 | 451 | | if (!_hasPendingSealingMember) |
| | | 452 | | { |
| | 54925 | 453 | | if (!_sealingMembers.MoveNext()) |
| | | 454 | | break; |
| | 52968 | 455 | | _pendingSealingMember = _sealingMembers.Current; |
| | 52968 | 456 | | _hasPendingSealingMember = true; |
| | | 457 | | } |
| | 53031 | 458 | | if (!meter.TryConsumeDependencyEntries(1)) |
| | 64 | 459 | | return false; |
| | 52967 | 460 | | _result = _result.AddMembership( |
| | 52967 | 461 | | _pendingSealingMember, |
| | 52967 | 462 | | _sealingComponent!.Key, |
| | 52967 | 463 | | out int copiedPersistentNodes); |
| | 52967 | 464 | | RecordPersistentCopies(copiedPersistentNodes); |
| | 52967 | 465 | | _hasPendingSealingMember = false; |
| | | 466 | | } |
| | 1957 | 467 | | _sealingComponent = null; |
| | 1957 | 468 | | _componentActive = false; |
| | 1957 | 469 | | _nodeActive = false; |
| | 1957 | 470 | | _queueRead = 0; |
| | 1957 | 471 | | _queueWrite = 0; |
| | 1957 | 472 | | _memberCount = 0; |
| | 1957 | 473 | | return true; |
| | | 474 | | } |
| | | 475 | | |
| | 52086 | 476 | | private static long GetArrayBytes(int count, int elementBytes) => count == 0 |
| | 52086 | 477 | | ? 0L |
| | 52086 | 478 | | : Align8(checked(24L + ((long)count * elementBytes))); |
| | | 479 | | |
| | | 480 | | private static long GetStampSetBytes(int capacity) |
| | | 481 | | { |
| | 17362 | 482 | | int tableSize = 1; |
| | 17362 | 483 | | int required = checked(capacity * 2); |
| | 128996 | 484 | | while (tableSize < required) |
| | 111634 | 485 | | tableSize = checked(tableSize * 2); |
| | 17362 | 486 | | return checked( |
| | 17362 | 487 | | 48L |
| | 17362 | 488 | | + GetArrayBytes(tableSize, NavigationCellAddressBytes) |
| | 17362 | 489 | | + GetArrayBytes(tableSize, 8)); |
| | | 490 | | } |
| | | 491 | | |
| | 52042 | 492 | | private static long Align8(long value) => checked((value + 7L) & ~7L); |
| | | 493 | | |
| | | 494 | | private void RecordPersistentCopies(int count) |
| | | 495 | | { |
| | 72139 | 496 | | _ownsResultRoot = true; |
| | 72139 | 497 | | if (count == 0) |
| | 267 | 498 | | return; |
| | 71872 | 499 | | _copiedPersistentNodes = checked(_copiedPersistentNodes + count); |
| | 71872 | 500 | | } |
| | | 501 | | |
| | | 502 | | private int GetRetainedCopiedPersistentNodes() => |
| | 17591 | 503 | | Math.Min(_copiedPersistentNodes, _result.PersistentMapNodeCount); |
| | | 504 | | } |