[cfe-dev] [LLVMdev] Use of Smart Pointers in LLVM Projects

Reid Kleckner rnk at google.com
Thu Jul 17 18:13:46 PDT 2014


On Thu, Jul 17, 2014 at 5:42 PM, Nico Weber <thakis at chromium.org> wrote:

> On Thu, Jul 17, 2014 at 4:43 PM, Lang Hames <lhames at gmail.com> wrote:
>
>> Where unique_ptr fits the memory ownership model I'm all for using it.
>> The syntactic overhead is a small price to pay for self-documentation and
>> compile-time guarantees.
>>
>
> +1. I liked the patches that added unique_ptrs I've seen so far. WebKit
> also heavily uses pointer classes (OwnPtr etc) heavily, and I think it
> works well there too.
>

Branching off in a completely different direction, I want to point out that
there is an abstraction penalty to passing unque_ptr by value.

By it's nature, unique_ptr is not trivially copyable: it has no copy ctor
and it's move ctor and dtor are non-trivial.  Therefore, it cannot be
passed in registers, or even directly on the stack on most platforms.
 Typically, you end up with a hidden pointer to a temporary unique_ptr on
the caller's stack when you pass or return one.

However, ownership transfers are typically accompanied by new/delete, which
are more expensive altogether than one more pointer chase.

2c
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20140717/b879dde5/attachment.html>


More information about the cfe-dev mailing list