Benefit from computing String Hash at compile time?

Osvaldo Pinali Doederlein opinali at gmail.com
Sat Dec 19 11:06:48 PST 2009


Em 19/12/2009 15:28, Paul Benedict escreveu:
> Moving on...
> If anyone at Sun is still listening (::grins::), I prefer to emit a
> static method that contains a duplicate of the hashCode() algorithm.
> Then, no one has to worry about JDK version upgrades and
> String.hashCode() is free for future tweaking.
>
> static int $switch_hashCode(String s) {... }
> switch ($switch_hashCode(s)) {
> ...
> }
>
> Paul
>    

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.

A+
Osvaldo



More information about the coin-dev mailing list