Want reasonable reference counting? Disable automatic sharing of immutable

Timon Gehr timon.gehr at gmx.ch
Sat Nov 13 04:57:36 UTC 2021


On 12.11.21 23:07, Andrei Alexandrescu wrote:
> 
> The real challenge is figuring out exactly the minimum amount of change 
> to the language specification to allow defining such a scheme.

I think one reason we haven't been making progress on this may be that 
your intuition for the size of the minimum required amount of change to 
the language specification is somewhat too small.

You have stated requirements, some of them are impossible to satisfy, 
most of them will require a sizeable language change. We have to go 
through those problems one by one and address them in order.

You have to factor in that _a lot_ of the work is actually clarifying 
existing semantics and fixing issues there.

My suggestion would be to prioritize satisfying the requirements that do 
not relate to type qualifiers.


> 
> - be as close in semantics to T[] as possible, i.e. most code should be able to simply replace T[] with RCSlice!T and work unchanged

`T[]` is magic. We first need to make it possible to define a template 
in druntime that works as a drop-in replacement for `T[]`, even without 
reference counting.

> - manage its own memory by using reference counting

Easy, except for lifetime bugs in throwing constructors. Some hassle 
with default init.

> - work in pure code just like T[] does

Impossible, due to strong purity. `__mutable` would have fixed this.

> - work with qualifiers like T[] does,

Impossible. `T[]` is magic and `inout` is broken.

> both RCSlice!(qual T)

Easy.

> and qual(RCSlice!T)

Impossible. Would need an escape hatch for the reference count, which 
you have shot down before. `__mutable` would have fixed this.

Adding appropriate subtyping relationships between them also requires a 
new language feature.

> - work in @nogc code just like T[] does

Very easy.

> - work in @safe code just like T[] does 

Impossible. This has spawned DIP1000 and large amounts of follow-up 
work. And all of this work still does not solve the problem it set out 
to address. I think this is the main problem. It's not actually very 
important to support type/function qualifiers beyond `pure @safe @nogc 
nothrow`. The issue is that you cannot borrow out a `ref T`. This could 
be solved with some explicit language support, where the compiler tracks 
the lifetime of the reference and lets the container know once it's dead.


More information about the Digitalmars-d mailing list