[patterns-instanceof-primitive] RFR: 8303374: Compiler Implementation for Primitive types in patterns, instanceof, and switch

Michael Hixson duke at openjdk.org
Fri Mar 10 00:04:43 UTC 2023


On Tue, 7 Mar 2023 13:23:29 GMT, Aggelos Biboudis <abimpoudis at openjdk.org> wrote:

> Prototype implementation for primitive types in patterns, instanceof, and switch.
> 
> draft JEP: https://openjdk.org/jeps/8288476
> 
> draft spec: https://cr.openjdk.org/~abimpoudis/instanceof/latest/

src/java.base/share/classes/java/lang/runtime/ExactnessMethods.java line 178:

> 176:                 (64 - (Long.numberOfLeadingZeros(n) +
> 177:                         Long.numberOfTrailingZeros(n))) ;
> 178:     }

Are you interested in optimizing the performance of these methods at this time?

For example, in this method:
* You can remove the `Long.MIN_VALUE` check.  That value "accidentally" works out with the rest of the logic; it has no leading zeros and 63 trailing zeros.  This optimization assumes that `Long.MIN_VALUE` isn't common enough to deserve its own fast path (at the expense of every other input), which seems true.
* If you assume that most inputs to this method are "small" integers that do fit in doubles (which I think is true), you can optimize for them by checking trailing zeros lazily.
  ```java
  n = Math.abs(n);
  int z = Long.numberOfLeadingZeros(n);
  return z > 10 || z + Long.numberOfTrailingZeros(n) > 10;
  ```

I can share benchmarks if that's useful.  (I needed this method years ago, and I wrote benchmarks back then.)

I'm assuming these methods are invoked whenever an expression like `longValue instanceof double` is evaluated, and that these methods aren't intrinsified (yet), so they seem worth optimizing.  But I could be mistaken.

-------------

PR: https://git.openjdk.org/amber/pull/91


More information about the amber-dev mailing list