Did Walter's pure optimization ever make it into dmd?

Dennis dkorpel at gmail.com
Mon Apr 2 08:52:27 UTC 2018


In this nice article Walter described how immutable and pure 
allow for optimizations:
http://www.drdobbs.com/architecture-and-design/optimizing-immutable-and-purity/228700592

```For these examples, I'll use the D programming language 
compiler that is currently under development. (...)
But what if bar is pure?

pure int bar(int);

int foo(int i)
{
     return bar(i) + bar(i);
}

Now the assembler output is:

     push    EAX    ; save argument i on stack
     call    bar    ; call bar(i)
     add     EAX,EAX   ; double result
     pop     ECX    ; clean stack
     ret      ; return to caller

bar(i) is called only once```

I've checked whether multiple calls to pure functions get 
optimized, and it seems like they *never* do. I've tried 
different versions of dmd with optimizaiton flags on/off, and 
also LDC/GDC.

In the case of lazy parameters (which get lowered to delegates 
that can be inferred to be pure), this can lead to doing the same 
string concatenation multiple times every time you use it.

```
import std.stdio: writeln;

void log(lazy string str)
{
     if (str.length > 0) writeln(str.ptr); //the lazy 'str' gets 
evaluated twice here
}

void main(string[] args) {
     log("first arg: "~args[0]);
}

```

So were these optimizations never finished and put into dmd? Or 
is there a bug that prevents these optimizations from happening?


More information about the Digitalmars-d mailing list