Repository navigation
Make dropping an empty BTreeMap free #161791
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
Merged
rust-bors
merged 7 commits into
rust-lang:main
from
jakedrew:fix/Making_and_dropping_an_empty_BTreeMap_is_not_free_161375
Sep 26, 2026
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
0109ed2
BTree: make dropping an empty BTreeMap free
jakedrew 756abe8
Adding codegen for the empty BTree drop
jakedrew 15831b0
BTree: documenting the unsafe in the empty map drop
jakedrew 70ff620
Check for dealloc instead of dying_next
jakedrew 4cf035f
Checking that an empty BTreeMap drop compiles down to only a `ret void`,
jakedrew 9c77e1e
Adding safety comment on the unsafe in BTreeMap drop
jakedrew d432494
Comment that `dying_next` is too costly to inline
jakedrew File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| // Regression test for https://github.com/rust-lang/rust/issues/161375 | ||
| // Checking that an empty BTree's drop is optimized away. | ||
| //@ compile-flags: -Copt-level=3 | ||
|
|
||
| #![crate_type = "lib"] | ||
|
|
||
| use std::collections::BTreeMap; | ||
|
|
||
| // CHECK-LABEL: @drop_btree( | ||
| // CHECK-NEXT: {{.*}}: | ||
| // CHECK-NEXT: ret void | ||
| #[no_mangle] | ||
| pub fn drop_btree() { | ||
| let _ = BTreeMap::<(), ()>::new(); | ||
| } |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.
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 guess that part of my confusion here is why this isn't sufficiently optimized into a no-op for an empty tree, and hence needed at all.
I figure it makes sense just due to the combined complexity of the function, but is there maybe other detail we could add here to make that clear, so maybe this could be fixed later?
View changes since the review
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.
Yes so at
-Copt-level=3inlining doesn’t occur because it’stoo costly to inline (cost=850, threshold=250), and setting inline-threshold to 2000 does inline to justret void.I’ll add comment to explain this.
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.
Would
inline(always)on some of the internal methods make the difference?Mostly trying to see if we can potentially circumvent the need for this.
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.
Adding
inline(always)ondying_nextdoes fix the empty drop without the root check.But the catch is that
IntoIter::nextusesdying_nexttoo, so it gets inlined there as well.With a small test:
At
-Copt-level=3this function grows from 165 to 429 lines of IR.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.
Yup, seems definitely not worth it. Thank you for the extra research; I figured this was the right choice, just, wanted to make extra sure.