Note that, later in his talk [1], Chandler clarifies that he still recommends passing unique_ptr by value, because while compilers may currently generate better code for passing them by reference, passing them by value is still more optimizable in the long term (since passing them by reference encodes a double indirection -- you're passing a reference to a pointer -- in the type system).
Cheers, Botond [1] https://youtu.be/rHIkrotSwcc?t=2418 On Mon, Oct 14, 2019 at 8:43 AM Nathan Froyd <nfr...@mozilla.com> wrote: > > On Mon, Oct 14, 2019 at 3:58 AM Henri Sivonen <hsivo...@mozilla.com> wrote: > > On Mon, Oct 14, 2019 at 9:05 AM Gerald Squelart <gsquel...@mozilla.com> > > wrote: > > > > > > I'm in the middle of watching Chandler Carruth's CppCon talk "There Are > > > No Zero-Cost Abstractions" and there's this interesting insight: > > > https://youtu.be/rHIkrotSwcc?t=1041 > > > > > > The spoiler is already in the title (sorry!), which is that passing > > > std::unique_ptr by value is more expensive than passing it by rvalue > > > reference, even with no exceptions! > > > > > > I wrote the same example using our own mozilla::UniquePtr, and got the > > > same result: https://godbolt.org/z/-FVMcV (by-value on the left, by-rref > > > on the right.) > > > So I certainly need to recalibrate my gutfeelometer. > > > > The discussion in the talk about what is needed to fix this strongly > > suggested (without uttering "Rust") that Rust might be getting this > > right. With panic=abort, Rust gets this right ( > > https://rust.godbolt.org/z/SZQaAS ) which really makes one appreciate > > both Rust-style move semantics and the explicitly not-committal ABI. > > With a little voodoo placement of [[clang::trivial_abi]] > (https://quuxplusone.github.io/blog/2018/05/02/trivial-abi-101/, > https://reviews.llvm.org/D41039) on Pair specializations and UniquePtr > itself, one can make the by-value function look more like what you > might expect, but at the cost (!) of making the rvalue-ref function > look more like the original by-value function, > https://godbolt.org/z/A1wjl8. I think that's a reasonable tradeoff to > make if we wanted to start using [[clang::trivial_abi]]. > > -Nathan > _______________________________________________ > dev-platform mailing list > dev-platform@lists.mozilla.org > https://lists.mozilla.org/listinfo/dev-platform _______________________________________________ dev-platform mailing list dev-platform@lists.mozilla.org https://lists.mozilla.org/listinfo/dev-platform