perf: spatial hash grid, sqrt elimination, GC-free hot path#1
Open
OldCrow wants to merge 3 commits intotjards:masterfrom
Open
perf: spatial hash grid, sqrt elimination, GC-free hot path#1OldCrow wants to merge 3 commits intotjards:masterfrom
OldCrow wants to merge 3 commits intotjards:masterfrom
Conversation
Reduces per-frame complexity from O(n^2) to O(n) for bounded-density interactions, extending usable agent count from ~200 to 2000+. Performance: - Spatial hash grid (spatial-grid.js): O(1) neighbor queries replace O(n) full scans in herd and shepherd update loops - Squared distance comparisons: range checks avoid sqrt, only computed when normalizing direction vectors - GC pressure eliminated: pre-allocated limitMagnitude output, removed per-frame array spreads and precomputeOtherShepherds allocation Bug fixes: - Shepherd closest-herd-member search uses full list (not grid) to handle wrap-around boundaries correctly Additions: - .gitignore - OPTIMIZATION.md: problem statement, methodology, per-file changes, design decisions (spatial hash vs KD-tree, GC avoidance, batched rendering), measured results, future work - FPS counter overlay (dark background for visibility on light canvas) Measured results (144Hz monitor): - 144 FPS sustained to 1100 agents (vsync-capped) - ~100 FPS at 2100 agents (est. ~7x over original O(n^2)) No external dependencies. All vanilla JavaScript. Co-Authored-By: Oz <oz-agent@warp.dev>
Cantor pairing is only bijective for non-negative integers. query() could produce negative cell coordinates for agents near the top/left canvas edges, causing hash collisions with positive cells. This returned duplicate agents in results, multi-counting their forces. Clamp minCx/minCy to 0 since agents always wrap to non-negative positions—negative cells are guaranteed empty. Co-Authored-By: Oz <oz-agent@warp.dev>
Author
|
Pushed a bugfix in 5637394: the spatial grid's Cantor pairing hash is only bijective for non-negative integers, but Fix: clamp |
Co-Authored-By: Oz <oz-agent@warp.dev>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Reduces per-frame complexity from O(n²) to O(n) for bounded-density interactions, extending usable agent count from ~200 to 2000+ at 60fps.
Changes
spatial-grid.js): O(1) neighbor queries replace O(n) full scans in herd and shepherd update loopsMath.sqrt, only computed when normalizing direction vectorslimitMagnitudeIntooutput, removed per-frameprecomputeOtherShepherds()array allocation and[...herd.members]spread.gitignoreandOPTIMIZATION.md: documentation covering methodology, design decisions, measured results, and future workBug Fix
Shepherd closest-herd-member search uses full list scan (not spatial grid) to correctly handle wrap-around boundaries.
Measured Results (144Hz monitor)
No external dependencies. All vanilla JavaScript.
Co-Authored-By: Oz oz-agent@warp.dev