AliasSeq seems to compile slightly faster with static foreach

Jonathan M Davis newsgroup.d at jmdavisprog.com
Fri Jan 5 13:22:22 UTC 2018


On Friday, January 05, 2018 06:10:25 Jonathan M Davis via Digitalmars-d 
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.

And actually, by tracking down every stray foreach over an AliasSeq which
was used with values (and thus could be converted to using static foreach
with only or only with iota and lockstep if indices were needed), and the
build time is down to about

8.6 seconds.

So, it's now down to a bit over half of what it was originally by switching
all of the foreach's over AliasSeq's of values to using static foreach over
only's of those same values.

- Jonathan M Davis



More information about the Digitalmars-d mailing list