In BTreeMap::eq, do not compare the elements if the sizes are different.#149125
Merged
bors merged 2 commits intorust-lang:mainfrom Dec 2, 2025
Merged
In BTreeMap::eq, do not compare the elements if the sizes are different.#149125bors merged 2 commits intorust-lang:mainfrom
BTreeMap::eq, do not compare the elements if the sizes are different.#149125bors merged 2 commits intorust-lang:mainfrom
Conversation
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Reverts #147101 in library/alloc/src/btree/
#147101 replaced some instances of code like
a.len() == b.len() && a.iter().eq(&b)with justa.iter().eq(&b), but the optimization that PR introduced only applies forTrustedLeniterators, andBTreeMap's itertors are notTrustedLen, so this theoretically regressed perf for comparing largeBTreeMap/BTreeSets with unequal lengths but equal prefixes, (and also made it so that comparing two different-lengthBTreeMap/BTreeSets with elements whosePartialEqimpls that can panic now can panic, though this is not a "promised" behaviour either way (cc #149122))Given that
TrustedLenis an unsafe trait, I opted to not implement it forBTreeMap's iterators, and instead just revert the change. If someone else wants to auditBTreeMap's iterators to make sure they always return the right number of items (even in the face of incorrect userOrdimpls) and then implementTrustedLenfor them so that the optimization works for them, then this can be closed in favor of that (or if the perf regression is deemed too theoretical, this can be closed outright).Example of theoretical perf regression: https://play.rust-lang.org/?version=beta&mode=release&edition=2024&gist=a37e3d61e6bf02669b251315c9a44fe2 (very rough estimates, using
Instant::elapsed).In release mode on stable the comparison takes ~23.68µs.
In release mode on beta/nightly the comparison takes ~48.351057ms.