AliasSeq seems to compile slightly faster with static foreach

Meta jared771 at gmail.com
Fri Jan 5 13:16:52 UTC 2018


On Friday, 5 January 2018 at 13:10:25 UTC, Jonathan M Davis wrote:
> There was a recent PR for Phobos where Seb added static to a 
> bunch of foreach's that used AliasSeq. It hadn't actually 
> occurred to me that that was legal (I've basically just been 
> using static foreach where foreach with AliasSeq doesn't work), 
> but it is legal (which I suppose isn't surprising when you 
> think about it; I just hadn't). However, that got me to 
> wondering if such a change was purely aesthetic or whether it 
> might actually have an impact on build times - particularly 
> since running dub test for one of my recent projects keeps 
> taking longer and longer. So, I added static to a bunch of 
> foreach's over AliasSeqs in that project to see if it would 
> have any effect. The result was that dub test went from about 
> 16.5 seconds on my system to about 15.8 seconds - and that's 
> just by adding static to the foreach's over AliasSeqs, not 
> fundamentally changing what any of the code did. That's not a 
> huge speed up, but it's definitely something and far more than 
> I was expecting.
>
> Of course, you have to be careful with such a change, because 
> static foreach doesn't introduce a new scope, and double braces 
> are potentially required where they weren't before, but given 
> that I'd very much like to streamline that test build, adding 
> static to those foreach's was surprisingly worthwhile.
>
> 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.
>
> - Jonathan M Davis

It does not make any sense to me as to why using only instead of 
AliasSeq resulted in a speedup. I would've expected no change or 
worse performance. Any theories?


More information about the Digitalmars-d mailing list