foreach, opApply, and inline

Tor Myklebust tmyklebu at csclub.uwaterloo.ca
Sun Oct 7 22:50:48 PDT 2007


BCS <ao at pathlink.com> wrote:
> Reply to Tor,
> 
> > When I compile the following code with dmd 1.015, separate functions
> > are generated for main, subset's opApply, and the body of the foreach.
> > Is there a way to get dmd to inline opApply and the body of the
> > foreach when the opApply is known and the body of the foreach is
> > known?  (More generally, is it possible to declare a function
> > accepting a delegate argument so that the delegate will get inlined if
> > it is known at compile time?)
> > 
> 
> I assume you tried DMD's -inline flag?

Yes.  That was compiled with -O -inline -release.

> I have often though that the inlining should work in the more general case 
> where small function get called with a known delegate.

I don't think it should in general; if the "small function" calls the
delegate multiple times, this can result in extremely bloated code.
For the specific case of opApply() happening because of a foreach loop,
our intuition as programmers is that the compiled result won't contain
any unnecessary function calls.  (Imagine if "for (size_t i=0;i<n;i++)
foo += bar[i];" generated a function for doing "foo += bar[i]" and a
function for doing the iteration itself.  You'd have to write for-loops
yourself using if-goto again.  That would suck, wouldn't it?)

Tor Myklebust



More information about the Digitalmars-d mailing list