Skip to content
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

Vec::resize for bytes should be a single memset #120050

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

scottmcm
Copy link
Member

Really I just started by trying to see if specializing iter::repeat_n would help the perf issue that kept me from removing Vec::extend_with last time I tried, but I noticed in the process that a resize for bytes doesn't set all the new space with a single memset: https://play.rust-lang.org/?version=nightly&mode=release&edition=2021&gist=35175ec844b46fcd95e2d0aad526859e

So using repeat_n to implement it -- like VecDeque uses, with the specialization for next to avoid a branch -- means that the optimizer notices the resize can set all the values with a single memset.

@rustbot
Copy link
Collaborator

rustbot commented Jan 17, 2024

r? @m-ou-se

(rustbot has picked a reviewer for you, use r? to override)

@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 Jan 17, 2024
@scottmcm
Copy link
Member Author

@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 Jan 17, 2024
bors added a commit to rust-lang-ci/rust that referenced this pull request Jan 17, 2024
`Vec::resize` for bytes should be a single `memset`

Really I just started by trying to see if specializing `iter::repeat_n` would help the perf issue that kept me from removing `Vec::extend_with` last time I tried, but I noticed in the process that a resize for bytes doesn't set all the new space with a single `memset`: <https://play.rust-lang.org/?version=nightly&mode=release&edition=2021&gist=35175ec844b46fcd95e2d0aad526859e>

So using `repeat_n` to implement it -- like `VecDeque` uses, with the specialization for `next` to avoid a branch -- means that the optimizer notices the resize can set all the values with a single memset.
@bors
Copy link
Contributor

bors commented Jan 17, 2024

⌛ Trying commit 8652062 with merge 0db48a7...

@bors
Copy link
Contributor

bors commented Jan 17, 2024

☀️ Try build successful - checks-actions
Build commit: 0db48a7 (0db48a7c3c2ca5e498da14eae53a7474e16886df)

@rust-timer

This comment has been minimized.

@rust-timer
Copy link
Collaborator

Finished benchmarking commit (0db48a7): comparison URL.

Overall result: ❌✅ regressions and improvements - ACTION NEEDED

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 a highly reliable metric that was used to determine the overall result at the top of this comment.

mean range count
Regressions ❌
(primary)
0.4% [0.2%, 0.5%] 15
Regressions ❌
(secondary)
3.3% [3.1%, 3.7%] 6
Improvements ✅
(primary)
-1.5% [-1.5%, -1.5%] 1
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) 0.2% [-1.5%, 0.5%] 16

Max RSS (memory usage)

Results

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)
2.9% [0.3%, 5.5%] 2
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
-8.3% [-12.7%, -4.7%] 4
Improvements ✅
(secondary)
-4.9% [-4.9%, -4.9%] 1
All ❌✅ (primary) -4.6% [-12.7%, 5.5%] 6

Cycles

Results

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)
20.2% [18.6%, 22.7%] 6
Improvements ✅
(primary)
-1.5% [-1.5%, -1.5%] 1
Improvements ✅
(secondary)
-2.9% [-4.3%, -2.3%] 5
All ❌✅ (primary) -1.5% [-1.5%, -1.5%] 1

Binary size

This benchmark run did not return any relevant results for this metric.

Bootstrap: 665.423s -> 664.993s (-0.06%)
Artifact size: 308.34 MiB -> 308.35 MiB (0.01%)

@rustbot rustbot added perf-regression Performance regression. and removed S-waiting-on-perf Status: Waiting on a perf run to be completed. labels Jan 17, 2024
@Kobzol
Copy link
Contributor

Kobzol commented Jan 17, 2024

@scottmcm
Copy link
Member Author

@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 Jan 17, 2024
@bors
Copy link
Contributor

bors commented Jan 17, 2024

⌛ Trying commit 6c32590 with merge 27371af...

bors added a commit to rust-lang-ci/rust that referenced this pull request Jan 17, 2024
`Vec::resize` for bytes should be a single `memset`

Really I just started by trying to see if specializing `iter::repeat_n` would help the perf issue that kept me from removing `Vec::extend_with` last time I tried, but I noticed in the process that a resize for bytes doesn't set all the new space with a single `memset`: <https://play.rust-lang.org/?version=nightly&mode=release&edition=2021&gist=35175ec844b46fcd95e2d0aad526859e>

So using `repeat_n` to implement it -- like `VecDeque` uses, with the specialization for `next` to avoid a branch -- means that the optimizer notices the resize can set all the values with a single memset.
@bors
Copy link
Contributor

bors commented Jan 17, 2024

☀️ Try build successful - checks-actions
Build commit: 27371af (27371af9b8c50c2f3b165000d3bcd9684c4a0807)

@rust-timer

This comment has been minimized.

@rust-timer
Copy link
Collaborator

Finished benchmarking commit (27371af): comparison URL.

Overall result: ❌✅ regressions and improvements - ACTION NEEDED

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 a highly reliable metric that was used to determine the overall result at the top of this comment.

mean range count
Regressions ❌
(primary)
0.4% [0.3%, 0.7%] 13
Regressions ❌
(secondary)
2.1% [1.9%, 2.3%] 6
Improvements ✅
(primary)
-0.5% [-0.9%, -0.3%] 4
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) 0.2% [-0.9%, 0.7%] 17

Max RSS (memory usage)

