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

Merged
rust-bors[bot] merged 1 commit into
rust-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 into
rust-lang:mainfrom
kpreid:vec-iter-drop

Conversation

@kpreid

@kpreidkpreid commented Nov 4, 2025

Copy link
Copy Markdown
Contributor

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.

@rustbotrustbot 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

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

@bors try @rust-timer queue

@rust-timer

This comment has been minimized.

@rust-bors

This comment has been minimized.

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

rust-borsBot commented Nov 4, 2025

Copy link
Copy Markdown
Contributor

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

@rust-timer

This comment has been minimized.

Comment threadtests/codegen-llvm/vec-into-iter-drops.rs
Comment threadlibrary/alloc/src/vec/into_iter.rs Outdated
@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.

meanrangecount
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.

meanrangecount
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.

meanrangecount
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.

meanrangecount
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%)

@rustbotrustbot removed the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Nov 5, 2025
@lqd

lqd commented Nov 5, 2025

Copy link
Copy Markdown
Member

@bors try @rust-timer queue

@rust-timer

This comment has been minimized.

@rust-bors

This comment has been minimized.

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

rust-borsBot commented Nov 5, 2025

Copy link
Copy Markdown
Contributor

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

@rust-timer

This comment has been minimized.

@cyb0124

Copy link
Copy Markdown

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)]pubstructA{_a:Option<Box<Option<A>>>,}pubfntest(slot:&mutOption<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
ContributorAuthor

@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.

meanrangecount
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.

meanrangecount
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.

meanrangecount
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.

meanrangecount
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%)

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

cyb0124 commented Nov 6, 2025

Copy link
Copy Markdown

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
ContributorAuthor

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.

rust-borsBot 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
ContributorAuthor

Rerolling reviewer due to lack of response.

r? libs

@rustbotrustbot assigned jhpratt and unassigned scottmcmApr 3, 2026
@jhpratt

Copy link
Copy Markdown
Member

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-borsBot 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.

@rustbotrustbot added the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Apr 7, 2026
@Kobzol

Kobzol commented Apr 7, 2026

Copy link
Copy Markdown
Member

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

@rust-bors

rust-borsBot commented Apr 7, 2026

Copy link
Copy Markdown
Contributor

☀️ 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.

meanrangecount
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.

meanrangecount
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.

meanrangecount
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.

meanrangecount
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%)

@rustbotrustbot 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

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

@bors r+

@rust-bors

rust-borsBot commented Apr 8, 2026

Copy link
Copy Markdown
Contributor

📌 Commit f700fdc has been approved by jhpratt

It is now in the queue for this repository.

@rust-borsrust-borsBot 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-borsrust-borsBot 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

rust-borsBot commented Apr 8, 2026

Copy link
Copy Markdown
Contributor

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

@rust-bors
rust-borsBot merged commit 30d0309 into rust-lang:mainApr 8, 2026
13 checks passed
@rustbotrustbot added this to the 1.96.0 milestone Apr 8, 2026
@github-actions

Copy link
Copy Markdown
Contributor
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.

meanrangecount
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.

meanrangecount
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.

meanrangecount
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.

meanrangecount
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%)

@kpreid
kpreid deleted the vec-iter-drop branch September 1, 2026 17:49
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

merged-by-borsThis PR was explicitly merged by bors.T-libsRelevant 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.

12 participants

@kpreid@rustbot@Kobzol@rust-timer@lqd@cyb0124@Mark-Simulacrum@rust-log-analyzer@jhpratt@nnethercote@scottmcm@wesleywiser
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Add copy buttons to all
 blocks\n(function() {\n function addCopyButtons() {\n document.querySelectorAll('pre code').forEach(function(codeBlock) {\n if (codeBlock.parentElement.hasAttribute('data-copy-added')) return;\n codeBlock.parentElement.setAttribute('data-copy-added', 'true');\n \n var btn = document.createElement('button');\n btn.textContent = 'Copy';\n btn.style.cssText = 'position:absolute;top:4px;right:4px;padding:2px 8px;font-size:11px;background:#4ecdc4;border:none;border-radius:4px;color:#1a1a2e;cursor:pointer;opacity:0.7;transition:opacity 0.2s;';\n btn.onmouseover = function() { this.style.opacity = '1'; };\n btn.onmouseout = function() { this.style.opacity = '0.7'; };\n btn.onclick = function() {\n navigator.clipboard.writeText(codeBlock.textContent).then(function() {\n btn.textContent = 'Copied!';\n setTimeout(function() { btn.textContent = 'Copy'; }, 1500);\n });\n };\n codeBlock.parentElement.style.position = 'relative';\n codeBlock.parentElement.appendChild(btn);\n });\n }\n \n addCopyButtons();\n \n // Re-run on dynamic content\n var observer = new MutationObserver(addCopyButtons);\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "Add Copy Buttons to Code Blocks");
}
} catch(__e) { console.warn('[Userscript:Add Copy Buttons to Code Blocks]', __e); }
})();
(function(){
try {
var __m = "github.com";
var __re = new RegExp('^' + "github\\.com" + '
Skip to content

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

Merged
rust-bors[bot] merged 1 commit into
rust-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 into
rust-lang:mainfrom
kpreid:vec-iter-drop

Conversation

@kpreid

@kpreidkpreid commented Nov 4, 2025

Copy link
Copy Markdown
Contributor

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.

@rustbotrustbot 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

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

@bors try @rust-timer queue

@rust-timer

This comment has been minimized.

@rust-bors

This comment has been minimized.

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

rust-borsBot commented Nov 4, 2025

Copy link
Copy Markdown
Contributor

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

@rust-timer

This comment has been minimized.

Comment threadtests/codegen-llvm/vec-into-iter-drops.rs
Comment threadlibrary/alloc/src/vec/into_iter.rs Outdated
@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.

meanrangecount
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.

meanrangecount
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.

meanrangecount
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.

meanrangecount
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%)

@rustbotrustbot removed the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Nov 5, 2025
@lqd

lqd commented Nov 5, 2025

Copy link
Copy Markdown
Member

@bors try @rust-timer queue

@rust-timer

This comment has been minimized.

@rust-bors

This comment has been minimized.

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

rust-borsBot commented Nov 5, 2025

Copy link
Copy Markdown
Contributor

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

@rust-timer

This comment has been minimized.

@cyb0124

Copy link
Copy Markdown

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)]pubstructA{_a:Option<Box<Option<A>>>,}pubfntest(slot:&mutOption<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
ContributorAuthor

@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.

meanrangecount
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.

meanrangecount
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.

meanrangecount
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.

meanrangecount
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%)

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

cyb0124 commented Nov 6, 2025

Copy link
Copy Markdown

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
ContributorAuthor

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.

rust-borsBot 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
ContributorAuthor

Rerolling reviewer due to lack of response.

r? libs

@rustbotrustbot assigned jhpratt and unassigned scottmcmApr 3, 2026
@jhpratt

Copy link
Copy Markdown
Member

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-borsBot 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.

@rustbotrustbot added the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Apr 7, 2026
@Kobzol

Kobzol commented Apr 7, 2026

Copy link
Copy Markdown
Member

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

@rust-bors

rust-borsBot commented Apr 7, 2026

Copy link
Copy Markdown
Contributor

☀️ 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.

meanrangecount
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.

meanrangecount
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.

meanrangecount
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.

meanrangecount
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%)

@rustbotrustbot 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

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

@bors r+

@rust-bors

rust-borsBot commented Apr 8, 2026

Copy link
Copy Markdown
Contributor

📌 Commit f700fdc has been approved by jhpratt

It is now in the queue for this repository.

@rust-borsrust-borsBot 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-borsrust-borsBot 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

rust-borsBot commented Apr 8, 2026

Copy link
Copy Markdown
Contributor

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

@rust-bors
rust-borsBot merged commit 30d0309 into rust-lang:mainApr 8, 2026
13 checks passed
@rustbotrustbot added this to the 1.96.0 milestone Apr 8, 2026
@github-actions

Copy link
Copy Markdown
Contributor
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.

meanrangecount
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.

meanrangecount
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.

meanrangecount
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.

meanrangecount
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%)

@kpreid
kpreid deleted the vec-iter-drop branch September 1, 2026 17:49
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

merged-by-borsThis PR was explicitly merged by bors.T-libsRelevant 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.

12 participants

@kpreid@rustbot@Kobzol@rust-timer@lqd@cyb0124@Mark-Simulacrum@rust-log-analyzer@jhpratt@nnethercote@scottmcm@wesleywiser
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Force GitHub README to respect dark mode\n(function() {\n var style = document.createElement('style');\n style.textContent = '\n .markdown-body {\n color-scheme: dark light;\n }\n .markdown-body pre { background: #161b22 !important; }\n .markdown-body code { background: rgba(110, 118, 129, 0.4) !important; }\n .markdown-body table th, .markdown-body table td { border-color: #30363d !important; }\n .markdown-body img { background: #0d1117; }\n .markdown-body blockquote { border-left-color: #8b949e; }\n .markdown-body hr { border-color: #30363d; }\n ';\n document.head.appendChild(style);\n})();", "GitHub Dark Mode README Fix"); } } catch(__e) { console.warn('[Userscript:GitHub Dark Mode README Fix]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content

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

Merged
rust-bors[bot] merged 1 commit into
rust-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 into
rust-lang:mainfrom
kpreid:vec-iter-drop

Conversation

@kpreid

@kpreidkpreid commented Nov 4, 2025

Copy link
Copy Markdown
Contributor

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.

@rustbotrustbot 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

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

@bors try @rust-timer queue

@rust-timer

This comment has been minimized.

@rust-bors

This comment has been minimized.

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

