foreach thoughts

deadalnix deadalnix at gmail.com
Tue Jan 14 10:26:47 PST 2014


On Tuesday, 14 January 2014 at 09:07:43 UTC, Manu wrote:
> Can anyone comment on the codegen when using these statements? 
> Is it
> identical to my reference 'if' statement?
> Liberal use of loops like this will probably obliterate 
> unoptimised
> performance... :/

I can't comment for every compiler, but when using LLVM as
backend, here is what to expect.

LLVM is really good at inlining. However, it inline mostly
bottom-up, which mean that the front of the subrange will be
inlined in the outer front, popFront of the subrange will be
inlined in the popFront of the outer range, etc...

You'll generally have pretty good performance, but 2 bad
scenarios can happen.

For most ranges, the compiler will find good optimization between
front/popFront/empty and friends. But the way this stuff will be
inlined, you'll get bigger and bigger front/popFront/empty until
they all are inlined in the loop and everything is simplified
away by the optimizer. If you wrap enough range into one another,
you'll reach a point where the compiler will consider that these
method became too big to be good candidate to inline. Things
should improve over time as LLVM team is working on a better top
down inliner.

The second caveat is automatic heap promotion of the closure
frame pointer. Right now, optimizers aren't really good at heap
to stack promotion in D as they mostly don't understand the
runtime (LDC have some progress in that direction, but this is
still quite simplistic). That mean that you can end up with
unwanted heap allocation.

Realistically, it is possible for a compiler to see through any
reasonably size range code, but for now, it is kind of lacking in
some directions.


More information about the Digitalmars-d mailing list