JDK-8198477 : Arrays.binarySearch on constant array should be optimized by the JIT
  • Type: Enhancement
  • Component: hotspot
  • Sub-Component: compiler
  • Affected Version: 10
  • Priority: P4
  • Status: Open
  • Resolution: Unresolved
  • Submitted: 2018-02-21
  • Updated: 2019-01-15
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.
Other
tbdUnresolved
Related Reports
Relates :  
Description
The various kinds of switch statements (strings, enums, non-dense ints, and more) are being code-generated (by javac) through a unified framework that uses Arrays.binarySwitch over constant arrays in order to classify data in log time.  (See JDK-8161250.)  Even non-dense integer case sequences, formerly handled by lookupswitch, may be translated into a binary search over a sorted array of ints or longs, and mapped to a dense index to a tableswitch.

See calls to binarySearch in this file:
http://hg.openjdk.java.net/amber/amber/file/switch/src/java.base/share/classes/java/lang/runtime/SwitchBootstraps.java

This means that new code shapes will hit the JIT which perform binary searching over short int (and also long) arrays.  The JIT will lose its ability to prune dead code, if it cannot predict the result of executing the binary search that computes the mapping to the tableswitch index.

It will sometimes be the case that the JIT will be able to predict the value (or reduced range of values) of a switch value.  (This happens if the method containing the switch is inlined into a method which supplies a constant operand to the switch.)  If that happens, the JIT should "manually" perform the binarySwitch over the constant index array.  (The array can be trusted, as it will probably be defined as @Stable or perhaps frozen.)  If the JIT can predict the result of the binarySearch, or if it can predict a reduced range of possible results, it can also predict a subset of live tableswitch control points.  This will allow it to fold up dead code, with the usual benefits.