rust-borsBot commented Nov 4, 2025

Copy link
Copy Markdown
Contributor

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

@rust-timer

This comment has been minimized.

Comment threadtests/codegen-llvm/vec-into-iter-drops.rs
Comment threadlibrary/alloc/src/vec/into_iter.rs Outdated
@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.

meanrangecount
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.

meanrangecount
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.

meanrangecount
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.

meanrangecount
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%)

@rustbotrustbot removed the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Nov 5, 2025
@lqd

lqd commented Nov 5, 2025

Copy link
Copy Markdown
Member

@bors try @rust-timer queue

@rust-timer

This comment has been minimized.

@rust-bors

This comment has been minimized.

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

rust-borsBot commented Nov 5, 2025

Copy link
Copy Markdown
Contributor

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

@rust-timer

This comment has been minimized.

@cyb0124

Copy link
Copy Markdown

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)]pubstructA{_a:Option<Box<Option<A>>>,}pubfntest(slot:&mutOption<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
ContributorAuthor

@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.

meanrangecount
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.

meanrangecount
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.

meanrangecount
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.

meanrangecount
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%)

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

cyb0124 commented Nov 6, 2025

Copy link
Copy Markdown

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
ContributorAuthor

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.

rust-borsBot 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
ContributorAuthor

Rerolling reviewer due to lack of response.

r? libs

@rustbotrustbot assigned jhpratt and unassigned scottmcmApr 3, 2026
@jhpratt

Copy link
Copy Markdown
Member

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-borsBot 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.

@rustbotrustbot added the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Apr 7, 2026
@Kobzol

Kobzol commented Apr 7, 2026

Copy link
Copy Markdown
Member

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

@rust-bors

rust-borsBot commented Apr 7, 2026

Copy link
Copy Markdown
Contributor

☀️ 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.

meanrangecount
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.

meanrangecount
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.

meanrangecount
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.

meanrangecount
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%)

@rustbotrustbot 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

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

@bors r+

@rust-bors

rust-borsBot commented Apr 8, 2026

Copy link
Copy Markdown
Contributor

📌 Commit f700fdc has been approved by jhpratt

It is now in the queue for this repository.

@rust-borsrust-borsBot 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-borsrust-borsBot 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

rust-borsBot commented Apr 8, 2026

Copy link
Copy Markdown
Contributor

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

@rust-bors
rust-borsBot merged commit 30d0309 into rust-lang:mainApr 8, 2026
13 checks passed
@rustbotrustbot added this to the 1.96.0 milestone Apr 8, 2026
@github-actions

Copy link
Copy Markdown
Contributor
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.

meanrangecount
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.

meanrangecount
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.

meanrangecount
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.

meanrangecount
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%)

@kpreid
kpreid deleted the vec-iter-drop branch September 1, 2026 17:49
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

merged-by-borsThis PR was explicitly merged by bors.T-libsRelevant 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.

12 participants

