<div dir="ltr"><br><div class="gmail_extra"><br><div class="gmail_quote">On Thu, Sep 25, 2014 at 1:44 AM, Renato Golin <span dir="ltr"><<a href="mailto:renato.golin@linaro.org" target="_blank">renato.golin@linaro.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><span class="">On 25 September 2014 06:16, David Blaikie <<a href="mailto:dblaikie@gmail.com">dblaikie@gmail.com</a>> wrote:<br>
> I can go & dredge up some examples if we want to discuss the particular<br>
> merits & whether each of those cases would be better solved in some other<br>
> way, but it seemed pervasive enough in the current codebase that some<br>
> incremental improvement could be gained by replacing these cases with a more<br>
> specific tool for the job. We might still consider this tool to be "not<br>
> ideal".<br>
<br>
</span>Having done that in another code base, I can see both merits and problems.<br>
<br>
Our smart pointer behaved more or less like it's described above<br>
(explicit acquire/release) </blockquote><div><br></div><div>I'm not quite sure I follow you (or that you're following me). The goal isn't about being explicit about acquire/release (indeed I wouldn't mind more implicit acquisition from an always-owning pointer (unique_ptr) to a sometimes-owning pointer (whatever we end up calling it), though release is always going to be explicit I suspect).</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">and we've seen around 20% performance<br>
improvement over shared_ptr due to savings on acquire/release<br>
semantics.</blockquote><div><br>The existing use cases I'm interested in aren't using shared_ptr and wouldn't be ideally migrated to it (in some cases the existing ownership might be on the stack (so it can't be shared) or several layers up the call stack through which raw pointers have been passed (eg: build something, pass it through a few APIs, underlying thing 'wants' to take ownership, but it will be constructed/destroyed before this API returns - so we hack around it either by having a "OwnsThing" flag in the underlying thing, or having a "takeThing" we hope we call before the underlying thing dies and destroys the thing)). </div><div><br></div><div>We're dealing with types that have raw pointer + bool indicating ownership members or worse, types which take ownership but we lie to about giving them ownership (so some API takes a non-owning T* (or T&), passes it as owning to some other thing, then is sure to take ownership back from that thing and call 'release()" on the unique_ptr to ensure ownership was never really taken).<br></div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"> We had three derivative types of pointers<br>
(Shared/Owned/Linked) which would just differ on the default behaviour<br>
of construction / destruction, but all of them could still explicitly<br>
call getClear / getLink / etc.<br>
<br>
The downside was that almost every interaction with smart pointers had<br>
to be carefully planned and there was a lot of room for errors,<br></blockquote><div><br></div><div>My hope is that having a single construct for conditional ownership will be less confusing than the existing ad-hoc solutions in many places. It's possible that the right answer is to remove the conditional ownership in these APIs entirely, but I'm not sure that's possible/practical/desired (it might be - which is why I've been hesitant to write this abstraction myself just yet - sort of letting it stew in my head a bit to see what feels right).</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
especially from people that didn't know the context. In the end, the<br>
amount of work that had to be put to make it work was similar than<br>
when dealing with normal pointers,</blockquote><div><br></div><div>I rather hope that's not the case - these conditionally owning raw pointers are pretty subtle, easy to miss a delete and leak, easy to have an early return and fail to take back and release ownership from something that wasn't really owning in the first place, etc.</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"> but you had to learn yet another<br>
pointer semantics.<br>
<br>
The marginal gain was that pointer interactions were explicit, making<br>
it easier for someone *not* used to C++ pointer semantics to<br>
understand when reading code, not necessarily when writing it. The<br>
marginal lost was getting people that already knew the STL and Boost<br>
smart pointer semantics to get confused.<br></blockquote><div><br></div><div>This is another pointer semantic - I'm not suggesting replacing unique_ptr or shared_ptr - I want to use them as much as possible where they represent the right semantic. I'm just dealing with a situation that doesn't map directly to either of those: conditional ownership.</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<br>
Having done that, I still rather use normal pointers and have valgrind<br>
/ sanitizers tell me when I screwed up.<br>
<br>
My tuppence.<br>
<br>
cheers,<br>
--renato<br>
</blockquote></div><br></div></div>