Want reasonable reference counting? Disable automatic sharing of immutable

Stefan Koch uplink.coder at googlemail.com
Mon Nov 15 11:58:56 UTC 2021


On Monday, 15 November 2021 at 09:22:16 UTC, Dukc wrote:
>
> I think he meant from optimisation viewpoint. C++ `const` is so 
> weak that it can't be used for actual optimisations anyway, at 
> least usually. So no performance is lost because of `mutable`. 
> In D the compiler can make some assumption based on `const` and 
> `immutable`, so we lose some performance potential if we 
> implement `mutable`.

You can only optimize based on `immutable` and even then you need 
to run data-flow analysis to figure out whether someone _could_ 
do any casts which make the optimization invalid.

And const doesn't provide any additional grantees an optimizer 
could use directly.
It's a feature to prevent misuse of an API only.

I have personal experience which show that as soon as multiple 
threads involved const does more harm than good because it only 
allows you to reason about _your access rights_ you cannot infer 
anything about anyone else.



More information about the Digitalmars-d mailing list