Skip to content

Reduce repeated spatial grid cell calculations - #288

Closed
fderop wants to merge 1 commit into
cortex-command-community:developmentfrom
fderop:perf/dense-collision-grid
Closed

fderop wants to merge 1 commit into
cortex-command-community:developmentfrom
fderop:perf/dense-collision-grid

Conversation

@fderop

@fderop fderop commented Oct 2, 2026 •

Copy link
Copy Markdown

Grid registration recalculated each cell ID for every included team, then again for the used-cell set.
Add now loops over cells first and shares each calculated ID across eligible teams and the used-cell set.
Queries also skip modulo when coordinates are already inside the grid:

if (cellX >= 0 && cellX < m_Width && cellY >= 0 && cellY < m_Height) {
	return (cellY * m_Width) + cellX;
}

Out-of-range coordinates retain the original wrapping path.
The 20-pixel cell size, team filters, duplicate entries, and per-cell insertion order remain unchanged.
Exact candidate-vector and pixel-query checks pass across wrapping, teams, rotations, flips, and scales.

Three process pairs show 27.2% less dense lookup time, 45.5% less dense rebuild time, and 4.5% less dense pixel-query time.
Sparse pixel queries regress 1.4%, or 0.022 ms per 300,000 calls.
Gameplay mean simulation time improves about 1.0–1.4%, with matching particle sequences, but those gains overlap process variation.
The stock scenario's maximum tick also worsens slightly. These results do not establish a general FPS or latency gain.

Only SpatialPartitionGrid.cpp changes in production. Correctness checks, raw results, and reproduction instructions.

@fderop
fderop marked this pull request as ready for review October 2, 2026 18:28
@Causeless

Copy link
Copy Markdown
Contributor

Really the main note from this PR is just "These results do not establish a general FPS or latency gain" - these changes could theoretically be better in performance (barely), but this doesn't feel proper. For example, the entire grid modulo thing can be rewritten to just remove the possibility of branch misprediction whatsoever, if we want to be proper about this.

The reason things were originally written this way is ultimately because it's not going to make much end difference to the gameplay experience. Benchmarking that something is 5% faster when that cost is already only 0.01% of the frametime is an irrelevant metric- especially when even despite these optimizations the LLM's own generated benchmarks concede that the worst-case tick times are higher.

The LLM also states that spatial queries appear slower It's worth noting that the grid construction is done in a background thread, whereas the spatial queries are performed in the main sim thread. So- if we take the AI's description at face value- this change slows the game down, ultimately, not speeds it up.

Overall this looks like there's basically zero real difference in the end, so I don't see this as being a valuable change in it's current state.

@fderop

fderop commented Oct 3, 2026

Copy link
Copy Markdown
Author

you're right, opened this pr prematurely, closing

@fderop fderop closed this Oct 3, 2026
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.

2 participants