Improving DIP74: functions borrow by default, retain only if needed

H. S. Teoh via Digitalmars-d digitalmars-d at puremagic.com
Fri Feb 27 11:50:49 PST 2015


On Fri, Feb 27, 2015 at 10:24:26AM -0800, Andrei Alexandrescu via Digitalmars-d wrote:
> DIP74's function call protocol for RCOs has the caller insert opAddRef
> for each RCO passed by value. Then the callee has the responsibility
> to call opRelease (or defer that to another entity). This choice of
> protocol mimics the constructor/destructor protocol and probably shows
> our C++ bias.
> 
> However, ARC does not do that. Instead, it implicitly assumes the
> callee is a borrower of the reference. Only if the callee wants to
> copy the parameter to a member or a global (i.e. save it beyond the
> duration of the call), a new call to retain() (= opAddRef) is
> inserted. That way, functions that only need to look at the object but
> not store it incur no reference call overhead.
> 
> So I was thinking of changing DIP74 as follows:
> 
> * Caller does NOT insert an opAddRef for byval RCOs
> 
> * Callee does NOT insert an opRelease for its byval RCO parameters

So if the callee assigns the RCO to something else, that's when
opAddRef/retain will get called, but if the callee doesn't do that, then
no call is inserted? Sounds reasonable.


> It seems everything will just work with this change (including all
> move scenarios), but it is simple enough to make me worry I'm missing
> something.  Thoughts?
[...]

As long as there is no sharing of the RCO between threads, this looks
like it should work. (But I'm no ARC expert, so don't take my word for
it.) But if there's sharing, the story becomes drastically more complex.


T

-- 
Lottery: tax on the stupid. -- Slashdotter


More information about the Digitalmars-d mailing list