Looking for more library optimization patterns

bearophile bearophileHUGS at lycos.com
Thu Jan 30 10:36:08 PST 2014


Kai Nacke:

> LDC includes the SimplifyDRuntimeCalls pass which should 
> replace D library calls with more efficient ones. This is a 
> clone of a former LLVM pass which did the same for C runtime 
> calls.
>
> An example is that printf("foo\n") is replaced by puts("foo").
>
> Do you know of similar patterns in D which are worth to be 
> implemented?
> E.g. replacing std.stdio.writefln("foo") with 
> std.stdio.writeln("foo")

A de-optimization I'd really like in LDC (and dmd) is to not call 
the run-time functions when you perform a verctor operation on 
arrays statically known to be very short:

void main() {
     int[3] a, b;
     a[] += b[];
}

And just rewrite that with a for loop (as done for cases where 
the run time function is not available), and let ldc2 compile it 
on its own.

See a thread in D.learn:
http://forum.dlang.org/thread/lqmqsnucadaqlkxkoffc@forum.dlang.org

I'd like to replace code like:


size_t i = 0;
foreach (immutable j, const ref bj; bodies) {
     foreach (const ref bk; bodies[j + 1 .. $]) {
         foreach (immutable m; TypeTuple!(0, 1, 2))
             r[i][m] = bj.x[m] - bk.x[m];
         i++;
     }
}


With:

size_t i = 0;
foreach (immutable j, const ref bj; bodies)
     foreach (const ref bk; bodies[j + 1 .. $])
         r[i++][] = bj.x[] - bk.x[];


And keep the same performance, instead of seeing a significant 
slowdown.

Bye,
bearophile


More information about the digitalmars-d-ldc mailing list