-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
Implement clone_from for BTreeMap and BTreeSet #66648
Conversation
(rust_highfive has picked a reviewer for you, use r? to override) |
@bors try @rust-timer queue |
Awaiting bors try build completion |
Implement clone_from for BTreeMap and BTreeSet See #28481. This results in up to 90% speedups on simple data types when `self` and `other` are the same size, and is generally comparable or faster. Some concerns: 1. This implementation requires an `Ord` bound on the `Clone` implementation for `BTreeMap` and `BTreeSet`. Since these structs can only be created externally for keys with `Ord` implemented, this should be fine? If not, there's certainly a less safe way to do this. 2. Changing `next_unchecked` on `RangeMut` to return mutable key references allows for replacing the entire overlapping portion of both maps without changing the external interface in any way. However, if `clone_from` fails it can leave the `BTreeMap` in an invalid state, which might be unacceptable. This probably needs an FCP since it changes a trait bound, but (as far as I know?) that change cannot break any external code.
☀️ Try build successful - checks-azure |
Queued 633e948 with parent 5fa0af2, future comparison URL. |
Ping from triage: |
Pinging again from triage: |
☔ The latest upstream changes (presumably #67540) made this pull request unmergeable. Please resolve the merge conflicts. |
b104b9a
to
a576335
Compare
a576335
to
8651aa0
Compare
This has been rebased and cleaned up to account for the formatting. Is there any interest in merging it? I believe |
r? @KodrAus |
@KodrAus any updates? |
r? @Amanieu |
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 your patience @crgl!
I've just left a few comments to start, but have one question about the safety of mutating keys within the map.
None | ||
} | ||
} { | ||
self.split_off(&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 feel that a lot of duplicate work being done here. We first find the nth key, the pass it to split_off
so that it can find the position of that key (again!), after which it actually splits the tree. Would it be possible to avoid doing the key lookup twice?
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.
Not that I can see, unfortunately! However, split_off
doesn't exactly find the key and then split the tree. Because split_off
has a target key, it can split the tree as it's searching for the key position on the edges it descends on. In theory it would be better to have a split_after
function that splits off the first n keys, but in practice the BTree
doesn't have any subtree size information available (and traverses the whole tree every time it needs to recalculate length as a result). Similarly, it would definitely be more efficient if nth
was specialized for BTree
iterators, but I don't see an easy way to do that. Please let me know if you have any ideas about how to avoid iterating through (up to) half the tree!
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.
My point is that you are traversing the tree twice: once in the nth
call in the iterator, and once in split_off
. However fixing this is likely to be very complicated, so it's fine to leave it as it is for now.
r=me if @KodrAus has no other comments to make |
@bors r=Amanieu rollup |
📌 Commit 6c3e477 has been approved by |
Implement clone_from for BTreeMap and BTreeSet See rust-lang#28481. This results in up to 90% speedups on simple data types when `self` and `other` are the same size, and is generally comparable or faster. Some concerns: 1. This implementation requires an `Ord` bound on the `Clone` implementation for `BTreeMap` and `BTreeSet`. Since these structs can only be created externally for keys with `Ord` implemented, this should be fine? If not, there's certainly a less safe way to do this. 2. Changing `next_unchecked` on `RangeMut` to return mutable key references allows for replacing the entire overlapping portion of both maps without changing the external interface in any way. However, if `clone_from` fails it can leave the `BTreeMap` in an invalid state, which might be unacceptable. ~This probably needs an FCP since it changes a trait bound, but (as far as I know?) that change cannot break any external code.~
Rollup of 6 pull requests Successful merges: - #66648 (Implement clone_from for BTreeMap and BTreeSet) - #68468 (BTreeMap: tag and explain unsafe internal functions or assert preconditions) - #68626 (Use termize instead of term_size) - #68640 (Document remaining undocumented `From` implementations for IPs) - #68651 (Document `From` implementation for NonZero nums) - #68655 (Fix revision annotations in borrowck-feature-nll-overrides-migrate) Failed merges: r? @ghost
See #28481. This results in up to 90% speedups on simple data types when
self
andother
are the same size, and is generally comparable or faster. Some concerns:Ord
bound on theClone
implementation forBTreeMap
andBTreeSet
. Since these structs can only be created externally for keys withOrd
implemented, this should be fine? If not, there's certainly a less safe way to do this.next_unchecked
onRangeMut
to return mutable key references allows for replacing the entire overlapping portion of both maps without changing the external interface in any way. However, ifclone_from
fails it can leave theBTreeMap
in an invalid state, which might be unacceptable.This probably needs an FCP since it changes a trait bound, but (as far as I know?) that change cannot break any external code.