Results

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)
4.3% [0.2%, 8.4%] 2
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
-2.5% [-3.7%, -1.4%] 2
Improvements ✅
(secondary)
-1.2% [-1.5%, -0.7%] 3
All ❌✅ (primary) 0.9% [-3.7%, 8.4%] 4

Cycles

Results

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)
1.1% [1.1%, 1.1%] 1
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
-1.1% [-1.1%, -1.1%] 1
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) 0.0% [-1.1%, 1.1%] 2

Binary size

Results

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.4%, 0.9%] 29
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) 0.7% [0.4%, 0.9%] 29

Bootstrap: 663.454s -> 664.862s (0.21%)
Artifact size: 308.30 MiB -> 308.31 MiB (0.01%)

@rustbot rustbot removed the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Jan 18, 2024
@m-ou-se m-ou-se 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 Feb 14, 2024
@rust-log-analyzer

This comment has been minimized.

@scottmcm
Copy link
Member Author

scottmcm commented Jul 4, 2024

@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 Jul 4, 2024
@bors
Copy link
Contributor

bors commented Aug 4, 2024

☔ The latest upstream changes (presumably #128614) made this pull request unmergeable. Please resolve the merge conflicts.

@Dylan-DPC
Copy link
Member

@scottmcm the try run didn't get executed so you will have to resolve conflicts and run it again

@scottmcm
Copy link
Member Author

@bors try @rust-timer queue

@rust-timer

This comment has been minimized.

@bors
Copy link
Contributor

bors commented Sep 18, 2024

⌛ Trying commit 1697af2 with merge f5c3755...

bors added a commit to rust-lang-ci/rust that referenced this pull request Sep 18, 2024
`Vec::resize` for bytes should be a single `memset`

Really I just started by trying to see if specializing `iter::repeat_n` would help the perf issue that kept me from removing `Vec::extend_with` last time I tried, but I noticed in the process that a resize for bytes doesn't set all the new space with a single `memset`: <https://play.rust-lang.org/?version=nightly&mode=release&edition=2021&gist=35175ec844b46fcd95e2d0aad526859e>

So using `repeat_n` to implement it -- like `VecDeque` uses, with the specialization for `next` to avoid a branch -- means that the optimizer notices the resize can set all the values with a single memset.
Comment on lines +3015 to +3018
// Because there's no user code being run here, we can skip it for ZSTs.
// That helps tests in debug mode that do things like `vec![(); HUGE]`.
// See <https://github.com/rust-lang/rust/pull/118094>
if !T::IS_ZST {
Copy link
Member Author

@scottmcm scottmcm Sep 18, 2024

Choose a reason for hiding this comment

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

cc @JarvisCraft in case you have thoughts on this approach, since I removed your specialization from #118094 in this PR

Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks for tagging me! I've commented below on what I am unsure about, though the optimization really looks promising.

@bors
Copy link
Contributor

bors commented Sep 18, 2024

☀️ Try build successful - checks-actions
Build commit: f5c3755 (f5c37550f884726ba52e559b2750e8585e7c12da)

@rust-timer

This comment has been minimized.

@rust-timer
Copy link
Collaborator

Finished benchmarking commit (f5c3755): comparison URL.

Overall result: ❌✅ regressions and improvements - ACTION NEEDED

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 a highly reliable metric that was used to determine the overall result at the top of this comment.

mean range count
Regressions ❌
(primary)
0.3% [0.2%, 0.4%] 7
Regressions ❌
(secondary)
0.5% [0.2%, 0.8%] 11
Improvements ✅
(primary)
-0.4% [-0.4%, -0.4%] 1
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) 0.2% [-0.4%, 0.4%] 8

Max RSS (memory usage)

Results (primary 0.9%, secondary -2.5%)

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.8% [3.8%, 7.8%] 2
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
-2.4% [-3.5%, -1.4%] 3
Improvements ✅
(secondary)
-2.5% [-2.5%, -2.5%] 1
All ❌✅ (primary) 0.9% [-3.5%, 7.8%] 5

Cycles

Results (secondary 2.5%)

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)
2.5% [2.4%, 2.6%] 2
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) - - 0

Binary size

Results (primary 0.2%)

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.3% [0.1%, 0.4%] 28
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
-0.1% [-0.2%, -0.0%] 11
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) 0.2% [-0.2%, 0.4%] 39

Bootstrap: 768.777s -> 770.588s (0.24%)
Artifact size: 341.28 MiB -> 341.28 MiB (-0.00%)

@rustbot rustbot removed the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Sep 18, 2024
T: Copy,
{
fn spec_extend_elem(&mut self, n: usize, value: T) {
self.extend_elem_copy(n, value)
Copy link
Contributor

@JarvisCraft JarvisCraft Sep 18, 2024

Choose a reason for hiding this comment

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

I am not sure if this specialization is correct for all cases of vec![ZST; N].

At the moment:

  1. impl Copy for Foo permits <Foo as Clone>::clone to have side-effects.
  2. The docs of vec! explicitly mention that it works via Clone.

Which means that at the moment for vec![ZST; N] any effects of ZST::clone are observed N times as can be seen in the example. With this change, they won't since the specialization skips any calls to this method.

This is the reason why I've only implemented the specialization for () previously.

As mentioned in #118094 (comment), this kind of change is still allowed, although I expect that there must be an explicit proof that this is a valid optimization and an update to vec!'s doc is probably required to explicitly state that such optimization may occur.

An alternative may be to add a perma-unstable fn to Clone like is_trivially_cloneable() defaulting to false and only overridable by rustc on derive, but this of course is more tedious.

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.

9 participants