@kpreid@rustbot@Kobzol@rust-timer@lqd@cyb0124@Mark-Simulacrum@rust-log-analyzer@jhpratt@nnethercote@scottmcm@wesleywiser
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Highlight search terms from Google/DuckDuckGo/Bing referrer\n(function() {\n var ref = document.referrer;\n var terms = [];\n \n if (ref.includes('google.com') || ref.includes('duckduckgo.com') || ref.includes('bing.com')) {\n var url = new URL(ref);\n var q = url.searchParams.get('q') || url.searchParams.get('p');\n if (q) {\n terms = q.split(/\\s+/).filter(function(t) { return t.length > 2; });\n }\n }\n \n if (terms.length === 0) return;\n \n var style = document.createElement('style');\n style.textContent = '.userscript-highlight { background: #fbbf24; color: #1a1a2e; padding: 1px 3px; border-radius: 2px; }';\n document.head.appendChild(style);\n \n function highlight(node) {\n if (node.nodeType === 3) { // text node\n var text = node.textContent;\n var found = false;\n terms.forEach(function(term) {\n var regex = new RegExp('(' + term.replace(/[.*+?^${}()|[\\]\\\\]/g, '\\\\') + ')', 'gi');\n if (regex.test(text)) {\n found = true;\n var frag = document.createDocumentFragment();\n var parts = text.split(regex);\n parts.forEach(function(part, i) {\n if (i % 2 === 0) {\n frag.appendChild(document.createTextNode(part));\n } else {\n var span = document.createElement('span');\n span.className = 'userscript-highlight';\n span.textContent = part;\n frag.appendChild(span);\n }\n });\n node.parentNode.replaceChild(frag, node);\n }\n });\n } else if (node.nodeType === 1 && node.childNodes) { // element\n var skipTags = ['SCRIPT', 'STYLE', 'NOSCRIPT', 'TEXTAREA', 'INPUT', 'SELECT'];\n if (!skipTags.includes(node.tagName)) {\n Array.from(node.childNodes).forEach(highlight);\n }\n }\n }\n \n highlight(document.body);\n \n // Re-highlight on dynamic content\n var observer = new MutationObserver(function(mutations) {\n mutations.forEach(function(m) {\n m.addedNodes.forEach(function(node) {\n if (node.nodeType === 1 || node.nodeType === 3) highlight(node);\n });\n });\n });\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "Highlight Search Terms"); } } catch(__e) { console.warn('[Userscript:Highlight Search Terms]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content

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

Merged
rust-bors[bot] merged 1 commit into
rust-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 into
rust-lang:mainfrom
kpreid:vec-iter-drop

Conversation

@kpreid

@kpreidkpreid commented Nov 4, 2025

Copy link
Copy Markdown
Contributor

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.

@rustbotrustbot 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

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

@bors try @rust-timer queue

@rust-timer

This comment has been minimized.

@rust-bors

This comment has been minimized.

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

rust-borsBot commented Nov 4, 2025

Copy link
Copy Markdown
Contributor

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

@rust-timer

This comment has been minimized.

Comment threadtests/codegen-llvm/vec-into-iter-drops.rs
Comment threadlibrary/alloc/src/vec/into_iter.rs Outdated
@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.

meanrangecount
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.

meanrangecount
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.

meanrangecount
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.

meanrangecount
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%)

@rustbotrustbot removed the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Nov 5, 2025
@lqd

lqd commented Nov 5, 2025

Copy link
Copy Markdown
Member

@bors try @rust-timer queue

@rust-timer

This comment has been minimized.

@rust-bors

This comment has been minimized.

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

rust-borsBot commented Nov 5, 2025

Copy link
Copy Markdown
Contributor

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

@rust-timer

This comment has been minimized.

@cyb0124

Copy link
Copy Markdown

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)]pubstructA{_a:Option<Box<Option<A>>>,}pubfntest(slot:&mutOption<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
ContributorAuthor

@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.

meanrangecount
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.

meanrangecount
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.

meanrangecount
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.

meanrangecount
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%)

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

cyb0124 commented Nov 6, 2025

Copy link
Copy Markdown

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
ContributorAuthor

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.

rust-borsBot 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
ContributorAuthor

Rerolling reviewer due to lack of response.

r? libs

@rustbotrustbot assigned jhpratt and unassigned scottmcmApr 3, 2026
@jhpratt

Copy link
Copy Markdown
Member

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-borsBot 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.

@rustbotrustbot added the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Apr 7, 2026
@Kobzol

Kobzol commented Apr 7, 2026

Copy link
Copy Markdown
Member

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

@rust-bors

rust-borsBot commented Apr 7, 2026

Copy link
Copy Markdown
Contributor

☀️ 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.

meanrangecount
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.

meanrangecount
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.

meanrangecount
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.

meanrangecount
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%)

@rustbotrustbot 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

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

@bors r+

@rust-bors

rust-borsBot commented Apr 8, 2026

Copy link
Copy Markdown
Contributor

📌 Commit f700fdc has been approved by jhpratt

It is now in the queue for this repository.

@rust-borsrust-borsBot 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-borsrust-borsBot 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

rust-borsBot commented Apr 8, 2026

Copy link
Copy Markdown
Contributor

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

@rust-bors
rust-borsBot merged commit 30d0309 into rust-lang:mainApr 8, 2026
13 checks passed
@rustbotrustbot added this to the 1.96.0 milestone Apr 8, 2026
@github-actions

Copy link
Copy Markdown
Contributor
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.

meanrangecount
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.

meanrangecount
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.

meanrangecount
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.

meanrangecount
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%)

@kpreid
kpreid deleted the vec-iter-drop branch September 1, 2026 17:49
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

merged-by-borsThis PR was explicitly merged by bors.T-libsRelevant 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.

12 participants

