perf: make uLt() platform-specific to avoid JVM regression
The inline uLt() using XOR trick in commonMain regressed JVM by ~30% because HotSpot's Long.compareUnsigned is a JIT intrinsic (single unsigned CMP + SETB), while the XOR trick generates 2 extra XOR insns that HotSpot doesn't optimize away. Convert uLt to expect/actual: - JVM: Long.compareUnsigned (HotSpot intrinsic) - Android: XOR trick (avoids ULong.constructor-impl NOOP calls) - Native: XOR trick (no JVM intrinsics available) JVM verify: 2.1x → 1.5x (restored, slightly better than 1.6x baseline) Android: unchanged (still uses XOR trick) https://claude.ai/code/session_01EMY5RnXb9rnsyU2KbXrSaY
This commit is contained in:
+32
@@ -0,0 +1,32 @@
|
||||
/*
|
||||
* Copyright (c) 2025 Vitor Pamplona
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||
* this software and associated documentation files (the "Software"), to deal in
|
||||
* the Software without restriction, including without limitation the rights to use,
|
||||
* copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
|
||||
* Software, and to permit persons to whom the Software is furnished to do so,
|
||||
* subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
||||
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
||||
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
|
||||
* AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||||
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
package com.vitorpamplona.quartz.utils.secp256k1
|
||||
|
||||
/**
|
||||
* Android: XOR-with-MIN_VALUE trick avoids the ULong.constructor-impl
|
||||
* NOOP invokestatic calls that Kotlin's toULong() generates. Produces
|
||||
* pure arithmetic bytecode (lxor, lcmp) with zero method calls.
|
||||
* ART's JIT compiles this to EOR + CMP + CSET on ARM64.
|
||||
*/
|
||||
internal actual fun uLt(
|
||||
a: Long,
|
||||
b: Long,
|
||||
): Boolean = (a xor Long.MIN_VALUE) < (b xor Long.MIN_VALUE)
|
||||
Reference in New Issue
Block a user