Skip to content

Spatial Tiers: Multi-Resolution Simulation (#228) - #237

Merged
nockawa merged 14 commits into
mainfrom
feature/228-spatial-tiers
Apr 10, 2026
Merged

nockawa merged 14 commits into
mainfrom
feature/228-spatial-tiers

Conversation

@nockawa

@nockawa nockawa commented Apr 10, 2026

Copy link
Copy Markdown
Collaborator

Summary

Implements the full spatial tiers system for multi-resolution ECS simulation (issues #228-#234). Game code can run different systems at different frequencies for different spatial regions, enabling 10M entities within ~43% tick budget.

Core features

  • Spatial Grid — 2D cell grid with Morton-encoded keys, per-cell tier assignment, multi-observer helpers (SetCellTierMin, ResetAllTiers, SetTierInAABB)
  • Spatial Clusters — entities grouped by cell, migration with hysteresis dead zones, engine-driven cell-crossing detection at tick fence
  • Per-Cell Cluster Index — SoA broadphase with AABB-vs-AABB narrowphase, 2D+3D unified storage, category filtering, zero-alloc ref-struct enumerator
  • Tier-Filtered Dispatch — SimTier flags enum, TierClusterIndex with dual invalidation + merge cache, per-system tier/amortization/checkerboard parameters
  • Cluster Dormancy — sleep counters, DormancyReporter with thread-local deferred wakes, heartbeat periodic wakes, one-tick wake latency
  • Checkerboard Dispatch — two-phase Red/Black split by (cellX+cellY)%2, conflict-free parallel writes via DagScheduler re-dispatch
  • SpatialGridAccessor — readonly struct on TickContext for game-side tier assignment and coordinate queries
  • TierBudgetMetrics — per-tier cost/entity tracking for adaptive budget-driven tier boundaries

Sub-issues resolved

Code review fixes (16 items)

  • Pool leak in pure-Transient parallel path
  • Race in GetOrCreateTierRangeView
  • HashSet alloc per EvaluateRegion (double-buffer swap)
  • ArrayPool inconsistency in SpatialInterestSystem
  • TierClusterIndex sentinel collision (-1 for "not built")
  • CellClusterPool int overflow guard
  • SpatialGridAccessor null safety (ThrowIfInvalid)
  • QueryNearest 3D radius (float.MaxValue)
  • ConfigureSpatialGrid double-call guard
  • SleepThresholdTicks clamped property
  • DormancyReporter O(1) HasPendingRequests
  • ResetAllTiers spurious version bump
  • Debug assert for single-bit tier invariant
  • try/finally for _rebuildInProgress canary
  • ViewBase.WithTier generic overload
  • Misleading narrowphase category comment

Tests added (25)

  • 8 WorldToCellRange tests
  • 4 SetCellTier strict tests
  • 13 3D cluster spatial tests (Z-axis AABB/radius filtering, Union3F, tick fence, destroy)

Documentation updates (54 fixes across 8 docs)

  • Design docs 01-05 aligned with implementation
  • overview/13-runtime.md: new Spatial Tiers section
  • rules/spatial.md: 10 new correctness invariants
  • README status: Draft → Implemented

Test plan

  • Full test suite: 3210 passed, 0 failed, 34 skipped
  • Clean build (0 errors, 0 warnings in engine)
  • Code review: no critical bugs, all Medium+ items fixed
  • Doc audit: all 54 discrepancies resolved

🤖 Generated with Claude Code

@nockawa
nockawa merged commit 06eb78a into main Apr 10, 2026
5 checks passed
@nockawa
nockawa deleted the feature/228-spatial-tiers branch April 10, 2026 15:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant