What should happen here?

H. S. Teoh hsteoh at quickfur.ath.cx
Fri Sep 24 16:21:16 UTC 2021


On Fri, Sep 24, 2021 at 11:31:46AM -0400, Steven Schveighoffer via Digitalmars-d wrote:
> On 9/24/21 11:25 AM, deadalnix wrote:
> > On Thursday, 23 September 2021 at 19:54:56 UTC, Steven Schveighoffer wrote:
> > > You can. But wouldn't you prefer just pushing something on the
> > > stack?
> > > 
> > 
> > Not really. If the optimizer can remove dead stack pushes, then
> > program will become 2x slower instantly in addition of consuming
> > more stack memory.
> > 
> 
> You think pushing on the stack is going be 2x slower than calling
> `GC.addRoot`?
[...]

I still prefer GC.addRoot.

For one thing, that's the "official" way to inform the GC that a certain
object is still needed and therefore should not be collected.

Secondly, it self-documents the intent of the code, instead of some
arcane workaround like struct Pin (that may or may not work in the
future depending on how smart optimizers become).

Third, if the overhead of calling GC.addRoot becomes an actual problem,
it can always be turned into an intrinsic that the compiler can, based
on certain conditions, replace it with an equivalent internal flag that
ensures the value stays on the stack until the end of the scope.


T

-- 
Three out of two people have difficulties with fractions. -- Dirk Eddelbuettel


More information about the Digitalmars-d mailing list