Strings in Switch

Roel Spilker r.spilker at gmail.com
Thu Dec 10 00:53:20 PST 2009


Frederik

Two things:
- We're talking switch statements on String literals here. I don't expect
people to use long Strings here.
- Performance would only be a problem if you would execute the code very
often. Luckily, the hashCode of String is cached. So that should take care
of a potential performance hazard.

Roel

On Wed, Dec 9, 2009 at 4:34 PM, Fredrik Öhrström <
fredrik.ohrstrom at oracle.com> wrote:

> Yes, this is much better. This is much easier to understand and the
> pattern is trivial to catch in the JVM. There are  many opportunities
> for the compiler (even without strings-in-switch awareness) to optimize
> this sequence of compares and it avoids forcing a full calculation of a
> hash code that has to traverse the full string. Remember that a string
> compare can terminate early, a hashcode calculation cannot. Also a
> string compare works on as large blocks as possible per iteration
> (8bytes in 64 bit machines, even 16byte blocks with SSE2). If the JVM
> decides that it would be beneficial to use its own internal hashcodes to
> optimize the code, then it can do so.
>
>



More information about the coin-dev mailing list