| | | 1 | | //======================================================================= |
| | | 2 | | // TrailblazerWorldContextSettings.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 FixedMathSharp; |
| | | 9 | | using Trailblazer.Pathing; |
| | | 10 | | |
| | | 11 | | namespace Trailblazer; |
| | | 12 | | |
| | | 13 | | /// <summary> |
| | | 14 | | /// Freezes the finite graph, lifecycle, and query-workspace ceilings for one world context. |
| | | 15 | | /// </summary> |
| | | 16 | | public sealed class TrailblazerWorldContextSettings |
| | | 17 | | { |
| | | 18 | | internal const int MinimumIngressBytes = 256; |
| | 1 | 19 | | internal static readonly long MinimumActiveSnapshotBytes = checked( |
| | 1 | 20 | | NavigationWorldGraph.Empty.RetainedBytes * 3L); |
| | 1 | 21 | | internal static readonly int MinimumPersistentGraphPages = checked( |
| | 1 | 22 | | NavigationWorldGraph.Empty.PersistentPageCount * 3); |
| | | 23 | | |
| | | 24 | | /// <summary>Gets the recommended finite settings used when a host does not provide custom limits.</summary> |
| | | 25 | | public static TrailblazerWorldContextSettings Default { get; } = CreateDefault(); |
| | | 26 | | |
| | | 27 | | /// <summary>Creates an immutable explicit set of context-owned runtime ceilings.</summary> |
| | 353 | 28 | | public TrailblazerWorldContextSettings( |
| | 353 | 29 | | NavigationOperationLimits operationLimits, |
| | 353 | 30 | | MaintenanceWorkBudget maintenanceBudget, |
| | 353 | 31 | | GuideSampleWorkBudget guideSampleBudget, |
| | 353 | 32 | | Fixed64 movementGroupPadding, |
| | 353 | 33 | | int maxIngressEntries, |
| | 353 | 34 | | long maxIngressBytes, |
| | 353 | 35 | | int maxActiveSnapshots, |
| | 353 | 36 | | long maxActiveSnapshotBytes, |
| | 353 | 37 | | int maxRetiredSnapshots, |
| | 353 | 38 | | long maxRetiredSnapshotBytes, |
| | 353 | 39 | | int maxPersistentGraphPages, |
| | 353 | 40 | | int maxDynamicCellSlotsPerMap, |
| | 353 | 41 | | int maxDynamicCellSlots, |
| | 353 | 42 | | int navigationAreaCount, |
| | 353 | 43 | | int maxAreaPolicies, |
| | 353 | 44 | | int maxAreaRulesPerPolicy, |
| | 353 | 45 | | int maxAreaRules, |
| | 353 | 46 | | int maxConcurrentSnapshotLeases, |
| | 353 | 47 | | NavigationQueryLimits queryLimits) |
| | | 48 | | { |
| | 353 | 49 | | SwiftThrowHelper.ThrowIfArgument( |
| | 353 | 50 | | operationLimits.MaxPendingOperations <= 0, |
| | 353 | 51 | | nameof(operationLimits), |
| | 353 | 52 | | "Operation limits must be explicitly initialized."); |
| | 352 | 53 | | SwiftThrowHelper.ThrowIfArgument( |
| | 352 | 54 | | maintenanceBudget.MaxConsumedEnvelopes <= 0, |
| | 352 | 55 | | nameof(maintenanceBudget), |
| | 352 | 56 | | "Maintenance work budget must be explicitly initialized."); |
| | 351 | 57 | | ThrowIfNonPositive(maxIngressEntries, nameof(maxIngressEntries)); |
| | 350 | 58 | | ThrowIfNonPositive(maxIngressBytes, nameof(maxIngressBytes)); |
| | 349 | 59 | | SwiftThrowHelper.ThrowIfArgumentOutOfRange( |
| | 349 | 60 | | maxIngressBytes < MinimumIngressBytes, |
| | 349 | 61 | | null, |
| | 349 | 62 | | nameof(maxIngressBytes)); |
| | 348 | 63 | | SwiftThrowHelper.ThrowIfArgumentOutOfRange( |
| | 348 | 64 | | maxActiveSnapshots < 3, |
| | 348 | 65 | | maxActiveSnapshots, |
| | 348 | 66 | | nameof(maxActiveSnapshots), |
| | 348 | 67 | | "Snapshot capacity must reserve the current, fail-closed, and candidate roots."); |
| | 347 | 68 | | SwiftThrowHelper.ThrowIfArgumentOutOfRange( |
| | 347 | 69 | | maxActiveSnapshotBytes < MinimumActiveSnapshotBytes, |
| | 347 | 70 | | null, |
| | 347 | 71 | | nameof(maxActiveSnapshotBytes), |
| | 347 | 72 | | "Snapshot bytes must reserve the current, fail-closed, and candidate empty roots."); |
| | 345 | 73 | | ThrowIfNegative(maxRetiredSnapshots, nameof(maxRetiredSnapshots)); |
| | 344 | 74 | | ThrowIfNegative(maxRetiredSnapshotBytes, nameof(maxRetiredSnapshotBytes)); |
| | 343 | 75 | | SwiftThrowHelper.ThrowIfArgumentOutOfRange( |
| | 343 | 76 | | maxPersistentGraphPages < MinimumPersistentGraphPages, |
| | 343 | 77 | | maxPersistentGraphPages, |
| | 343 | 78 | | nameof(maxPersistentGraphPages), |
| | 343 | 79 | | "Persistent pages must reserve the current, fail-closed, and candidate empty roots."); |
| | 341 | 80 | | ThrowIfNegative(maxDynamicCellSlotsPerMap, nameof(maxDynamicCellSlotsPerMap)); |
| | 340 | 81 | | ThrowIfNegative(maxDynamicCellSlots, nameof(maxDynamicCellSlots)); |
| | 339 | 82 | | SwiftThrowHelper.ThrowIfArgument( |
| | 339 | 83 | | maxDynamicCellSlots < maxDynamicCellSlotsPerMap, |
| | 339 | 84 | | nameof(maxDynamicCellSlots), |
| | 339 | 85 | | "Context dynamic-cell capacity cannot be smaller than the per-map capacity."); |
| | 338 | 86 | | SwiftThrowHelper.ThrowIfArgument( |
| | 338 | 87 | | maxDynamicCellSlotsPerMap < operationLimits.MaxOverlayCellsPerMap, |
| | 338 | 88 | | nameof(maxDynamicCellSlotsPerMap), |
| | 338 | 89 | | "Per-map dynamic-cell capacity must cover every cell overlay admitted by the operation limits."); |
| | 337 | 90 | | SwiftThrowHelper.ThrowIfArgument( |
| | 337 | 91 | | maxDynamicCellSlots < operationLimits.MaxOverlayCells, |
| | 337 | 92 | | nameof(maxDynamicCellSlots), |
| | 337 | 93 | | "Context dynamic-cell capacity must cover every cell overlay admitted by the operation limits."); |
| | 336 | 94 | | SwiftThrowHelper.ThrowIfArgumentOutOfRange( |
| | 336 | 95 | | navigationAreaCount <= 0 || navigationAreaCount > ushort.MaxValue + 1, |
| | 336 | 96 | | navigationAreaCount, |
| | 336 | 97 | | nameof(navigationAreaCount)); |
| | 335 | 98 | | ThrowIfNonPositive(maxAreaPolicies, nameof(maxAreaPolicies)); |
| | 334 | 99 | | ThrowIfNonPositive(maxAreaRulesPerPolicy, nameof(maxAreaRulesPerPolicy)); |
| | 333 | 100 | | ThrowIfNonPositive(maxAreaRules, nameof(maxAreaRules)); |
| | 332 | 101 | | SwiftThrowHelper.ThrowIfArgument( |
| | 332 | 102 | | maxAreaRules < maxAreaRulesPerPolicy |
| | 332 | 103 | | || maxAreaRulesPerPolicy < navigationAreaCount, |
| | 332 | 104 | | nameof(maxAreaRules), |
| | 332 | 105 | | "Area-rule capacity must cover the configured context area layout."); |
| | 331 | 106 | | int maximumRetainedAreaPolicies = System.Math.Min( |
| | 331 | 107 | | maxAreaPolicies, |
| | 331 | 108 | | maxAreaRules / navigationAreaCount); |
| | 331 | 109 | | long minimumAreaPolicyWork = System.Math.Max( |
| | 331 | 110 | | navigationAreaCount, |
| | 331 | 111 | | 2L * maximumRetainedAreaPolicies) + 1L; |
| | 331 | 112 | | SwiftThrowHelper.ThrowIfArgument( |
| | 331 | 113 | | maintenanceBudget.MaxDependencyEntries < minimumAreaPolicyWork, |
| | 331 | 114 | | nameof(maintenanceBudget), |
| | 331 | 115 | | "Dependency work must fit one exact area-policy publication."); |
| | 329 | 116 | | SwiftThrowHelper.ThrowIfArgumentOutOfRange( |
| | 329 | 117 | | movementGroupPadding < Fixed64.Zero, |
| | 329 | 118 | | null, |
| | 329 | 119 | | nameof(movementGroupPadding)); |
| | 328 | 120 | | ThrowIfNonPositive(maxConcurrentSnapshotLeases, nameof(maxConcurrentSnapshotLeases)); |
| | 328 | 121 | | SwiftThrowHelper.ThrowIfArgument( |
| | 328 | 122 | | queryLimits.MaxConcurrentNavigationQueries <= 0, |
| | 328 | 123 | | nameof(queryLimits), |
| | 328 | 124 | | "Query limits must be explicitly initialized."); |
| | 327 | 125 | | SwiftThrowHelper.ThrowIfArgument( |
| | 327 | 126 | | queryLimits.MaxConcurrentNavigationQueries > maxConcurrentSnapshotLeases, |
| | 327 | 127 | | nameof(queryLimits), |
| | 327 | 128 | | "Concurrent queries cannot exceed the context snapshot-lease ceiling."); |
| | | 129 | | |
| | 326 | 130 | | OperationLimits = operationLimits; |
| | 326 | 131 | | MaintenanceBudget = maintenanceBudget; |
| | 326 | 132 | | GuideSampleBudget = guideSampleBudget; |
| | 326 | 133 | | MovementGroupPadding = movementGroupPadding; |
| | 326 | 134 | | MaxIngressEntries = maxIngressEntries; |
| | 326 | 135 | | MaxIngressBytes = maxIngressBytes; |
| | 326 | 136 | | MaxActiveSnapshots = maxActiveSnapshots; |
| | 326 | 137 | | MaxActiveSnapshotBytes = maxActiveSnapshotBytes; |
| | 326 | 138 | | MaxRetiredSnapshots = maxRetiredSnapshots; |
| | 326 | 139 | | MaxRetiredSnapshotBytes = maxRetiredSnapshotBytes; |
| | 326 | 140 | | MaxPersistentGraphPages = maxPersistentGraphPages; |
| | 326 | 141 | | MaxDynamicCellSlotsPerMap = maxDynamicCellSlotsPerMap; |
| | 326 | 142 | | MaxDynamicCellSlots = maxDynamicCellSlots; |
| | 326 | 143 | | NavigationAreaCount = navigationAreaCount; |
| | 326 | 144 | | MaxAreaPolicies = maxAreaPolicies; |
| | 326 | 145 | | MaxAreaRulesPerPolicy = maxAreaRulesPerPolicy; |
| | 326 | 146 | | MaxAreaRules = maxAreaRules; |
| | 326 | 147 | | MaxConcurrentSnapshotLeases = maxConcurrentSnapshotLeases; |
| | 326 | 148 | | QueryLimits = queryLimits; |
| | 326 | 149 | | } |
| | | 150 | | |
| | | 151 | | /// <summary>Gets the map-operation, preparation, and semantic-overlay ceilings.</summary> |
| | | 152 | | public NavigationOperationLimits OperationLimits { get; } |
| | | 153 | | |
| | | 154 | | /// <summary>Gets the deterministic work budget consumed at each maintenance boundary.</summary> |
| | | 155 | | public MaintenanceWorkBudget MaintenanceBudget { get; } |
| | | 156 | | |
| | | 157 | | /// <summary>Gets the deterministic work budget consumed by one graph flow guide sample.</summary> |
| | | 158 | | public GuideSampleWorkBudget GuideSampleBudget { get; } |
| | | 159 | | |
| | | 160 | | /// <summary>Gets the world-space padding added to the mean radius of one movement group.</summary> |
| | | 161 | | public Fixed64 MovementGroupPadding { get; } |
| | | 162 | | |
| | | 163 | | /// <summary>Gets the maximum coalesced GridForge ingress entries.</summary> |
| | | 164 | | public int MaxIngressEntries { get; } |
| | | 165 | | |
| | | 166 | | /// <summary>Gets the maximum retained GridForge ingress bytes.</summary> |
| | | 167 | | public long MaxIngressBytes { get; } |
| | | 168 | | |
| | | 169 | | /// <summary>Gets the maximum current, fail-closed, and unpublished active snapshots.</summary> |
| | | 170 | | public int MaxActiveSnapshots { get; } |
| | | 171 | | |
| | | 172 | | /// <summary>Gets the maximum bytes retained by active snapshots.</summary> |
| | | 173 | | public long MaxActiveSnapshotBytes { get; } |
| | | 174 | | |
| | | 175 | | /// <summary>Gets the maximum leased snapshot generations retained after publication.</summary> |
| | | 176 | | public int MaxRetiredSnapshots { get; } |
| | | 177 | | |
| | | 178 | | /// <summary>Gets the maximum bytes retained by leased retired snapshots.</summary> |
| | | 179 | | public long MaxRetiredSnapshotBytes { get; } |
| | | 180 | | |
| | | 181 | | /// <summary>Gets the maximum persistent registry, semantic, physical, and index pages.</summary> |
| | | 182 | | public int MaxPersistentGraphPages { get; } |
| | | 183 | | |
| | | 184 | | /// <summary>Gets the maximum non-reused dynamic cell slots retained by one map.</summary> |
| | | 185 | | public int MaxDynamicCellSlotsPerMap { get; } |
| | | 186 | | |
| | | 187 | | /// <summary>Gets the maximum non-reused dynamic cell slots retained by the context.</summary> |
| | | 188 | | public int MaxDynamicCellSlots { get; } |
| | | 189 | | |
| | | 190 | | /// <summary>Gets the exact contiguous context area layout spanning IDs zero through count minus one.</summary> |
| | | 191 | | public int NavigationAreaCount { get; } |
| | | 192 | | |
| | | 193 | | /// <summary>Gets the maximum immutable navigation-area policies retained by the context.</summary> |
| | | 194 | | public int MaxAreaPolicies { get; } |
| | | 195 | | |
| | | 196 | | /// <summary>Gets the maximum direct-indexed rules retained by one area policy.</summary> |
| | | 197 | | public int MaxAreaRulesPerPolicy { get; } |
| | | 198 | | |
| | | 199 | | /// <summary>Gets the maximum direct-indexed area rules retained across the context.</summary> |
| | | 200 | | public int MaxAreaRules { get; } |
| | | 201 | | |
| | | 202 | | /// <summary>Gets the maximum concurrently checked-out immutable graph snapshots.</summary> |
| | | 203 | | public int MaxConcurrentSnapshotLeases { get; } |
| | | 204 | | |
| | | 205 | | /// <summary>Gets the context-owned navigation query admission and retention ceilings.</summary> |
| | | 206 | | public NavigationQueryLimits QueryLimits { get; } |
| | | 207 | | |
| | 1 | 208 | | private static TrailblazerWorldContextSettings CreateDefault() => new( |
| | 1 | 209 | | new NavigationOperationLimits( |
| | 1 | 210 | | maxPendingOperations: 32, |
| | 1 | 211 | | maxPendingDescriptorBytes: 1_048_576, |
| | 1 | 212 | | maxPreparedMapBytes: 16_777_216, |
| | 1 | 213 | | maxBatchItems: 32, |
| | 1 | 214 | | maxBatchDescriptorBytes: 1_048_576, |
| | 1 | 215 | | maxBatchSortScratchBytes: 1_048_576, |
| | 1 | 216 | | maxCorridorCells: 64, |
| | 1 | 217 | | maxMaps: 16, |
| | 1 | 218 | | maxRetainedMapIdentities: 32, |
| | 1 | 219 | | maxOverlayCellsPerMap: 4_096, |
| | 1 | 220 | | maxOverlayConnectionsPerMap: 1_024, |
| | 1 | 221 | | maxOverlayTransitionsPerMap: 1_024, |
| | 1 | 222 | | maxOverlayCells: 16_384, |
| | 1 | 223 | | maxOverlayConnections: 4_096, |
| | 1 | 224 | | maxOverlayTransitions: 4_096, |
| | 1 | 225 | | maxTransitionRulesPerMap: 1_024, |
| | 1 | 226 | | maxTransitionRules: 4_096), |
| | 1 | 227 | | new MaintenanceWorkBudget( |
| | 1 | 228 | | maxConsumedEnvelopes: 4_096, |
| | 1 | 229 | | maxBaselineAddresses: 65_536, |
| | 1 | 230 | | maxOverlaySlots: 16_384, |
| | 1 | 231 | | maxComponentNodes: 65_536, |
| | 1 | 232 | | maxSeamCandidateProbes: 65_536, |
| | 1 | 233 | | maxExplicitEdges: 65_536, |
| | 1 | 234 | | maxDependencyEntries: 65_536, |
| | 1 | 235 | | maxSurfaceComponentEdges: 65_536), |
| | 1 | 236 | | new GuideSampleWorkBudget( |
| | 1 | 237 | | maxCurrentNodeLookupProbes: 128, |
| | 1 | 238 | | maxCursorLegScans: 128, |
| | 1 | 239 | | maxCursorRebases: 8, |
| | 1 | 240 | | maxPortalChecks: 32, |
| | 1 | 241 | | maxPrismChecks: 32, |
| | 1 | 242 | | maxTraceIntervals: 32, |
| | 1 | 243 | | maxLocalRecoveryAttempts: 1), |
| | 1 | 244 | | movementGroupPadding: Fixed64.Half, |
| | 1 | 245 | | maxIngressEntries: 16_384, |
| | 1 | 246 | | maxIngressBytes: 4_194_304, |
| | 1 | 247 | | maxActiveSnapshots: 3, |
| | 1 | 248 | | maxActiveSnapshotBytes: 66_379_544, |
| | 1 | 249 | | maxRetiredSnapshots: 8, |
| | 1 | 250 | | maxRetiredSnapshotBytes: 67_108_864, |
| | 1 | 251 | | maxPersistentGraphPages: 660_550, |
| | 1 | 252 | | maxDynamicCellSlotsPerMap: 4_096, |
| | 1 | 253 | | maxDynamicCellSlots: 16_384, |
| | 1 | 254 | | navigationAreaCount: 1, |
| | 1 | 255 | | maxAreaPolicies: 64, |
| | 1 | 256 | | maxAreaRulesPerPolicy: 4_096, |
| | 1 | 257 | | maxAreaRules: 65_536, |
| | 1 | 258 | | maxConcurrentSnapshotLeases: 8, |
| | 1 | 259 | | queryLimits: NavigationQueryLimits.Default); |
| | | 260 | | |
| | | 261 | | private static void ThrowIfNonPositive(int value, string parameterName) => |
| | 1681 | 262 | | SwiftThrowHelper.ThrowIfArgumentOutOfRange(value <= 0, value, parameterName); |
| | | 263 | | |
| | | 264 | | private static void ThrowIfNonPositive(long value, string parameterName) => |
| | 350 | 265 | | SwiftThrowHelper.ThrowIfArgumentOutOfRange(value <= 0, null, parameterName); |
| | | 266 | | |
| | | 267 | | private static void ThrowIfNegative(int value, string parameterName) => |
| | 1026 | 268 | | SwiftThrowHelper.ThrowIfArgumentOutOfRange(value < 0, value, parameterName); |
| | | 269 | | |
| | | 270 | | private static void ThrowIfNegative(long value, string parameterName) => |
| | 344 | 271 | | SwiftThrowHelper.ThrowIfArgumentOutOfRange(value < 0, null, parameterName); |
| | | 272 | | } |