What should happen here?

H. S. Teoh hsteoh at quickfur.ath.cx
Sun Sep 26 14:01:56 UTC 2021


On Sat, Sep 25, 2021 at 07:13:50PM -0700, Walter Bright via Digitalmars-d wrote:
> On 9/25/2021 6:02 PM, Steven Schveighoffer wrote:
> > As I said, the optimizer is fighting you the entire way.
> 
> I'd reframe it as the user is trying to impose his own semantics on
> the optimizer :-)

IMO, those are just the symptoms.

The core of the problem is that we failed to inform the GC of a
reference to some object (because said reference was passed into C land
and no longer exists in D land).

GC.addRoot is precisely the ticket that solves this core issue: it
informs the GC about said reference. **This is what addRoot is for.**

All the other elaborate attempts to "fix" this problem without using
GC.addRoot is just skirting around the issue without actually addressing
the problem.  No wonder it feels likes "the optimizer is fighting you
the entire way."  Actually, the optimizer is NOT trying to fight you;
it's merely telling you that **the semantics expressed in your code is
not what you think it is**.  It's simply reducing your code to its
actual semantics as defined by the specs. The fact that this reduction
isn't what you expect is a sign that the original code doesn't actually
mean what you think it means.

It's like trying to prevent a boolean expression from reducing to a
constant value by adding more clauses to it.  If there's already a
tautology in your expression, it's not gonna change no matter what else
you try to add to it.  Fix the tautology, and all of the problems go
away. There's no need for all the other fluff.

Seriously, guys, just use GC.addRoot. **That's what it's for.**  Stop
trying to cure cancer with Tylenol already! :-D


[...]
> > But I still am wishing for a simple "keep this alive" mechanism that
> > doesn't add too much cruft, and is guaranteed to keep it alive.
> 
> That's exactly what addRoot() is for.

If calling addRoot causing "performance problems" (or whatever other
objections one may raise), we can look into improving its performance in
various ways. But avoiding to call it in the first place is not a
solution.


T

-- 
"Computer Science is no more about computers than astronomy is about telescopes." -- E.W. Dijkstra


More information about the Digitalmars-d mailing list