Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Query::get_multiple for accessing multiple entities simultaneously from the same query #2276

Closed
alice-i-cecile opened this issue May 29, 2021 · 6 comments
Labels
A-ECS Entities, components, systems, and events C-Feature A new feature, making something new possible S-Ready-For-Implementation This issue is ready for an implementation PR. Go for it!

Comments

@alice-i-cecile
Copy link
Member

What problem does this solve or what need does it fill?

When working with queries, you sometimes need to access the data on multiple components at once. This is particularly frustrating if you require mutable access, due to Rust's borrow checking rules.

What solution would you like?

Add the Query::get_multiple(entities: HashSet<Entity>) -> HashMap<Entity, Result<[Elided](https://docs.rs/bevy/0.5.0/bevy/ecs/system/struct.Query.html#method.get_mut)>> method and the associated get_multiple_mut. By passing in a HashSet of entities, we can ensure that we never have mutable access to the entities at the same time.

Also add get_component analogues for this pattern.

What alternative(s) have you considered?

iter_combinations solves the common case, but other more bespoke access patterns certainly exist.

You can work around this by storing data, but that only works for Clone components.

@TheRawMeatball felt this should use a different API / impl:

it'd be more like a wrapper you put the query into
and then it'd pass out smart pointers wrappers of smart pointers

Additional context

#1470 may be easier to work around with this style of API.

It may make sense to use this implementation for iter_combinations in some way, but be mindful of perf as iter_combinations is likely to be used in tight loops for things like collision detection.

@alice-i-cecile alice-i-cecile added C-Feature A new feature, making something new possible A-ECS Entities, components, systems, and events labels May 29, 2021
@alice-i-cecile
Copy link
Member Author

Unsurprisingly, a parallel API for direct world access is probably a good idea too.

@alice-i-cecile alice-i-cecile changed the title Query::get_multiple for accessing multiple entities simultaneously from the same entity Query::get_multiple for accessing multiple entities simultaneously from the same query Aug 4, 2021
@alice-i-cecile
Copy link
Member Author

rust-lang/rfcs#3100 treads similar ground, creating a trait for this style of functionality in the standard library. We could migrate this functionality to use that trait later once it's stabilized.

@alice-i-cecile
Copy link
Member Author

Duplicate of #2042.

@burdges
Copy link

burdges commented Jun 20, 2024

Just fyi, if you really really want access to multiple entries, then you might consider a cuckoo table. Those could've some index method which finds the positions for the key, and then you access those positions directly as if they were a Vec. Any write accesses could bump buckets contents of course, but only into a few other buckets, whic the index still locates. I've long meant to write a cuckoo table crate, because there are a bunch of really cool optimizations, but not yet done so.

@alice-i-cecile
Copy link
Member Author

Really neat data structure! Would the main benefits be performance?

@burdges
Copy link

burdges commented Jun 20, 2024

I'm unsure how performance works out, usually cuckoo filters wind up more memory efficent. I think few people use them because all the optimizations look more subtle than hash tables.

Afaik they work fine with the same good hashers and hash tables, like SipHash or HashBrown, but they're more sensitive to bad hashers, and some devs love plugging in bad hasher for speed.

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 S-Ready-For-Implementation This issue is ready for an implementation PR. Go for it!
Projects
None yet
Development

No branches or pull requests

2 participants