RFR: 8263087: Add a MethodHandle combinator that switches over a set of MethodHandles
jvernee at openjdk.java.net
Tue Apr 13 22:39:57 UTC 2021
On Tue, 13 Apr 2021 22:00:57 GMT, Remi Forax <forax at univ-mlv.fr> wrote:
> About your benchmark, did you test with some strings going into "default", because it is usually in that case that you need a proper lookup switch,
another way to say it is that, your results are too good when you use a cascade of guardWithTest.
Yes, for the benchmarks I ran, the default case was just as likely as the other cases, so e.g. if there were 10 cases, there was a 1/11 chance the default case was hit. This might need tweaking to be more realistic but...
Note that the cascading guard with test actually works more like a binary search, where each guard tests against a pivot point in the search, and then decides to go either to the left or the right side of the tree. So, when looking up the default value we don't necessarily need to do a search over all the cases. Only for hash collisions does it fall back to a linear search over all the values with the same hash code.
This is also how C2 translates `lookupswitch` as far as I know (but maybe John can confirm or deny whether my reading of the C2 code is correct), so I'm not surprised to see that the if-tree approach is so close to a native `lookupswitch` instruction in performance.
More information about the core-libs-dev