@gc attribute for bypassign @nogc

Lodovico Giaretta via Digitalmars-d digitalmars-d at puremagic.com
Sun Jul 24 15:33:49 PDT 2016


On Sunday, 24 July 2016 at 22:13:02 UTC, 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

In this kind of situation, the solution is to not use @nogc and 
use the built-in gc profiler to decide which functions need 
memory usage optimizations.
We should be very careful, that using @nogc is not the aim. It is 
just one of the available tools to avoid our application randomly 
freezing due to collections. That is the aim. And there are other 
tools to achieve it: the allocators library and the built-in gc 
profiler.


More information about the Digitalmars-d mailing list