Skip to content

Use try_fold in Vec::extend_desugared#156440

Open
el-ev wants to merge 1 commit into
rust-lang:mainfrom
el-ev:perf_collect
Open

Use try_fold in Vec::extend_desugared#156440
el-ev wants to merge 1 commit into
rust-lang:mainfrom
el-ev:perf_collect

Conversation

@el-ev
Copy link
Copy Markdown
Contributor

@el-ev el-ev commented May 11, 2026

Try to revive #68046.
Resolves #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.

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

rustbot commented May 11, 2026

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

@el-ev el-ev marked this pull request as draft May 11, 2026 08:39
@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels May 11, 2026
@el-ev
Copy link
Copy Markdown
Contributor Author

el-ev commented May 11, 2026

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%                                                                                                                     

@el-ev el-ev marked this pull request as ready for review May 11, 2026 09:52
@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels May 11, 2026
@the8472
Copy link
Copy Markdown
Member

the8472 commented May 11, 2026

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

@bors try @rust-timer queue

@rust-timer

This comment has been minimized.

@rustbot rustbot added the S-waiting-on-perf Status: Waiting on a perf run to be completed. label May 11, 2026
@rust-bors

This comment has been minimized.

rust-bors Bot pushed a commit that referenced this pull request May 11, 2026
Use `try_fold` in `Vec::extend_desugared`
@rust-bors
Copy link
Copy Markdown
Contributor

rust-bors Bot commented May 11, 2026

☀️ Try build successful (CI)
Build commit: dc931d4 (dc931d446f7059c1a67b165a4c202d6107484ba5, parent: 64a965e9013a9d14e83c4d370af26f6be6bf96fb)

@rust-timer

This comment has been minimized.

@rust-timer
Copy link
Copy Markdown
Collaborator

Error occured while categorizing benchmark run:

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

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

Labels

S-waiting-on-perf Status: Waiting on a perf run to be completed. S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs Relevant to the library team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

chain() make collect very slow

5 participants