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
This commit is contained in:
+8
-2
@@ -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<ByteArray, Long> {
|
||||
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)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user