@kpreid@rustbot@Kobzol@rust-timer@lqd@cyb0124@Mark-Simulacrum@rust-log-analyzer@jhpratt@nnethercote@scottmcm@wesleywiser
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Strip utm_, fbclid, gclid, etc. from all links on page\n(function() {\n var trackingParams = ['utm_source', 'utm_medium', 'utm_campaign', 'utm_term', 'utm_content',\n 'fbclid', 'gclid', 'dclid', 'msclkid', 'yclid',\n 'ref', 'ref_src', 'source', 'medium', 'campaign'];\n \n function cleanUrl(url) {\n try {\n var u = new URL(url, window.location.origin);\n var changed = false;\n trackingParams.forEach(function(p) {\n if (u.searchParams.has(p)) {\n u.searchParams.delete(p);\n changed = true;\n }\n });\n return changed ? u.toString() : url;\n } catch (e) {\n return url;\n }\n }\n \n function cleanLinks() {\n document.querySelectorAll('a[href]').forEach(function(a) {\n var clean = cleanUrl(a.href);\n if (clean !== a.href) a.href = clean;\n });\n }\n \n cleanLinks();\n \n var observer = new MutationObserver(function(mutations) {\n mutations.forEach(function(m) {\n m.addedNodes.forEach(function(node) {\n if (node.nodeType === 1) {\n if (node.tagName === 'A') cleanLinks();\n node.querySelectorAll('a[href]').forEach(function(a) {\n var clean = cleanUrl(a.href);\n if (clean !== a.href) a.href = clean;\n });\n }\n });\n });\n });\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "Remove Tracking Parameters from Links"); } } catch(__e) { console.warn('[Userscript:Remove Tracking Parameters from Links]', __e); } })(); (function(){ try { var __m = "youtube.com"; var __re = new RegExp('^' + "youtube\\.com" + '
Skip to content

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

Merged
rust-bors[bot] merged 1 commit into
rust-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 into
rust-lang:mainfrom
kpreid:vec-iter-drop

Conversation

@kpreid

@kpreidkpreid commented Nov 4, 2025

Copy link
Copy Markdown
Contributor

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.

@rustbotrustbot 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

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

@bors try @rust-timer queue

@rust-timer

This comment has been minimized.

@rust-bors

This comment has been minimized.

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

rust-borsBot commented Nov 4, 2025

Copy link
Copy Markdown
Contributor

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

@rust-timer

This comment has been minimized.

Comment threadtests/codegen-llvm/vec-into-iter-drops.rs
Comment threadlibrary/alloc/src/vec/into_iter.rs Outdated
@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.

meanrangecount
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.

meanrangecount
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.

meanrangecount
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.

meanrangecount
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%)

@rustbotrustbot removed the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Nov 5, 2025
@lqd

lqd commented Nov 5, 2025

Copy link
Copy Markdown
Member

@bors try @rust-timer queue

@rust-timer

This comment has been minimized.

@rust-bors

This comment has been minimized.

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

rust-borsBot commented Nov 5, 2025

Copy link
Copy Markdown
Contributor

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

@rust-timer

This comment has been minimized.

@cyb0124

Copy link
Copy Markdown

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)]pubstructA{_a:Option<Box<Option<A>>>,}pubfntest(slot:&mutOption<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
ContributorAuthor

@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.

meanrangecount
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.

meanrangecount
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.

meanrangecount
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.

meanrangecount
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%)

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

cyb0124 commented Nov 6, 2025

Copy link
Copy Markdown

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
ContributorAuthor

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.

rust-borsBot 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
ContributorAuthor

Rerolling reviewer due to lack of response.

r? libs

@rustbotrustbot assigned jhpratt and unassigned scottmcmApr 3, 2026
@jhpratt

Copy link
Copy Markdown
Member

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-borsBot 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.

@rustbotrustbot added the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Apr 7, 2026
@Kobzol

Kobzol commented Apr 7, 2026

Copy link
Copy Markdown
Member

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

@rust-bors

rust-borsBot commented Apr 7, 2026

Copy link
Copy Markdown
Contributor

☀️ 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.

meanrangecount
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.

meanrangecount
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.

meanrangecount
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.

meanrangecount
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%)

@rustbotrustbot 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

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

@bors r+

@rust-bors

rust-borsBot commented Apr 8, 2026

Copy link
Copy Markdown
Contributor

📌 Commit f700fdc has been approved by jhpratt

It is now in the queue for this repository.

@rust-borsrust-borsBot 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-borsrust-borsBot 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

rust-borsBot commented Apr 8, 2026

Copy link
Copy Markdown
Contributor

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

@rust-bors
rust-borsBot merged commit 30d0309 into rust-lang:mainApr 8, 2026
13 checks passed
@rustbotrustbot added this to the 1.96.0 milestone Apr 8, 2026
@github-actions

Copy link
Copy Markdown
Contributor
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.

meanrangecount
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.

meanrangecount
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.

meanrangecount
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.

meanrangecount
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%)

@kpreid
kpreid deleted the vec-iter-drop branch September 1, 2026 17:49
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

merged-by-borsThis PR was explicitly merged by bors.T-libsRelevant 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.

12 participants

@kpreid@rustbot@Kobzol@rust-timer@lqd@cyb0124@Mark-Simulacrum@rust-log-analyzer@jhpratt@nnethercote@scottmcm@wesleywiser
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Auto-enable theater mode on YouTube\n(function() {\n function tryTheater() {\n var btn = document.querySelector('button[aria-label=\"Theater mode\"], ytd-player #player button[title=\"Theater mode\"]');\n if (btn && !btn.classList.contains('activated')) {\n btn.click();\n }\n }\n \n // Try immediately\n tryTheater();\n \n // Try after navigation (SPA)\n var lastUrl = location.href;\n setInterval(function() {\n if (location.href !== lastUrl) {\n lastUrl = location.href;\n setTimeout(tryTheater, 500);\n }\n }, 1000);\n \n // Also try on player load\n var observer = new MutationObserver(tryTheater);\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "YouTube Theater Mode Default"); } } catch(__e) { console.warn('[Userscript:YouTube Theater Mode Default]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content

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

Merged
rust-bors[bot] merged 1 commit into
rust-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 into
rust-lang:mainfrom
kpreid:vec-iter-drop

Conversation

@kpreid

@kpreidkpreid commented Nov 4, 2025

Copy link
Copy Markdown
Contributor

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.

@rustbotrustbot 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

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

@bors try @rust-timer queue

@rust-timer

This comment has been minimized.

@rust-bors

This comment has been minimized.

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

rust-borsBot commented Nov 4, 2025

Copy link
Copy Markdown
Contributor

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

@rust-timer

This comment has been minimized.

Comment threadtests/codegen-llvm/vec-into-iter-drops.rs
Comment threadlibrary/alloc/src/vec/into_iter.rs Outdated
@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.

meanrangecount
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.

meanrangecount
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.

meanrangecount
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.

meanrangecount
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%)

@rustbotrustbot removed the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Nov 5, 2025
@lqd

lqd commented Nov 5, 2025

Copy link
Copy Markdown
Member

@bors try @rust-timer queue

@rust-timer

This comment has been minimized.

@rust-bors

This comment has been minimized.

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

rust-borsBot commented Nov 5, 2025

Copy link
Copy Markdown
Contributor

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

@rust-timer

This comment has been minimized.

@cyb0124

Copy link
Copy Markdown

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)]pubstructA{_a:Option<Box<Option<A>>>,}pubfntest(slot:&mutOption<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
ContributorAuthor

@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.

meanrangecount
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.

meanrangecount
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.

meanrangecount
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.

meanrangecount
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%)

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

cyb0124 commented Nov 6, 2025

Copy link
Copy Markdown

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
ContributorAuthor

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.

rust-borsBot 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
ContributorAuthor

Rerolling reviewer due to lack of response.

r? libs

@rustbotrustbot assigned jhpratt and unassigned scottmcmApr 3, 2026
@jhpratt

Copy link
Copy Markdown
Member

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-borsBot 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.

@rustbotrustbot added the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Apr 7, 2026
@Kobzol

Kobzol commented Apr 7, 2026

Copy link
Copy Markdown
Member

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

@rust-bors

rust-borsBot commented Apr 7, 2026

Copy link
Copy Markdown
Contributor

☀️ 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.

meanrangecount
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.

meanrangecount
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.

meanrangecount
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.

meanrangecount
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%)

@rustbotrustbot 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

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

@bors r+

@rust-bors

rust-borsBot commented Apr 8, 2026

Copy link
Copy Markdown
Contributor

📌 Commit f700fdc has been approved by jhpratt

It is now in the queue for this repository.

@rust-borsrust-borsBot 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-borsrust-borsBot 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

rust-borsBot commented Apr 8, 2026

Copy link
Copy Markdown
Contributor

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

@rust-bors
rust-borsBot merged commit 30d0309 into rust-lang:mainApr 8, 2026
13 checks passed
@rustbotrustbot added this to the 1.96.0 milestone Apr 8, 2026
@github-actions

Copy link
Copy Markdown
Contributor
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.

meanrangecount
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.

meanrangecount
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.

meanrangecount
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.

meanrangecount
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%)

@kpreid
kpreid deleted the vec-iter-drop branch September 1, 2026 17:49
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

merged-by-borsThis PR was explicitly merged by bors.T-libsRelevant 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.

12 participants

@kpreid@rustbot@Kobzol@rust-timer@lqd@cyb0124@Mark-Simulacrum@rust-log-analyzer@jhpratt@nnethercote@scottmcm@wesleywiser
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Remove or un-stick sticky/fixed headers that block content\n(function() {\n function unstick() {\n document.querySelectorAll('header, nav, [role=\"banner\"], .header, .navbar, .sticky, .fixed-top, [style*=\"position: fixed\"], [style*=\"position:sticky\"]').forEach(function(el) {\n if (el.style.position === 'fixed' || el.style.position === 'sticky' || \n getComputedStyle(el).position === 'fixed' || getComputedStyle(el).position === 'sticky') {\n el.style.position = 'static';\n el.style.top = 'auto';\n el.style.zIndex = 'auto';\n }\n });\n }\n \n unstick();\n \n var observer = new MutationObserver(unstick);\n observer.observe(document.body, { childList: true, subtree: true, attributes: true, attributeFilter: ['style', 'class'] });\n})();", "Kill Sticky Headers"); } } catch(__e) { console.warn('[Userscript:Kill Sticky Headers]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content

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

Merged
rust-bors[bot] merged 1 commit into
rust-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 into
rust-lang:mainfrom
kpreid:vec-iter-drop

Conversation

@kpreid

@kpreidkpreid commented Nov 4, 2025

Copy link
Copy Markdown
Contributor

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.

@rustbotrustbot 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

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

@bors try @rust-timer queue

