Skip to content

Reduce SpatialQueryPipeline overhead#696

Merged
Jondolf merged 2 commits into
mainfrom
reduce-spatial-query-overhead
Apr 13, 2025
Merged

Reduce SpatialQueryPipeline overhead#696
Jondolf merged 2 commits into
mainfrom
reduce-spatial-query-overhead

Conversation

@Jondolf

@Jondolf Jondolf commented Apr 13, 2025

Copy link
Copy Markdown
Member

Objective

The SpatialQueryPipeline currently has some egregious overhead for scenes with a large number of entities. It allocates an EntityHashMap with all colliders (Arced) and some other data, from scratch, every frame. Even worse, it allocates QBVH nodes for all entities up to the largest collider Entity index. For a scene with 999,999 non-physics entities and one collider, it seems to allocate a million QBVH nodes every frame. This produces profiles like this:

Really bad spatial query pipeline performance

Yikes!

Solution

Just store QBVH proxies in a Vec, and use those vec indices rather than Entity indices for QBVH building. This also lets us replace a bunch of hashmap lookups with vector indexing, get rid of some Entity conversions, and avoid table scans for Added<Collider>.

This is still kind of a duct-tape fix. We should ideally have separate BVHs for dynamic or kinematic and static or sleeping bodies, along with incremental updates. It would also be good if we didn't have to store all the duplicate collider data and just queried the ECS directly, assuming the query overhead is negligible. This will be worked on in the future.

Still, this seems to be much better!

Reduced overhead

@Jondolf Jondolf added C-Performance Improvements or questions related to performance A-Spatial-Query Relates to spatial queries, such as ray casting, shape casting, and intersection tests labels Apr 13, 2025
@Jondolf Jondolf added this to the 0.3 milestone Apr 13, 2025
@Jondolf Jondolf enabled auto-merge (squash) April 13, 2025 15:04
@Jondolf Jondolf merged commit 8553092 into main Apr 13, 2025
@Jondolf Jondolf deleted the reduce-spatial-query-overhead branch April 13, 2025 15:46
Jondolf added a commit that referenced this pull request May 6, 2025
# Objective

Fixes #720.

#696 accidentally regressed `SpatialQueryPipeline::shape_intersections_callback`, mixing up the order of the input shapes.

## Solution

Fix the order!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-Spatial-Query Relates to spatial queries, such as ray casting, shape casting, and intersection tests C-Performance Improvements or questions related to performance

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant