Benefit from computing String Hash at compile time?

Reinier Zwitserloot reinier at zwitserloot.com
Sun Dec 20 02:10:23 PST 2009


Huh?

All you need to do is this:

add a method to java.lang.String with the signature:

public synthetic int switchCode() {
    return hashCode();
}


Why would this cause performance issues?

--Reinier Zwitserloot


On Sun, Dec 20, 2009 at 1:30 AM, Paul Benedict <pbenedict at apache.org> wrote:

> > This is wasteful, first because strings used in switch statements and
> also
> > in hashed collections will be hashed twice; second (and much more
> > important), every execution switch(str) needs to call your special
> hashcode
> > function again for str, as this hashcode cannot be cached in str. This
> extra
> > cost makes switch-on-string O(N) on the str.length, which makes the
> hashing
> > compilation strategy pointless.
>
> You make a good point. As for being "hashed twice", that's simply the
> cost of removing the reliance on String.hashCode(). Well, couldn't
> $switch_hashCode() perform some caching for itself?
>
> Paul
>
>



More information about the coin-dev mailing list