Small @nogc experience report

H. S. Teoh hsteoh at quickfur.ath.cx
Fri Sep 7 17:12:15 UTC 2018


On Fri, Sep 07, 2018 at 04:44:05PM +0000, Peter Alexander via Digitalmars-d wrote:
> I recently wrote a small program of ~600 lines of code to solve an
> optimisation puzzle. Profiling showed that GC allocations were using
> non-trivial CPU, so I decided to try and apply @nogc to remove
> allocations.  This is a small experience report of my efforts.

Interesting report.  I'd say, if I had to do this, my first approach
wouldn't be to use @nogc at all, but to use GC.disable() and scheduling
my own calls to GC.collect() at the opportune time.  It depends on what
your code is doing, obviously, but IME, the bulk of GC performance
issues I've had in D was caused by the GC being overly eager to collect.
By letting garbage accumulate for a little longer and invoking
GC.collect at a lower frequency than it would normally would collect has
resulted in significant performance boosts from 25% or so up to even 40%
(but YMMV, of course).

This approach allows you to continue enjoying the convenience of having
a GC while letting you tweak the GC collection frequency until you get
good performance wins. After that, you can squeeze more performance out
by identifying parts of the code where lots of garbage is generated, and
changing the code to generate less garbage.  In a project of mine
similar to yours, after I found a good low frequency schedule for
collections, I found an allocation hotspot that was allocating lots of
new arrays where existing ones could be reused.  Retouching just one or
two places in that code to reuse a few arrays resulted in more
performance gains.

tl;dr: IME, most GC performance issues can be alleviated by using
GC.disable and scheduling GC.collect manually, and following the usual
GC performance advice (reduce the amount of garbage your program
generates, which translates to less need for collections and also less
work per collection).  I haven't found much need to actually use @nogc
(but YMMV, seems some people around here swear by @nogc).


T

-- 
"The whole problem with the world is that fools and fanatics are always so certain of themselves, but wiser people so full of doubts." -- Bertrand Russell. "How come he didn't put 'I think' at the end of it?" -- Anonymous


More information about the Digitalmars-d mailing list