/*
Compile with -O -inline -release
This program compares performance of 2 loops, one initializing a
scope local variable w/ a function and the other not.
The function used as an initializer is not inlined.
There is about a 11x difference on my system.
*/
import std.stdio, std.date, std.math;
// local copy of std.math.abs()
// (imported functions are not inlined - see DMD bug #67)
int abs(int x)
{
return x >= 0 ? x : -x;
}
void main()
{
int sum = 0;
d_time s = getUTCtime();
for(int i = 0; i < 100_000_000; i++)
{
int val = abs(i);
sum += val - val;
}
d_time e = getUTCtime();
writefln("std.math.abs(): sum= ",sum,", secs = ",(e - s)/cast(double)TicksPerSecond);
d_time tmp = e - s;
sum = 0;
s = getUTCtime();
for(int i = 0; i < 100_000_000; i++)
{
int val;
val = abs(i);
sum += val - val;
}
e = getUTCtime();
writefln("local abs(): sum= ",sum,", secs = ",(e - s)/cast(double)TicksPerSecond);
writefln("ratio = ", tmp / cast(double)(e - s));
}
Comment #1 by bugs-d — 2006-03-25T02:36:39Z
There's a comment in inline.c for DeclarationExp's saying:
// Should scan variable initializers
But it isn't happening right now. Adding a basic scan of the ExpInitializer (if present) the same way DeclarationExp::inlineCost works seems to resolve this, but considering how trivial that is... I can't help but expect that there's more to it than just that.
-[Unknown]
Comment #2 by godaves — 2006-03-25T12:01:11Z
(In reply to comment #1)
> There's a comment in inline.c for DeclarationExp's saying:
>
> // Should scan variable initializers
>
> But it isn't happening right now. Adding a basic scan of the ExpInitializer
> (if present) the same way DeclarationExp::inlineCost works seems to resolve
> this, but considering how trivial that is... I can't help but expect that
> there's more to it than just that.
>
I've noticed that too and agree there must be more to it for some cases. But couldn't that just be re-written by the compiler as two expressions and then inlined?
int i = foo(); => int i; i = foo(); => doInline();
Are there cases where that wouldn't be semantically identical?
Thanks,
- Dave
> -[Unknown]
Comment #3 by bugs-d — 2006-03-25T12:34:21Z
Created attachment 8
Add a basic scan to DeclarationExp.
AFAICT, that's exactly what adding a scan in there does. I'm just compiling DMD without backend hooks, but I can see with my changes that it now wants to inline things (by turning on CANINLINE_LOG.)
It's just calling the inline stuff on ExpInitializer's exp, which is probably an AssignExp or something, which is a BinExp, which then has the CallExp checked, which then finally gets inlined.
I'm just worried it wasn't added because it might cause fallout. I don't really know the code base, so I'm not privy to what fallout might be caused.
Anyway, here's a patch that shows what I changed to enable inlining in these cases. If I had the time on my hands, I might try running DStress with this patch on gdc, to see if it causes any regressions.
-[Unknown]
Comment #4 by matti.niemenmaa+dbugzilla — 2006-04-03T06:09:59Z