Skip to content
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

Don't use mem::zeroed in vec::IntoIter #120952

Merged
merged 1 commit into from
Feb 17, 2024
Merged

Conversation

saethlin
Copy link
Member

@saethlin saethlin commented Feb 11, 2024

mem::zeroed is not a trivial function. Maybe it was once, but now it involves multiple locals, copies, and an intrinsic that gets monomorphized into a call to panic_nounwind for iterators of types like Vec<&T>. Of course all that complexity is trivially optimized out, but generating a bunch of IR where we don't need to just so we can optimize it away later is silly.

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Feb 11, 2024
@saethlin
Copy link
Member Author

@bors try @rust-timer queue

@rust-timer

This comment has been minimized.

@rustbot rustbot added the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Feb 11, 2024
bors added a commit to rust-lang-ci/rust that referenced this pull request Feb 11, 2024
Don't use mem::zeroed in vec::IntoIter

r? `@ghost`
@bors
Copy link
Contributor

bors commented Feb 11, 2024

⌛ Trying commit 35a9a09 with merge 218b423...

@bors
Copy link
Contributor

bors commented Feb 11, 2024

☀️ Try build successful - checks-actions
Build commit: 218b423 (218b423933a990ea9b14a658106cbc7180a2ac1b)

1 similar comment
@bors
Copy link
Contributor

bors commented Feb 11, 2024

☀️ Try build successful - checks-actions
Build commit: 218b423 (218b423933a990ea9b14a658106cbc7180a2ac1b)

@rust-timer

This comment has been minimized.

@rust-timer
Copy link
Collaborator

Finished benchmarking commit (218b423): comparison URL.

Overall result: no relevant changes - no action needed

Benchmarking this pull request likely means that it is perf-sensitive, so we're automatically marking it as not fit for rolling up. While you can manually mark this PR as fit for rollup, we strongly recommend not doing so since this PR may lead to changes in compiler perf.

@bors rollup=never
@rustbot label: -S-waiting-on-perf -perf-regression

Instruction count

This benchmark run did not return any relevant results for this metric.

Max RSS (memory usage)

Results

This is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
-6.1% [-9.6%, -2.2%] 5
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) -6.1% [-9.6%, -2.2%] 5

Cycles

Results

This is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
2.2% [2.2%, 2.2%] 1
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) - - 0

Binary size

Results

This is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.

mean range count
Regressions ❌
(primary)
0.1% [0.0%, 0.3%] 3
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
-0.1% [-0.4%, -0.0%] 31
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) -0.1% [-0.4%, 0.3%] 34

Bootstrap: 664.283s -> 663.002s (-0.19%)
Artifact size: 308.29 MiB -> 308.29 MiB (-0.00%)

@rustbot rustbot removed the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Feb 12, 2024
@saethlin
Copy link
Member Author

@bors try @rust-timer queue

@rust-timer

This comment has been minimized.

@rustbot rustbot added the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Feb 12, 2024
@bors
Copy link
Contributor

bors commented Feb 12, 2024

⌛ Trying commit 78a4d71 with merge 0183a22...

bors added a commit to rust-lang-ci/rust that referenced this pull request Feb 12, 2024
Don't use mem::zeroed in vec::IntoIter

`mem::zeroed` is not a trivial function. Maybe it was once, but now it involves multiple locals, copies, and an intrinsic that gets monomorphized into a call to `panic_nounwind` for iterators of types like `Vec<&T>`. Of course all that complexity is trivially optimized out, but generating a bunch of IR where we don't need to just so we can optimize it away later is silly.

r? `@ghost`
@bors
Copy link
Contributor

bors commented Feb 12, 2024

☀️ Try build successful - checks-actions
Build commit: 0183a22 (0183a224a76c9b4106c34175108ff9a671c6dc52)

@rust-timer

This comment has been minimized.

@rust-timer
Copy link
Collaborator

Finished benchmarking commit (0183a22): comparison URL.

Overall result: no relevant changes - no action needed

Benchmarking this pull request likely means that it is perf-sensitive, so we're automatically marking it as not fit for rolling up. While you can manually mark this PR as fit for rollup, we strongly recommend not doing so since this PR may lead to changes in compiler perf.

@bors rollup=never
@rustbot label: -S-waiting-on-perf -perf-regression

Instruction count

This benchmark run did not return any relevant results for this metric.

Max RSS (memory usage)

Results

