Strings in Switch

Joseph D. Darcy Joe.Darcy at Sun.COM
Wed Dec 9 10:30:35 PST 2009


Fredrik Öhrström wrote:
> This discussion reeks of premature optimization.... A tableswitch on
> arbitrary large numbers (aka hashcodes) must be compiled into a sequence
> of compares anyway (at least on the x86 platform). If the tableswitch
> happens on a sequence of relatively consecutive  numbers, then the JVM
> can create a jump table. But for hashcodes, no way!
>   

Implementing strings in switch has always been a fertile topic for 
discussion!

The purpose of the synthesized initial switch in the two-switch strings 
in switch implementation is to create a dense contiguous set of integral 
jump targets for the second switch that are easy to digest for a JVM.

When designing this strings in switch implementation, various factors 
came into play including minimizing the worst-case behavior in terms of 
number of character comparisons.  Currently the strings being switched 
on is expected to be traversed at most twice, once to compute the hash 
code and again to be compared at the hash site.  (If there are hash 
collisions, multiple compares could occur at the hash site.)

For a chain of if-equals-else-if-equals chain, the number of expected 
character comparisons will be likely be higher since when the string 
being switched is present as a target, on average it would be compared 
with about half the target strings.

Depending on the fraction of strings that have hash codes precomputed, 
the fraction of switched on strings that are or are not in the target 
list, and various other properties of the strings being switched on and 
the strings in the target set, different strings in switch 
implementations can be driven to have pathological behavior.

That said, I believe the current strings in switch implementation is 
correct and should have acceptable performance.

I'd be willing to investigate re-engineering the strings in switch 
implementation once:

1) A greater number of the Coin features are implemented, specified, and 
tested.
2) There is some usage of strings in switch to guide the implementation 
strategy.

-Joe




More information about the coin-dev mailing list