rust icon indicating copy to clipboard operation
rust copied to clipboard

Use `try_fold` in `Vec::extend_desugared`

Open IrisUntainted opened this issue 6 months ago • 21 comments

View all comments

Try to revive rust-lang/rust#68046. Resolves rust-lang/rust#63340.

Vec::extend_desugared is the fallback path for collect&extend from non-TrustedLen iterators. It previously used while let Some(element) = iterator.next(), which makes adapters like Chain to execute extra code on every call to next().

This PR switches to try_fold, which lets adapters use their specialized fold implementations, avoiding overhead.

IrisUntainted avatar May 11 '26 08:05 IrisUntainted

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

Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: libs
  • libs expanded to 7 candidates
  • Random selection from Mark-Simulacrum, nia-e

rustbot avatar May 11 '26 08:05 rustbot

Tested locally:

bench_extend_chain_filter                                                                                                                                                    
                            main (ns)   HEAD (ns)   change                                                                                                                     
    0000_0010                  189.88      158.91     -16%                                                                                                                     
    0000_0100                  724.40      339.66     -53%                                                                                                                     
    0000_1000                 5106.20     1248.02     -76%                                                                                                                     
    0010_0010                  186.01      128.01     -31%                                                                                                                     
    0100_0100                  602.00      205.02     -66%                                                                                                                     
    1000_1000                 5251.13     1167.29     -78%
                                                                                                                                                                               
bench_collect_chain_filter                                      
                            main (ns)   HEAD (ns)   change                                                                                                                     
    0010                       142.93      137.68      -4%        
    0100                       512.12      305.18     -40%
    1000                      3004.60     1267.32     -58%                                                                                                                     
   
bench_extend_flatten                                                                                                                                                         
                            main (ns)   HEAD (ns)   change        
    0001_0010                  108.39      115.08      +6%
    0010_0100                  838.90      534.83     -36%                                                                                                                     
    0100_0100                 6745.96     1984.14     -71%
    1000_0010                 6196.60     1655.14     -73%                                                                                                                     

IrisUntainted avatar May 11 '26 09:05 IrisUntainted

Vec code is very hot and highly responsive to perturbations, let's see what perf says

@bors try @rust-timer queue

the8472 avatar May 11 '26 18:05 the8472

Awaiting bors try build completion.

@rustbot label: +S-waiting-on-perf

rust-timer avatar May 11 '26 18:05 rust-timer

:hourglass: Trying commit 278c76e32485e04f2df4bfdad7edcf027d85b730 with merge dc931d446f7059c1a67b165a4c202d6107484ba5…

To cancel the try build, run the command @bors try cancel.

Workflow: https://github.com/rust-lang/rust/actions/runs/25688568632

rust-bors[bot] avatar May 11 '26 18:05 rust-bors[bot]

:sunny: Try build successful (CI) Build commit: dc931d446f7059c1a67b165a4c202d6107484ba5 (dc931d446f7059c1a67b165a4c202d6107484ba5, parent: 64a965e9013a9d14e83c4d370af26f6be6bf96fb)

rust-bors[bot] avatar May 11 '26 20:05 rust-bors[bot]

Queued dc931d446f7059c1a67b165a4c202d6107484ba5 with parent 64a965e9013a9d14e83c4d370af26f6be6bf96fb, future comparison URL. There are currently 0 preceding artifacts in the queue. It will probably take at least ~1.0 hours until the benchmark run finishes.

rust-timer avatar May 11 '26 20:05 rust-timer

Error occured while categorizing benchmark run:

could not find start commit for bound Commit("64a965e9013a9d14e83c4d370af26f6be6bf96fb")

rust-timer avatar May 11 '26 21:05 rust-timer

@rust-timer queue

mati865 avatar May 13 '26 21:05 mati865

Awaiting bors try build completion.

@rustbot label: +S-waiting-on-perf

rust-timer avatar May 13 '26 21:05 rust-timer

@rust-timer build dc931d446f7059c1a67b165a4c202d6107484ba5

mati865 avatar May 13 '26 21:05 mati865

Failed to enqueue some commit SHAs. Maybe they were already benchmarked?

