Benefit from computing String Hash at compile time?

Joseph D. Darcy Joe.Darcy at Sun.COM
Mon Dec 21 19:32:56 PST 2009


Paul Benedict wrote:
> Reinier,
>
>   
>> There is no need to explain that it can't ever change; that notion is
>> already inherent in the fact that the algorithm is explained in the javadoc.
>> The mistake seems to be in what 'part of the java spec' means. It does not
>> actually mean: Cannot possibly change.
>>     
>
> The algorithm is explained. The documentation is good, isn't it? It
> is, however, the documentation is for that version of the Java
> platform.
>
>   
>> The javadoc of String should also not be bogged down with the implementation
>> detail that string-on-switch is dependent on it. Implementation details have
>> no place in javadoc.
>>     
>
> I agree with you. No one has to reveal implementation details. All
> that is necessary is a note that the algorithm must not change from
> JDK version to JDK version.
>
> 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)) {
> ...
> }
>
>   

I'm still listening, but mostly on vacation until early 2010.

I'm quite familiar with the compatibility policies used to evolve the 
JDK and I've written about those in my blog; e.g.

"JDK Release Types and Compatibility Regions"
http://blogs.sun.com/darcy/entry/release_types_compatibility_regions

"Kinds of Compatibility: Source, Binary, and Behavioral"
http://blogs.sun.com/darcy/entry/kinds_of_compatibility

There is a vanishingly small chance changing the hash algorithm of 
string would ever be contemplated for Java SE; the behavioral 
compatibility risk would be too great given the ubiquitous use of the 
String class.  As one of my professors was fond of saying, "making 
virtue of a necessity," since the string hashing algorithm effectively 
cannot be changed, the current strings in switch implementation assumes 
it will be stable.

-Joe



More information about the coin-dev mailing list