Skip to content

Use iter::repeat_n to implement Vec::extend_with #133662

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

paolobarbolini
Copy link
Contributor

@paolobarbolini paolobarbolini commented Nov 30, 2024

This replaces the Vec::extend_with manual implementation with iter::repeat_n, simplifying the code and fixing the issue identified in #120050.

@rustbot
Copy link
Collaborator

rustbot commented Nov 30, 2024

r? @Noratrieb

rustbot has assigned @Noratrieb.
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

@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 Nov 30, 2024
@joboet
Copy link
Member

joboet commented Nov 30, 2024

Let's see what effect this has in the compiler!
@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 Nov 30, 2024
@bors
Copy link
Collaborator

bors commented Nov 30, 2024

⌛ Trying commit 4bf7072 with merge 243c5cc...

bors added a commit to rust-lang-ci/rust that referenced this pull request Nov 30, 2024
…epeatn, r=<try>

Use `iter::repeat_n` to implement `Vec::extend_with`

This replaces the `Vec::extend_with` manual implementation, which is used by `Vec::resize` and `Vec` `SpecFromElem`, with `iter::repeat_n`.

I've compared the codegen output between:

1. the current `Vec::resize` impl
2. this branch
3. this branch + rust-lang#130887

3 gives the closest codegen output to 1, with some output improvements. 2 doesn't look good: https://rust.godbolt.org/z/Yrc83EhjY.
May also help rust-lang#120050?
@Noratrieb
Copy link
Member

given that this branch doesn't look good without #130887, let's block it on that.
@rustbot blocked

@rustbot rustbot added S-blocked Status: Blocked on something else such as an RFC or other implementation work. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Nov 30, 2024
@bors
Copy link
Collaborator

bors commented Nov 30, 2024

☀️ Try build successful - checks-actions
Build commit: 243c5cc (243c5cc6905efbdf778394d96f4b83701962964f)

@rust-timer

This comment has been minimized.

@rust-timer
Copy link
Collaborator

Finished benchmarking commit (243c5cc): comparison URL.

Overall result: ❌✅ regressions and improvements - please read the text below

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

Next Steps: If you can justify the regressions found in this try perf run, please indicate this with @rustbot label: +perf-regression-triaged along with sufficient written justification. If you cannot justify the regressions please fix the regressions and do another perf run. If the next run shows neutral or positive results, the label will be automatically removed.

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

Instruction count

This is the most reliable metric that we have; it was used to determine the overall result at the top of this comment. However, even this metric can sometimes exhibit noise.

mean range count
Regressions ❌
(primary)
0.3% [0.1%, 0.7%] 16
Regressions ❌
(secondary)
0.4% [0.1%, 0.6%] 9
Improvements ✅
(primary)
-0.9% [-0.9%, -0.9%] 1
Improvements ✅
(secondary)
-1.3% [-1.8%, -0.5%] 8
All ❌✅ (primary) 0.3% [-0.9%, 0.7%] 17

Max RSS (memory usage)

Results (primary 0.1%)

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

mean range count
Regressions ❌
(primary)
5.6% [0.9%, 11.4%] 3
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
-8.1% [-11.1%, -5.0%] 2
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) 0.1% [-11.1%, 11.4%] 5

Cycles

Results (secondary -2.1%)

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

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

Binary size

Results (primary 0.4%)

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

mean range count
Regressions ❌
(primary)
0.5% [0.0%, 1.3%] 43
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
-0.1% [-0.2%, -0.0%] 7
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) 0.4% [-0.2%, 1.3%] 50

Bootstrap: 773.805s -> 774.205s (0.05%)
Artifact size: 332.32 MiB -> 331.64 MiB (-0.20%)

@rustbot rustbot added perf-regression Performance regression. and removed S-waiting-on-perf Status: Waiting on a perf run to be completed. labels Nov 30, 2024
@paolobarbolini
Copy link
Contributor Author

paolobarbolini commented Dec 1, 2024

Could we do another perf run? I've cherry-picked the commit from #130887 since, at least via Compiler Explorer, the codegen looked better

@joboet
Copy link
Member

joboet commented Dec 1, 2024

Sure!
@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 Dec 1, 2024
@bors
Copy link
Collaborator

bors commented Dec 1, 2024

⌛ Trying commit b080e18 with merge 78d5ed4...

bors added a commit to rust-lang-ci/rust that referenced this pull request Dec 1, 2024
…epeatn, r=<try>

Use `iter::repeat_n` to implement `Vec::extend_with`

This replaces the `Vec::extend_with` manual implementation, which is used by `Vec::resize` and `Vec` `SpecFromElem`, with `iter::repeat_n`.

I've compared the codegen output between:

1. the current `Vec::resize` impl
2. this branch
3. this branch + rust-lang#130887

3 gives the closest codegen output to 1, with some output improvements. 2 doesn't look good: https://rust.godbolt.org/z/Yrc83EhjY.
May also help rust-lang#120050?

---

WARNING: DO NOT MERGE - in order to run the perf run in rust-lang#133662 (comment) this PR currently also contains commits from rust-lang#130887
@bors
Copy link
Collaborator

bors commented Dec 1, 2024

☀️ Try build successful - checks-actions
Build commit: 78d5ed4 (78d5ed40d7c459a7e5ea4cb6bea9918a493a800f)

@rust-timer

This comment has been minimized.

@rust-timer
Copy link
Collaborator

Finished benchmarking commit (78d5ed4): comparison URL.

Overall result: ❌ regressions - please read the text below

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

Next Steps: If you can justify the regressions found in this try perf run, please indicate this with @rustbot label: +perf-regression-triaged along with sufficient written justification. If you cannot justify the regressions please fix the regressions and do another perf run. If the next run shows neutral or positive results, the label will be automatically removed.

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

Instruction count

This is the most reliable metric that we have; it was used to determine the overall result at the top of this comment. However, even this metric can sometimes exhibit noise.

mean range count
Regressions ❌
(primary)
0.4% [0.1%, 0.7%] 17
Regressions ❌
(secondary)
1.3% [0.1%, 3.2%] 16
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) 0.4% [0.1%, 0.7%] 17

Max RSS (memory usage)

Results (primary -5.6%, secondary 0.3%)

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

mean range count
Regressions ❌
(primary)
5.3% [5.3%, 5.3%] 1
Regressions ❌
(secondary)
1.5% [1.0%, 1.8%] 3
Improvements ✅
(primary)
-11.1% [-15.5%, -6.8%] 2
Improvements ✅
(secondary)
-3.2% [-3.2%, -3.2%] 1
All ❌✅ (primary) -5.6% [-15.5%, 5.3%] 3

Cycles

Results (primary -1.5%, secondary 35.1%)

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

mean range count
Regressions ❌
(primary)
0.9% [0.9%, 0.9%] 1
Regressions ❌
(secondary)
35.1% [33.0%, 39.1%] 6
Improvements ✅
(primary)
-4.0% [-4.0%, -4.0%] 1
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) -1.5% [-4.0%, 0.9%] 2

Binary size

Results (primary 0.6%)

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

mean range count
Regressions ❌
(primary)
0.7% [0.1%, 1.7%] 33
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
-0.1% [-0.2%, -0.0%] 9
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) 0.6% [-0.2%, 1.7%] 42

Bootstrap: 767.632s -> 769.266s (0.21%)
Artifact size: 332.19 MiB -> 332.16 MiB (-0.01%)

@rustbot rustbot removed the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Dec 1, 2024
@paolobarbolini
Copy link
Contributor Author

Looking at performance data borrow checking time in the Cycles benchmark for wg-grammar is the main culprit. I'm confused why that is.

The fact that this also fixes #120050 makes these changes still worth it to me.

@Soveu
Copy link
Contributor

Soveu commented Jun 18, 2025

@paolobarbolini #130887 has been merged
@rustbot label -S-blocked +S-waiting-on-author

@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-blocked Status: Blocked on something else such as an RFC or other implementation work. labels Jun 18, 2025
@paolobarbolini paolobarbolini force-pushed the vec-extend-with-via-repeatn branch from c45fdfc to 29565d7 Compare June 18, 2025 21:01
@paolobarbolini
Copy link
Contributor Author

I've rebased on top of master. Should we do another perf run?

@scottmcm
Copy link
Member

Sure, with the rebase we might as well give it a shot

@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 Jun 19, 2025
bors added a commit that referenced this pull request Jun 19, 2025
…=<try>

Use `iter::repeat_n` to implement `Vec::extend_with`

This replaces the `Vec::extend_with` manual implementation with `iter::repeat_n`, simplifying the code and also possibly fixing the issue identified in #120050.
@bors
Copy link
Collaborator

bors commented Jun 19, 2025

⌛ Trying commit 29565d7 with merge c79c24a...

@bors
Copy link
Collaborator

bors commented Jun 19, 2025

☀️ Try build successful - checks-actions
Build commit: c79c24a (c79c24ad439e510f1baa231dcaf131000f552308)

@rust-timer

This comment has been minimized.

@rust-timer
Copy link
Collaborator

Finished benchmarking commit (c79c24a): comparison URL.

Overall result: ❌✅ regressions and improvements - please read the text below

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.

Next Steps: If you can justify the regressions found in this try perf run, please do so in sufficient writing along with @rustbot label: +perf-regression-triaged. If not, please fix the regressions and do another perf run. If its results are neutral or positive, the label will be automatically removed.

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

Instruction count

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

mean range count
Regressions ❌
(primary)
0.7% [0.2%, 3.0%] 12
Regressions ❌
(secondary)
3.0% [0.2%, 7.5%] 16
Improvements ✅
(primary)
-0.9% [-1.3%, -0.5%] 2
Improvements ✅
(secondary)
-0.5% [-1.2%, -0.2%] 3
All ❌✅ (primary) 0.5% [-1.3%, 3.0%] 14

Max RSS (memory usage)

Results (primary 2.2%, secondary -0.1%)

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

mean range count
Regressions ❌
(primary)
4.9% [1.1%, 7.3%] 4
Regressions ❌
(secondary)
4.7% [3.3%, 6.5%] 3
Improvements ✅
(primary)
-3.3% [-4.2%, -2.4%] 2
Improvements ✅
(secondary)
-7.2% [-8.5%, -5.9%] 2
All ❌✅ (primary) 2.2% [-4.2%, 7.3%] 6

Cycles

Results (primary 1.7%, secondary 3.6%)

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

mean range count
Regressions ❌
(primary)
1.7% [0.7%, 3.6%] 3
Regressions ❌
(secondary)
3.6% [2.2%, 10.5%] 7
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) 1.7% [0.7%, 3.6%] 3

Binary size

Results (primary 0.6%, secondary 0.1%)

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

mean range count
Regressions ❌
(primary)
0.6% [0.1%, 1.7%] 38
Regressions ❌
(secondary)
0.1% [0.1%, 0.1%] 1
Improvements ✅
(primary)
-0.1% [-0.1%, -0.1%] 1
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) 0.6% [-0.1%, 1.7%] 39

Bootstrap: 692.944s -> 693.357s (0.06%)
Artifact size: 372.01 MiB -> 371.96 MiB (-0.01%)

@rustbot rustbot removed the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Jun 19, 2025
@paolobarbolini
Copy link
Contributor Author

@scottmcm Do you think that these results are worth it given that this also acts as an alternative to #120050?

@scottmcm
Copy link
Member

scottmcm commented Jul 25, 2025

Personally I still want to do this, but at the same time with my reviewer hat on I don't know if I can justify the perf numbers. Anywhere outside the standard library I'd just do it, but Vec is important enough that we'll take less-elegant implementations for perf reasons :/

I wonder if maybe there's a tweak for it that could help. An idea that came to mind: what if it specialized on Copy, with that path having the obvious loop (which wouldn't even need SetLenOnDrop), and used repeat_n for the more-complex non-Copy path. My instinct is that the image regression is probably dealing in trivial Copy stuff, so emitting simpler code for that (no we-have-defer-at-home codepath) could even be good.

EDIT: or, I suppose, if we're not allowed to specialize on Copy any more this could just branch on if const { needs_drop::<T>() }? I don't know that that would help as much, though.

This replaces the manual `Vec::extend_with` implementation with
`iter::repeat_n` and `Vec::extend_trusted`. This simplifies the
code and gets LLVM to remove the special case for the last element
when `T` is trivial to clone.
After running perf on the previous commit, it was found that the
compile-time regression was too significant to ignore.
This introduces a specialized, simplified implementation for
`T: Copy` that will contain the perf regression.
@paolobarbolini paolobarbolini force-pushed the vec-extend-with-via-repeatn branch from 29565d7 to bbcb772 Compare July 26, 2025 03:41
@paolobarbolini
Copy link
Contributor Author

Specializing on Copy sounds like a great idea. This should now be ready for another perf run.

@scottmcm
Copy link
Member

Let's give it a shot!

@bors try @rust-timer queue

@rust-timer

This comment has been minimized.

@rust-bors
Copy link

rust-bors bot commented Jul 26, 2025

⌛ Trying commit bbcb772 with merge 4f5a239

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

rust-bors bot added a commit that referenced this pull request Jul 26, 2025
…=<try>

Use `iter::repeat_n` to implement `Vec::extend_with`
@rustbot rustbot added the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Jul 26, 2025
// SAFETY: the above `reserve` call guarantees `n` to be in bounds.
let unfilled = unsafe { unfilled.get_unchecked_mut(..n) };
for elem in unfilled {
*elem = MaybeUninit::new(value);
Copy link
Contributor

Choose a reason for hiding this comment

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

Wouldn't unfilled.fill(MaybeUninit::new(value)) fit here better?

Copy link
Member

Choose a reason for hiding this comment

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

Lots of options. Because it's copy could even just *p.add(i) = value;.

But let's see what the perf run says before worrying too much about it.

@rust-bors
Copy link

rust-bors bot commented Jul 26, 2025

☀️ Try build successful (CI)
Build commit: 4f5a239 (4f5a23979e54bf7cdad9d71f114d5e3ccbf1d3cf, parent: 0dd07bd2165db70ffbe0b903635de26ca630bf20)

@rust-timer

This comment has been minimized.

@rust-timer
Copy link
Collaborator

Finished benchmarking commit (4f5a239): comparison URL.

Overall result: ❌✅ regressions and improvements - please read the text below

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.

Next Steps: If you can justify the regressions found in this try perf run, please do so in sufficient writing along with @rustbot label: +perf-regression-triaged. If not, please fix the regressions and do another perf run. If its results are neutral or positive, the label will be automatically removed.

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

Instruction count

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

mean range count
Regressions ❌
(primary)
0.2% [0.1%, 0.7%] 10
Regressions ❌
(secondary)
2.1% [0.1%, 6.8%] 19
Improvements ✅
(primary)
-0.2% [-0.7%, -0.1%] 26
Improvements ✅
(secondary)
-0.3% [-0.6%, -0.2%] 16
All ❌✅ (primary) -0.1% [-0.7%, 0.7%] 36

Max RSS (memory usage)

Results (primary 1.0%, secondary -2.0%)

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

mean range count
Regressions ❌
(primary)
2.7% [1.5%, 4.0%] 3
Regressions ❌
(secondary)
2.0% [0.8%, 3.2%] 2
Improvements ✅
(primary)
-3.9% [-3.9%, -3.9%] 1
Improvements ✅
(secondary)
-4.6% [-6.5%, -2.6%] 3
All ❌✅ (primary) 1.0% [-3.9%, 4.0%] 4

Cycles

Results (secondary 4.0%)

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

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

Binary size

Results (primary 0.4%, secondary 0.3%)

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

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

Bootstrap: 467.876s -> 469.154s (0.27%)
Artifact size: 376.61 MiB -> 376.65 MiB (0.01%)

@rustbot rustbot removed the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Jul 26, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
perf-regression Performance regression. S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. 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.

8 participants