-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
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
Use immutable key for HashMap
and HashSet
#12086
Use immutable key for HashMap
and HashSet
#12086
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm for it. I think it's a direct improvement.
Could you replace all the Box::from(x)
and .into_boxed_string()
with x.into()
for the Box<str>
and Box<[T]>
?
It should work, practically all the relevant conversions are implemented https://doc.rust-lang.org/stable/alloc/boxed/struct.Box.html#impl-From%3CCow%3C'_,+str%3E%3E-for-Box%3Cstr%3E
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note that this does require us to more proactively use the entry APIs to avoid allocating while using these maps, which I'm not opposed to, but it is a potential ergonomics hazard.
I'm similarly concerned about needing to double allocate for many of these, since the conversion from String/Vec to Box does require a shrink_to_fit
reallocation.
674fee4
to
a26b436
Compare
…haoua/bevy into hash_map_set_immutable_key
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't see anywhere where we do allocations when we didn't before, so it looks good to me.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't see anywhere where we do allocations when we didn't before
We may be able to eliminate many of tthe allocations entirely by using entry_ref
where possible.
I made a few suggestions, but in reality, we can use this since the parameter to get
and entry_ref
take any reference that hash to the same result.
Co-authored-by: James Liu <contact@jamessliu.com>
Co-authored-by: James Liu <contact@jamessliu.com>
Co-authored-by: James Liu <contact@jamessliu.com>
Co-authored-by: James Liu <contact@jamessliu.com>
You added a new example but didn't add metadata for it. Please update the root Cargo.toml file. |
1 similar comment
You added a new example but didn't add metadata for it. Please update the root Cargo.toml file. |
You added a new example but didn't add metadata for it. Please update the root Cargo.toml file. |
1 similar comment
You added a new example but didn't add metadata for it. Please update the root Cargo.toml file. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for addressing those changes as a part of this PR.
# Objective Memory usage optimisation ## Solution `HashMap` and `HashSet`'s keys are immutable. So using mutable types like `String`, `Vec<T>`, or `PathBuf` as a key is a waste of memory: they have an extra `usize` for their capacity and may have spare capacity. This PR replaces these types by their immutable equivalents `Box<str>`, `Box<[T]>`, and `Box<Path>`. For more context, I recommend watching the [Use Arc Instead of Vec](https://www.youtube.com/watch?v=A4cKi7PTJSs) video. --------- Co-authored-by: James Liu <contact@jamessliu.com>
# Objective Memory usage optimisation ## Solution `HashMap` and `HashSet`'s keys are immutable. So using mutable types like `String`, `Vec<T>`, or `PathBuf` as a key is a waste of memory: they have an extra `usize` for their capacity and may have spare capacity. This PR replaces these types by their immutable equivalents `Box<str>`, `Box<[T]>`, and `Box<Path>`. For more context, I recommend watching the [Use Arc Instead of Vec](https://www.youtube.com/watch?v=A4cKi7PTJSs) video. --------- Co-authored-by: James Liu <contact@jamessliu.com>
Objective
Memory usage optimisation
Solution
HashMap
andHashSet
's keys are immutable. So using mutable types likeString
,Vec<T>
, orPathBuf
as a key is a waste of memory: they have an extrausize
for their capacity and may have spare capacity.This PR replaces these types by their immutable equivalents
Box<str>
,Box<[T]>
, andBox<Path>
.For more context, I recommend watching the Use Arc Instead of Vec video.