On Friday, January 05, 2018 13:16:52 Meta via Digitalmars-d wrote: > 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?
I don't know. It's probably related to however foreach over an AliasSeq is implemented. The fact that only is faster than using an array literal doesn't surprise me though, since CTFE probably does a bunch of extra, unnecessary allocation when dealing with array literal. Maybe the speed difference between AliasSeq and only is a sign of something that could be improved in the compiler's implementation, or maybe only is just fundamentally faster for one reason or another. I don't know. But from what I can tell, the speed difference is large enough that it's kind of crazy to use AliasSeq with values when static foreach and only will work just as well. - Jonathan M Davis