This is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
-6.0% [-19.2%, -2.2%] 6
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) -6.0% [-19.2%, -2.2%] 6

Cycles

Results

This is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
-1.5% [-1.9%, -1.1%] 2
Improvements ✅
(secondary)
-2.0% [-2.0%, -2.0%] 1
All ❌✅ (primary) -1.5% [-1.9%, -1.1%] 2

Binary size

Results

This is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.

mean range count
Regressions ❌
(primary)
0.3% [0.0%, 0.8%] 3
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
-0.1% [-0.4%, -0.1%] 35
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) -0.1% [-0.4%, 0.8%] 38

Bootstrap: 666.51s -> 663.607s (-0.44%)
Artifact size: 308.32 MiB -> 308.30 MiB (-0.01%)

@rustbot rustbot removed the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Feb 12, 2024
self.ptr = unsafe { old.add(1) };

Some(unsafe { ptr::read(old.as_ptr()) })
if self.ptr == unsafe { NonNull::new_unchecked(self.end as *mut T) } {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if the goal is IR reduction, well, NonNull::new_unchecked does contain some assert_unsafe_precondition! machinery too, the non_null! macro avoids that.

@saethlin
Copy link
Member Author

@bors try @rust-timer queue

@rust-timer

This comment has been minimized.

@rustbot rustbot added the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Feb 12, 2024
@bors
Copy link
Contributor

bors commented Feb 12, 2024

⌛ Trying commit eaa3133 with merge 491feef...

bors added a commit to rust-lang-ci/rust that referenced this pull request Feb 12, 2024
Don't use mem::zeroed in vec::IntoIter

`mem::zeroed` is not a trivial function. Maybe it was once, but now it involves multiple locals, copies, and an intrinsic that gets monomorphized into a call to `panic_nounwind` for iterators of types like `Vec<&T>`. Of course all that complexity is trivially optimized out, but generating a bunch of IR where we don't need to just so we can optimize it away later is silly.

r? `@ghost`
@bors
Copy link
Contributor

bors commented Feb 13, 2024

☀️ Try build successful - checks-actions
Build commit: 491feef (491feef8f7714821d702726a1652d7196845cbad)

@rust-timer

This comment has been minimized.

@rust-timer
Copy link
Collaborator

Finished benchmarking commit (491feef): comparison URL.

Overall result: ❌ regressions - no action needed

Benchmarking this pull request likely means that it is perf-sensitive, so we're automatically marking it as not fit for rolling up. While you can manually mark this PR as fit for rollup, we strongly recommend not doing so since this PR may lead to changes in compiler perf.

@bors rollup=never
@rustbot label: -S-waiting-on-perf -perf-regression

Instruction count

This is a highly reliable metric that was used to determine the overall result at the top of this comment.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
2.9% [2.9%, 2.9%] 1
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) - - 0

Max RSS (memory usage)

Results

This is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.

mean range count
Regressions ❌
(primary)
12.7% [12.7%, 12.7%] 1
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
-16.1% [-16.1%, -16.1%] 1
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) -1.7% [-16.1%, 12.7%] 2

Cycles

Results

This is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
-2.0% [-2.0%, -2.0%] 1
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) -2.0% [-2.0%, -2.0%] 1

Binary size

Results

This is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.

mean range count
Regressions ❌
(primary)
0.2% [0.0%, 0.5%] 3
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
-0.2% [-0.2%, -0.1%] 36
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) -0.1% [-0.2%, 0.5%] 39

Bootstrap: 664.301s -> 664.247s (-0.01%)
Artifact size: 308.35 MiB -> 308.31 MiB (-0.01%)

@rustbot rustbot removed the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Feb 13, 2024
@saethlin
Copy link
Member Author

The small binary size reduction in debug builds is the only real evidence that this PR is working as intended.

So I'm happy to apply whatever cleanups in this module seem sensible, because I think tidying up the code is the primary outcome of this now.

@saethlin
Copy link
Member Author

r? the8472

@saethlin saethlin marked this pull request as ready for review February 15, 2024 23:22
Comment on lines -330 to -333
let new_end = unsafe { non_null!(self.end, T).sub(1) };
*non_null!(mut self.end, T) = new_end;

Some(unsafe { ptr::read(new_end.as_ptr()) })
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

next_back isn't covered by codegen/vec-iter. can you check if it still has the nonnull attribute?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While working on this, I think I realized that the transmute strategy I was trying to adopt was working by sheer luck. I thought I was getting !nonnull from the transmute range assumes, but we don't insert those for pointers. I'm looking into that, but not in this PR.

