From e408db09442ecd46c2f7485dc2056a6dd0b165df Mon Sep 17 00:00:00 2001 From: Claude Date: Thu, 9 Apr 2026 13:27:25 +0000 Subject: [PATCH] perf: inline unsignedMultiplyHighFallback to eliminate 32K function calls MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Trace profiling showed unsignedMultiplyHighFallback at 16.9% of verify time (32,524 calls × 82ns = 2.674ms). Although called from inside an inline crossinline lambda, the function itself was a regular dispatch. Adding @Suppress("NOTHING_TO_INLINE") inline makes the Kotlin compiler embed the 4-multiply arithmetic directly at each call site, eliminating all function dispatch overhead. https://claude.ai/code/session_01EMY5RnXb9rnsyU2KbXrSaY --- .../vitorpamplona/quartz/utils/secp256k1/MultiplyHigh.kt | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/quartz/src/commonMain/kotlin/com/vitorpamplona/quartz/utils/secp256k1/MultiplyHigh.kt b/quartz/src/commonMain/kotlin/com/vitorpamplona/quartz/utils/secp256k1/MultiplyHigh.kt index 77992e7824..d5eb182e97 100644 --- a/quartz/src/commonMain/kotlin/com/vitorpamplona/quartz/utils/secp256k1/MultiplyHigh.kt +++ b/quartz/src/commonMain/kotlin/com/vitorpamplona/quartz/utils/secp256k1/MultiplyHigh.kt @@ -52,8 +52,14 @@ internal expect fun unsignedMultiplyHigh( * if b < 0) and the unsigned correction terms (+ (a & (b >> 63)) + (b & (a >> 63))). * Saves ~8 instructions per call on Android < API 31, where this is the hot path * (~30,000 calls per signature verify). + * + * MUST be inline: this function is called ~32,000 times per verify via the fused + * fieldMulReduce crossinline lambda. Without inline, each call is a real function + * dispatch (~82ns on ART). With inline, the Kotlin compiler embeds the arithmetic + * directly at each call site — zero dispatch overhead. */ -internal fun unsignedMultiplyHighFallback( +@Suppress("NOTHING_TO_INLINE") +internal inline fun unsignedMultiplyHighFallback( a: Long, b: Long, ): Long {