How does Rebindable suppress the compiler's optimizations for immutable?

Jonathan M Davis newsgroup.d at jmdavisprog.com
Fri Feb 15 08:27:00 UTC 2019


On Thursday, February 14, 2019 11:59:31 PM MST Stefan Koch via Digitalmars-
d-learn wrote:
> On Thursday, 14 February 2019 at 23:55:18 UTC, SimonN wrote:
> > std.typecons.Rebindable!(immutable A) is implemented as:
> >     private union {
> >
> >         immutable(A) original;
> >         A stripped;
> >
> >     }
> >
> >     ... at trusted assignment operators...
> >
> >     @property inout(immutable(A)) get() @trusted pure nothrow
> >
> > @nogc inout
> >
> >     {
> >
> >         return original;
> >
> >     }
> >
> >     alias get this;
> >
> > This conforms with the D safety spec: All access to the unsafe
> > union goes through the @trusted get() and the trusted
> > assignment operators.
> >
> >     Rebindable!(immutable A) r = a1;
> >     // r.original is a1
> >     r = a2;
> >     // r.original is a2
> >
> > But the compiler may assume that immutable variables -- such as
> > the immutable(A) original -- never change and thus may optimize
> > code. Since immutable(A) original is assignable in the union,
> > such optimization would produce wrong behavior: In the final
> > line, the compiler could think that r.original is a1 without
> > examining r.original.
> >
> > How does Rebindable prevent the compiler from optimizing
> > according to immutable's rules?
>
> It's easy. You cannot use immutable as the only basis of
> optimization.
> You need to proof actual immutability via data-flow-analysis over
> the whole life-time of your immutable.
> When you cannot guarantee actual immutability (within the frame
> of interest) don't perform optimizations which are dependent on
> it.
>
> So much for the theory, in practice I think that most
> optimizations based on immutable are disabled.
> Think of immutable as hint for the programmer, not for the
> compiler.

The type system is supposed to guarantee and be able to rely on immutable
never mutating. That's pretty much the whole point. Honestly, I'm pretty
sure that Rebindable technically violates the type system to do what it
does. However, I would expect that the use of a union would tend to kill
compiler optimizations given how tricky things get with those, so I wouldn't
expect that to be a problem. But yeah, I suspect that the compiler doesn't
do a lot of optimizations based on immutable anyway. I don't know for sure,
but it usually seems to be the case that the optimizations that D could get
from its type system end up being theoretical rather than actual whenever
anyone looks into it.

- Jonathan M Davis





More information about the Digitalmars-d-learn mailing list