JDK-8055913 : jdk.nashorn.internal.ir.Node.hashCode() delegates to Object.hashCode() and is hot
  • Type: Enhancement
  • Component: core-libs
  • Sub-Component: jdk.nashorn
  • Priority: P4
  • Status: Resolved
  • Resolution: Fixed
  • OS: generic
  • CPU: generic
  • Submitted: 2014-08-25
  • Updated: 2015-06-04
  • Resolved: 2014-08-26
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
8u40Fixed 9 b29Fixed
Profiling a simple scenario: 

$ ~/Install/jdk9u20/bin/java -jar dist/nashorn.jar -Dnashorn.typeInfo.disabled=false --class-cache-size=0 --persistent-code-cache=false -scripting --log=time test/script/basic/compile-octane.js -- --iterations 5 

...yields a few simple low-hanging fruits, here is one of them. Out of 320 seconds of run, 4 seconds are spent in native hashcode for Node

4.550	jdk.nashorn.internal.codegen.ProgramPoints.setProgramPoint(jdk.nashorn.internal.ir.Optimistic)
4.650	java.util.HashSet.contains(java.lang.Object)
3.770	java.util.HashMap.containsKey(java.lang.Object)
3.900	java.util.HashMap.hash(java.lang.Object)
3.800	jdk.nashorn.internal.ir.Node.hashCode()
3.800	java.lang.Object.hashCode()

While the native hashcode can be fast, it still requires trip to the VM for initializing, it touches the object header and/or stores the identity hashcode somewhere, etc. We need to figure out: a) which of the Node subclasses delegate equals/hashCode to Node, and whether they should use their own hashCode/equals; b) whether the plain Java hashcode (e.g. static int HC; hashCode() { return HC++; }) would be better.

I think Long.hashCode(token) would be a good implementation.

Aw, yes, it should be stored in the instance field, and initialized off the static HC field :)

Surely you didn't mean incrementing a static field in hashCode, have you :-) ? We could derive a hash code from start, finish, and token fields.