-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
base: master
Are you sure you want to change the base?
Conversation
r? @m-ou-se (rustbot has picked a reviewer for you, use r? to override) |
@bors try @rust-timer queue |
This comment has been minimized.
This comment has been minimized.
`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.
☀️ Try build successful - checks-actions |
This comment has been minimized.
This comment has been minimized.
Finished benchmarking commit (0db48a7): comparison URL. Overall result: ❌✅ regressions and improvements - ACTION NEEDEDBenchmarking 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 @bors rollup=never Instruction countThis is a highly reliable metric that was used to determine the overall result at the top of this comment.
Max RSS (memory usage)ResultsThis 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.
CyclesResultsThis 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.
Binary sizeThis benchmark run did not return any relevant results for this metric. Bootstrap: 665.423s -> 664.993s (-0.06%) |
Looks like something bad has happened in borrow checking of |
@bors try @rust-timer queue |
This comment has been minimized.
This comment has been minimized.
`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.
☀️ Try build successful - checks-actions |
This comment has been minimized.
This comment has been minimized.
Finished benchmarking commit (27371af): comparison URL. Overall result: ❌✅ regressions and improvements - ACTION NEEDEDBenchmarking 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 @bors rollup=never Instruction countThis is a highly reliable metric that was used to determine the overall result at the top of this comment.
Max RSS (memory usage)ResultsThis 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.
CyclesResultsThis 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.
Binary sizeResultsThis 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.
Bootstrap: 663.454s -> 664.862s (0.21%) |
6c32590
to
f4bb10b
Compare
This comment has been minimized.
This comment has been minimized.
f4bb10b
to
dc67720
Compare
@bors try @rust-timer queue |
This comment has been minimized.
This comment has been minimized.
☔ The latest upstream changes (presumably #128614) made this pull request unmergeable. Please resolve the merge conflicts. |
@scottmcm the try run didn't get executed so you will have to resolve conflicts and run it again |
dc67720
to
1697af2
Compare
@bors try @rust-timer queue |
This comment has been minimized.
This comment has been minimized.
`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.
// 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 { |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
☀️ Try build successful - checks-actions |
This comment has been minimized.
This comment has been minimized.
Finished benchmarking commit (f5c3755): comparison URL. Overall result: ❌✅ regressions and improvements - ACTION NEEDEDBenchmarking 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 @bors rollup=never Instruction countThis is a highly reliable metric that was used to determine the overall result at the top of this comment.
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.
CyclesResults (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.
Binary sizeResults (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.
Bootstrap: 768.777s -> 770.588s (0.24%) |
T: Copy, | ||
{ | ||
fn spec_extend_elem(&mut self, n: usize, value: T) { | ||
self.extend_elem_copy(n, value) |
There was a problem hiding this comment.
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:
impl Copy for Foo
permits<Foo as Clone>::clone
to have side-effects.- The docs of
vec!
explicitly mention that it works viaClone
.
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.
Really I just started by trying to see if specializing
iter::repeat_n
would help the perf issue that kept me from removingVec::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 singlememset
: https://play.rust-lang.org/?version=nightly&mode=release&edition=2021&gist=35175ec844b46fcd95e2d0aad526859eSo using
repeat_n
to implement it -- likeVecDeque
uses, with the specialization fornext
to avoid a branch -- means that the optimizer notices the resize can set all the values with a single memset.