Skip to content

Explicitly forget the zero remaining elements in vec::IntoIter::fold().#148486

Merged
rust-bors[bot] merged 1 commit intorust-lang:mainfrom
kpreid:vec-iter-drop
Apr 8, 2026
Merged

Explicitly forget the zero remaining elements in vec::IntoIter::fold().#148486
rust-bors[bot] merged 1 commit intorust-lang:mainfrom
kpreid:vec-iter-drop

Conversation

@kpreid
Copy link
Copy Markdown
Contributor

@kpreid kpreid commented Nov 4, 2025

View all comments

[Original description:] This seems to help LLVM notice that dropping the elements in the destructor of IntoIter is not necessary. In cases it doesn’t help, it should be cheap since it is just one assignment.

This PR adds a function to vec::IntoIter() which is used used by fold() and spec_extend(), when those operations complete, to forget the zero remaining elements and only deallocate the allocation, ensuring that there will never be a useless loop to drop zero remaining elements when the iterator is dropped.

This is my first ever attempt at this kind of codegen micro-optimization in the standard library, so please let me know what should go into the PR or what sort of additional systematic testing might indicate this is a good or bad idea.

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Nov 4, 2025
@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented Nov 4, 2025

r? @Mark-Simulacrum

rustbot has assigned @Mark-Simulacrum.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@Kobzol
Copy link
Copy Markdown
Member

Kobzol commented Nov 4, 2025

@bors try @rust-timer queue

@rust-timer

This comment has been minimized.

@rust-bors

This comment has been minimized.

rust-bors bot added a commit that referenced this pull request Nov 4, 2025
Explicitly forget the zero remaining elements in `vec::IntoIter::fold()`.
@rustbot rustbot added the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Nov 4, 2025
@rust-bors
Copy link
Copy Markdown
Contributor

rust-bors bot commented Nov 4, 2025

☀️ Try build successful (CI)
Build commit: ae97583 (ae975837374d0ef9f9abcb691803628d975e8fb2, parent: e5efc336720901420a8891dcdb67ca0a475dc03c)

@rust-timer

This comment has been minimized.

@rust-timer
Copy link
Copy Markdown
Collaborator

Finished benchmarking commit (ae97583): comparison URL.

Overall result: ❌✅ regressions and improvements - no action needed

Benchmarking this pull request means it may be perf-sensitive – we'll automatically label it not fit for rolling up. You can override this, but we strongly advise not to, due to possible changes in compiler perf.

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

Instruction count

Our most reliable metric. Used to determine the overall result above. However, even this metric can be noisy.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
0.6% [0.5%, 0.6%] 3
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-0.2% [-0.2%, -0.2%] 2
All ❌✅ (primary) - - 0

Max RSS (memory usage)

Results (primary 3.3%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

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

Cycles

Results (secondary -2.7%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

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

Binary size

Results (primary 0.0%, secondary 0.1%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

mean range count
Regressions ❌
(primary)
0.0% [0.0%, 0.1%] 5
Regressions ❌
(secondary)
0.1% [0.1%, 0.1%] 1
Improvements ✅
(primary)
-0.1% [-0.1%, -0.1%] 1
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) 0.0% [-0.1%, 0.1%] 6

Bootstrap: 473.413s -> 473.632s (0.05%)
Artifact size: 390.72 MiB -> 390.71 MiB (-0.00%)

@rustbot rustbot removed the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Nov 5, 2025
@lqd
Copy link
Copy Markdown
Member

lqd commented Nov 5, 2025

@bors try @rust-timer queue

@rust-timer

This comment has been minimized.

@rust-bors

This comment has been minimized.

rust-bors bot added a commit that referenced this pull request Nov 5, 2025
Explicitly forget the zero remaining elements in `vec::IntoIter::fold()`.
@rustbot rustbot added the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Nov 5, 2025
@rust-bors
Copy link
Copy Markdown
Contributor

rust-bors bot commented Nov 5, 2025

☀️ Try build successful (CI)
Build commit: 48bf163 (48bf1634d442e6680a26e6e6305239e51914dbb3, parent: 53efb3d4f3b67d189a0c72eb475a52017d79d609)

@rust-timer

This comment has been minimized.

@cyb0124
Copy link
Copy Markdown

cyb0124 commented Nov 5, 2025

Hi, I posted the URLO topic that led to this.

I found another case of unnecessary drop_in_place call in my project. This time it's from Option::get_or_insert_with. Stripped it down to this:

#[derive(Default)]
pub struct A {
    _a: Option<Box<Option<A>>>,
}

pub fn test(slot: &mut Option<A>) {
    slot.get_or_insert_default();
}

Here https://godbolt.org/z/dPTb3r89T, test calls drop_in_place<Option<A>> right after it just checked it's a None.

Could you fix this as well while you're at it?

@kpreid
Copy link
Copy Markdown
Contributor Author

kpreid commented Nov 5, 2025

@cyb0124 That looks quite doable, but it is a completely different part of the code and so should not go in this PR.

Question: What is your particular goal in this area? Are you looking for code size reduction, execution time reduction, lack of spurious panic paths, or something else? Do you have a specific program you are trying to optimize?

@rust-timer
Copy link
Copy Markdown
Collaborator

Finished benchmarking commit (48bf163): comparison URL.

Overall result: ❌✅ regressions and improvements - no action needed

Benchmarking this pull request means it may be perf-sensitive – we'll automatically label it not fit for rolling up. You can override this, but we strongly advise not to, due to possible changes in compiler perf.

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

Instruction count

Our most reliable metric. Used to determine the overall result above. However, even this metric can be noisy.

mean range count
Regressions ❌
(primary)
0.4% [0.4%, 0.4%] 1
Regressions ❌
(secondary)
0.1% [0.1%, 0.1%] 2
Improvements ✅
(primary)
-0.5% [-0.5%, -0.5%] 1
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) -0.1% [-0.5%, 0.4%] 2

Max RSS (memory usage)

Results (secondary -3.4%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

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

Cycles

Results (primary -4.0%, secondary -1.1%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
4.2% [3.0%, 6.1%] 5
Improvements ✅
(primary)
-4.0% [-4.0%, -4.0%] 1
Improvements ✅
(secondary)
-5.5% [-9.6%, -1.7%] 6
All ❌✅ (primary) -4.0% [-4.0%, -4.0%] 1

Binary size

Results (primary 0.1%, secondary 0.2%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

mean range count
Regressions ❌
(primary)
0.1% [0.0%, 0.4%] 10
Regressions ❌
(secondary)
0.2% [0.2%, 0.2%] 1
Improvements ✅
(primary)
-0.2% [-0.2%, -0.2%] 1
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) 0.1% [-0.2%, 0.4%] 11

Bootstrap: 473.384s -> 475.775s (0.51%)
Artifact size: 390.72 MiB -> 391.11 MiB (0.10%)

@rustbot rustbot removed the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Nov 5, 2025
@cyb0124
Copy link
Copy Markdown

cyb0124 commented Nov 6, 2025

Question: What is your particular goal in this area? Are you looking for code size reduction, execution time reduction, lack of spurious panic paths, or something else? Do you have a specific program you are trying to optimize?

It's "lack of spurious panic paths". The description of this post and this post are pretty much exactly what I need. From what I can find, panicking in the destructor seems to be the best option for now, and it'd be nice to know statically that such panics are never reached. I know this is impossible to prove statically in general without new syntactic restrictions in the language, but cases like "get_or_insert_with shouldn't call destructor" and "into_iter().for_each(f) shouldn't call destructor if f doesn't unwind" should be simple enough..

@kpreid
Copy link
Copy Markdown
Contributor Author

kpreid commented Nov 6, 2025

Finished benchmarking commit (48bf163): comparison URL.

Well, that looks … good-ish? Seems unfortunate that this change increases binary size. I am not familiar with how or whether one might investigate that in the context of the rustc test suite.

It's "lack of spurious panic paths". … I know this is impossible to prove statically in general without new syntactic restrictions in the language, but cases like "get_or_insert_with shouldn't call destructor" and "into_iter().for_each(f) shouldn't call destructor if f doesn't unwind" should be simple enough..

The thing I would caution you about is that you — and people working on the standard library trying to help — may still find this a Sisyphean task, where it's never really complete enough to actually write the program you want and have it stay free of panic paths under maintenance.

matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Mar 10, 2026
In `Option::get_or_insert_with()`, forget the `None` instead of dropping it.

Per rust-lang#148486 (comment)

In `Option::get_or_insert_with()`, after replacing the `None` with `Some`, forget the `None` instead of dropping it.

This allows eliminating the `T: [const] Destruct` bounds, making the functions more flexible in (unstable) const contexts, and avoids generating an implicit `drop_in_place::<Option<T>>()` that will never do anything (and which might even persist after optimization).
rust-bors bot pushed a commit that referenced this pull request Mar 10, 2026
In `Option::get_or_insert_with()`, forget the `None` instead of dropping it.

Per #148486 (comment)

In `Option::get_or_insert_with()`, after replacing the `None` with `Some`, forget the `None` instead of dropping it.

This allows eliminating the `T: [const] Destruct` bounds, making the functions more flexible in (unstable) const contexts, and avoids generating an implicit `drop_in_place::<Option<T>>()` that will never do anything (and which might even persist after optimization).
rust-timer added a commit that referenced this pull request Mar 10, 2026
Rollup merge of #148562 - kpreid:get-init-drop, r=oli-obk

In `Option::get_or_insert_with()`, forget the `None` instead of dropping it.

Per #148486 (comment)

In `Option::get_or_insert_with()`, after replacing the `None` with `Some`, forget the `None` instead of dropping it.

This allows eliminating the `T: [const] Destruct` bounds, making the functions more flexible in (unstable) const contexts, and avoids generating an implicit `drop_in_place::<Option<T>>()` that will never do anything (and which might even persist after optimization).
@kpreid
Copy link
Copy Markdown
Contributor Author

kpreid commented Apr 3, 2026

Rerolling reviewer due to lack of response.

r? libs

@rustbot rustbot assigned jhpratt and unassigned scottmcm Apr 3, 2026
@jhpratt
Copy link
Copy Markdown
Member

jhpratt commented Apr 7, 2026

While I don't doubt that this an improvement in some situations, I'm personally not comfortable approving it without further input. cc @nnethercote as the resident performance expert — what are your thoughts? Particularly given the change in artifact size.

@nnethercote
Copy link
Copy Markdown
Contributor

The artifact size change for the Nov 6 perf run was 396.68 KiB. However, libLLVM.so's size varies unpredictably and should be ignored. The size change for librustc_driver.so is a much smaller 46.20 KiB.

But it's been long enough that another perf run is a good idea.

@bors try @rust-timer queue

@rust-timer

This comment has been minimized.

rust-bors bot pushed a commit that referenced this pull request Apr 7, 2026
Explicitly forget the zero remaining elements in `vec::IntoIter::fold()`.
@rust-bors

This comment has been minimized.

@rustbot rustbot added the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Apr 7, 2026
@Kobzol
Copy link
Copy Markdown
Member

Kobzol commented Apr 7, 2026

Such small rustc artifact size changes are purely PGO/BOLT noise (sadly), so I wouldn't take that into account.

@rust-bors
Copy link
Copy Markdown
Contributor

rust-bors bot commented Apr 7, 2026

☀️ Try build successful (CI)
Build commit: 32fc5f5 (32fc5f5276409829cc88f805241247597a8b62b8, parent: 49b6ac01d6f4c3da812039ae846407a20961aa4c)

@rust-timer

This comment has been minimized.

@rust-timer
Copy link
Copy Markdown
Collaborator

Finished benchmarking commit (32fc5f5): comparison URL.

Overall result: ❌ regressions - no action needed

Benchmarking this pull request means it may be perf-sensitive – we'll automatically label it not fit for rolling up. You can override this, but we strongly advise not to, due to possible changes in compiler perf.

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

Instruction count

Our most reliable metric. Used to determine the overall result above. However, even this metric can be noisy.

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

Max RSS (memory usage)

Results (primary 1.5%, secondary 1.4%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

mean range count
Regressions ❌
(primary)
7.2% [7.2%, 7.2%] 1
Regressions ❌
(secondary)
2.3% [2.0%, 2.7%] 2
Improvements ✅
(primary)
-4.1% [-4.1%, -4.1%] 1
Improvements ✅
(secondary)
-0.6% [-0.6%, -0.6%] 1
All ❌✅ (primary) 1.5% [-4.1%, 7.2%] 2

Cycles

Results (primary -2.3%, secondary -4.1%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
-2.3% [-2.3%, -2.3%] 1
Improvements ✅
(secondary)
-4.1% [-5.8%, -2.5%] 3
All ❌✅ (primary) -2.3% [-2.3%, -2.3%] 1

Binary size

Results (primary 0.1%, secondary 0.1%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

mean range count
Regressions ❌
(primary)
0.1% [0.0%, 0.9%] 70
Regressions ❌
(secondary)
0.1% [0.0%, 0.4%] 25
Improvements ✅
(primary)
-0.2% [-0.2%, -0.2%] 1
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) 0.1% [-0.2%, 0.9%] 71

Bootstrap: 487.434s -> 486.071s (-0.28%)
Artifact size: 395.10 MiB -> 395.10 MiB (-0.00%)

@rustbot rustbot removed the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Apr 7, 2026
@nnethercote
Copy link
Copy Markdown
Contributor

No negative perf impact.

@jhpratt
Copy link
Copy Markdown
Member

jhpratt commented Apr 8, 2026

Works for me then! There's a demonstrable improvement and no apparent downside.

@bors r+

@rust-bors
Copy link
Copy Markdown
Contributor

rust-bors bot commented Apr 8, 2026

📌 Commit f700fdc has been approved by jhpratt

It is now in the queue for this repository.

@rust-bors rust-bors bot 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 Apr 8, 2026
@rust-bors

This comment has been minimized.

@rust-bors rust-bors bot added merged-by-bors This PR was explicitly merged by bors. and removed S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. labels Apr 8, 2026
@rust-bors
Copy link
Copy Markdown
Contributor

rust-bors bot commented Apr 8, 2026

☀️ Test successful - CI
Approved by: jhpratt
Duration: 3h 15m 3s
Pushing 30d0309 to main...

@rust-bors rust-bors bot merged commit 30d0309 into rust-lang:main Apr 8, 2026
13 checks passed
@rustbot rustbot added this to the 1.96.0 milestone Apr 8, 2026
@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Apr 8, 2026

What is this? This is an experimental post-merge analysis report that shows differences in test outcomes between the merged PR and its parent PR.

Comparing e5efd33 (parent) -> 30d0309 (this PR)

Test differences

Show 10 test diffs

Stage 1

  • [codegen] tests/codegen-llvm/vec-into-iter-drops.rs: [missing] -> pass (J1)

Stage 2

  • [codegen] tests/codegen-llvm/vec-into-iter-drops.rs: [missing] -> pass (J0)

Additionally, 8 doctest diffs were found. These are ignored, as they are noisy.

Job group index

Test dashboard

Run

cargo run --manifest-path src/ci/citool/Cargo.toml -- \
    test-dashboard 30d0309fa821f7a0984a9629e0d227ca3c0d2eda --output-dir test-dashboard

And then open test-dashboard/index.html in your browser to see an overview of all executed tests.

Job duration changes

  1. dist-x86_64-apple: 1h 37m -> 2h 13m (+37.8%)
  2. dist-aarch64-apple: 1h 40m -> 2h 8m (+27.5%)
  3. dist-loongarch64-musl: 1h 26m -> 1h 48m (+25.5%)
  4. aarch64-apple: 2h 45m -> 3h 7m (+13.6%)
  5. dist-apple-various: 1h 47m -> 1h 34m (-11.4%)
  6. i686-gnu-nopt-1: 2h 22m -> 2h 11m (-7.9%)
  7. dist-aarch64-msvc: 1h 52m -> 1h 43m (-7.9%)
  8. dist-aarch64-linux: 1h 44m -> 1h 53m (+7.9%)
  9. x86_64-gnu-llvm-21-1: 1h 8m -> 1h 14m (+7.6%)
  10. x86_64-msvc-ext2: 1h 46m -> 1h 53m (+6.9%)
How to interpret the job duration changes?

Job durations can vary a lot, based on the actual runner instance
that executed the job, system noise, invalidated caches, etc. The table above is provided
mostly for t-infra members, for simpler debugging of potential CI slow-downs.

@rust-timer
Copy link
Copy Markdown
Collaborator

Finished benchmarking commit (30d0309): comparison URL.

Overall result: ❌✅ regressions and improvements - no action needed

@rustbot label: -perf-regression

Instruction count

Our most reliable metric. Used to determine the overall result above. However, even this metric can be noisy.

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

Max RSS (memory usage)

Results (primary -0.5%, secondary 1.9%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

mean range count
Regressions ❌
(primary)
2.6% [1.0%, 4.3%] 2
Regressions ❌
(secondary)
1.9% [1.1%, 2.6%] 3
Improvements ✅
(primary)
-3.6% [-4.8%, -2.4%] 2
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) -0.5% [-4.8%, 4.3%] 4

Cycles

Results (secondary 2.4%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

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

Binary size

Results (primary -0.0%, secondary -0.1%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

mean range count
Regressions ❌
(primary)
0.2% [0.0%, 0.7%] 5
Regressions ❌
(secondary)
0.0% [0.0%, 0.0%] 1
Improvements ✅
(primary)
-0.1% [-0.2%, -0.0%] 37
Improvements ✅
(secondary)
-0.1% [-0.1%, -0.0%] 22
All ❌✅ (primary) -0.0% [-0.2%, 0.7%] 42

Bootstrap: 491.183s -> 489.264s (-0.39%)
Artifact size: 395.44 MiB -> 395.30 MiB (-0.04%)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

merged-by-bors This PR was explicitly merged by bors. 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.