AliasSeq seems to compile slightly faster with static foreach

Jonathan M Davis newsgroup.d at jmdavisprog.com
Mon Jan 8 20:20:24 UTC 2018


On Monday, January 08, 2018 07:37:01 H. S. Teoh via Digitalmars-d wrote:
> On Sun, Jan 07, 2018 at 07:20:44PM -0700, Jonathan M Davis via
> Digitalmars-d wrote: [...]
>
> > LOL. Okay, I feel like an idiot now. Switching to static foreach
> > resulted in a slight speed-up, and then switching to only resulted in
> > a very large speed-up. However, it turns out that the main reason that
> > I got the speed-up that I did with only was because I screwed up.
> >
> > In a number of places, I was using only by itself, but in a bunch of
> > places, I was using only with lockstep and iota so that I could have
> > indices, and I screwed up with the iota call. I misremembered how iota
> > worked with a single argument; I thought that iota(0) created an
> > infinite range, whereas it creates a zero-length range. Once I fixed
> > it so that it was iota(size_t.max), it was actually slightly slower
> > than using array literals.  So, while I definitely got a speed-up by
> > switching away from using normal foreach with AliasSeq, ultimately,
> > the really large speed-up I got by switching to only was because I was
> > accidentally not even compiling in large sections of test code. :|
>
> [...]
>
> This is why sometimes I deliberately modify a unittest to make it fail,
> just so I'm sure that the code is actually being run. :-P  There's been
> at least one incident where I wrote a whole bunch of unittests and
> thought my code was OK because there was no runtime error, only to
> discover that it was only because I forgot to specify -unittest on the
> compiler command line. :-/
>
> Or in another incident, the unittest was inside a template, and I had
> protected it with a static if on a specific combination of template
> arguments so that it will only be instantiated once, but then it turned
> out that that one instantiation never actually happened.

I'll do stuff like put writelns in unit tests to verify that they're running
(the same for normal code too) when I actually suspect that something is
wrong, but if I don't suspect anything, then I won't, because I don't have
any reason to. In most cases, this sort of thing doesn't happen with a unit
test, because they pretty much always have at least one failure when I'm
writing them (especially when I'm being thorough with the tests), but here,
I had refactored existing tests, so a failure wasn't expected, and it wasn't
until I was writing new tests later that it became clear that something was
off.

Either way, managing to not even compile in code when using a foreach that's
intended to be static is a new one for me. That sort of thing is a lot
harder to do with a foreach (static or otherwise) over an AliasSeq, though I
suppose that something like std.meta.Filter would make it easy enough.

- Jonathan M Davis



More information about the Digitalmars-d mailing list