AliasSeq seems to compile slightly faster with static foreach

Timon Gehr timon.gehr at gmx.ch
Sun Jan 7 08:59:30 UTC 2018


On 05.01.2018 14:10, Jonathan M Davis wrote:
> Taking it a step further, I tried switching some of the static foreach's
> over to using array literals, since they held values rather than types, and
> that seemed to have minimal impact on the time to run dub test. However, by
> switching to using std.range.only, it suddenly was taking more like 11.8
> seconds. So, with a few small changes, I cut the time to run dub test down
> by almost a third.

This is weird, as the compiler will apply the following rewrites:

static foreach(i;only(0,1,2,3,4)){}

=>

static foreach(i;{
     int[] r=[];
     foreach(i;only(0,1,2,3,4)){
         r~=i;
     }
     return r;
}()){}

=> // (using CTFE)

static foreach(i;[0,1,2,3,4]){}

=> // (uses shortcut; not instantiating the AliasSeq template)

static foreach(i;AliasSeq!(0,1,2,3,4)){}


More information about the Digitalmars-d mailing list