Re: Passing UniquePtr by value is more expensive than by rref

2019-10-26 Thread Botond Ballo
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  wrote:
>
> On Mon, Oct 14, 2019 at 3:58 AM Henri Sivonen  wrote:
> > On Mon, Oct 14, 2019 at 9:05 AM Gerald Squelart  
> > 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


Re: Passing UniquePtr by value is more expensive than by rref

2019-10-14 Thread Nathan Froyd
On Mon, Oct 14, 2019 at 3:58 AM Henri Sivonen  wrote:
> On Mon, Oct 14, 2019 at 9:05 AM Gerald Squelart  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


Re: Passing UniquePtr by value is more expensive than by rref

2019-10-14 Thread Jan de Mooij
On Mon, Oct 14, 2019 at 8:05 AM Gerald Squelart 
wrote:

> A quick searchfox shows a few hundred by-value unique pointer's, we
> may want to look into these.
> Though I guess it's a trade-off between the expressiveness of by-value
> ("I'm stealing your value for sure") vs the more efficient but less obvious
> by-rref ("Maybe I'll take your value").
>

For what it's worth, this is what our documentation recommends [0]: "To
unconditionally transfer ownership of a UniquePtr into a method, use a
|UniquePtr| argument. To conditionally transfer ownership of a resource
into a method, should the method want it, use a |UniquePtr&&| argument."
I've definitely passed UniquePtr by value a number of times based on this
comment.

Jan

[0]
https://searchfox.org/mozilla-central/rev/6866d3a650c826f1cabd123663e84b95ee743701/mfbt/UniquePtr.h#179-186


> ___
> 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


Re: Passing UniquePtr by value is more expensive than by rref

2019-10-14 Thread Henri Sivonen
On Mon, Oct 14, 2019 at 9:05 AM Gerald Squelart  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.

(I had to put a side-effectful println! in bar to make sure a call to
bar is generated, since #[inline(never)] isn't enough to prevent the
compiler from eliding calls to functions it can see do nothing.)

-- 
Henri Sivonen
hsivo...@mozilla.com
___
dev-platform mailing list
dev-platform@lists.mozilla.org
https://lists.mozilla.org/listinfo/dev-platform


Passing UniquePtr by value is more expensive than by rref

2019-10-14 Thread Gerald Squelart
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.

A quick searchfox shows a few hundred by-value unique pointer's, we may 
want to look into these.
Though I guess it's a trade-off between the expressiveness of by-value ("I'm 
stealing your value for sure") vs the more efficient but less obvious by-rref 
("Maybe I'll take your value").

And there may be other types we should examine as well?

Cheers,
Gerald
___
dev-platform mailing list
dev-platform@lists.mozilla.org
https://lists.mozilla.org/listinfo/dev-platform