Vec::resize for bytes should be a single memset by scottmcm · Pull Request #120050 · rust-lang/rust (original) (raw)

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 andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Conversation

This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters

[ Show hidden characters]({{ revealButtonHref }})

scottmcm

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

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

@rust-timer

This comment has been minimized.

bors added a commit to rust-lang-ci/rust that referenced this pull request

Jan 17, 2024

@bors

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

@bors

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

@rust-timer

This comment has been minimized.

@rust-timer

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

@Kobzol

@scottmcm

@rust-timer

This comment has been minimized.

@bors

bors added a commit to rust-lang-ci/rust that referenced this pull request

Jan 17, 2024

@bors

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

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

@rust-timer

This comment has been minimized.

@rust-timer

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

@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

@rust-timer

This comment has been minimized.

@bors

@Dylan-DPC

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

@scottmcm

@scottmcm

@rust-timer

This comment has been minimized.

@bors

bors added a commit to rust-lang-ci/rust that referenced this pull request

Sep 18, 2024

@bors

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.

scottmcm

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 {

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

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

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

@rust-timer

This comment has been minimized.

@rust-timer

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

JarvisCraft

T: Copy,
{
fn spec_extend_elem(&mut self, n: usize, value: T) {
self.extend_elem_copy(n, value)

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.

bors added a commit to rust-lang-ci/rust that referenced this pull request

Nov 30, 2024

@bors

…epeatn, r=

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?

bors added a commit to rust-lang-ci/rust that referenced this pull request

Dec 1, 2024

@bors

…epeatn, r=

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

@paolobarbolini

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.