-
Notifications
You must be signed in to change notification settings - Fork 13.4k
Try to avoid locals when cloning into Box/Rc/Arc #80824
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
Conversation
r? @kennytm (rust-highfive has picked a reviewer for you, use r? to override) |
For generic `T: Clone`, we can allocate an uninitialized box beforehand, which gives the optimizer a chance to create the clone directly in the heap. For `T: Copy`, we can go further and do a simple memory copy, regardless of optimization level.
As we did with `Box`, we can allocate an uninitialized `Rc` or `Arc` beforehand, giving the optimizer a chance to skip the local value for regular clones, or avoid any local altogether for `T: Copy`.
When only other `Weak` references remain, we can directly move the data into the new unique allocation as a plain memory copy.
In addition to the |
// there is no data race in the implicit write caused by `read` | ||
// here (due to zeroing) because data is no longer accessed by | ||
// other threads (due to there being no more strong refs at this | ||
// point). |
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 have to admit, I don't really understand what this was saying about zeroing. Maybe that's outdated from the days of drop flags and such? The comment came from commit d77c4b0.
The job Click to see the possible cause of the failure (guessed by this bot)
|
The failure is because I put the shared specialization trait in Lines 160 to 168 in fe531d5
There's not a great place to put this |
Sounds good. or maybe inside |
The point is dealing with pre-allocated memory, so I decided |
@bors r+ (if we have got rust-lang/rfcs#2884 this PR wouldn't need those MaybeUninit unsafety nor the specialization trick to gain performance. ah well.) |
📌 Commit 1f1a3b4 has been approved by |
☀️ Test successful - checks-actions |
For generic
T: Clone
, we can allocate an uninitialized box beforehand,which gives the optimizer a chance to create the clone directly in the
heap. For
T: Copy
, we can go further and do a simple memory copy,regardless of optimization level.
The same applies to
Rc
/Arc::make_mut
when they must clone the data.