-
Notifications
You must be signed in to change notification settings - Fork 13.8k
Make Rc<T>::deref
zero-cost
#141348
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
base: master
Are you sure you want to change the base?
Make Rc<T>::deref
zero-cost
#141348
Conversation
This comment has been minimized.
This comment has been minimized.
df34f84
to
d3a7429
Compare
This comment has been minimized.
This comment has been minimized.
bc84ec6
to
19fb34b
Compare
The Miri subtree was changed cc @rust-lang/miri |
19fb34b
to
f5245ba
Compare
@bors try @rust-timer queue |
This comment has been minimized.
This comment has been minimized.
Make `Rc<T>::deref` zero-cost This PR makes `Rc::deref` zero-cost by changing the internal pointer so that it points to the value directly instead of the allocation. This is split out from #132553, which will also make `Arc::deref` zero-cost.
☀️ Try build successful - checks-actions |
This comment has been minimized.
This comment has been minimized.
Finished benchmarking commit (8ef4a25): comparison URL. Overall result: ❌✅ regressions and improvements - please read the text belowBenchmarking 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 the most reliable metric that we have; it was used to determine the overall result at the top of this comment. However, even this metric can sometimes exhibit noise.
Max RSS (memory usage)Results (primary -0.3%, secondary -0.0%)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 (primary -0.5%, secondary -1.9%)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%, secondary 1.6%)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: 775.728s -> 776.531s (0.10%) |
Reminder, once the PR becomes ready for a review, use |
62c8b9c
to
eccd4c2
Compare
This comment has been minimized.
This comment has been minimized.
eccd4c2
to
1266321
Compare
☔ The latest upstream changes (presumably #147197) made this pull request unmergeable. Please resolve the merge conflicts. |
1266321
to
616991d
Compare
This comment has been minimized.
This comment has been minimized.
☔ The latest upstream changes (presumably #147340) made this pull request unmergeable. Please resolve the merge conflicts. |
616991d
to
31d5740
Compare
This PR was rebased onto a different master commit. Here's a range-diff highlighting what actually changed. Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers. |
//! | Padding | `size_of::<RefCounts>().next_multiple_of(align_of::<T>()) - size_of::<RefCounts>()` | | ||
//! | `RefCounts` | `size_of::<RefCounts>()` | | ||
//! | `T` | `size_of::<T>()` | |
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.
So this puts the data after the refcount. Would it later still be possible to change it to data, padding and then the refcounts? That'd make it possible to convert a Rc<[T]>
to Vec<T>
without memcpy.
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.
@the8472: I think there are several problems:
- The current design employs the strategy from [DRAFT]
Rc
: allow deduping bothderef
andclone
across types #133061, where using a fixed offset of the reference counter could potentially allowing sharing the sameclone
implementation across differentRc
types. - Not using a fixed offset makes the debug pretty printer more difficult to write.
- The buffer allocated with
Rc
has alignment ofalign_of::<RefCounts>().max(align_of::<T>())
, which might be different withalign_of::<T>()
. SinceVec<T>
will deallocate using aLayout
withalign_of::<T>()
, and deallocating requires the alignment being exactly the same as the one that was used to do allocation according to https://doc.rust-lang.org/stable/std/alloc/trait.Allocator.html#memory-fitting,Vec<T>
might not be able to deallocate the memory allocated byRc<[T]>
, at least not with the currentVec
implementation.
This PR makes
Rc::deref
zero-cost by changing the internal pointer to point directly to the value instead of to the allocation.This PR is split from #132553, which will also make
Arc::deref
zero-cost.Review status:
RefCounts
andRcLayout
typesRefCounter
traitRawWeak
typeRawWeak
methods for sized valuesRawWeak
methods for slice valuesRawWeak
RawRc
typeRawRc
methods for sized valuesRawRc
methods forMaybeUninit<T>
valuesRawRc
methods for slice valuesRawRc
methods fordyn Any
typeRawRc
RawUniqueRc
typeRawUniqueRc
methods for sized valuesRawUniqueRc
alloc::rc::{Rc,Weak,UniqueRc}
withalloc::raw_rc
typesRc
implementation