@rust-timer

This comment has been minimized.

@rust-bors

This comment has been minimized.

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

rust-borsBot commented Nov 4, 2025

Copy link
Copy Markdown
Contributor

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

@rust-timer

This comment has been minimized.

Comment threadtests/codegen-llvm/vec-into-iter-drops.rs
Comment threadlibrary/alloc/src/vec/into_iter.rs Outdated
@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.

meanrangecount
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.

meanrangecount
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.

meanrangecount
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.

meanrangecount
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%)

@rustbotrustbot removed the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Nov 5, 2025
@lqd

lqd commented Nov 5, 2025

Copy link
Copy Markdown
Member

@bors try @rust-timer queue

@rust-timer

This comment has been minimized.

@rust-bors

This comment has been minimized.

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

rust-borsBot commented Nov 5, 2025

Copy link
Copy Markdown
Contributor

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

@rust-timer

This comment has been minimized.

@cyb0124

Copy link
Copy Markdown

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)]pubstructA{_a:Option<Box<Option<A>>>,}pubfntest(slot:&mutOption<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
ContributorAuthor

@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.

meanrangecount
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.

meanrangecount
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.

meanrangecount
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.

meanrangecount
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%)

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

cyb0124 commented Nov 6, 2025

Copy link
Copy Markdown

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
ContributorAuthor

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.

rust-borsBot 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
ContributorAuthor

Rerolling reviewer due to lack of response.

r? libs

@rustbotrustbot assigned jhpratt and unassigned scottmcmApr 3, 2026
@jhpratt

Copy link
Copy Markdown
Member

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-borsBot 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.

@rustbotrustbot added the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Apr 7, 2026
@Kobzol

Kobzol commented Apr 7, 2026

Copy link
Copy Markdown
Member

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

@rust-bors

rust-borsBot commented Apr 7, 2026

Copy link
Copy Markdown
Contributor

☀️ 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.

meanrangecount
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.

meanrangecount
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.

meanrangecount
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.

meanrangecount
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%)

@rustbotrustbot 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

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

@bors r+

@rust-bors

rust-borsBot commented Apr 8, 2026

Copy link
Copy Markdown
Contributor

📌 Commit f700fdc has been approved by jhpratt

It is now in the queue for this repository.

@rust-borsrust-borsBot 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-borsrust-borsBot 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

rust-borsBot commented Apr 8, 2026

Copy link
Copy Markdown
Contributor

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

@rust-bors
rust-borsBot merged commit 30d0309 into rust-lang:mainApr 8, 2026
13 checks passed
@rustbotrustbot added this to the 1.96.0 milestone Apr 8, 2026
@github-actions

Copy link
Copy Markdown
Contributor
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.

meanrangecount
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.

meanrangecount
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.

meanrangecount
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.

meanrangecount
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%)

@kpreid
kpreid deleted the vec-iter-drop branch September 1, 2026 17:49
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

merged-by-borsThis PR was explicitly merged by bors.T-libsRelevant 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.

12 participants

