Skip to content

Use sparse entity-to-node mapping for ContactGraph#689

Merged
Jondolf merged 4 commits intomainfrom
sparse-entity-to-node
Apr 13, 2025
Merged

Use sparse entity-to-node mapping for ContactGraph#689
Jondolf merged 4 commits intomainfrom
sparse-entity-to-node

Conversation

@Jondolf
Copy link
Member

@Jondolf Jondolf commented Apr 2, 2025

Objective

The ContactGraph maintains an entity-to-node mapping using a custom EntityDataIndex. This is similar to the Coarena in Rapier or SecondaryMap in slotmap.

This works fine for physics entities. However, the structure's keys are the indices of the vector itself, which means that space is allocated for every entity from index 0 to the largest physics Èntity index. If you had a million non-physics entities, and one collider spawned later, it would also allocate space for those million non-physics entities.

Solution

Replace EntityDataIndex with a similar SparseSecondaryEntityMap, backed by a HashMap rather than a Vec. This is modeled after the SparseSecondaryMap in slotmap.

This does most likely make random access slightly slower, but reduces memory usage.

Alternative: ColliderId

We could keep the current data structure, but maintain collider IDs that are separate from Entity IDs.. This would let us only allocate for physics entities.

However, we would then need to maintain an Entity-to-ColliderId mapping (or query for ColliderId) at least to make the public API less painful. So, a sparse mapping is required regardless.

We can explore this option later to see if it would be any faster, but for simplicity, I would prefer to keep using Entity directly, and so only have the sparse mapping.

@Jondolf Jondolf added C-Performance Improvements or questions related to performance A-Collision Relates to the broad phase, narrow phase, colliders, or other collision functionality labels Apr 2, 2025
@Jondolf Jondolf added this to the 0.3 milestone Apr 2, 2025
nalgebra = { version = "0.33", features = ["convert-glam029"], optional = true }
serde = { version = "1", features = ["derive"], optional = true }
derive_more = "1"
arrayvec = "0.7"
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also noticed that this is unused in 3D

@Jondolf
Copy link
Member Author

Jondolf commented Apr 5, 2025

Using Entity IDs is actually also a problem for the SpatialQueryPipeline, so I'm looking into adding separate collider IDs and reworking this stuff further.

@Jondolf
Copy link
Member Author

Jondolf commented Apr 13, 2025

Or I'll actually merge this now and handle the SpatialQueryPipeline separately.

@Jondolf Jondolf enabled auto-merge (squash) April 13, 2025 10:38
@Jondolf Jondolf merged commit 0b7bba7 into main Apr 13, 2025
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-Collision Relates to the broad phase, narrow phase, colliders, or other collision functionality C-Performance Improvements or questions related to performance

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant