Make Rc<T>::deref and Arc<T>::deref zero-cost by EFanZh · Pull Request #132553 · rust-lang/rust (original) (raw)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is because passing a ZST is zero-cost, but passing a reference to a ZST is not. I assumed that most of the time, the allocator will the ZST type Global, so if for some reason (maybe with opt-level=z) this function is not inlined into caller, caller does’t have to pay for passing an allocator.

Another reason is that if A::default is not a trivial function, code for constructing an allocator could be potentially shared by all callers, so that the binary sized could benefit from this.