Re: Switch statement code generation

anton@mips.complang.tuwien.ac.at (Anton Ertl)
Wed, 11 Nov 2009 19:42:15 GMT

          From comp.compilers

Related articles
[9 earlier articles]
Re: Switch statement code generation bartc@freeuk.com (bartc) (2009-11-05)
Re: Switch statement code generation nathan.mccauley@gmail.com (nathan.mccauley@gmail.com) (2009-11-07)
Re: Switch statement code generation DrDiettrich1@aol.com (Hans-Peter Diettrich) (2009-11-08)
Re: Switch statement code generation bear@sonic.net (Ray) (2009-11-09)
Re: Switch statement code generation DrDiettrich1@aol.com (Hans-Peter Diettrich) (2009-11-11)
Re: Switch statement code generation derek@knosof.co.uk (Derek M. Jones) (2009-11-11)
Re: Switch statement code generation anton@mips.complang.tuwien.ac.at (2009-11-11)
Re: Switch statement code generation idbaxter@semdesigns.com (Ira Baxter) (2009-11-14)
Re: Switch statement code generation cfc@shell01.TheWorld.com (Chris F Clark) (2009-11-15)
Re: Switch statement code generation pertti.kellomaki@tut.fi (Pertti Kellomaki) (2009-11-16)
Re: Switch statement code generation haberg_20080406@math.su.se (Hans Aberg) (2009-11-17)
| List of all articles for this month |

From: anton@mips.complang.tuwien.ac.at (Anton Ertl)
Newsgroups: comp.compilers
Date: Wed, 11 Nov 2009 19:42:15 GMT
Organization: Institut fuer Computersprachen, Technische Universitaet Wien
References: 09-11-002 09-11-014 09-11-023 09-11-027
Keywords: code
Posted-Date: 13 Nov 2009 00:49:40 EST

Hans-Peter Diettrich <DrDiettrich1@aol.com> writes:
>nathan.mccauley@gmail.com schrieb:
>
>>>> I'm trying to put together a list of various methods to do code generate
>>>> for switch statements. ...
>>
>> Has anyone seen tries used for the string-based switch statements?
...
>[If there's a lot of strings, a patricia trie might be faster than
>hashing or binary search since it doesn't require repeated scans of
>the string during the match process. -John]


It's very doubtful that it's faster than hashing. In hashing you
traverse the tree once for computing the hash function and once for
the final match. All other match attempts (and there are typically <1
of those) will typically mismatch at the first character, so no
complete scan is necessary. You typically have to fetch one cache
line per match attempt (i.e., typically 1-2; if you use external
chaining, add another cache line fetch for the initial table lookup).


In contrast, in the trie method you have to chase a pointer for each
substring you look at; if there are a lot of string, it's probably at
every character for the first few characters. And for each pointer
you chase, you will have to fetch a cache line.


Ok, if there is little competition for the D-cache, the first few
levels of the trie might end up in the D-cache. OTOH, if a few words
are looked up often, all the lines necessary for their lookup will be
in the D-cache with a hash table, too.


Overall, I guess you can find cases where the patricia trie is faster
(an application that does little but looking up lots of different,
relatively short strings) but in most cases I think that hashing is
faster, assuming good implementations of each data structure.


For our switch statement, we have to consider another issue: the data
structures can be hard-coded; then at each node of the trie there will
be a conditional or an indirect branch; for the hash table we have an
indirect jump after computing the hash function, and then one or two
conditional branches afterward. Depending on how repetetive and
predictable the strings coming in are, the branch predictors may
predict very well or very badly (i.e., 50% mispredicts for conditional
branches, 100% for indirect branches).


Anyway, the more branches there are, the worse the effect of bad
predictability will be (hitting the trie harder). It may be better
not to hard-code the data structure to avoid some of the
mispredictions; in the end, though, you probably will perform one
indirect jump if you don't hard-code the data structure; I think that
hard-coding the hash table will not be worse than not hard-coding it,
but hard-coding the trie can be worse (depending on circumstances).


- anton
--
M. Anton Ertl
anton@mips.complang.tuwien.ac.at
http://www.complang.tuwien.ac.at/anton/



Post a followup to this message

Return to the comp.compilers page.
Search the comp.compilers archives again.