Skip to content

implement EntityIndexMap/Set#17449

Merged
alice-i-cecile merged 2 commits intobevyengine:mainfrom
Victoronz:entity-index-set-and-map
Jan 24, 2025
Merged

implement EntityIndexMap/Set#17449
alice-i-cecile merged 2 commits intobevyengine:mainfrom
Victoronz:entity-index-set-and-map

Conversation

@Victoronz
Copy link
Copy Markdown
Contributor

@Victoronz Victoronz commented Jan 20, 2025

Objective

We do not have EntityIndexMap/EntityIndexSet.

Usual HashMaps/HashSets do not guarantee any order, which can be awkward for some use cases.
The indexmap versions remember insertion order, which then also becomes their iteration order.
They can be thought of as a HashTable + Vec, which means fast iteration and removal, indexing by index (not just key), and slicing!
Performance should otherwise be comparable.

Solution

Because indexmap is structured to mirror hashbrown, it suffers the same issue of not having the Hasher generic on their iterators. #16912 solved this issue for EntityHashMap/EntityHashSet with a wrapper around the hashbrown version, so this PR does the same.

Hopefully these wrappers can be removed again in the future by having hashbrown/indexmap adopt that generic in their iterators themselves!

@alice-i-cecile alice-i-cecile added A-ECS Entities, components, systems, and events D-Straightforward Simple bug fixes and API improvements, docs, test and examples S-Needs-Review Needs reviewer attention (from anyone!) to move forward labels Jan 20, 2025
@alice-i-cecile
Copy link
Copy Markdown
Member

As discussed on Discord, we should adopt #17447 to apply to the EntityIndexMap type once this is merged.

@Victoronz
Copy link
Copy Markdown
Contributor Author

Victoronz commented Jan 20, 2025

Note: this currently only includes slicing via deref. It'd put this PR well over a thousand lines to add the wrappers for those as well, so I'll do that as a follow-up.
This means some types here seem useless to wrap: In actuality, they can be sliced, which in turn are able to then return EntitySetIterators.

@BenjaminBrienen BenjaminBrienen added the C-Feature A new feature, making something new possible label Jan 20, 2025
github-merge-queue bot pushed a commit that referenced this pull request Jan 20, 2025
…17450)

# Objective

Noticed while doing #17449, I had left these `DerefMut` impls in.
Obtaining mutable references to those inner iterator types allows for
`mem::swap`, which can be used to swap an incorrectly behaving instance
into the wrappers.

## Solution

Remove them!
@BenjaminBrienen BenjaminBrienen added S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it and removed S-Needs-Review Needs reviewer attention (from anyone!) to move forward labels Jan 20, 2025
@alice-i-cecile
Copy link
Copy Markdown
Member

Actually, before I merge this, can you say a bit more on the motivation here? Adding code like this isn't free, and we don't have any internal users currently.

@alice-i-cecile alice-i-cecile added S-Waiting-on-Author The author needs to make changes or address concerns before this can be merged and removed S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it labels Jan 24, 2025
@Victoronz
Copy link
Copy Markdown
Contributor Author

Actually, before I merge this, can you say a bit more on the motivation here? Adding code like this isn't free, and we don't have any internal users currently.

Fair, I've added some more extensive motivation.

@alice-i-cecile alice-i-cecile added S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it and removed S-Waiting-on-Author The author needs to make changes or address concerns before this can be merged labels Jan 24, 2025
@alice-i-cecile alice-i-cecile added this pull request to the merge queue Jan 24, 2025
Merged via the queue into bevyengine:main with commit 39a1e2b Jan 24, 2025
mrchantey pushed a commit to mrchantey/bevy that referenced this pull request Feb 4, 2025
…evyengine#17450)

# Objective

Noticed while doing bevyengine#17449, I had left these `DerefMut` impls in.
Obtaining mutable references to those inner iterator types allows for
`mem::swap`, which can be used to swap an incorrectly behaving instance
into the wrappers.

## Solution

Remove them!
mrchantey pushed a commit to mrchantey/bevy that referenced this pull request Feb 4, 2025
# Objective

We do not have `EntityIndexMap`/`EntityIndexSet`.

Usual `HashMap`s/`HashSet`s do not guarantee any order, which can be
awkward for some use cases.
The `indexmap` versions remember insertion order, which then also
becomes their iteration order.
They can be thought of as a `HashTable` + `Vec`, which means fast
iteration and removal, indexing by index (not just key), and slicing!
Performance should otherwise be comparable.

## Solution

Because `indexmap` is structured to mirror `hashbrown`, it suffers the
same issue of not having the `Hasher` generic on their iterators. bevyengine#16912
solved this issue for `EntityHashMap`/`EntityHashSet` with a wrapper
around the hashbrown version, so this PR does the same.

Hopefully these wrappers can be removed again in the future by having
`hashbrown`/`indexmap` adopt that generic in their iterators themselves!
github-merge-queue bot pushed a commit that referenced this pull request Mar 17, 2025
# Objective

Continuation of #17449.

#17449 implemented the wrapper types around `IndexMap`/`Set` and co.,
however punted on the slice types.
They are needed to support creating `EntitySetIterator`s from their
slices, not just the base maps and sets.

## Solution

Add the wrappers, in the same vein as #17449 and #17589 before.

The `Index`/`IndexMut` implementations take up a lot of space, however
they cannot be merged because we'd then get overlaps.

They are simply named `Slice` to match the `indexmap` naming scheme, but
this means they cannot be differentiated properly until their modules
are made public, which is already a follow-up mentioned in #17954.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-ECS Entities, components, systems, and events C-Feature A new feature, making something new possible D-Straightforward Simple bug fixes and API improvements, docs, test and examples S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants