Optimize away immediately-called delegate literals?

Steven Schveighoffer schveiguy at yahoo.com
Wed Mar 14 04:20:58 PDT 2012


On Mon, 12 Mar 2012 20:28:15 -0400, H. S. Teoh <hsteoh at quickfur.ath.cx>  
wrote:

>
> Hmph.
>
> I tried this code:
>
> 	import std.stdio;
> 	struct A {
> 		int[] data;
> 		int opApply(int delegate(ref int) dg) {
> 			foreach (d; data) {
> 				if (dg(d)) return 1;
> 			}
> 			return 0;
> 		}
> 	}
> 	void main() {
> 		A a;
> 		int n = 0;
>
> 		a.data = [1,2,3,4,5];
> 		foreach (d; a) {
> 			n++;
> 		}
> 	}
>
> With both dmd and gdc, the delegate is never inlined. :-(  Compiling
> with gdc -O3 causes opApply to get inlined and loop-unrolled, but the
> call to the delegate is still there. With dmd -O, even opApply is not
> inlined, and the code is generally much longer per loop iteration than
> gdc -O3.

IIRC, ldc does inline opApply.  But this is somewhat hearsay since I don't  
use ldc.  I'm just remembering what others have posted here.

-Steve


More information about the Digitalmars-d mailing list