Make Rc<T>::deref and Arc<T>::deref zero-cost by EFanZh · Pull Request #132553 · rust-lang/rust (original) (raw)
Currently, Rc<T> and Arc<T> store pointers to RcInner<T> and ArcInner<T>. This PR changes the pointers so that they point to T directly instead.
This is based on the assumption that we access the T value more frequently than accessing reference counts. With this change, accessing the data can be done without offsetting pointers from RcInner<T> and ArcInner<T> to their contained data. This change might also enables some possibly useful future optimizations, such as:
- Convert
&[Rc<T>]into&[&T]within O(1) time. - Convert
&[Rc<T>]intoVec<&T>utilizingmemcpy. - Convert
&Option<Rc<T>>intoOption<&T>without branching. - Make
Rc<T>andArc<T>FFI compatible types whereT: Sized.