Skip to content

Accelerate repeated spatial unit queries - #2

Closed
dgant wants to merge 2 commits into
developfrom
codex/cache-unit-queries
Closed

dgant wants to merge 2 commits into
developfrom
codex/cache-unit-queries

Conversation

@dgant

@dgant dgant commented Oct 29, 2026 •

Copy link
Copy Markdown
Owner

Repeated local unit searches scan every accessible unit on each call. This change adds a lazy spatial index for radius, rectangle, tile, and closest-unit queries while preserving result order, predicate order, closest-unit ties, and the existing geometry semantics.

The first local query and broad searches use a linear scan. Repeated local queries share the index until the unit snapshot or a unit position changes. Bounds and filters are evaluated at query time so latency-compensated type changes remain visible. Closest-unit queries avoid an intermediate list, and maximum unit dimensions are computed once for the spatial bounds.

This PR changes four production files and one spatial regression test file. Player, resource, neutral, pylon, bullet, larva, Interceptor, and transport query implementations remain as upstream. It introduces no public API or dependency changes.

Validation on commit 9304f3f:

  • Full Maven package on Windows Java 8u171 x86: 58 tests, zero failures/errors, one intentional skip for the non-Windows native-loader test.
  • A full SC-Docker/Wine game versus Stardust on Fighting Spirit, seed 1101, completed normally: PurpleWave won after 33,668 frames with no crash or game/realtime timeout. The previously tested standalone-cache PurpleWave bot used all 149 JBWAPI/BWEM/BWTA runtime classes from this exact Maven build; bot logic and dependencies were unchanged.
  • Both Maven CI runs and CodeQL passed on this commit.
  • Spatial regression tests cover indexed-versus-linear results across all unit types, ordering and ties, snapshot changes, event position updates, latency-compensated type changes, and coordinate limits.
  • A bounded probe using 1,000 real Unit objects matched the upstream linear radius-query implementation in all 20,000 queries, including result order.
  • Timings below use radius 128 on a 4,096-pixel square, 200 fresh unit snapshots per run, eight warmup pairs, and the median of nine alternating measured pairs. Index rebuild cost is included. The Java 8 x86 heap was fixed at 768 MB.
Queries per frame Linear total Indexed total Change in query time
1 2.039 ms 2.173 ms 6.6% slower; about 0.7 microseconds per call
10 20.912 ms 13.261 ms 36.6% less
100 245.812 ms 37.067 ms 84.9% less

These are synthetic query timings, not a demonstrated PurpleWave game-speed improvement. The improvement applies to repeated local searches; single-query workloads pay a small dispatch cost.

@dgant dgant changed the title Cache repeated unit queries Accelerate repeated spatial unit queries Nov 2, 2026
@dgant dgant closed this Nov 4, 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.

1 participant