[Dlang-study] [lifetime] Initial thoughts on lifetime management

Michel Fortin michel.fortin at michelf.ca
Thu Oct 29 10:44:28 PDT 2015


Le 29 oct. 2015 à 12:14, Timon Gehr <timon.gehr at gmx.ch> a écrit :

> On 10/29/2015 03:33 PM, Michel Fortin wrote:
>> I think I understand how Swift's `isUniquelyReferenced` works. It's not costly. It's also quite clever. I'll rewrite the function signature to its D equivalent for this discussion:
>> 
>> 	bool isUniquelyReferenced(ref Object o);
>> 
>> This function simply returns true if the counter for "o" is 1.
> 
> By somewhat costly, I mean that it prevents transformations that do not satisfy (#references=1) <-> (count=1).

True, however:

1. It has the effect of preventing an elision only where you actually call the function, which is the same cost as any function taking a RC parameter by ref.

2. Any time `isUniquelyReferenced` prevents an opInc/opDec elision, it is guarantied to return false. If you made it an intrinsic you could take advantage of that by replacing the call with a false constant whenever it would get in the way of an elision.

3. Even without the intrinsic, if you use it to implement copy-on-write, any time it prevents an elision is a time you'll end up making a copy, and the copy going to be more costly regardless.


-- 
Michel Fortin
michel.fortin at michelf.ca
https://michelf.ca




More information about the Dlang-study mailing list