-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Missed code elimination with std::mem::replace/std::mem::swap #44701
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
Comments
Haven't studied the ASM in detail, but with 1.19 and earlier the drop function contains a lot less assembly output. Maybe this is related to #40454 ? |
Indeed, 1.19 doesn't use a temporary, and #40454 looks suspicious, so I've investigated a little: https://godbolt.org/g/Fo2bZ1 . With 1.20, beta and nightly, So it looks like there has been a regression in the optimizer since 1.20, and the new |
@aidanhs The label should probably be changed since the issue appears to be a recent regression. |
Yes, this looks almost identical to the |
Actually, if this is about |
Edit: As pointed out by @oyvindln, the problem appears to be a regression introduced in 1.20.
Demonstration: https://godbolt.org/g/5uuzVL
Version: rustc 1.22.0-nightly (277476c 2017-09-16) , -C opt-level=3 -C target-cpu=native
Code:
Expected result: An optimal code would move
self.buf
directly intoself.pool
and then resetself.buf
in-place. An acceptable code would moveself.buf
into a temporary on the stack, move the temporary intoself.pool
and resetself.buf
in-place.Observed result:
std::Vec<u8>
(each 24 bytes) is allocated on the stack,-48(%rbp)
(A) and-96(%rbp)
(B).self.buf
is copied to A.self.buf
is reset in-place.self.pool
.Steps 4 and 5 is a completely unnecessary copying of 48 bytes and could be safely removed. Replacing
std::mem::replace
with an equivalentstd::mem::swap
call produces a slightly different code with the same basic problem.Compiler output:
The text was updated successfully, but these errors were encountered: