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

Michael Hixson duke at openjdk.org
Fri Mar 10 18:15:39 UTC 2023


On Fri, 10 Mar 2023 13:56:23 GMT, Raffaello Giulietti <rgiulietti at openjdk.org> wrote:

>> 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.
>
> @michaelhixson The more compact variant given above in the comment by @biboudis, compiles to minimal machine code. There's hardly any need for an intrinsic.

@rgiulietti In local benchmarks, the compact version of `long_double` is slower than what was in the PR originally, which in turn is slower than the "lazy trailing zeros" version in my previous comment.

Did you arrive at that compact form through benchmarking?  If so, great - my benchmarks might be bad or I might have conflicting results because I'm on Windows.  If not, I'll suggest that faster forms of this method may exist, which may be worth investigating... eventually.

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

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


More information about the amber-dev mailing list