Optimize away immediately-called delegate literals?
Brad Roberts
braddr at puremagic.com
Mon Mar 12 20:02:12 PDT 2012
On 3/12/2012 4:15 PM, Peter Alexander wrote:
> On Sunday, 11 March 2012 at 06:49:27 UTC, H. S. Teoh wrote:
>> On Sun, Mar 11, 2012 at 01:29:01AM -0500, Nick Sabalausky wrote:
>>> Suppose you have a delegate literal and immediately call it:
>>>
>>> auto a = x + (){ doStuff(); return y; }() + z;
>>>
>>> Does DMD ever (or always?) optimize away a delegate if it's executed
>>> immediately and never stored into a variable? If not, can it, and
>>> would it be a simple change? Is something like this already on the
>>> table?
>> [...]
>>
>> I've always wondered about whether delegates passed to opApply ever get
>> inlined.
>
> Don't wonder. Find out!
>
> import std.stdio;
> void doStuff() { writeln("Howdy!"); }
> void main() {
> int x = 1, y = 2, z = 3;
> auto a = x + (){ doStuff(); return y; }() + z;
> writeln(a);
> }
See also: bug 4440
The patch in there, if it hasn't bit rotten to badly (I suspect it has) will handle _this_ case. But almost no other
case of inlining delegates.
It'd be a good area for someone who wants an interesting and non-trivial problem to dig into. It wouldn't touch all
that much of the codebase as the inliner is fairly self-contained. At least, that's what I recall from when I looked at
this stuff a couple years ago.
Later,
Brad
More information about the Digitalmars-d
mailing list