foreach thoughts

David Nadlinger code at klickverbot.at
Wed Jan 15 03:55:43 PST 2014


On Wednesday, 15 January 2014 at 07:08:14 UTC, deadalnix wrote:
> Actually this is a useful optimization technique when you need 
> to skip a lot of objects that way. See 
> http://www.onversity.com/load/d-loop.pdf

Well, yes, expect for the fact that the case discussed here isn't 
an instance of the optimization. Even the "pre-skip" loop 
performs two conditional jumps per array element. And worse, the 
form the loop is in causes LLVM to miss two additional 
optimization opportunities, compared to the plain C (D) version:

First, LLVM actually optimizes away the even/odd conditional 
branch in the plain version, replacing it with a shl/sar/and 
combination. The rotated loop structure in the filter version 
seems to cause the optimizer to miss that opportunity.

And second, as a consequence the loop in the filter version is no 
longer a good candidate for vectorization, whereas LLVM will 
happily emit AVX/… instructions in the plain case if you let it.

David


More information about the Digitalmars-d mailing list