@saethlin saethlin force-pushed the vec-into-iter branch 2 times, most recently from 9c5a27f to 83a9d95 Compare February 16, 2024 05:03
@bors
Copy link
Contributor

bors commented Feb 16, 2024

☔ The latest upstream changes (presumably #120486) made this pull request unmergeable. Please resolve the merge conflicts.

@the8472
Copy link
Member

the8472 commented Feb 17, 2024

Looks like the use of zeroed() was introduced in #53804. Back then we didn't do the length-tracking for ZSTs with the end pointer. so ptr ended up unaligned. So that doesn't apply anymore.

@bors r+ rollup

@bors
Copy link
Contributor

bors commented Feb 17, 2024

📌 Commit 7c2db70 has been approved by the8472

It is now in the queue for this repository.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Feb 17, 2024
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Feb 17, 2024
Don't use mem::zeroed in vec::IntoIter

`mem::zeroed` is not a trivial function. Maybe it was once, but now it involves multiple locals, copies, and an intrinsic that gets monomorphized into a call to `panic_nounwind` for iterators of types like `Vec<&T>`. Of course all that complexity is trivially optimized out, but generating a bunch of IR where we don't need to just so we can optimize it away later is silly.
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Feb 17, 2024
Don't use mem::zeroed in vec::IntoIter

`mem::zeroed` is not a trivial function. Maybe it was once, but now it involves multiple locals, copies, and an intrinsic that gets monomorphized into a call to `panic_nounwind` for iterators of types like `Vec<&T>`. Of course all that complexity is trivially optimized out, but generating a bunch of IR where we don't need to just so we can optimize it away later is silly.
bors added a commit to rust-lang-ci/rust that referenced this pull request Feb 17, 2024
…iaskrgr

Rollup of 8 pull requests

Successful merges:

 - rust-lang#120952 (Don't use mem::zeroed in vec::IntoIter)
 - rust-lang#121079 (distribute tool documentations and avoid file conflicts on `x install`)
 - rust-lang#121085 (errors: only eagerly translate subdiagnostics)
 - rust-lang#121091 (use build.rustc config and skip-stage0-validation flag)
 - rust-lang#121193 (Use fulfillment in next trait solver coherence)
 - rust-lang#121209 (Make `CodegenBackend::join_codegen` infallible.)
 - rust-lang#121210 (Fix `cfg(target_abi = "sim")` on `i386-apple-ios`)
 - rust-lang#121228 (create stamp file for clippy)

r? `@ghost`
`@rustbot` modify labels: rollup
bors added a commit to rust-lang-ci/rust that referenced this pull request Feb 17, 2024
…iaskrgr

Rollup of 9 pull requests

Successful merges:

 - rust-lang#120952 (Don't use mem::zeroed in vec::IntoIter)
 - rust-lang#121085 (errors: only eagerly translate subdiagnostics)
 - rust-lang#121091 (use build.rustc config and skip-stage0-validation flag)
 - rust-lang#121149 (Fix typo in VecDeque::handle_capacity_increase() doc comment.)
 - rust-lang#121193 (Use fulfillment in next trait solver coherence)
 - rust-lang#121209 (Make `CodegenBackend::join_codegen` infallible.)
 - rust-lang#121210 (Fix `cfg(target_abi = "sim")` on `i386-apple-ios`)
 - rust-lang#121228 (create stamp file for clippy)
 - rust-lang#121231 (remove a couple of redundant clones)

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit 5997286 into rust-lang:master Feb 17, 2024
11 checks passed
@rustbot rustbot added this to the 1.78.0 milestone Feb 17, 2024
rust-timer added a commit to rust-lang-ci/rust that referenced this pull request Feb 17, 2024
Rollup merge of rust-lang#120952 - saethlin:vec-into-iter, r=the8472

Don't use mem::zeroed in vec::IntoIter

`mem::zeroed` is not a trivial function. Maybe it was once, but now it involves multiple locals, copies, and an intrinsic that gets monomorphized into a call to `panic_nounwind` for iterators of types like `Vec<&T>`. Of course all that complexity is trivially optimized out, but generating a bunch of IR where we don't need to just so we can optimize it away later is silly.
@saethlin saethlin deleted the vec-into-iter branch February 17, 2024 20:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-libs Relevant to the library team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants