@gc attribute for bypassign @nogc

bitwise via Digitalmars-d digitalmars-d at puremagic.com
Sun Jul 24 15:13:02 PDT 2016


Any thoughts on an @gc attribute for bypassing @nogc?

As much as I like to cringe at the GC when I'm using C#, I can't 
ignore my level of productivity, as it compares to projects I've 
done in C++.

If I had to write a butter-smooth AAA game, it may be a problem, 
but at present, my thoughts on a few small jitters here and there 
are....who cares?

As long as GC usage is kept to a minimum, things will usually 
work ok fine.

For example, in a game, I may create a script with an "update" 
method that gets called each frame. I wouldn't want people(or 
myself) accidentally allocating memory here, possible 30-60 times 
per second, so I would  make that function @nogc. But, there 
*will* be cases where I actually want to allocate something, for 
example, to spawn a piece of cake.

Example:

class Cake{}

class MyScript {
     SysTime when;
     this() { when = Clock.currTime + 5.seconds; }
     void update() @nogc {
         // ...
         if(Clock.currTime >= when) {
             @gc {
                 Cake c = new Cake();
             }
         }
     }
}

void main(string[] args) {
     MyScript script = new MyScript();

     foreach(i; 0..10) {
         script.update();
         Thread.sleep(1.seconds);
     }
}

There is the following, which is clever. But if it came down to 
having to do this to bypass @nogc, I simply wouldn't use @nogc.

https://p0nce.github.io/d-idioms/#Bypassing-@nogc

When you have to do it thousands of times throughout your 
codebase, then yes, it's that bad.

     Bit



More information about the Digitalmars-d mailing list