rust-timer avatar May 13 '26 21:05 rust-timer

Scratch that. Let's start over.

@bors try @rust-timer queue

mati865 avatar May 13 '26 21:05 mati865

This pull request is already queued and waiting for a try build to finish.

rust-timer avatar May 13 '26 21:05 rust-timer

:hourglass: Trying commit 278c76e32485e04f2df4bfdad7edcf027d85b730 with merge 8902743fec7a53b48dd90e2ff86517f69bd1fe0f…

To cancel the try build, run the command @bors try cancel.

Workflow: https://github.com/rust-lang/rust/actions/runs/25826941847

rust-bors[bot] avatar May 13 '26 21:05 rust-bors[bot]

:sunny: Try build successful (CI) Build commit: 8902743fec7a53b48dd90e2ff86517f69bd1fe0f (8902743fec7a53b48dd90e2ff86517f69bd1fe0f, parent: 800892799d7666fe1dc17abd862100a6cf273718)

rust-bors[bot] avatar May 13 '26 23:05 rust-bors[bot]

Queued 8902743fec7a53b48dd90e2ff86517f69bd1fe0f with parent 800892799d7666fe1dc17abd862100a6cf273718, future comparison URL. There is currently 1 preceding artifact in the queue. It will probably take at least ~1.9 hours until the benchmark run finishes.

rust-timer avatar May 13 '26 23:05 rust-timer

Finished benchmarking commit (8902743fec7a53b48dd90e2ff86517f69bd1fe0f): comparison URL.

Overall result: ❌✅ regressions and improvements - please read:

Benchmarking means the PR may be perf-sensitive. It's automatically marked not fit for rolling up. Overriding is possible but disadvised: it risks changing compiler perf.

Next, please: If you can, justify the regressions found in this try perf run in writing along with @rustbot label: +perf-regression-triaged. If not, fix the regressions and do another perf run. Neutral or positive results will clear the label automatically.

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

Instruction count

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

mean range count
Regressions ❌
(primary)
7.6% [0.2%, 274.5%] 46
Regressions ❌
(secondary)
0.9% [0.1%, 4.0%] 5
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-0.2% [-0.3%, -0.0%] 16
All ❌✅ (primary) 7.6% [0.2%, 274.5%] 46

Max RSS (memory usage)

Results (primary 2.8%, secondary -2.5%)

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

mean range count
Regressions ❌
(primary)
3.6% [0.8%, 14.2%] 7
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
-2.7% [-2.7%, -2.7%] 1
Improvements ✅
(secondary)
-2.5% [-2.6%, -2.3%] 2
All ❌✅ (primary) 2.8% [-2.7%, 14.2%] 8

Cycles

Results (primary 16.7%, secondary 3.7%)

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

mean range count
Regressions ❌
(primary)
16.7% [1.4%, 243.0%] 18
Regressions ❌
(secondary)
3.7% [2.2%, 5.1%] 5
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) 16.7% [1.4%, 243.0%] 18

Binary size

Results (primary 1.2%, secondary 0.6%)

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

mean range count
Regressions ❌
(primary)
1.2% [0.0%, 4.0%] 60
Regressions ❌
(secondary)
0.6% [0.0%, 1.0%] 9
Improvements ✅
(primary)
-0.2% [-0.3%, -0.2%] 2
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) 1.2% [-0.3%, 4.0%] 62

Bootstrap: 507.838s -> 524.419s (3.27%) Artifact size: 398.10 MiB -> 402.39 MiB (1.08%)

rust-timer avatar May 14 '26 00:05 rust-timer

@joboet https://github.com/rust-lang/rust/pull/138752 shows a similar +260% performance loss. Any idea what causes that?

hkBst avatar May 27 '26 08:05 hkBst

You can see in the diff of the codegen profiles (direct link) that something about this likely causes CGUs to shuffle such that the incr-println scenario now invalidates a bunch more LLVM CGUs, resulting in a ton more work.

I haven't yet had a chance to look at the implementation delta, but generally speaking I'm not particularly worried about the perf result there -- it's a fairly random effect most likely.

Mark-Simulacrum avatar Jun 07 '26 02:06 Mark-Simulacrum