@kpreid@rustbot@Kobzol@rust-timer@lqd@cyb0124@Mark-Simulacrum@rust-log-analyzer@jhpratt@nnethercote@scottmcm@wesleywiser
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Universal Dark Mode - works on any site\n(function() {\n var enabled = true;\n \n function applyDarkMode() {\n if (!enabled) return;\n \n // Create style element if it doesn't exist\n var style = document.getElementById('universal-dark-mode-style');\n if (!style) {\n style = document.createElement('style');\n style.id = 'universal-dark-mode-style';\n document.head.appendChild(style);\n }\n \n // Dark mode CSS - inverts colors but preserves images/video\n style.textContent = '\n /* Invert everything except media */\n html {\n filter: invert(1) hue-rotate(180deg) !important;\n background: #1a1a2e !important;\n }\n \n /* Restore images, videos, iframes, canvas */\n img, video, iframe, canvas, svg, picture, [style*=\"background-image\"] {\n filter: invert(1) hue-rotate(180deg) !important;\n }\n \n /* Preserve specific elements that should not be inverted */\n .no-dark-mode, .no-dark-mode *,\n [data-theme=\"light\"], [data-theme=\"light\"],\n .ace_editor, .ace_editor *,\n .CodeMirror, .CodeMirror *,\n .monaco-editor, .monaco-editor *,\n .markdown-body pre, .markdown-body pre *,\n .highlight, .highlight *,\n pre code, pre code * {\n filter: none !important;\n }\n \n /* Fix common UI elements */\n .modal, .popup, .dropdown-menu, .tooltip, .popover {\n filter: invert(1) hue-rotate(180deg) !important;\n background: #2d2d44 !important;\n border-color: #444 !important;\n }\n \n /* Scrollbars */\n ::-webkit-scrollbar { background: #1a1a2e !important; }\n ::-webkit-scrollbar-thumb { background: #444 !important; }\n ::-webkit-scrollbar-thumb:hover { background: #555 !important; }\n \n /* Selection */\n ::selection { background: #4ecdc4 !important; color: #1a1a2e !important; }\n ::-moz-selection { background: #4ecdc4 !important; color: #1a1a2e !important; }\n ';\n }\n \n function removeDarkMode() {\n var style = document.getElementById('universal-dark-mode-style');\n if (style) style.remove();\n }\n \n // Toggle with Alt+Shift+D\n document.addEventListener('keydown', function(e) {\n if (e.altKey && e.shiftKey && e.key === 'D') {\n e.preventDefault();\n enabled = !enabled;\n if (enabled) {\n applyDarkMode();\n console.log('[Universal Dark Mode] Enabled');\n } else {\n removeDarkMode();\n console.log('[Universal Dark Mode] Disabled');\n }\n }\n });\n \n // Apply on load\n applyDarkMode();\n \n // Re-apply on dynamic content\n var observer = new MutationObserver(function(mutations) {\n if (enabled && !document.getElementById('universal-dark-mode-style')) {\n applyDarkMode();\n }\n });\n observer.observe(document.head, { childList: true });\n \n console.log('[Universal Dark Mode] Loaded - Press Alt+Shift+D to toggle');\n})();", "Universal Dark Mode"); } } catch(__e) { console.warn('[Userscript:Universal Dark Mode]', __e); } })(); })();
Skip to content

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

Merged
rust-bors[bot] merged 1 commit into
rust-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 into
rust-lang:mainfrom
kpreid:vec-iter-drop

Conversation

@kpreid

@kpreidkpreid commented Nov 4, 2025

Copy link
Copy Markdown
Contributor

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.

@rustbotrustbot 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

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

@bors try @rust-timer queue

@rust-timer

This comment has been minimized.

@rust-bors

This comment has been minimized.

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

rust-borsBot commented Nov 4, 2025

Copy link
Copy Markdown
Contributor

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

@rust-timer

This comment has been minimized.

Comment threadtests/codegen-llvm/vec-into-iter-drops.rs
Comment threadlibrary/alloc/src/vec/into_iter.rs Outdated
@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.

meanrangecount
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.

meanrangecount
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.

meanrangecount
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.

meanrangecount
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%)

@rustbotrustbot removed the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Nov 5, 2025
@lqd

lqd commented Nov 5, 2025

Copy link
Copy Markdown
Member

@bors try @rust-timer queue

@rust-timer

This comment has been minimized.

@rust-bors

This comment has been minimized.

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

rust-borsBot commented Nov 5, 2025

Copy link
Copy Markdown
Contributor

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

@rust-timer

This comment has been minimized.

@cyb0124

Copy link
Copy Markdown

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)]pubstructA{_a:Option<Box<Option<A>>>,}pubfntest(slot:&mutOption<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
ContributorAuthor

@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.

meanrangecount
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.

meanrangecount
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.

meanrangecount
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.

meanrangecount
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%)

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

cyb0124 commented Nov 6, 2025

Copy link
Copy Markdown

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
ContributorAuthor

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.

rust-borsBot 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
ContributorAuthor

Rerolling reviewer due to lack of response.

r? libs

@rustbotrustbot assigned jhpratt and unassigned scottmcmApr 3, 2026
@jhpratt

Copy link
Copy Markdown
Member

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-borsBot 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.

@rustbotrustbot added the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Apr 7, 2026
@Kobzol

Kobzol commented Apr 7, 2026

Copy link
Copy Markdown
Member

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

@rust-bors

rust-borsBot commented Apr 7, 2026

Copy link
Copy Markdown
Contributor

☀️ 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.

meanrangecount
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.

meanrangecount
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.

meanrangecount
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.

meanrangecount
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%)

@rustbotrustbot 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

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

@bors r+

@rust-bors

rust-borsBot commented Apr 8, 2026

Copy link
Copy Markdown
Contributor

📌 Commit f700fdc has been approved by jhpratt

It is now in the queue for this repository.

@rust-borsrust-borsBot 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-borsrust-borsBot 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

rust-borsBot commented Apr 8, 2026

Copy link
Copy Markdown
Contributor

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

@rust-bors
rust-borsBot merged commit 30d0309 into rust-lang:mainApr 8, 2026
13 checks passed
@rustbotrustbot added this to the 1.96.0 milestone Apr 8, 2026
@github-actions

Copy link
Copy Markdown
Contributor
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.

meanrangecount
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.

meanrangecount
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.

meanrangecount
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.

meanrangecount
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%)

@kpreid
kpreid deleted the vec-iter-drop branch September 1, 2026 17:49
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

merged-by-borsThis PR was explicitly merged by bors.T-libsRelevant 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.

12 participants

@kpreid@rustbot@Kobzol@rust-timer@lqd@cyb0124@Mark-Simulacrum@rust-log-analyzer@jhpratt@nnethercote@scottmcm@wesleywiser