Strings in Switch

Joseph D. Darcy Joe.Darcy at Sun.COM
Sun Dec 6 22:07:59 PST 2009


Paul Benedict wrote:
> Joe,
>
> I reviewed the check-in and read a presentation about the
> implementation. I get that it translates into two switch statements,
> but I think there are cases where the duality can be eliminated. If
> the first switch statement produces no hash collisions, I don't think
> the second switch statement is necessary. Thoughts?
>
>   

There are many possible desugarings of strings in switch into one or 
more switch statements and other existing code structures, some of which 
are alluded to the comments of the current strings in switch implementation.

If there is no "bad" control flow in the original strings in switch (no 
fall throughs, etc.) and no collisions with the chosen hash function, 
then yes the the code
  case "foo":
can be replaced with
  case "foo".hashCode():
      if ("foo".equals(...")) {...}
if care is taken to implement the semantics of any default alternative 
that is present.

However, for the initial strings in switch implementation in javac, we 
choose to pursue a single general-purpose strings in switch translation 
that should always provide at least reasonable performance since it 
results in less compiler code to test for what is currently a low 
duty-cycle code structure.

If special cases of strings in switch turn out to have high duty-cycles, 
that would justify additional engineering to support different 
implementations tailored to different code inputs.

-Joe




More information about the coin-dev mailing list