RFR: 8339531: Improve performance of MemorySegment::mismatch [v9]
Brett Okken
duke at openjdk.org
Fri Sep 13 13:06:14 UTC 2024
On Wed, 11 Sep 2024 13:15:57 GMT, Per Minborg <pminborg at openjdk.org> wrote:
>> src/java.base/share/classes/jdk/internal/foreign/SegmentBulkOperations.java line 244:
>>
>>> 242: return (Architecture.isLittleEndian()
>>> 243: ? Long.numberOfTrailingZeros(x)
>>> 244: : Long.numberOfLeadingZeros(x)) / 8;
>>
>> Would there be value in having a constant LongToIntFunction lambda to capture this logic?
>>
>>
>> private static final LongToIntFunction LONG_LEADING_ZEROS = Architecture.isLittleEndian() ? Long::numberOfTrailingZeros : Long::numberOfLeadingZeros;
>
> Performance-wise, I suspect there would not be that much of a difference. The compiler will see that Architecture.isLittleEndian provides a stable value and can optimize and eliminate code (I think). I am unsure which is easier to read.
part of the value would be if it allowed just removing this method entirely and moving the logic above:
if (s != d) {
return start + offset + mismatch(s, d);
}
would become:
if (s != d) {
return start + offset + (LONG_LEADING_ZEROS.apply(s^d) / 8);
}
-------------
PR Review Comment: https://git.openjdk.org/jdk/pull/20848#discussion_r1758828018
More information about the core-libs-dev
mailing list