System programming in D (Was: The God Language)

bearophile bearophileHUGS at lycos.com
Thu Jan 5 15:58:10 PST 2012


Peter Alexander:

> void foo(int x)
> {
>      int[10] a;
>      foreach (ref e; a)
>          e = bar(x);
> }
> 
> If bar is pure then you can safely transform this into:
> 
> void foo(int x)
> {
>      int[10] a;
>      auto barx = bar(x);
>      foreach (ref e; a)
>          e = barx;
> }

If bar is pure, but it throws exceptions, the two versions of the code behave differently, so it's a wrong optimization. You need bar to be pure nothrow.

Moving pure nothrow functions out of loops is an easy optimization, and even simple D compilers are meant to do it. Aggressively optimizing D compilers are also free to memoize some results of pure (and probably nothrow too) functions.

-------

Regarding the discussion about virtual functions, show me a D compiler able to de-virtualize very well, as the Oracle JVM does :-)
Time ago I have asked LLVM devs to improve this situation for LDC, they have now fixed most of my bug reports, so I think they will eventually fix this too (maybe partially):
http://llvm.org/bugs/show_bug.cgi?id=3100

Bye,
bearophile


More information about the Digitalmars-d mailing list