-
Notifications
You must be signed in to change notification settings - Fork 13.9k
Explicitly forget the zero remaining elements in vec::IntoIter::fold().
#148486
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
Open
kpreid
wants to merge
2
commits into
rust-lang:master
Choose a base branch
from
kpreid:vec-iter-drop
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+115
−6
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
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,67 @@ | ||
| //@ compile-flags: -Copt-level=3 | ||
| // Test that we can avoid generating an element dropping loop when `vec::IntoIter` is consumed. | ||
| #![crate_type = "lib"] | ||
|
|
||
| use std::vec; | ||
|
|
||
| struct Bomb; | ||
| impl Drop for Bomb { | ||
| #[inline] | ||
| fn drop(&mut self) { | ||
| panic!("dropped") | ||
| } | ||
| } | ||
|
|
||
| /// Test case originally from https://users.rust-lang.org/t/unnecessary-drop-in-place-emitted-for-a-fully-consumed-intoiter/135119 | ||
| /// | ||
| /// What we are looking for is that there should be no calls to `impl Drop for Bomb` | ||
| /// because every element is unconditionally forgotten. | ||
| // | ||
| // CHECK-LABEL: @vec_for_each_doesnt_drop | ||
| #[no_mangle] | ||
| pub fn vec_for_each_doesnt_drop(v: vec::Vec<(usize, Option<Bomb>)>) -> usize { | ||
| // CHECK-NOT: panic | ||
| // CHECK-NOT: {{call.*drop_in_place}} | ||
| // CHECK-NOT: Bomb$u20$as$u20$core..ops..drop..Drop | ||
| let mut last = 0; | ||
| v.into_iter().for_each(|(x, bomb)| { | ||
| last = x; | ||
| std::mem::forget(bomb); | ||
| }); | ||
| last | ||
| } | ||
|
|
||
| /// Test that does *not* get the above optimization we are expecting: | ||
| /// it uses a normal `for` loop which calls `Iterator::next()` and then drops the iterator, | ||
| /// and dropping the iterator drops remaining items. | ||
| /// | ||
| /// This test exists to prove that the above CHECK-NOT is looking for the right things. | ||
| /// However, it might start failing if LLVM figures out that there are no remaining items. | ||
| // | ||
| // CHECK-LABEL: @vec_for_loop | ||
| #[no_mangle] | ||
| pub fn vec_for_loop(v: vec::Vec<(usize, Option<Bomb>)>) -> usize { | ||
| // CHECK: {{call.*drop_in_place}} | ||
| let mut last = 0; | ||
| for (x, bomb) in v { | ||
| last = x; | ||
| std::mem::forget(bomb); | ||
| } | ||
| last | ||
| } | ||
|
|
||
| /// Test where there still should be drops because there are no forgets. | ||
| /// | ||
| /// This test exists to prove that the above CHECK-NOT is looking for the right things | ||
| /// and does not say anything interesting about codegen itself. | ||
| // | ||
| // CHECK-LABEL: @vec_for_each_does_drop | ||
| #[no_mangle] | ||
| pub fn vec_for_each_does_drop(v: vec::Vec<(usize, Option<Bomb>)>) -> usize { | ||
| // CHECK: begin_panic | ||
| let mut last = 0; | ||
| v.into_iter().for_each(|(x, bomb)| { | ||
| last = x; | ||
| }); | ||
| last | ||
| } | ||
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.
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.
suggestion:
CHECK-NOTtests are generally quite fragile, being prone to accidentally checking for something that never existed at all, or having the implementation in the standard library change such that a previously-useful test no longer does anything useful.Thus I would suggest that whatever you're checking for here you also write another function that intentionally does trigger whatever you expect to not be there in this one, with positive
CHECKs for the same things that areCHECK-NOTs here.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.
Good point. However, I encounter a problem: I add more tests to this file and LLVM decides that they should call
drop_in_place::<IntoIter>()instead of inlining it, defeating the entire point of this change. That’s a sign that this optimization is fragile and more work is needed, I guess. (Or maybe the perf results will show that it’s good despite that.)I am now trying to write more explicit code that doesn’t rely on inlining to help.
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’ve pushed a new commit 5fa286d that performs explicit forgetting instead of relying on inlining and dead code elimination to achieve anything.