Spatially index indoor door proximity to reduce PFE CPU
Implement first CPU priority from true on-CPU analysis tsk_nka9m9gvx8 (capture tsk_sccd4kxghg): Indoor HandleDoorStates owns19.49% inclusive CPU samples, with far-character CharacterId stat lookups7.01% and GetStat3.21% of all samples. Remove doors×all-character tick scans, preserving gameplay. Plan: isolated worktree /home/mathias/Projects/worktrees/engines-indoor-doors, branch perf/index-indoor-door-proximity from origin/develop. Reuse immutable spatial publication patterns from dynel/terminal indexes, existing indoor door tests and Tools/PfeHotLoopHarness. Index both endpoints; capture dictionary IDs/positions once per character tick pass; evaluate nearby candidates with strict XZ distance<3, irrespective of floor/Y as current behavior. Reconcile membership at heartbeat ownership boundary, preserving locked/manual-open behavior and avoiding stale membership after move/teleport/despawn/ID reuse. Audit removal/tick concurrency before changing lists. No production deploy or experiment, no unrelated stat/physics refactor. Acceptance: all existing indoor proximity and room/lock/entrance regressions pass; new boundary, mirror dedup, teleport/removal, churn/teardown and linear-ID-read/sparse-candidate tests verify invariants. Extend infrastructure-free harness with door workload and empirical baseline/after process CPU, operation and allocation evidence. Engine solution builds with warnings-as-errors in existing CI-compatible container; git diff --check clean. No commit or PR unless requested. Verified implementation - Worktree /home/mathias/Projects/worktrees/engines-indoor-doors, branch perf/index-indoor-door-proximity, base origin/develop383970a86. Primary repository remained untouched/clean. - Added DoorProximityIndex beside IndoorCellManagementPfSystem, reusing terminal/dynel immutable pre-expanded neighborhood pattern.8-unit XZ buckets with adjacent9 neighborhoods, strict squared distance<9, both copied endpoints deduplicated per candidate bucket. Y remains intentionally ignored; non-finite/out-of-range coordinates yield no candidates. Static topology constructed at setup; lazy count-based initialization supports existing synthetic fixtures. Lock changes do not require topology rebuild. - Owner door tick clears unlocked membership, visits each concurrent NPC/player dictionary once, uses dictionary key rather than stat-backed CharacterId, reads Position once, checks nearby indexed doors only, and reconciles open state. Full reconciliation removes stale memberships after teleports/removal even without lifecycle callback. Locked/manual-open doors retain original proximity exclusion/state behavior. Debug opening log uses door/count rather than dereferencing removed GetCharacter results. - Tick, removal callbacks and cleanup serialize on one door-state gate, without taking character TickLock. Late removal skips replacements with reused identity; actual last-occupant removal clears membership and closes unlocked doors. Cleanup closes processing gate and releases topology; later ticks/removals cannot repopulate. Validation - Red regression reproduced128 stat-derived ID reads for1 far NPC/128doors; green asserts zero ID-property reads and exactly one Position read. Added strict3-unit, negative bucket/Y-ignore, overlapping mirrors/no duplicates, teleport + missing-callback removal, locked/manual-open/unlock, late identity replacement, NaN/Infinity, sparse candidate and zero-allocation query tests; randomized20-tick100NPC/64two-sideddoor oracle matches former brute-force distance semantics.10k concurrent index readers validate copied topology.1k concurrent add/remove callbacks versus owner ticks + cleanup race pass. Player removal covered. - 19 focused door tests are included in88 broader door/indoor/entrance/exit-proxy/lockpick tests: all88 pass, zero skips/failures. Three-engine Release build with TreatWarningsAsErrors=true passes zero warnings/errors using navigation-ci-bookworm-validation:local. git diff --check passes. Existing broad unit-test project emits unrelated baseline warnings while building auxiliary web/UI/tools; no source-warning fixes included. - Extended existing PfeHotLoopHarness with doors scenario and README; calls production HandleDoorStates with synthetic playfield,2kNPCs/128two-sideddoors,128near NPCs, single-owner ticks, no services/listeners/game data/DB/RabbitMQ. Same1000tick shape before9.430s process CPU/9.418s elapsed, after0.100s CPU/0.102s elapsed (~94.3x/~98.94% kernel reduction locally), identical136,232 aggregate bytes, zero GC. Logical operations256m are avoided legacy door×character comparisons, not actual indexed checks. Three longer10k-tick runs use0.58/0.57/0.57s process CPU (57–58us/tick), p50~30.5–31.3us after tiered warmup; allocation remains136bytes/tick from concurrent dictionary enumerators/harness. No production-wide savings claim. - Evidence /tmp/indoor-doors-evidence/: doors-before/after JSON/text, three after-repeat JSON/text runs, smoke, red/green/broad test and engine build logs. Container-generated untracked Avalonia scratch directories cleaned; only five intended source/test/harness/doc files changed, including new index. No deployment. Pull request - #2592: perf: Index indoor door proximity checks - https://git.project-rk.com/prk/engines/pulls/2592 - Head perf/index-indoor-door-proximity -> develop. Ready for review; branch pushed with upstream, worktree clean. - Implementation commit b58ab5420; latest develop cab433782 merged without rewriting history as bf8c2013c. - Post-merge validation:129 relevant door/indoor/entrance/lockpick/Battlestation/PlayfieldHelper tests pass with zero skips/failures, TheThreeEngines Release warnings-as-errors build passes zero warnings/errors, production-shaped door harness passes, complete base diff limited to five intended files and git diff --check clean. Evidence /tmp/indoor-doors-evidence/pr-{engine-build,tests,doors}.txt. User /open-pr explicitly authorized committing and PR creation; task status unchanged.
Checklist
2 of 2 complete- Implement nearby-only door proximity with correct ownership; verify behavioral, boundary, removal/teleport and concurrency regressions.
- Measure before/after CPU and candidate scaling in shared harness; pass engine build, broader indoor/lock regressions and diff validation.
- Area
- Project Rubi-Ka
- Project
- engines
- Kind
- Feature
- Priority
- High
- Task ID
tsk_2ta52vjemp- Updated