From 9a8ed53279459007fb55d328f132842fe370f362 Mon Sep 17 00:00:00 2001 From: Claude Date: Sun, 5 Apr 2026 23:34:49 +0000 Subject: [PATCH] perf: sha256StreamWithCount uses ThreadLocal instead of pool Extends the ThreadLocal SHA256 optimization to the streaming hash function. The pool is retained for EventHasherSerializer and HashingByteArrayBuilder which use its acquire/release pattern for long-lived incremental hashing. https://claude.ai/code/session_01BhU63WUe9AhikZxRdw3Lpg --- .../quartz/utils/sha256/Sha256.jvmAndroid.kt | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/quartz/src/jvmAndroid/kotlin/com/vitorpamplona/quartz/utils/sha256/Sha256.jvmAndroid.kt b/quartz/src/jvmAndroid/kotlin/com/vitorpamplona/quartz/utils/sha256/Sha256.jvmAndroid.kt index 1cf55bc5f..5ac3608ba 100644 --- a/quartz/src/jvmAndroid/kotlin/com/vitorpamplona/quartz/utils/sha256/Sha256.jvmAndroid.kt +++ b/quartz/src/jvmAndroid/kotlin/com/vitorpamplona/quartz/utils/sha256/Sha256.jvmAndroid.kt @@ -37,7 +37,7 @@ private val threadLocalDigest = actual fun sha256(data: ByteArray): ByteArray = threadLocalDigest.get().digest(data) -/** Pool is still available for streaming use cases that need incremental hashing. */ +/** Pool for incremental hashing (used by EventHasherSerializer, HashingByteArrayBuilder). */ val pool = Sha256Pool(25) /** @@ -54,6 +54,12 @@ fun sha256StreamWithCount( bufferSize: Int = 8192, ): Pair { val countingStream = CountingInputStream(inputStream) - val hash = pool.hashStream(countingStream, bufferSize) + val digest = threadLocalDigest.get() + val buffer = ByteArray(bufferSize) + var bytesRead: Int + while (countingStream.read(buffer).also { bytesRead = it } != -1) { + digest.update(buffer, 0, bytesRead) + } + val hash = digest.digest() return Pair(hash, countingStream.bytesRead) }