| | | 1 | | //======================================================================= |
| | | 2 | | // NavigationWorldGraph.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 System.Threading; |
| | | 10 | | using GridForge.Configuration; |
| | | 11 | | using GridForge.Grids; |
| | | 12 | | using GridForge.Grids.Topology; |
| | | 13 | | using GridForge.Spatial; |
| | | 14 | | |
| | | 15 | | namespace Trailblazer.Pathing; |
| | | 16 | | |
| | | 17 | | /// <summary>Stores one immutable context-owned navigation graph root.</summary> |
| | | 18 | | internal sealed partial class NavigationWorldGraph |
| | | 19 | | { |
| | | 20 | | internal const long BaseRetainedBytes = 128L; |
| | | 21 | | |
| | | 22 | | private readonly NavigationInstanceDirectory _instances; |
| | | 23 | | private readonly PersistentGridConfigurationMap<string> _mapIndex; |
| | | 24 | | private readonly NavigationSurfaceComponentKeySet _closedStructuralComponents; |
| | | 25 | | private readonly NavigationSurfaceComponentKeySet _additionalClosedStructuralComponents; |
| | | 26 | | private readonly bool _allStructuralComponentsClosed; |
| | | 27 | | private readonly NavigationExplicitConnectionIndex _explicitConnections; |
| | | 28 | | private readonly NavigationAutomaticSeamIndex _automaticSeams; |
| | | 29 | | private readonly NavigationTransitionPageRoot _transitionPages; |
| | | 30 | | private int _leaseCount; |
| | | 31 | | |
| | 644 | 32 | | internal NavigationWorldGraph( |
| | 644 | 33 | | long graphVersion, |
| | 644 | 34 | | NavigationMapInstance[] instances, |
| | 644 | 35 | | NavigationAreaCatalog? areaCatalog = null, |
| | 644 | 36 | | NavigationExplicitConnectionIndex? explicitConnections = null, |
| | 644 | 37 | | NavigationSurfaceComponentIndex? surfaceComponents = null) |
| | | 38 | | { |
| | 644 | 39 | | GraphVersion = graphVersion; |
| | 644 | 40 | | _instances = NavigationInstanceDirectory.Create(instances); |
| | 644 | 41 | | AreaCatalog = areaCatalog ?? NavigationAreaCatalog.Empty; |
| | 644 | 42 | | _mapIndex = BuildMapIndex(instances); |
| | 644 | 43 | | _explicitConnections = explicitConnections ?? NavigationExplicitConnectionIndex.Empty; |
| | 644 | 44 | | _automaticSeams = NavigationAutomaticSeamIndex.Empty; |
| | 644 | 45 | | SurfaceComponents = surfaceComponents ?? NavigationSurfaceComponentIndex.Empty; |
| | 644 | 46 | | _transitionPages = NavigationTransitionPageRoot.Empty; |
| | 644 | 47 | | TransitionRules = NavigationTransitionRuleTable.Empty; |
| | 644 | 48 | | _closedStructuralComponents = NavigationSurfaceComponentKeySet.Empty; |
| | 644 | 49 | | _additionalClosedStructuralComponents = NavigationSurfaceComponentKeySet.Empty; |
| | 644 | 50 | | _allStructuralComponentsClosed = false; |
| | 644 | 51 | | long bytes = checked( |
| | 644 | 52 | | BaseRetainedBytes |
| | 644 | 53 | | + _instances.RetainedBytes |
| | 644 | 54 | | + _mapIndex.RetainedBytes |
| | 644 | 55 | | + _closedStructuralComponents.RetainedBytes |
| | 644 | 56 | | + SurfaceComponents.RetainedBytes |
| | 644 | 57 | | + _explicitConnections.RetainedBytes |
| | 644 | 58 | | + _automaticSeams.RetainedBytes |
| | 644 | 59 | | + _transitionPages.RetainedBytes |
| | 644 | 60 | | + TransitionRules.RetainedBytes |
| | 644 | 61 | | + AreaCatalog.RetainedBytes); |
| | 644 | 62 | | PersistentPageCount = _instances.PersistentPageCount |
| | 644 | 63 | | + 1 + _mapIndex.Count |
| | 644 | 64 | | + _closedStructuralComponents.PersistentPageCount |
| | 644 | 65 | | + SurfaceComponents.PersistentPageCount |
| | 644 | 66 | | + _explicitConnections.PersistentPageCount |
| | 644 | 67 | | + _automaticSeams.PersistentPageCount |
| | 644 | 68 | | + _transitionPages.PersistentPageCount |
| | 644 | 69 | | + TransitionRules.PersistentPageCount |
| | 644 | 70 | | + AreaCatalog.PersistentPageCount; |
| | 2594 | 71 | | for (int i = 0; i < instances.Length; i++) |
| | | 72 | | { |
| | 653 | 73 | | bytes = checked(bytes + instances[i].RetainedBytes); |
| | 653 | 74 | | PersistentPageCount += instances[i].PersistentPageCount; |
| | | 75 | | } |
| | 644 | 76 | | RetainedBytes = bytes; |
| | 644 | 77 | | } |
| | | 78 | | |
| | 8295 | 79 | | private NavigationWorldGraph( |
| | 8295 | 80 | | long graphVersion, |
| | 8295 | 81 | | NavigationInstanceDirectory instances, |
| | 8295 | 82 | | NavigationAreaCatalog areaCatalog, |
| | 8295 | 83 | | PersistentGridConfigurationMap<string> mapIndex, |
| | 8295 | 84 | | NavigationSurfaceComponentIndex surfaceComponents, |
| | 8295 | 85 | | NavigationExplicitConnectionIndex explicitConnections, |
| | 8295 | 86 | | NavigationAutomaticSeamIndex automaticSeams, |
| | 8295 | 87 | | NavigationTransitionPageRoot transitionPages, |
| | 8295 | 88 | | NavigationTransitionRuleTable transitionRules, |
| | 8295 | 89 | | NavigationSurfaceComponentKeySet closedStructuralComponents, |
| | 8295 | 90 | | NavigationSurfaceComponentKeySet additionalClosedStructuralComponents, |
| | 8295 | 91 | | bool allStructuralComponentsClosed, |
| | 8295 | 92 | | long retainedBytes, |
| | 8295 | 93 | | int persistentPageCount) |
| | | 94 | | { |
| | 8295 | 95 | | GraphVersion = graphVersion; |
| | 8295 | 96 | | _instances = instances; |
| | 8295 | 97 | | AreaCatalog = areaCatalog; |
| | 8295 | 98 | | _mapIndex = mapIndex; |
| | 8295 | 99 | | SurfaceComponents = surfaceComponents; |
| | 8295 | 100 | | _explicitConnections = explicitConnections; |
| | 8295 | 101 | | _automaticSeams = automaticSeams; |
| | 8295 | 102 | | _transitionPages = transitionPages; |
| | 8295 | 103 | | TransitionRules = transitionRules; |
| | 8295 | 104 | | _closedStructuralComponents = closedStructuralComponents; |
| | 8295 | 105 | | _additionalClosedStructuralComponents = additionalClosedStructuralComponents; |
| | 8295 | 106 | | _allStructuralComponentsClosed = allStructuralComponentsClosed; |
| | 8295 | 107 | | RetainedBytes = retainedBytes; |
| | 8295 | 108 | | PersistentPageCount = persistentPageCount; |
| | 8295 | 109 | | } |
| | | 110 | | |
| | | 111 | | internal static NavigationWorldGraph Empty { get; } = new(0, Array.Empty<NavigationMapInstance>()); |
| | | 112 | | |
| | | 113 | | internal static NavigationWorldGraph CreateEmpty(long graphVersion) => |
| | 39 | 114 | | new(graphVersion, Array.Empty<NavigationMapInstance>()); |
| | | 115 | | |
| | | 116 | | internal long GraphVersion { get; } |
| | | 117 | | |
| | | 118 | | internal NavigationAreaCatalog AreaCatalog { get; } |
| | | 119 | | |
| | | 120 | | internal NavigationSurfaceComponentIndex SurfaceComponents { get; } |
| | | 121 | | |
| | 762762 | 122 | | internal NavigationExplicitConnectionIndex ExplicitConnections => _explicitConnections; |
| | | 123 | | |
| | 605201 | 124 | | internal NavigationAutomaticSeamIndex AutomaticSeams => _automaticSeams; |
| | | 125 | | |
| | | 126 | | internal NavigationTransitionRuleTable TransitionRules { get; } |
| | | 127 | | |
| | 4133 | 128 | | internal NavigationTransitionPageRoot TransitionPages => _transitionPages; |
| | | 129 | | |
| | | 130 | | internal bool TryGetMapId(GridConfigurationKey key, out string mapId) => |
| | 98776 | 131 | | _mapIndex.TryGetValue(key, out mapId!); |
| | | 132 | | |
| | | 133 | | internal bool TryGetSeamPrism( |
| | | 134 | | NavigationCellAddress address, |
| | | 135 | | out GridCellPrism prism) |
| | | 136 | | { |
| | 294350 | 137 | | if (_instances.TryGet(address.MapId, out NavigationMapInstance instance) |
| | 294350 | 138 | | && instance.Map.GridBinding.TryGetCellPrism(address.Index, out prism)) |
| | | 139 | | { |
| | 294349 | 140 | | return true; |
| | | 141 | | } |
| | 1 | 142 | | prism = default; |
| | 1 | 143 | | return false; |
| | | 144 | | } |
| | | 145 | | |
| | | 146 | | internal bool HasEffectiveCell(NavigationCellAddress address) => |
| | 2540247 | 147 | | _instances.TryGet(address.MapId, out NavigationMapInstance instance) |
| | 2540247 | 148 | | && instance.TryGetSlot(address.Index, out int slot) |
| | 2540247 | 149 | | && instance.TryGetEffectiveCell(slot, out _); |
| | | 150 | | |
| | | 151 | | internal bool TryGetSemanticState( |
| | | 152 | | NavigationCellAddress address, |
| | | 153 | | out NavigationCellSemanticSource source, |
| | | 154 | | out bool hasCell, |
| | | 155 | | out NavigationCell cell) |
| | | 156 | | { |
| | 158075 | 157 | | if (_instances.TryGet(address.MapId, out NavigationMapInstance instance)) |
| | | 158 | | { |
| | 113968 | 159 | | return instance.TryGetSemanticState( |
| | 113968 | 160 | | address.Index, |
| | 113968 | 161 | | out source, |
| | 113968 | 162 | | out hasCell, |
| | 113968 | 163 | | out cell); |
| | | 164 | | } |
| | 44107 | 165 | | source = default; |
| | 44107 | 166 | | hasCell = false; |
| | 44107 | 167 | | cell = default; |
| | 44107 | 168 | | return false; |
| | | 169 | | } |
| | | 170 | | |
| | | 171 | | internal NavigationTransitionPage.Enumerator EnumerateOutgoingTransitions( |
| | | 172 | | NavigationMediumStateRef source) |
| | | 173 | | { |
| | | 174 | | System.Diagnostics.Debug.Assert( |
| | | 175 | | TryGetNodeLocation(source.Node, out _, out _), |
| | | 176 | | "transition enumeration receives a state owned by this immutable graph"); |
| | 11 | 177 | | NavigationMapInstance instance = _instances.Get(source.Node.MapOrdinal); |
| | 11 | 178 | | _transitionPages.TryGet( |
| | 11 | 179 | | new NavigationTransitionPageAddress( |
| | 11 | 180 | | instance.MapId, |
| | 11 | 181 | | source.Node.CellSlot / NavigationSemanticPage.SlotCount), |
| | 11 | 182 | | out NavigationTransitionPage page); |
| | 11 | 183 | | return page?.GetOutgoingEnumerator(this, source) ?? default; |
| | | 184 | | } |
| | | 185 | | |
| | | 186 | | internal NavigationTransitionPage.Enumerator EnumerateOutgoingTransitionCandidates( |
| | | 187 | | NavigationMediumStateRef source) |
| | | 188 | | { |
| | | 189 | | System.Diagnostics.Debug.Assert( |
| | | 190 | | TryGetNodeLocation(source.Node, out _, out _), |
| | | 191 | | "transition enumeration receives a state owned by this immutable graph"); |
| | 231 | 192 | | NavigationMapInstance instance = _instances.Get(source.Node.MapOrdinal); |
| | 231 | 193 | | _transitionPages.TryGet( |
| | 231 | 194 | | new NavigationTransitionPageAddress( |
| | 231 | 195 | | instance.MapId, |
| | 231 | 196 | | source.Node.CellSlot / NavigationSemanticPage.SlotCount), |
| | 231 | 197 | | out NavigationTransitionPage page); |
| | 231 | 198 | | return page?.GetOutgoingCandidateEnumerator(this, source) ?? default; |
| | | 199 | | } |
| | | 200 | | |
| | | 201 | | internal NavigationTransitionPage.Enumerator EnumerateIncomingTransitions( |
| | | 202 | | NavigationMediumStateRef destination) |
| | | 203 | | { |
| | | 204 | | System.Diagnostics.Debug.Assert( |
| | | 205 | | TryGetNodeLocation(destination.Node, out _, out _), |
| | | 206 | | "transition enumeration receives a state owned by this immutable graph"); |
| | 6 | 207 | | NavigationMapInstance instance = _instances.Get(destination.Node.MapOrdinal); |
| | 6 | 208 | | _transitionPages.TryGet( |
| | 6 | 209 | | new NavigationTransitionPageAddress( |
| | 6 | 210 | | instance.MapId, |
| | 6 | 211 | | destination.Node.CellSlot / NavigationSemanticPage.SlotCount), |
| | 6 | 212 | | out NavigationTransitionPage page); |
| | 6 | 213 | | return page?.GetIncomingEnumerator(this, destination) ?? default; |
| | | 214 | | } |
| | | 215 | | |
| | | 216 | | internal NavigationTransitionPage.Enumerator EnumerateIncomingTransitionCandidates( |
| | | 217 | | NavigationMediumStateRef destination) |
| | | 218 | | { |
| | | 219 | | System.Diagnostics.Debug.Assert( |
| | | 220 | | TryGetNodeLocation(destination.Node, out _, out _), |
| | | 221 | | "transition enumeration receives a state owned by this immutable graph"); |
| | 45 | 222 | | NavigationMapInstance instance = _instances.Get(destination.Node.MapOrdinal); |
| | 45 | 223 | | _transitionPages.TryGet( |
| | 45 | 224 | | new NavigationTransitionPageAddress( |
| | 45 | 225 | | instance.MapId, |
| | 45 | 226 | | destination.Node.CellSlot / NavigationSemanticPage.SlotCount), |
| | 45 | 227 | | out NavigationTransitionPage page); |
| | 45 | 228 | | return page?.GetIncomingCandidateEnumerator(this, destination) ?? default; |
| | | 229 | | } |
| | | 230 | | |
| | | 231 | | internal NavigationPublishedTransition GetPublishedTransition( |
| | | 232 | | NavigationIncomingTransitionRef incoming) |
| | | 233 | | { |
| | 64 | 234 | | bool pageFound = _transitionPages.TryGet( |
| | 64 | 235 | | incoming.SourcePage, |
| | 64 | 236 | | out NavigationTransitionPage page); |
| | | 237 | | System.Diagnostics.Debug.Assert(pageFound, |
| | | 238 | | "incoming transition references retain their exact immutable source page"); |
| | 64 | 239 | | NavigationPublishedTransition transition = default; |
| | 64 | 240 | | bool transitionFound = page.TryGetOutgoing(incoming.Owner, out transition); |
| | | 241 | | System.Diagnostics.Debug.Assert(transitionFound, |
| | | 242 | | "incoming transition references retain their exact immutable owner"); |
| | 64 | 243 | | return transition; |
| | | 244 | | } |
| | | 245 | | |
| | | 246 | | internal bool IsTransitionActive( |
| | | 247 | | NavigationPublishedTransition transition, |
| | | 248 | | NavigationMediumStateRef state, |
| | | 249 | | bool outgoing) |
| | | 250 | | { |
| | 67 | 251 | | if (!TryGetMediumStateRef( |
| | 67 | 252 | | transition.SourceAddress, |
| | 67 | 253 | | transition.Definition.SourceMedium, |
| | 67 | 254 | | out NavigationMediumStateRef source) |
| | 67 | 255 | | || !TryGetMediumStateRef( |
| | 67 | 256 | | transition.Definition.Destination, |
| | 67 | 257 | | transition.Definition.DestinationMedium, |
| | 67 | 258 | | out NavigationMediumStateRef destination)) |
| | | 259 | | { |
| | 2 | 260 | | return false; |
| | | 261 | | } |
| | 65 | 262 | | return outgoing ? source.Equals(state) : destination.Equals(state); |
| | | 263 | | } |
| | | 264 | | |
| | | 265 | | internal bool IsTransitionEndpoint( |
| | | 266 | | NavigationPublishedTransition transition, |
| | | 267 | | NavigationMediumStateRef state, |
| | | 268 | | bool outgoing) |
| | | 269 | | { |
| | 289 | 270 | | bool addressFound = TryGetNodeAddress(state.Node, out NavigationCellAddress address); |
| | | 271 | | System.Diagnostics.Debug.Assert(addressFound, |
| | | 272 | | "transition endpoint checks receive a state owned by this immutable graph"); |
| | 289 | 273 | | return outgoing |
| | 289 | 274 | | ? state.Medium == transition.Definition.SourceMedium |
| | 289 | 275 | | && address.Equals(transition.SourceAddress) |
| | 289 | 276 | | : state.Medium == transition.Definition.DestinationMedium |
| | 289 | 277 | | && address.Equals(transition.Definition.Destination); |
| | | 278 | | } |
| | | 279 | | |
| | 55687 | 280 | | internal int MapCount => _instances.Count; |
| | | 281 | | |
| | | 282 | | internal bool TryGetCoveredAddressGeneration( |
| | | 283 | | int configurationOrdinal, |
| | | 284 | | out string mapId, |
| | | 285 | | out GridCoveredAddressGeneration generation) |
| | | 286 | | { |
| | 949 | 287 | | if ((uint)configurationOrdinal >= (uint)_mapIndex.Count) |
| | | 288 | | { |
| | 1 | 289 | | mapId = string.Empty; |
| | 1 | 290 | | generation = default; |
| | 1 | 291 | | return false; |
| | | 292 | | } |
| | 948 | 293 | | mapId = _mapIndex.GetValueAt(configurationOrdinal); |
| | 948 | 294 | | return TryGetCoveredAddressGeneration(mapId, out generation); |
| | | 295 | | } |
| | | 296 | | |
| | | 297 | | internal bool TryGetCoveredAddressGeneration( |
| | | 298 | | string mapId, |
| | | 299 | | out GridCoveredAddressGeneration generation) |
| | | 300 | | { |
| | 2195 | 301 | | if (_instances.TryGet(mapId, out NavigationMapInstance instance) |
| | 2195 | 302 | | && instance.IsMaterialized) |
| | | 303 | | { |
| | 2193 | 304 | | NavigationGridGenerationIdentity identity = instance.GridIdentity; |
| | 2193 | 305 | | generation = new GridCoveredAddressGeneration( |
| | 2193 | 306 | | identity.ConfigurationKey, |
| | 2193 | 307 | | identity.GridIndex, |
| | 2193 | 308 | | identity.GridSpawnToken, |
| | 2193 | 309 | | instance.GridLastChangeSequence); |
| | 2193 | 310 | | return true; |
| | | 311 | | } |
| | 2 | 312 | | generation = default; |
| | 2 | 313 | | return false; |
| | | 314 | | } |
| | | 315 | | |
| | | 316 | | internal bool TryGetSurfaceComponent( |
| | | 317 | | NavigationCellAddress address, |
| | | 318 | | TraversalMedium medium, |
| | | 319 | | out NavigationSurfaceComponentKey key, |
| | | 320 | | out long version) |
| | | 321 | | { |
| | 2333287 | 322 | | if (SurfaceComponents.TryGet(address, medium, out NavigationSurfaceComponent component)) |
| | | 323 | | { |
| | 2192959 | 324 | | key = component.Key; |
| | 2192959 | 325 | | version = component.Version; |
| | 2192959 | 326 | | return true; |
| | | 327 | | } |
| | 140328 | 328 | | key = default; |
| | 140328 | 329 | | version = 0; |
| | 140328 | 330 | | return false; |
| | | 331 | | } |
| | | 332 | | |
| | | 333 | | internal bool AreInSameSurfaceComponent( |
| | | 334 | | NavigationCellAddress left, |
| | | 335 | | TraversalMedium leftMedium, |
| | | 336 | | NavigationCellAddress right, |
| | | 337 | | TraversalMedium rightMedium) => |
| | 976 | 338 | | SurfaceComponents.TryGet(left, leftMedium, out NavigationSurfaceComponent leftComponent) |
| | 976 | 339 | | && SurfaceComponents.TryGet(right, rightMedium, out NavigationSurfaceComponent rightComponent) |
| | 976 | 340 | | && leftComponent.Key.Equals(rightComponent.Key); |
| | | 341 | | |
| | | 342 | | internal long RetainedBytes { get; } |
| | | 343 | | |
| | | 344 | | internal int PersistentPageCount { get; } |
| | | 345 | | |
| | | 346 | | internal static long EmptyMapIndexRetainedBytes => |
| | 1 | 347 | | PersistentGridConfigurationMap<string>.Empty.RetainedBytes; |
| | | 348 | | |
| | 1 | 349 | | internal static int EmptyMapIndexPersistentPageCount => 1; |
| | | 350 | | |
| | | 351 | | internal static long EmptyClosedStructuralComponentsRetainedBytes => |
| | 1 | 352 | | NavigationSurfaceComponentKeySet.Empty.RetainedBytes; |
| | | 353 | | |
| | | 354 | | internal static int EmptyClosedStructuralComponentsPersistentPageCount => |
| | 1 | 355 | | NavigationSurfaceComponentKeySet.Empty.PersistentPageCount; |
| | | 356 | | |
| | 30842 | 357 | | internal int LeaseCount => Volatile.Read(ref _leaseCount); |
| | | 358 | | |
| | 43071 | 359 | | internal NavigationMapInstance GetInstance(int mapOrdinal) => _instances.Get(mapOrdinal); |
| | | 360 | | |
| | | 361 | | internal bool IsWithinDynamicSlotCapacity(int maximumPerMap, int maximumTotal) |
| | | 362 | | { |
| | 21334 | 363 | | int total = 0; |
| | 139732 | 364 | | for (int i = 0; i < _instances.Count; i++) |
| | | 365 | | { |
| | 48549 | 366 | | int count = _instances.Get(i).DynamicSlotCount; |
| | 48549 | 367 | | if (count > maximumPerMap || count > maximumTotal - total) |
| | 17 | 368 | | return false; |
| | 48532 | 369 | | total += count; |
| | | 370 | | } |
| | 21317 | 371 | | return true; |
| | | 372 | | } |
| | | 373 | | |
| | 9424 | 374 | | internal void Checkout() => Interlocked.Increment(ref _leaseCount); |
| | | 375 | | |
| | 9423 | 376 | | internal void Return() => Interlocked.Decrement(ref _leaseCount); |
| | | 377 | | |
| | | 378 | | internal bool TryGetMap(string mapId, out NavigationMapInstance instance) => |
| | 109352 | 379 | | _instances.TryGet(mapId, out instance); |
| | | 380 | | |
| | | 381 | | internal bool IsSurfaceAddressClosed( |
| | | 382 | | NavigationCellAddress address, |
| | | 383 | | TraversalMedium medium) => |
| | 963961 | 384 | | _allStructuralComponentsClosed |
| | 963961 | 385 | | || (SurfaceComponents.TryGet( |
| | 963961 | 386 | | address, |
| | 963961 | 387 | | medium, |
| | 963961 | 388 | | out NavigationSurfaceComponent component) |
| | 963961 | 389 | | && (_closedStructuralComponents.Contains(component.Key) |
| | 963961 | 390 | | || _additionalClosedStructuralComponents.Contains(component.Key))); |
| | | 391 | | |
| | | 392 | | internal bool IsSurfaceComponentClosed(NavigationSurfaceComponentKey key) => |
| | 35770 | 393 | | _allStructuralComponentsClosed |
| | 35770 | 394 | | || _closedStructuralComponents.Contains(key) |
| | 35770 | 395 | | || _additionalClosedStructuralComponents.Contains(key); |
| | | 396 | | |
| | | 397 | | internal NavigationWorldGraph WithSurfaceComponents( |
| | | 398 | | NavigationSurfaceComponentIndex surfaceComponents) |
| | | 399 | | { |
| | 1701 | 400 | | if (ReferenceEquals(surfaceComponents, SurfaceComponents)) |
| | 130 | 401 | | return this; |
| | 1571 | 402 | | return new NavigationWorldGraph( |
| | 1571 | 403 | | GraphVersion, |
| | 1571 | 404 | | _instances, |
| | 1571 | 405 | | AreaCatalog, |
| | 1571 | 406 | | _mapIndex, |
| | 1571 | 407 | | surfaceComponents, |
| | 1571 | 408 | | _explicitConnections, |
| | 1571 | 409 | | _automaticSeams, |
| | 1571 | 410 | | _transitionPages, |
| | 1571 | 411 | | TransitionRules, |
| | 1571 | 412 | | _closedStructuralComponents, |
| | 1571 | 413 | | _additionalClosedStructuralComponents, |
| | 1571 | 414 | | _allStructuralComponentsClosed, |
| | 1571 | 415 | | checked(RetainedBytes - SurfaceComponents.RetainedBytes |
| | 1571 | 416 | | + surfaceComponents.RetainedBytes), |
| | 1571 | 417 | | PersistentPageCount - SurfaceComponents.PersistentPageCount |
| | 1571 | 418 | | + surfaceComponents.PersistentPageCount); |
| | | 419 | | } |
| | | 420 | | |
| | | 421 | | internal NavigationWorldGraph WithAutomaticSeams(NavigationAutomaticSeamIndex seams) |
| | | 422 | | { |
| | 1201 | 423 | | if (ReferenceEquals(seams, _automaticSeams)) |
| | 1009 | 424 | | return this; |
| | 192 | 425 | | return new NavigationWorldGraph( |
| | 192 | 426 | | GraphVersion, |
| | 192 | 427 | | _instances, |
| | 192 | 428 | | AreaCatalog, |
| | 192 | 429 | | _mapIndex, |
| | 192 | 430 | | SurfaceComponents, |
| | 192 | 431 | | _explicitConnections, |
| | 192 | 432 | | seams, |
| | 192 | 433 | | _transitionPages, |
| | 192 | 434 | | TransitionRules, |
| | 192 | 435 | | _closedStructuralComponents, |
| | 192 | 436 | | _additionalClosedStructuralComponents, |
| | 192 | 437 | | _allStructuralComponentsClosed, |
| | 192 | 438 | | checked(RetainedBytes - _automaticSeams.RetainedBytes + seams.RetainedBytes), |
| | 192 | 439 | | PersistentPageCount - _automaticSeams.PersistentPageCount |
| | 192 | 440 | | + seams.PersistentPageCount); |
| | | 441 | | } |
| | | 442 | | |
| | | 443 | | internal NavigationWorldGraph WithTransitionPublication( |
| | | 444 | | NavigationTransitionPageRoot pages, |
| | | 445 | | NavigationTransitionRuleTable rules) |
| | | 446 | | { |
| | 2998 | 447 | | if (ReferenceEquals(pages, _transitionPages) |
| | 2998 | 448 | | && ReferenceEquals(rules, TransitionRules)) |
| | | 449 | | { |
| | 2862 | 450 | | return this; |
| | | 451 | | } |
| | 136 | 452 | | long priorBytes = checked( |
| | 136 | 453 | | _transitionPages.RetainedBytes |
| | 136 | 454 | | + TransitionRules.RetainedBytes); |
| | 136 | 455 | | long nextBytes = checked( |
| | 136 | 456 | | pages.RetainedBytes |
| | 136 | 457 | | + rules.RetainedBytes); |
| | 136 | 458 | | int priorPages = checked( |
| | 136 | 459 | | _transitionPages.PersistentPageCount |
| | 136 | 460 | | + TransitionRules.PersistentPageCount); |
| | 136 | 461 | | int nextPages = checked( |
| | 136 | 462 | | pages.PersistentPageCount |
| | 136 | 463 | | + rules.PersistentPageCount); |
| | 136 | 464 | | return new NavigationWorldGraph( |
| | 136 | 465 | | GraphVersion, |
| | 136 | 466 | | _instances, |
| | 136 | 467 | | AreaCatalog, |
| | 136 | 468 | | _mapIndex, |
| | 136 | 469 | | SurfaceComponents, |
| | 136 | 470 | | _explicitConnections, |
| | 136 | 471 | | _automaticSeams, |
| | 136 | 472 | | pages, |
| | 136 | 473 | | rules, |
| | 136 | 474 | | _closedStructuralComponents, |
| | 136 | 475 | | _additionalClosedStructuralComponents, |
| | 136 | 476 | | _allStructuralComponentsClosed, |
| | 136 | 477 | | checked(RetainedBytes - priorBytes + nextBytes), |
| | 136 | 478 | | PersistentPageCount - priorPages + nextPages); |
| | | 479 | | } |
| | | 480 | | |
| | | 481 | | internal NavigationWorldGraph WithClosedStructuralComponents( |
| | | 482 | | NavigationSurfaceComponentKeySet closed, |
| | | 483 | | bool closeAllStructuralComponents, |
| | | 484 | | long graphVersion) |
| | | 485 | | { |
| | 3448 | 486 | | if (ReferenceEquals(closed, _closedStructuralComponents) |
| | 3448 | 487 | | && ReferenceEquals( |
| | 3448 | 488 | | _additionalClosedStructuralComponents, |
| | 3448 | 489 | | NavigationSurfaceComponentKeySet.Empty) |
| | 3448 | 490 | | && closeAllStructuralComponents == _allStructuralComponentsClosed |
| | 3448 | 491 | | && graphVersion == GraphVersion) |
| | | 492 | | { |
| | 220 | 493 | | return this; |
| | | 494 | | } |
| | 3228 | 495 | | return new NavigationWorldGraph( |
| | 3228 | 496 | | graphVersion, |
| | 3228 | 497 | | _instances, |
| | 3228 | 498 | | AreaCatalog, |
| | 3228 | 499 | | _mapIndex, |
| | 3228 | 500 | | SurfaceComponents, |
| | 3228 | 501 | | _explicitConnections, |
| | 3228 | 502 | | _automaticSeams, |
| | 3228 | 503 | | _transitionPages, |
| | 3228 | 504 | | TransitionRules, |
| | 3228 | 505 | | closed, |
| | 3228 | 506 | | NavigationSurfaceComponentKeySet.Empty, |
| | 3228 | 507 | | closeAllStructuralComponents, |
| | 3228 | 508 | | checked(RetainedBytes - GetClosedRootRetainedBytes() |
| | 3228 | 509 | | + closed.RetainedBytes), |
| | 3228 | 510 | | PersistentPageCount - GetClosedRootPersistentPages() |
| | 3228 | 511 | | + closed.PersistentPageCount); |
| | | 512 | | } |
| | | 513 | | |
| | | 514 | | internal NavigationWorldGraph WithOwnedStructuralClosure( |
| | | 515 | | NavigationSurfaceComponentKeySet baseline, |
| | | 516 | | NavigationSurfaceComponentKeySet affected, |
| | | 517 | | bool closeAllStructuralComponents, |
| | | 518 | | long graphVersion) |
| | | 519 | | { |
| | 756 | 520 | | if (closeAllStructuralComponents) |
| | | 521 | | { |
| | 409 | 522 | | return WithClosedStructuralComponents( |
| | 409 | 523 | | NavigationSurfaceComponentKeySet.Empty, |
| | 409 | 524 | | true, |
| | 409 | 525 | | graphVersion); |
| | | 526 | | } |
| | 347 | 527 | | NavigationSurfaceComponentKeySet additional = ReferenceEquals(baseline, affected) |
| | 347 | 528 | | ? NavigationSurfaceComponentKeySet.Empty |
| | 347 | 529 | | : affected; |
| | 347 | 530 | | return new NavigationWorldGraph( |
| | 347 | 531 | | graphVersion, |
| | 347 | 532 | | _instances, |
| | 347 | 533 | | AreaCatalog, |
| | 347 | 534 | | _mapIndex, |
| | 347 | 535 | | SurfaceComponents, |
| | 347 | 536 | | _explicitConnections, |
| | 347 | 537 | | _automaticSeams, |
| | 347 | 538 | | _transitionPages, |
| | 347 | 539 | | TransitionRules, |
| | 347 | 540 | | baseline, |
| | 347 | 541 | | additional, |
| | 347 | 542 | | false, |
| | 347 | 543 | | checked(RetainedBytes - GetClosedRootRetainedBytes() |
| | 347 | 544 | | + baseline.RetainedBytes |
| | 347 | 545 | | + GetAdditionalClosedRootRetainedBytes(baseline, additional)), |
| | 347 | 546 | | PersistentPageCount - GetClosedRootPersistentPages() |
| | 347 | 547 | | + baseline.PersistentPageCount |
| | 347 | 548 | | + GetAdditionalClosedRootPersistentPages(baseline, additional)); |
| | | 549 | | } |
| | | 550 | | |
| | | 551 | | internal NavigationWorldGraph WithStructuralClosureFrom( |
| | | 552 | | NavigationWorldGraph owner, |
| | | 553 | | long graphVersion) => |
| | 206 | 554 | | new( |
| | 206 | 555 | | graphVersion, |
| | 206 | 556 | | _instances, |
| | 206 | 557 | | AreaCatalog, |
| | 206 | 558 | | _mapIndex, |
| | 206 | 559 | | SurfaceComponents, |
| | 206 | 560 | | _explicitConnections, |
| | 206 | 561 | | _automaticSeams, |
| | 206 | 562 | | _transitionPages, |
| | 206 | 563 | | TransitionRules, |
| | 206 | 564 | | owner._closedStructuralComponents, |
| | 206 | 565 | | owner._additionalClosedStructuralComponents, |
| | 206 | 566 | | owner._allStructuralComponentsClosed, |
| | 206 | 567 | | checked(RetainedBytes - GetClosedRootRetainedBytes() |
| | 206 | 568 | | + owner._closedStructuralComponents.RetainedBytes |
| | 206 | 569 | | + GetAdditionalClosedRootRetainedBytes( |
| | 206 | 570 | | owner._closedStructuralComponents, |
| | 206 | 571 | | owner._additionalClosedStructuralComponents)), |
| | 206 | 572 | | PersistentPageCount - GetClosedRootPersistentPages() |
| | 206 | 573 | | + owner._closedStructuralComponents.PersistentPageCount |
| | 206 | 574 | | + GetAdditionalClosedRootPersistentPages( |
| | 206 | 575 | | owner._closedStructuralComponents, |
| | 206 | 576 | | owner._additionalClosedStructuralComponents)); |
| | | 577 | | |
| | | 578 | | internal bool HasClosedStructuralScope => |
| | 17669 | 579 | | _allStructuralComponentsClosed |
| | 17669 | 580 | | || _closedStructuralComponents.Count != 0 |
| | 17669 | 581 | | || _additionalClosedStructuralComponents.Count != 0; |
| | | 582 | | |
| | | 583 | | internal NavigationSurfaceComponentKeySet ClosedStructuralComponents => |
| | 516 | 584 | | _closedStructuralComponents; |
| | | 585 | | |
| | | 586 | | internal bool RetainsClosedComponentRoot(NavigationSurfaceComponentKeySet root) => |
| | 24 | 587 | | ReferenceEquals(root, _closedStructuralComponents) |
| | 24 | 588 | | || ReferenceEquals(root, _additionalClosedStructuralComponents); |
| | | 589 | | |
| | 3831 | 590 | | private long GetClosedRootRetainedBytes() => checked( |
| | 3831 | 591 | | _closedStructuralComponents.RetainedBytes |
| | 3831 | 592 | | + GetAdditionalClosedRootRetainedBytes( |
| | 3831 | 593 | | _closedStructuralComponents, |
| | 3831 | 594 | | _additionalClosedStructuralComponents)); |
| | | 595 | | |
| | 3831 | 596 | | private int GetClosedRootPersistentPages() => checked( |
| | 3831 | 597 | | _closedStructuralComponents.PersistentPageCount |
| | 3831 | 598 | | + GetAdditionalClosedRootPersistentPages( |
| | 3831 | 599 | | _closedStructuralComponents, |
| | 3831 | 600 | | _additionalClosedStructuralComponents)); |
| | | 601 | | |
| | | 602 | | private static long GetAdditionalClosedRootRetainedBytes( |
| | | 603 | | NavigationSurfaceComponentKeySet baseline, |
| | | 604 | | NavigationSurfaceComponentKeySet additional) => |
| | 4384 | 605 | | ReferenceEquals(additional, NavigationSurfaceComponentKeySet.Empty) |
| | 4384 | 606 | | || ReferenceEquals(additional, baseline) |
| | 4384 | 607 | | ? 0L |
| | 4384 | 608 | | : additional.RetainedBytes; |
| | | 609 | | |
| | | 610 | | private static int GetAdditionalClosedRootPersistentPages( |
| | | 611 | | NavigationSurfaceComponentKeySet baseline, |
| | | 612 | | NavigationSurfaceComponentKeySet additional) => |
| | 4384 | 613 | | ReferenceEquals(additional, NavigationSurfaceComponentKeySet.Empty) |
| | 4384 | 614 | | || ReferenceEquals(additional, baseline) |
| | 4384 | 615 | | ? 0 |
| | 4384 | 616 | | : additional.PersistentPageCount; |
| | | 617 | | |
| | 886 | 618 | | internal bool AreAllStructuralComponentsClosed => _allStructuralComponentsClosed; |
| | | 619 | | |
| | | 620 | | internal NavigationWorldGraph ReopenStructuralScopes(long graphVersion) |
| | | 621 | | { |
| | 55 | 622 | | if (!_allStructuralComponentsClosed |
| | 55 | 623 | | && _closedStructuralComponents.Count == 0 |
| | 55 | 624 | | && _additionalClosedStructuralComponents.Count == 0) |
| | 5 | 625 | | return this; |
| | 50 | 626 | | NavigationSurfaceComponentKeySet open = NavigationSurfaceComponentKeySet.Empty; |
| | 50 | 627 | | return new NavigationWorldGraph( |
| | 50 | 628 | | graphVersion, |
| | 50 | 629 | | _instances, |
| | 50 | 630 | | AreaCatalog, |
| | 50 | 631 | | _mapIndex, |
| | 50 | 632 | | SurfaceComponents, |
| | 50 | 633 | | _explicitConnections, |
| | 50 | 634 | | _automaticSeams, |
| | 50 | 635 | | _transitionPages, |
| | 50 | 636 | | TransitionRules, |
| | 50 | 637 | | open, |
| | 50 | 638 | | NavigationSurfaceComponentKeySet.Empty, |
| | 50 | 639 | | false, |
| | 50 | 640 | | checked(RetainedBytes - GetClosedRootRetainedBytes() + open.RetainedBytes), |
| | 50 | 641 | | PersistentPageCount - GetClosedRootPersistentPages() |
| | 50 | 642 | | + open.PersistentPageCount); |
| | | 643 | | } |
| | | 644 | | |
| | | 645 | | internal int CaptureMaintenanceSnapshot( |
| | | 646 | | GridWorld world, |
| | | 647 | | NavigationWorldGraph previousGraph, |
| | | 648 | | ReadOnlySpan<GridEventInfo> events, |
| | | 649 | | ReadOnlySpan<NavigationGridChangeScope> resnapshotScopes, |
| | | 650 | | bool resnapshotAll, |
| | | 651 | | ReadOnlySpan<NavigationGridChangeScope> blockedScopes, |
| | | 652 | | bool blockAll, |
| | | 653 | | NavigationOperationFrameChange[] changes, |
| | | 654 | | int changeCount, |
| | | 655 | | MaintenanceWorkMeter maintenanceMeter, |
| | | 656 | | long maximumActiveSnapshotBytes, |
| | | 657 | | int maximumPersistentGraphPages, |
| | | 658 | | NavigationGridBaselineCapture[] baselineCaptures, |
| | | 659 | | ref PersistentStringMap<NavigationBaselineRebuild> baselineRebuilds, |
| | | 660 | | Span<VoxelIndex> baselineAddressScratch, |
| | | 661 | | Span<GridCoveredAddress> baselineCoveredAddressScratch, |
| | | 662 | | Span<NavigationGridChangeScope> deferredScopes, |
| | | 663 | | out int deferredScopeCount, |
| | | 664 | | out bool deferAll, |
| | | 665 | | int[] affectedOrdinals, |
| | | 666 | | int[] affectedStamps, |
| | | 667 | | ref int affectedStamp, |
| | | 668 | | out int affectedCollectionCount) |
| | | 669 | | { |
| | 20256 | 670 | | deferredScopeCount = 0; |
| | 20256 | 671 | | deferAll = false; |
| | 20256 | 672 | | affectedCollectionCount = 0; |
| | 42786 | 673 | | for (int changeIndex = 0; changeIndex < changeCount; changeIndex++) |
| | | 674 | | { |
| | 1137 | 675 | | if (changes[changeIndex].Kind == NavigationOperationFrameChangeKind.MapRemove) |
| | 8 | 676 | | baselineRebuilds = baselineRebuilds.Remove(changes[changeIndex].MapId!, out _); |
| | | 677 | | } |
| | 20256 | 678 | | if (_instances.Count == 0) |
| | 11470 | 679 | | return 0; |
| | | 680 | | |
| | 8786 | 681 | | int affectedCount = CollectAffectedMaps( |
| | 8786 | 682 | | events, |
| | 8786 | 683 | | resnapshotScopes, |
| | 8786 | 684 | | resnapshotAll, |
| | 8786 | 685 | | blockedScopes, |
| | 8786 | 686 | | blockAll, |
| | 8786 | 687 | | changes, |
| | 8786 | 688 | | changeCount, |
| | 8786 | 689 | | affectedOrdinals, |
| | 8786 | 690 | | affectedStamps, |
| | 8786 | 691 | | ref affectedStamp); |
| | 8786 | 692 | | affectedCollectionCount = affectedCount; |
| | 8786 | 693 | | NavigationBaselineRebuild.GetRetainedTotals( |
| | 8786 | 694 | | baselineRebuilds, |
| | 8786 | 695 | | out long rebuildRetainedBytes, |
| | 8786 | 696 | | out int rebuildPersistentPages); |
| | 8786 | 697 | | int remainingBaselineAddresses = maintenanceMeter.RemainingBaselineAddresses; |
| | 20398 | 698 | | for (int affectedIndex = 0; affectedIndex < affectedCount; affectedIndex++) |
| | | 699 | | { |
| | 1413 | 700 | | int mapIndex = affectedOrdinals[affectedIndex]; |
| | 1413 | 701 | | baselineCaptures[mapIndex] = default; |
| | 1413 | 702 | | NavigationMapInstance current = _instances.Get(mapIndex); |
| | 1413 | 703 | | GridConfigurationKey configurationKey = current.Map.GridBinding.Key; |
| | 1413 | 704 | | bool isBlocked = blockAll || ContainsScope(blockedScopes, configurationKey); |
| | 1413 | 705 | | bool requiresRecoveryBaseline = resnapshotAll |
| | 1413 | 706 | | || ContainsScope(resnapshotScopes, configurationKey) |
| | 1413 | 707 | | || EventsRequireBaseline( |
| | 1413 | 708 | | events, |
| | 1413 | 709 | | configurationKey, |
| | 1413 | 710 | | current.Map.DefaultCell.HasValue); |
| | 1413 | 711 | | bool operationChanged = !current.IsMaterialized |
| | 1413 | 712 | | && IsOperationChanged(changes, changeCount, current.MapId); |
| | 1413 | 713 | | bool requiresBaseline = requiresRecoveryBaseline || operationChanged; |
| | 1413 | 714 | | if (isBlocked || !requiresBaseline) |
| | | 715 | | continue; |
| | 1231 | 716 | | NavigationMapInstance? prior = null; |
| | 1231 | 717 | | bool isDelta = !requiresRecoveryBaseline |
| | 1231 | 718 | | && IsCellOverlayOnlyChanged(changes, changeCount, current.MapId) |
| | 1231 | 719 | | && previousGraph.TryGetMap(current.MapId, out prior) |
| | 1231 | 720 | | && prior.IsMaterialized; |
| | 1231 | 721 | | baselineRebuilds.TryGetValue( |
| | 1231 | 722 | | current.MapId, |
| | 1231 | 723 | | out NavigationBaselineRebuild? rebuild); |
| | 1231 | 724 | | if (rebuild != null && !rebuild.Matches(current)) |
| | | 725 | | { |
| | 1 | 726 | | RemoveBaselineRebuild( |
| | 1 | 727 | | ref baselineRebuilds, |
| | 1 | 728 | | current.MapId, |
| | 1 | 729 | | rebuild, |
| | 1 | 730 | | ref rebuildRetainedBytes, |
| | 1 | 731 | | ref rebuildPersistentPages); |
| | 1 | 732 | | rebuild = null; |
| | | 733 | | } |
| | 1231 | 734 | | if (rebuild != null |
| | 1231 | 735 | | && rebuild.TryGetCompletedCapture( |
| | 1231 | 736 | | out NavigationGridBaselineCapture readyCapture)) |
| | | 737 | | { |
| | 39 | 738 | | baselineCaptures[mapIndex] = readyCapture; |
| | 39 | 739 | | continue; |
| | | 740 | | } |
| | 1192 | 741 | | if (rebuild != null) |
| | 63 | 742 | | isDelta = false; |
| | | 743 | | int addressCount; |
| | 1192 | 744 | | bool requiresDefaultDiscovery = !isDelta && current.Map.DefaultCell.HasValue; |
| | 1192 | 745 | | if (isDelta) |
| | | 746 | | { |
| | 36 | 747 | | if (!current.TryCopyNewCanonicalAddresses( |
| | 36 | 748 | | prior!, |
| | 36 | 749 | | baselineAddressScratch, |
| | 36 | 750 | | out addressCount)) |
| | | 751 | | { |
| | 1 | 752 | | isDelta = false; |
| | 1 | 753 | | addressCount = 0; |
| | | 754 | | } |
| | | 755 | | } |
| | 1156 | 756 | | else if (rebuild == null && !requiresDefaultDiscovery) |
| | | 757 | | { |
| | 1046 | 758 | | addressCount = current.AddressCount <= remainingBaselineAddresses |
| | 1046 | 759 | | ? current.CopyCanonicalAddresses(baselineAddressScratch) |
| | 1046 | 760 | | : 0; |
| | | 761 | | } |
| | | 762 | | else |
| | | 763 | | { |
| | 110 | 764 | | addressCount = 0; |
| | | 765 | | } |
| | | 766 | | |
| | 1192 | 767 | | bool requiresChunkedBaseline = !isDelta |
| | 1192 | 768 | | && (requiresDefaultDiscovery |
| | 1192 | 769 | | || rebuild != null |
| | 1192 | 770 | | || current.AddressCount > remainingBaselineAddresses); |
| | 1192 | 771 | | if (!requiresChunkedBaseline && addressCount <= remainingBaselineAddresses) |
| | | 772 | | { |
| | 1062 | 773 | | bool consumed = maintenanceMeter.TryConsumeBaselineAddresses(addressCount); |
| | | 774 | | System.Diagnostics.Debug.Assert(consumed, |
| | | 775 | | "The capture is bounded by the meter's remaining baseline addresses."); |
| | 1062 | 776 | | remainingBaselineAddresses -= addressCount; |
| | | 777 | | System.Diagnostics.Debug.Assert(addressCount > 0 || !isDelta, |
| | | 778 | | "a nonmaterialized cell-only delta owns at least one new canonical address"); |
| | 1062 | 779 | | world.TryCaptureNavigationBaseline( |
| | 1062 | 780 | | configurationKey, |
| | 1062 | 781 | | baselineAddressScratch.Slice(0, addressCount), |
| | 1062 | 782 | | out GridNavigationBaseline? baseline); |
| | 1062 | 783 | | baselineCaptures[mapIndex] = new NavigationGridBaselineCapture( |
| | 1062 | 784 | | addressCount, |
| | 1062 | 785 | | baseline, |
| | 1062 | 786 | | isDelta); |
| | | 787 | | } |
| | | 788 | | else |
| | | 789 | | { |
| | 130 | 790 | | if (!isDelta) |
| | | 791 | | { |
| | 130 | 792 | | if (rebuild == null) |
| | | 793 | | { |
| | 67 | 794 | | var proposed = new NavigationBaselineRebuild(current); |
| | 67 | 795 | | if (TryAddBaselineRebuild( |
| | 67 | 796 | | ref baselineRebuilds, |
| | 67 | 797 | | proposed, |
| | 67 | 798 | | RetainedBytes, |
| | 67 | 799 | | PersistentPageCount, |
| | 67 | 800 | | maximumActiveSnapshotBytes, |
| | 67 | 801 | | maximumPersistentGraphPages, |
| | 67 | 802 | | ref rebuildRetainedBytes, |
| | 67 | 803 | | ref rebuildPersistentPages)) |
| | | 804 | | { |
| | 66 | 805 | | rebuild = proposed; |
| | | 806 | | } |
| | | 807 | | } |
| | 130 | 808 | | if (rebuild != null && !rebuild.RequiresCoveredDiscovery) |
| | | 809 | | { |
| | 70 | 810 | | long beforeBytes = rebuild.RetainedBytes; |
| | 70 | 811 | | int beforePages = rebuild.PersistentPageCount; |
| | 70 | 812 | | long otherBytes = rebuildRetainedBytes - beforeBytes; |
| | 70 | 813 | | int otherPages = rebuildPersistentPages - beforePages; |
| | 70 | 814 | | long maximumRebuildBytes = Math.Max( |
| | 70 | 815 | | 0, |
| | 70 | 816 | | maximumActiveSnapshotBytes - RetainedBytes - otherBytes); |
| | 70 | 817 | | int maximumRebuildPages = Math.Max( |
| | 70 | 818 | | 0, |
| | 70 | 819 | | maximumPersistentGraphPages - PersistentPageCount - otherPages); |
| | 70 | 820 | | int consumed = rebuild.Advance( |
| | 70 | 821 | | world, |
| | 70 | 822 | | current, |
| | 70 | 823 | | remainingBaselineAddresses, |
| | 70 | 824 | | maximumRebuildBytes, |
| | 70 | 825 | | maximumRebuildPages, |
| | 70 | 826 | | baselineAddressScratch, |
| | 70 | 827 | | baselineCoveredAddressScratch, |
| | 70 | 828 | | out NavigationGridBaselineCapture completedCapture, |
| | 70 | 829 | | out bool completed); |
| | 70 | 830 | | rebuildRetainedBytes = checked( |
| | 70 | 831 | | rebuildRetainedBytes - beforeBytes + rebuild.RetainedBytes); |
| | 70 | 832 | | rebuildPersistentPages = checked( |
| | 70 | 833 | | rebuildPersistentPages - beforePages + rebuild.PersistentPageCount); |
| | 70 | 834 | | maintenanceMeter.TryConsumeBaselineAddresses(consumed); |
| | 70 | 835 | | remainingBaselineAddresses -= consumed; |
| | 70 | 836 | | if (completed) |
| | | 837 | | { |
| | 17 | 838 | | baselineCaptures[mapIndex] = completedCapture; |
| | 17 | 839 | | continue; |
| | | 840 | | } |
| | | 841 | | } |
| | | 842 | | } |
| | 113 | 843 | | NavigationGridGenerationIdentity deferredIdentity = current.GridIdentity; |
| | 113 | 844 | | if (!deferredIdentity.IsValid |
| | 113 | 845 | | && previousGraph.TryGetMap(current.MapId, out NavigationMapInstance deferredPrior)) |
| | | 846 | | { |
| | 69 | 847 | | deferredIdentity = deferredPrior.GridIdentity; |
| | | 848 | | } |
| | 113 | 849 | | if (deferredScopeCount < deferredScopes.Length) |
| | | 850 | | { |
| | 112 | 851 | | deferredScopes[deferredScopeCount++] = new NavigationGridChangeScope( |
| | 112 | 852 | | configurationKey, |
| | 112 | 853 | | deferredIdentity.WorldSpawnToken, |
| | 112 | 854 | | deferredIdentity.GridIndex, |
| | 112 | 855 | | deferredIdentity.GridSpawnToken); |
| | | 856 | | } |
| | | 857 | | else |
| | | 858 | | { |
| | 1 | 859 | | deferredScopeCount = 0; |
| | 1 | 860 | | deferAll = true; |
| | | 861 | | } |
| | | 862 | | } |
| | | 863 | | } |
| | 8786 | 864 | | return affectedCount; |
| | | 865 | | } |
| | | 866 | | |
| | | 867 | | private static bool TryAddBaselineRebuild( |
| | | 868 | | ref PersistentStringMap<NavigationBaselineRebuild> rebuilds, |
| | | 869 | | NavigationBaselineRebuild rebuild, |
| | | 870 | | long graphRetainedBytes, |
| | | 871 | | int graphPersistentPages, |
| | | 872 | | long maximumRetainedBytes, |
| | | 873 | | int maximumPersistentPages, |
| | | 874 | | ref long rebuildRetainedBytes, |
| | | 875 | | ref int rebuildPersistentPages) |
| | | 876 | | { |
| | 67 | 877 | | PersistentStringMap<NavigationBaselineRebuild> next = rebuilds.Set( |
| | 67 | 878 | | rebuild.MapId, |
| | 67 | 879 | | rebuild); |
| | 67 | 880 | | long priorRegistryBytes = rebuilds.Count == 0 ? 0 : rebuilds.RetainedBytes; |
| | 67 | 881 | | int priorRegistryPages = rebuilds.Count == 0 |
| | 67 | 882 | | ? 0 |
| | 67 | 883 | | : 1 + rebuilds.PersistentNodeCount; |
| | 67 | 884 | | long nextRetainedBytes = checked( |
| | 67 | 885 | | rebuildRetainedBytes |
| | 67 | 886 | | - priorRegistryBytes |
| | 67 | 887 | | + next.RetainedBytes |
| | 67 | 888 | | + rebuild.RetainedBytes); |
| | 67 | 889 | | int nextPersistentPages = checked( |
| | 67 | 890 | | rebuildPersistentPages |
| | 67 | 891 | | - priorRegistryPages |
| | 67 | 892 | | + 1 |
| | 67 | 893 | | + next.PersistentNodeCount |
| | 67 | 894 | | + rebuild.PersistentPageCount); |
| | 67 | 895 | | if (graphRetainedBytes > maximumRetainedBytes - nextRetainedBytes |
| | 67 | 896 | | || graphPersistentPages > maximumPersistentPages - nextPersistentPages) |
| | | 897 | | { |
| | 1 | 898 | | return false; |
| | | 899 | | } |
| | | 900 | | |
| | 66 | 901 | | rebuilds = next; |
| | 66 | 902 | | rebuildRetainedBytes = nextRetainedBytes; |
| | 66 | 903 | | rebuildPersistentPages = nextPersistentPages; |
| | 66 | 904 | | return true; |
| | | 905 | | } |
| | | 906 | | |
| | | 907 | | private static void RemoveBaselineRebuild( |
| | | 908 | | ref PersistentStringMap<NavigationBaselineRebuild> rebuilds, |
| | | 909 | | string mapId, |
| | | 910 | | NavigationBaselineRebuild rebuild, |
| | | 911 | | ref long rebuildRetainedBytes, |
| | | 912 | | ref int rebuildPersistentPages) |
| | | 913 | | { |
| | 1 | 914 | | GetBaselineRebuildRegistryFootprint( |
| | 1 | 915 | | rebuilds, |
| | 1 | 916 | | out long priorRegistryBytes, |
| | 1 | 917 | | out int priorRegistryPages); |
| | 1 | 918 | | PersistentStringMap<NavigationBaselineRebuild> next = rebuilds.Remove(mapId, out _); |
| | 1 | 919 | | GetBaselineRebuildRegistryFootprint( |
| | 1 | 920 | | next, |
| | 1 | 921 | | out long nextRegistryBytes, |
| | 1 | 922 | | out int nextRegistryPages); |
| | 1 | 923 | | rebuildRetainedBytes = checked( |
| | 1 | 924 | | rebuildRetainedBytes |
| | 1 | 925 | | - priorRegistryBytes |
| | 1 | 926 | | - rebuild.RetainedBytes |
| | 1 | 927 | | + nextRegistryBytes); |
| | 1 | 928 | | rebuildPersistentPages = checked( |
| | 1 | 929 | | rebuildPersistentPages |
| | 1 | 930 | | - priorRegistryPages |
| | 1 | 931 | | - rebuild.PersistentPageCount |
| | 1 | 932 | | + nextRegistryPages); |
| | 1 | 933 | | rebuilds = next; |
| | 1 | 934 | | } |
| | | 935 | | |
| | | 936 | | internal static void GetBaselineRebuildRegistryFootprint( |
| | | 937 | | PersistentStringMap<NavigationBaselineRebuild> rebuilds, |
| | | 938 | | out long retainedBytes, |
| | | 939 | | out int persistentPages) |
| | | 940 | | { |
| | 4 | 941 | | bool hasRebuilds = rebuilds.Count != 0; |
| | 4 | 942 | | retainedBytes = hasRebuilds ? rebuilds.RetainedBytes : 0; |
| | 4 | 943 | | persistentPages = hasRebuilds ? 1 + rebuilds.PersistentNodeCount : 0; |
| | 4 | 944 | | } |
| | | 945 | | |
| | | 946 | | internal NavigationWorldGraph ApplyMaintenanceSnapshot( |
| | | 947 | | long worldSpawnToken, |
| | | 948 | | NavigationWorldGraph previousGraph, |
| | | 949 | | ReadOnlySpan<GridEventInfo> events, |
| | | 950 | | ReadOnlySpan<NavigationGridChangeScope> resnapshotScopes, |
| | | 951 | | bool resnapshotAll, |
| | | 952 | | ReadOnlySpan<NavigationGridChangeScope> blockedScopes, |
| | | 953 | | bool blockAll, |
| | | 954 | | NavigationOperationFrameChange[] changes, |
| | | 955 | | int changeCount, |
| | | 956 | | NavigationGridBaselineCapture[] baselineCaptures, |
| | | 957 | | int[] affectedOrdinals, |
| | | 958 | | int affectedCount, |
| | | 959 | | long graphVersion) |
| | | 960 | | { |
| | 20249 | 961 | | NavigationInstanceDirectory changed = _instances; |
| | 20249 | 962 | | long retainedBytes = RetainedBytes; |
| | 20249 | 963 | | int persistentPages = PersistentPageCount; |
| | 43310 | 964 | | for (int affectedIndex = 0; affectedIndex < affectedCount; affectedIndex++) |
| | | 965 | | { |
| | 1406 | 966 | | int mapIndex = affectedOrdinals[affectedIndex]; |
| | 1406 | 967 | | NavigationMapInstance current = changed.Get(mapIndex); |
| | 1406 | 968 | | GridConfigurationKey configurationKey = current.Map.GridBinding.Key; |
| | 1406 | 969 | | bool isBlocked = blockAll || ContainsScope(blockedScopes, configurationKey); |
| | 1406 | 970 | | bool requiresRecoveryBaseline = resnapshotAll |
| | 1406 | 971 | | || ContainsScope(resnapshotScopes, configurationKey) |
| | 1406 | 972 | | || EventsRequireBaseline( |
| | 1406 | 973 | | events, |
| | 1406 | 974 | | configurationKey, |
| | 1406 | 975 | | current.Map.DefaultCell.HasValue); |
| | 1406 | 976 | | bool requiresBaseline = requiresRecoveryBaseline |
| | 1406 | 977 | | || (!current.IsMaterialized && IsOperationChanged(changes, changeCount, current.MapId)); |
| | | 978 | | NavigationMapInstance next; |
| | 1406 | 979 | | if (isBlocked) |
| | | 980 | | { |
| | 44 | 981 | | next = current.FailClosed(graphVersion); |
| | | 982 | | } |
| | 1362 | 983 | | else if (!requiresBaseline) |
| | | 984 | | { |
| | 138 | 985 | | next = current.ApplyBatch(worldSpawnToken, events, false, graphVersion); |
| | | 986 | | } |
| | 1224 | 987 | | else if (!requiresRecoveryBaseline |
| | 1224 | 988 | | && IsCellOverlayOnlyChanged(changes, changeCount, current.MapId) |
| | 1224 | 989 | | && previousGraph.TryGetMap(current.MapId, out NavigationMapInstance prior)) |
| | | 990 | | { |
| | 118 | 991 | | next = current.MaterializeDelta( |
| | 118 | 992 | | prior, |
| | 118 | 993 | | baselineCaptures[mapIndex], |
| | 118 | 994 | | graphVersion).ApplyBatch(worldSpawnToken, events, false, graphVersion); |
| | | 995 | | } |
| | | 996 | | else |
| | | 997 | | { |
| | 1106 | 998 | | next = current.Materialize(baselineCaptures[mapIndex], graphVersion); |
| | | 999 | | } |
| | 1406 | 1000 | | if (!ReferenceEquals(current, next)) |
| | | 1001 | | { |
| | 1096 | 1002 | | retainedBytes = checked(retainedBytes - current.RetainedBytes + next.RetainedBytes); |
| | 1096 | 1003 | | persistentPages += next.PersistentPageCount - current.PersistentPageCount; |
| | 1096 | 1004 | | changed = changed.With(mapIndex, next); |
| | | 1005 | | } |
| | | 1006 | | } |
| | 20249 | 1007 | | if (ReferenceEquals(changed, _instances)) |
| | 19370 | 1008 | | return this; |
| | 879 | 1009 | | var updated = new NavigationWorldGraph( |
| | 879 | 1010 | | graphVersion, |
| | 879 | 1011 | | changed, |
| | 879 | 1012 | | AreaCatalog, |
| | 879 | 1013 | | _mapIndex, |
| | 879 | 1014 | | SurfaceComponents, |
| | 879 | 1015 | | _explicitConnections, |
| | 879 | 1016 | | _automaticSeams, |
| | 879 | 1017 | | _transitionPages, |
| | 879 | 1018 | | TransitionRules, |
| | 879 | 1019 | | _closedStructuralComponents, |
| | 879 | 1020 | | _additionalClosedStructuralComponents, |
| | 879 | 1021 | | _allStructuralComponentsClosed, |
| | 879 | 1022 | | retainedBytes, |
| | 879 | 1023 | | persistentPages); |
| | 879 | 1024 | | return updated; |
| | | 1025 | | } |
| | | 1026 | | |
| | | 1027 | | internal NavigationWorldGraph WithAreaCatalog( |
| | | 1028 | | NavigationAreaCatalog catalog, |
| | | 1029 | | long graphVersion) |
| | | 1030 | | { |
| | 3643 | 1031 | | if (ReferenceEquals(catalog, AreaCatalog)) |
| | 3424 | 1032 | | return this; |
| | 219 | 1033 | | long retainedBytes = checked(RetainedBytes - AreaCatalog.RetainedBytes + catalog.RetainedBytes); |
| | 219 | 1034 | | int persistentPages = PersistentPageCount - AreaCatalog.PersistentPageCount + catalog.PersistentPageCount; |
| | 219 | 1035 | | return new NavigationWorldGraph( |
| | 219 | 1036 | | graphVersion, |
| | 219 | 1037 | | _instances, |
| | 219 | 1038 | | catalog, |
| | 219 | 1039 | | _mapIndex, |
| | 219 | 1040 | | SurfaceComponents, |
| | 219 | 1041 | | _explicitConnections, |
| | 219 | 1042 | | _automaticSeams, |
| | 219 | 1043 | | _transitionPages, |
| | 219 | 1044 | | TransitionRules, |
| | 219 | 1045 | | _closedStructuralComponents, |
| | 219 | 1046 | | _additionalClosedStructuralComponents, |
| | 219 | 1047 | | _allStructuralComponentsClosed, |
| | 219 | 1048 | | retainedBytes, |
| | 219 | 1049 | | persistentPages); |
| | | 1050 | | } |
| | | 1051 | | |
| | | 1052 | | internal NavigationWorldGraph WithGraphVersion(long graphVersion) |
| | | 1053 | | { |
| | 341 | 1054 | | if (graphVersion == GraphVersion) |
| | 1 | 1055 | | return this; |
| | 340 | 1056 | | return new NavigationWorldGraph( |
| | 340 | 1057 | | graphVersion, |
| | 340 | 1058 | | _instances, |
| | 340 | 1059 | | AreaCatalog, |
| | 340 | 1060 | | _mapIndex, |
| | 340 | 1061 | | SurfaceComponents, |
| | 340 | 1062 | | _explicitConnections, |
| | 340 | 1063 | | _automaticSeams, |
| | 340 | 1064 | | _transitionPages, |
| | 340 | 1065 | | TransitionRules, |
| | 340 | 1066 | | _closedStructuralComponents, |
| | 340 | 1067 | | _additionalClosedStructuralComponents, |
| | 340 | 1068 | | _allStructuralComponentsClosed, |
| | 340 | 1069 | | RetainedBytes, |
| | 340 | 1070 | | PersistentPageCount); |
| | | 1071 | | } |
| | | 1072 | | |
| | | 1073 | | internal bool TryGetDependencyStamp( |
| | | 1074 | | NavigationAreaPolicyKey areaPolicy, |
| | | 1075 | | ReadOnlySpan<NavigationSurfaceComponentKey> componentKeys, |
| | | 1076 | | ReadOnlySpan<GraphPageDependencyAddress> pageAddresses, |
| | 18 | 1077 | | out GraphDependencyStamp stamp) => TryGetDependencyStamp( |
| | 18 | 1078 | | areaPolicy, |
| | 18 | 1079 | | componentKeys, |
| | 18 | 1080 | | pageAddresses, |
| | 18 | 1081 | | includeTransitionRules: false, |
| | 18 | 1082 | | out stamp); |
| | | 1083 | | |
| | | 1084 | | internal bool TryGetDependencyStamp( |
| | | 1085 | | NavigationAreaPolicyKey areaPolicy, |
| | | 1086 | | ReadOnlySpan<NavigationSurfaceComponentKey> componentKeys, |
| | | 1087 | | ReadOnlySpan<GraphPageDependencyAddress> pageAddresses, |
| | | 1088 | | bool includeTransitionRules, |
| | | 1089 | | out GraphDependencyStamp stamp) |
| | | 1090 | | { |
| | 20 | 1091 | | if (!AreaCatalog.TryGet(areaPolicy, out _)) |
| | | 1092 | | { |
| | 1 | 1093 | | stamp = null!; |
| | 1 | 1094 | | return false; |
| | | 1095 | | } |
| | | 1096 | | |
| | 19 | 1097 | | var components = new GraphComponentDependency[componentKeys.Length]; |
| | 19 | 1098 | | NavigationSurfaceComponentKey priorComponent = default; |
| | 54 | 1099 | | for (int i = 0; i < componentKeys.Length; i++) |
| | | 1100 | | { |
| | 11 | 1101 | | NavigationSurfaceComponentKey key = componentKeys[i]; |
| | 11 | 1102 | | if ((i > 0 && priorComponent.CompareTo(key) >= 0) |
| | 11 | 1103 | | || !TryGetComponentDependency(key, out components[i])) |
| | | 1104 | | { |
| | 3 | 1105 | | stamp = null!; |
| | 3 | 1106 | | return false; |
| | | 1107 | | } |
| | 8 | 1108 | | priorComponent = key; |
| | | 1109 | | } |
| | | 1110 | | |
| | 16 | 1111 | | var pages = new GraphPageDependency[pageAddresses.Length]; |
| | 16 | 1112 | | string? priorMapId = null; |
| | 16 | 1113 | | int priorPageIndex = -1; |
| | 56 | 1114 | | for (int i = 0; i < pageAddresses.Length; i++) |
| | | 1115 | | { |
| | 17 | 1116 | | GraphPageDependencyAddress address = pageAddresses[i]; |
| | 17 | 1117 | | int mapComparison = priorMapId == null |
| | 17 | 1118 | | ? 1 |
| | 17 | 1119 | | : string.CompareOrdinal(address.MapId, priorMapId); |
| | 17 | 1120 | | if (string.IsNullOrEmpty(address.MapId) |
| | 17 | 1121 | | || address.PageIndex < 0 |
| | 17 | 1122 | | || mapComparison < 0 |
| | 17 | 1123 | | || (mapComparison == 0 && address.PageIndex <= priorPageIndex) |
| | 17 | 1124 | | || !TryGetPageDependency( |
| | 17 | 1125 | | address, |
| | 17 | 1126 | | includeTransitionRules, |
| | 17 | 1127 | | out pages[i])) |
| | | 1128 | | { |
| | 5 | 1129 | | stamp = null!; |
| | 5 | 1130 | | return false; |
| | | 1131 | | } |
| | 12 | 1132 | | priorMapId = address.MapId; |
| | 12 | 1133 | | priorPageIndex = address.PageIndex; |
| | | 1134 | | } |
| | | 1135 | | |
| | 11 | 1136 | | stamp = new GraphDependencyStamp( |
| | 11 | 1137 | | areaPolicy, |
| | 11 | 1138 | | components, |
| | 11 | 1139 | | pages, |
| | 11 | 1140 | | includeTransitionRules, |
| | 11 | 1141 | | TransitionRules.Version); |
| | 11 | 1142 | | return true; |
| | | 1143 | | } |
| | | 1144 | | |
| | | 1145 | | internal bool TryGetComponentDependency( |
| | | 1146 | | NavigationSurfaceComponentKey key, |
| | | 1147 | | out GraphComponentDependency dependency) |
| | | 1148 | | { |
| | 7416 | 1149 | | if (!IsSurfaceComponentClosed(key) |
| | 7416 | 1150 | | && SurfaceComponents.TryGet(key, out NavigationSurfaceComponent component)) |
| | | 1151 | | { |
| | | 1152 | | System.Diagnostics.Debug.Assert(component.Key.Equals(key), |
| | | 1153 | | "Surface component records are indexed by their exact immutable key."); |
| | 7411 | 1154 | | dependency = new GraphComponentDependency( |
| | 7411 | 1155 | | key, |
| | 7411 | 1156 | | component.Version); |
| | 7411 | 1157 | | return true; |
| | | 1158 | | } |
| | 5 | 1159 | | dependency = default; |
| | 5 | 1160 | | return false; |
| | | 1161 | | } |
| | | 1162 | | |
| | | 1163 | | internal bool TryGetPageDependency( |
| | | 1164 | | GraphPageDependencyAddress address, |
| | 6434 | 1165 | | out GraphPageDependency dependency) => TryGetPageDependency( |
| | 6434 | 1166 | | address, |
| | 6434 | 1167 | | includeTransitionPage: false, |
| | 6434 | 1168 | | out dependency); |
| | | 1169 | | |
| | | 1170 | | internal bool TryGetPageDependency( |
| | | 1171 | | GraphPageDependencyAddress address, |
| | | 1172 | | bool includeTransitionPage, |
| | | 1173 | | out GraphPageDependency dependency) |
| | | 1174 | | { |
| | 32914 | 1175 | | if (!string.IsNullOrEmpty(address.MapId) |
| | 32914 | 1176 | | && address.PageIndex >= 0 |
| | 32914 | 1177 | | && TryGetMap(address.MapId, out NavigationMapInstance instance)) |
| | | 1178 | | { |
| | 32909 | 1179 | | long transitionVersion = includeTransitionPage && _transitionPages.TryGet( |
| | 32909 | 1180 | | new NavigationTransitionPageAddress(address.MapId, address.PageIndex), |
| | 32909 | 1181 | | out NavigationTransitionPage transitionPage) |
| | 32909 | 1182 | | ? transitionPage.Version |
| | 32909 | 1183 | | : 0; |
| | 32909 | 1184 | | dependency = instance.GetPageDependency(address.PageIndex, transitionVersion); |
| | 32909 | 1185 | | return true; |
| | | 1186 | | } |
| | 5 | 1187 | | dependency = default; |
| | 5 | 1188 | | return false; |
| | | 1189 | | } |
| | | 1190 | | |
| | | 1191 | | internal bool IsDependencyCurrent(GraphDependencyStamp stamp) |
| | | 1192 | | { |
| | 21604 | 1193 | | if (!AreaCatalog.TryGet(stamp.AreaPolicy, out _)) |
| | 2 | 1194 | | return false; |
| | 21602 | 1195 | | if (stamp.HasTransitionRuleDependency |
| | 21602 | 1196 | | && stamp.TransitionRuleVersion != TransitionRules.Version) |
| | | 1197 | | { |
| | 7 | 1198 | | return false; |
| | | 1199 | | } |
| | 99818 | 1200 | | for (int component = 0; component < stamp.Components.Length; component++) |
| | | 1201 | | { |
| | 28350 | 1202 | | GraphComponentDependency dependency = stamp.Components[component]; |
| | 28350 | 1203 | | if (IsSurfaceComponentClosed(dependency.Key) |
| | 28350 | 1204 | | || !SurfaceComponents.TryGet( |
| | 28350 | 1205 | | dependency.Key, |
| | 28350 | 1206 | | out NavigationSurfaceComponent current)) |
| | 22 | 1207 | | return false; |
| | | 1208 | | System.Diagnostics.Debug.Assert(current.Key.Equals(dependency.Key), |
| | | 1209 | | "Surface component records are indexed by their exact immutable key."); |
| | 28328 | 1210 | | if (current.Version != dependency.Version) |
| | 14 | 1211 | | return false; |
| | | 1212 | | } |
| | 94054 | 1213 | | for (int page = 0; page < stamp.Pages.Length; page++) |
| | | 1214 | | { |
| | 25492 | 1215 | | GraphPageDependency dependency = stamp.Pages[page]; |
| | 25492 | 1216 | | if (!TryGetPageDependency( |
| | 25492 | 1217 | | new GraphPageDependencyAddress(dependency.MapId, dependency.PageIndex), |
| | 25492 | 1218 | | stamp.HasTransitionRuleDependency, |
| | 25492 | 1219 | | out GraphPageDependency current) |
| | 25492 | 1220 | | || !current.Equals(dependency)) |
| | 24 | 1221 | | return false; |
| | | 1222 | | } |
| | 21535 | 1223 | | return true; |
| | | 1224 | | } |
| | | 1225 | | |
| | | 1226 | | private static PersistentGridConfigurationMap<string> BuildMapIndex( |
| | | 1227 | | NavigationMapInstance[] instances) |
| | | 1228 | | { |
| | 644 | 1229 | | PersistentGridConfigurationMap<string> index = PersistentGridConfigurationMap<string>.Empty; |
| | 2594 | 1230 | | for (int i = 0; i < instances.Length; i++) |
| | 653 | 1231 | | index = index.Set(instances[i].Map.GridBinding.Key, instances[i].MapId); |
| | 644 | 1232 | | return index; |
| | | 1233 | | } |
| | | 1234 | | |
| | | 1235 | | private int FindMapOrdinal(string mapId) |
| | | 1236 | | { |
| | 955384 | 1237 | | int low = 0; |
| | 955384 | 1238 | | int high = _instances.Count - 1; |
| | 1582791 | 1239 | | while (low <= high) |
| | | 1240 | | { |
| | 1582337 | 1241 | | int middle = low + ((high - low) >> 1); |
| | 1582337 | 1242 | | int comparison = string.CompareOrdinal(_instances.GetMapId(middle), mapId); |
| | 1582337 | 1243 | | if (comparison == 0) |
| | 954930 | 1244 | | return middle; |
| | 627407 | 1245 | | if (comparison < 0) |
| | 564669 | 1246 | | low = middle + 1; |
| | | 1247 | | else |
| | 62738 | 1248 | | high = middle - 1; |
| | | 1249 | | } |
| | 454 | 1250 | | return -1; |
| | | 1251 | | } |
| | | 1252 | | |
| | | 1253 | | private int CollectAffectedMaps( |
| | | 1254 | | ReadOnlySpan<GridEventInfo> events, |
| | | 1255 | | ReadOnlySpan<NavigationGridChangeScope> resnapshotScopes, |
| | | 1256 | | bool resnapshotAll, |
| | | 1257 | | ReadOnlySpan<NavigationGridChangeScope> blockedScopes, |
| | | 1258 | | bool blockAll, |
| | | 1259 | | NavigationOperationFrameChange[] changes, |
| | | 1260 | | int changeCount, |
| | | 1261 | | int[] ordinals, |
| | | 1262 | | int[] stamps, |
| | | 1263 | | ref int stamp) |
| | | 1264 | | { |
| | 8786 | 1265 | | if (++stamp == 0) |
| | | 1266 | | { |
| | 1 | 1267 | | Array.Clear(stamps, 0, stamps.Length); |
| | 1 | 1268 | | stamp = 1; |
| | | 1269 | | } |
| | | 1270 | | |
| | 8786 | 1271 | | int count = 0; |
| | 8786 | 1272 | | bool all = resnapshotAll || blockAll; |
| | 18316 | 1273 | | for (int i = 0; i < events.Length && !all; i++) |
| | | 1274 | | { |
| | 373 | 1275 | | if (events[i].ChangeKind == GridEventKind.WorldReset) |
| | | 1276 | | { |
| | 1 | 1277 | | all = true; |
| | 1 | 1278 | | break; |
| | | 1279 | | } |
| | 372 | 1280 | | MarkConfiguration(events[i].Configuration.ToGridKey(), ordinals, ref count, stamps, stamp); |
| | | 1281 | | } |
| | | 1282 | | |
| | 17784 | 1283 | | for (int i = 0; i < resnapshotScopes.Length && !all; i++) |
| | 106 | 1284 | | MarkConfiguration(resnapshotScopes[i].ConfigurationKey, ordinals, ref count, stamps, stamp); |
| | 17584 | 1285 | | for (int i = 0; i < blockedScopes.Length && !all; i++) |
| | 6 | 1286 | | MarkConfiguration(blockedScopes[i].ConfigurationKey, ordinals, ref count, stamps, stamp); |
| | 8786 | 1287 | | MarkOperationChanges(changes, changeCount, ordinals, ref count, stamps, stamp); |
| | | 1288 | | |
| | 8786 | 1289 | | if (all) |
| | | 1290 | | { |
| | 61 | 1291 | | count = _instances.Count; |
| | 266 | 1292 | | for (int i = 0; i < count; i++) |
| | 72 | 1293 | | ordinals[i] = i; |
| | | 1294 | | } |
| | | 1295 | | else |
| | 8725 | 1296 | | Array.Sort(ordinals, 0, count); |
| | 8786 | 1297 | | return count; |
| | | 1298 | | } |
| | | 1299 | | |
| | | 1300 | | private void MarkOperationChanges( |
| | | 1301 | | NavigationOperationFrameChange[] changes, |
| | | 1302 | | int changeCount, |
| | | 1303 | | int[] ordinals, |
| | | 1304 | | ref int count, |
| | | 1305 | | int[] stamps, |
| | | 1306 | | int stamp) |
| | | 1307 | | { |
| | 19838 | 1308 | | for (int i = 0; i < changeCount; i++) |
| | | 1309 | | { |
| | 1133 | 1310 | | NavigationOperationFrameChange change = changes[i]; |
| | 1133 | 1311 | | if (change.Kind == NavigationOperationFrameChangeKind.MapCommit) |
| | | 1312 | | { |
| | 948 | 1313 | | MarkMapId(change.MapId!, ordinals, ref count, stamps, stamp); |
| | 948 | 1314 | | continue; |
| | | 1315 | | } |
| | 185 | 1316 | | if (change.Kind != NavigationOperationFrameChangeKind.Overlay) |
| | | 1317 | | continue; |
| | 180 | 1318 | | ReadOnlySpan<NavigationMapOverlayDelta> deltas = change.PreparedOverlay!.Transaction.MapSpan; |
| | 750 | 1319 | | for (int delta = 0; delta < deltas.Length; delta++) |
| | 195 | 1320 | | MarkMapId(deltas[delta].MapId, ordinals, ref count, stamps, stamp); |
| | | 1321 | | } |
| | 8786 | 1322 | | } |
| | | 1323 | | |
| | | 1324 | | private void MarkMapId( |
| | | 1325 | | string mapId, |
| | | 1326 | | int[] ordinals, |
| | | 1327 | | ref int count, |
| | | 1328 | | int[] stamps, |
| | | 1329 | | int stamp) |
| | | 1330 | | { |
| | 1143 | 1331 | | int ordinal = FindMapOrdinal(mapId); |
| | 1143 | 1332 | | TryMarkOrdinal(ordinal, ordinals, ref count, stamps, stamp); |
| | 1143 | 1333 | | } |
| | | 1334 | | |
| | | 1335 | | private void MarkConfiguration( |
| | | 1336 | | GridConfigurationKey key, |
| | | 1337 | | int[] ordinals, |
| | | 1338 | | ref int count, |
| | | 1339 | | int[] stamps, |
| | | 1340 | | int stamp) |
| | | 1341 | | { |
| | 484 | 1342 | | if (_mapIndex.TryGetValue(key, out string mapId)) |
| | | 1343 | | { |
| | 232 | 1344 | | int ordinal = FindMapOrdinal(mapId); |
| | 232 | 1345 | | TryMarkOrdinal(ordinal, ordinals, ref count, stamps, stamp); |
| | | 1346 | | } |
| | 484 | 1347 | | } |
| | | 1348 | | |
| | | 1349 | | internal static bool TryMarkOrdinal( |
| | | 1350 | | int ordinal, |
| | | 1351 | | int[] ordinals, |
| | | 1352 | | ref int count, |
| | | 1353 | | int[] stamps, |
| | | 1354 | | int stamp) |
| | | 1355 | | { |
| | 1379 | 1356 | | if (ordinal < 0 || stamps[ordinal] == stamp) |
| | 34 | 1357 | | return false; |
| | 1345 | 1358 | | stamps[ordinal] = stamp; |
| | 1345 | 1359 | | ordinals[count++] = ordinal; |
| | 1345 | 1360 | | return true; |
| | | 1361 | | } |
| | | 1362 | | |
| | | 1363 | | private static bool ContainsScope( |
| | | 1364 | | ReadOnlySpan<NavigationGridChangeScope> scopes, |
| | | 1365 | | GridConfigurationKey key) |
| | | 1366 | | { |
| | 11042 | 1367 | | for (int i = 0; i < scopes.Length; i++) |
| | | 1368 | | { |
| | 245 | 1369 | | if (scopes[i].ConfigurationKey.Equals(key)) |
| | 223 | 1370 | | return true; |
| | | 1371 | | } |
| | 5276 | 1372 | | return false; |
| | | 1373 | | } |
| | | 1374 | | |
| | | 1375 | | private static bool EventsRequireBaseline( |
| | | 1376 | | ReadOnlySpan<GridEventInfo> events, |
| | | 1377 | | GridConfigurationKey key, |
| | | 1378 | | bool discoverDefaultPresence) |
| | | 1379 | | { |
| | 5514 | 1380 | | for (int i = 0; i < events.Length; i++) |
| | | 1381 | | { |
| | 260 | 1382 | | if ((events[i].ChangeKind == GridEventKind.GridAdded |
| | 260 | 1383 | | || events[i].ChangeKind == GridEventKind.GridChanged |
| | 260 | 1384 | | || discoverDefaultPresence |
| | 260 | 1385 | | && (events[i].ChangeKind == GridEventKind.SparseVoxelAdded |
| | 260 | 1386 | | || events[i].ChangeKind == GridEventKind.SparseVoxelRemoved)) |
| | 260 | 1387 | | && events[i].Configuration.ToGridKey().Equals(key)) |
| | | 1388 | | { |
| | 48 | 1389 | | return true; |
| | | 1390 | | } |
| | | 1391 | | } |
| | 2497 | 1392 | | return false; |
| | | 1393 | | } |
| | | 1394 | | |
| | | 1395 | | private static bool IsOperationChanged( |
| | | 1396 | | NavigationOperationFrameChange[] changes, |
| | | 1397 | | int changeCount, |
| | | 1398 | | string mapId) |
| | | 1399 | | { |
| | 6140 | 1400 | | for (int i = 0; i < changeCount; i++) |
| | | 1401 | | { |
| | 2871 | 1402 | | NavigationOperationFrameChange change = changes[i]; |
| | 2871 | 1403 | | if (change.Kind == NavigationOperationFrameChangeKind.MapCommit) |
| | | 1404 | | { |
| | 2630 | 1405 | | if (string.Equals(change.MapId, mapId, StringComparison.Ordinal)) |
| | 1896 | 1406 | | return true; |
| | | 1407 | | continue; |
| | | 1408 | | } |
| | 241 | 1409 | | if (change.Kind != NavigationOperationFrameChangeKind.Overlay) |
| | | 1410 | | continue; |
| | 239 | 1411 | | ReadOnlySpan<NavigationMapOverlayDelta> deltas = change.PreparedOverlay!.Transaction.MapSpan; |
| | 958 | 1412 | | for (int delta = 0; delta < deltas.Length; delta++) |
| | | 1413 | | { |
| | 479 | 1414 | | if (string.Equals(deltas[delta].MapId, mapId, StringComparison.Ordinal)) |
| | 239 | 1415 | | return true; |
| | | 1416 | | } |
| | | 1417 | | } |
| | 199 | 1418 | | return false; |
| | | 1419 | | } |
| | | 1420 | | |
| | | 1421 | | private static bool IsCellOverlayOnlyChanged( |
| | | 1422 | | NavigationOperationFrameChange[] changes, |
| | | 1423 | | int changeCount, |
| | | 1424 | | string mapId) |
| | | 1425 | | { |
| | 2133 | 1426 | | bool found = false; |
| | 6220 | 1427 | | for (int i = 0; i < changeCount; i++) |
| | | 1428 | | { |
| | 2871 | 1429 | | NavigationOperationFrameChange change = changes[i]; |
| | 2871 | 1430 | | if (change.Kind == NavigationOperationFrameChangeKind.MapCommit |
| | 2871 | 1431 | | && string.Equals(change.MapId, mapId, StringComparison.Ordinal)) |
| | | 1432 | | { |
| | 1894 | 1433 | | return false; |
| | | 1434 | | } |
| | 977 | 1435 | | if (change.Kind != NavigationOperationFrameChangeKind.Overlay) |
| | | 1436 | | continue; |
| | 243 | 1437 | | ReadOnlySpan<NavigationMapOverlayDelta> deltas = change.PreparedOverlay!.Transaction.MapSpan; |
| | 1932 | 1438 | | for (int delta = 0; delta < deltas.Length; delta++) |
| | | 1439 | | { |
| | 723 | 1440 | | if (string.Equals(deltas[delta].MapId, mapId, StringComparison.Ordinal)) |
| | 243 | 1441 | | found = true; |
| | | 1442 | | } |
| | | 1443 | | } |
| | 239 | 1444 | | return found; |
| | | 1445 | | } |
| | | 1446 | | } |