class templates and static if
Jonathan M Davis
jmdavisProg at gmx.com
Mon Feb 27 14:06:53 PST 2012
On Monday, February 27, 2012 23:55:39 Dmitry Olshansky wrote:
> On 27.02.2012 23:18, Jonathan M Davis wrote:
> > On Monday, February 27, 2012 10:55:12 Ali Çehreli wrote:
> >> On 02/27/2012 08:29 AM, Tyler Jameson Little wrote:
> >>> I didn't want to do subclassing, because my parser is a state-machine
> >>
> >> style
> >>
> >>> parser, so it's in a big switch. Pretty gross, but I would like it to
> >>
> >> be as
> >>
> >>> fast as possible. That's why I thought this model would be so cool,
> >>
> >> because
> >>
> >>> I could remove conditions from the generated code, and get rid of a
> >>
> >> lot of
> >>
> >>> the conditionals.
> >>
> >> I am pretty sure switch statements boil down to a sequence conditionals
> >> consisting of equality comparisons.
> >>
> >> I know that some compilers use optimizations where the comparisons are
> >> converted to a single lookup, but last I checked, dmd does not apply
> >> that optimization. Perhaps because it's not implemented yet, or because
> >> using a table lookup might be slower because of reaching outside of the
> >> cpu cache. (Or another reason that I am not aware of. :))
> >
> > Yeah. In theory, switch statements can be optimized better than if-else
> > chains, and eventually I'd fully expect that dmd would do that, but right
> > now, I don't think that they really are. You'd have to look at the
> > generated assembly though to be sure though.
>
> Please stop spreading the misconception that they are going to be
> optimized "probably and eventually". Switch statements over integers are
> much better then if/else chains and were for something like 20 years.
> They do produce nice improvements over a sequence of if/else because
> values are known in advance and are bounded.
> So it goes like this:
[snip]
> And for the record I seen all of that in assembly listings produced by
> dmd, when optimizing VM dispatch in std.regex. I had to revisit opcodes
> layout a bit so that dmd outputs jump table and not a binary-searching
> like stuff in a tight loop.
I'm not saying that dmd doesn't ever optimize switch statements. I'm just
saying that as I understand it, it doesn't always do so (its performance with
ranged case statements isn't great for instance). Odds are that it _does_
optimize straight integer case statements fairly well, because that's the
classic C stuff (and if you've verified that, all the better - I haven't). It's
the new stuff that's less likely to be well-optimized (like ranged case
statements or case statements with strings). And those probably _will_ be
better optimized eventually, but as I understand it, they aren't now.
So, if the reason for using a switch statement over and an if-else chain is
purely for performance (particularly if doing something else than just using
plain case statements with integers like in C), then you should probably
profile your code and/or look at the assembly to be sure that you're getting
the performance gain that you think that you're getting.
- Jonathan M Davis
More information about the Digitalmars-d-learn
mailing list