@gc attribute for bypassign @nogc

rikki cattermole via Digitalmars-d digitalmars-d at puremagic.com
Sun Jul 24 20:18:17 PDT 2016


On 25/07/2016 10:13 AM, bitwise wrote:
> 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
>

I've been saying for a very long time we need @assumenogc attribute like 
we have @trusted for @safe.
I have not been taken very seriously about this...

Although I see no reason to allow GC in @assumenogc as long as it does 
not trigger scan/collect phase of it. After all, why don't we have a 
hint on allocation if we do or do not want it to trigger a scan?

Or even better, a thread local stack to specify this!


More information about the Digitalmars-d mailing list