SpatialHash Implementation #115

Closed
opened 2025-07-12 19:03:00 +00:00 by john · 2 comments
Owner

Implement spatial hashing for efficient queries on 10,000+ entities.

Definition of Done:

  • O(1) average case entity lookup by position
  • Handles entity movement efficiently
  • Scales to 10,000+ entities on 1000x1000 grids
  • Benchmark showing performance improvement

Wiki References:

Implement spatial hashing for efficient queries on 10,000+ entities. **Definition of Done:** - O(1) average case entity lookup by position - Handles entity movement efficiently - Scales to 10,000+ entities on 1000x1000 grids - Benchmark showing performance improvement **Wiki References:** - [Performance and Profiling](https://gamedev.ffwf.net/gitea/john/McRogueFace/wiki/Performance-and-Profiling) - [Performance Optimization Workflow](https://gamedev.ffwf.net/gitea/john/McRogueFace/wiki/Performance-Optimization-Workflow) - [Entity Management](https://gamedev.ffwf.net/gitea/john/McRogueFace/wiki/Entity-Management)
Author
Owner

Benchmark Results (commit fcc0376)

Added tests/benchmarks/entity_scale_benchmark.py to establish baseline metrics.

Current Performance (2,000 entities on 100×100 grid)

Operation Time Scaling
Range Query 0.05ms O(n) - checks all 2,000 entities
N×N Visibility 128ms O(n²) - 0.064ms × 2,000 queries

Cost Per Entity Checked

0.052ms / 2000 entities = 26ns per entity

Extrapolation to 10,000 Entities

  • Single query: 10,000 × 26ns = 0.26ms
  • N×N (all-to-all): 10,000 × 0.26ms = 2,600ms (2.6 seconds)

Projected Gains with SpatialHash

Scenario Current With SpatialHash Speedup
Single query (10k entities) 0.26ms (checks 10k) ~0.005ms (checks ~20) 50×
N×N visibility (10k entities) 2,600ms ~50ms 50×

Mechanism: With bucket size 32 and query radius 15, we check ~16 buckets × ~5 entities = ~80 entities instead of 10,000.

Frame Budget Impact

For 100 AI entities doing visibility queries per frame:

  • Current: 26ms (exceeds 16.67ms budget)
  • With SpatialHash: 0.5ms (3% of budget)

This is the higher-priority optimization for enabling complex AI at scale.

## Benchmark Results (commit fcc0376) Added `tests/benchmarks/entity_scale_benchmark.py` to establish baseline metrics. ### Current Performance (2,000 entities on 100×100 grid) | Operation | Time | Scaling | |-----------|------|---------| | Range Query | 0.05ms | O(n) - checks all 2,000 entities | | N×N Visibility | 128ms | O(n²) - 0.064ms × 2,000 queries | ### Cost Per Entity Checked ``` 0.052ms / 2000 entities = 26ns per entity ``` ### Extrapolation to 10,000 Entities - **Single query**: 10,000 × 26ns = **0.26ms** - **N×N (all-to-all)**: 10,000 × 0.26ms = **2,600ms (2.6 seconds)** ### Projected Gains with SpatialHash | Scenario | Current | With SpatialHash | Speedup | |----------|---------|------------------|---------| | Single query (10k entities) | 0.26ms (checks 10k) | ~0.005ms (checks ~20) | **50×** | | N×N visibility (10k entities) | 2,600ms | ~50ms | **50×** | **Mechanism**: With bucket size 32 and query radius 15, we check ~16 buckets × ~5 entities = ~80 entities instead of 10,000. ### Frame Budget Impact For 100 AI entities doing visibility queries per frame: - **Current**: 26ms (exceeds 16.67ms budget) - **With SpatialHash**: 0.5ms (3% of budget) This is the higher-priority optimization for enabling complex AI at scale.
john closed this issue 2025-12-28 05:44:17 +00:00
Author
Owner

SpatialHash Implementation Complete

Implemented in commit 7d57ce2.

New API

# Query entities within radius using spatial hash
nearby = grid.entities_in_radius(x, y, radius)

Benchmark Results

Entities Query O(n) Query SpatialHash Speedup
100 0.025ms 0.003ms 8.7×
500 0.035ms 0.005ms 7.0×
1,000 0.040ms 0.004ms 9.0×
2,000 0.044ms 0.003ms 16.2×

N×N Visibility (full grid)

Entities O(n) approach SpatialHash Speedup
100 1ms 0ms 20×
500 7ms 0ms 39×
1,000 33ms 1ms 36×
2,000 74ms 1ms 105×

Implementation Details

  • Bucket size: 32 cells (configurable)
  • Uses weak_ptr to avoid keeping deleted entities
  • Automatic hash updates on entity position changes
  • O(1) insert/update, O(k) query where k = nearby entities
## SpatialHash Implementation Complete Implemented in commit 7d57ce2. ### New API ```python # Query entities within radius using spatial hash nearby = grid.entities_in_radius(x, y, radius) ``` ### Benchmark Results | Entities | Query O(n) | Query SpatialHash | Speedup | |----------|------------|-------------------|---------| | 100 | 0.025ms | 0.003ms | 8.7× | | 500 | 0.035ms | 0.005ms | 7.0× | | 1,000 | 0.040ms | 0.004ms | 9.0× | | 2,000 | 0.044ms | 0.003ms | **16.2×** | ### N×N Visibility (full grid) | Entities | O(n) approach | SpatialHash | Speedup | |----------|---------------|-------------|---------| | 100 | 1ms | 0ms | 20× | | 500 | 7ms | 0ms | 39× | | 1,000 | 33ms | 1ms | 36× | | 2,000 | 74ms | 1ms | **105×** | ### Implementation Details - Bucket size: 32 cells (configurable) - Uses weak_ptr to avoid keeping deleted entities - Automatic hash updates on entity position changes - O(1) insert/update, O(k) query where k = nearby entities
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
john/McRogueFace#115
No description provided.