About 30-40% event hashing performance boost
This commit is contained in:
+54
-2
@@ -20,6 +20,12 @@
|
||||
*/
|
||||
package com.vitorpamplona.quartz.nip01Core.crypto
|
||||
|
||||
import com.fasterxml.jackson.core.JsonEncoding
|
||||
import com.fasterxml.jackson.core.JsonGenerator
|
||||
import com.fasterxml.jackson.core.JsonProcessingException
|
||||
import com.fasterxml.jackson.core.util.BufferRecycler
|
||||
import com.fasterxml.jackson.core.util.ByteArrayBuilder
|
||||
import com.fasterxml.jackson.databind.JsonMappingException
|
||||
import com.fasterxml.jackson.databind.node.ArrayNode
|
||||
import com.fasterxml.jackson.databind.node.JsonNodeFactory
|
||||
import com.vitorpamplona.quartz.nip01Core.core.HexKey
|
||||
@@ -27,6 +33,7 @@ import com.vitorpamplona.quartz.nip01Core.core.toHexKey
|
||||
import com.vitorpamplona.quartz.nip01Core.jackson.JsonMapper
|
||||
import com.vitorpamplona.quartz.utils.Hex
|
||||
import com.vitorpamplona.quartz.utils.sha256.sha256
|
||||
import java.io.IOException
|
||||
|
||||
class EventHasher {
|
||||
companion object {
|
||||
@@ -64,13 +71,58 @@ class EventHasher {
|
||||
content: String,
|
||||
): String = JsonMapper.toJson(makeJsonObjectForId(pubKey, createdAt, kind, tags, content))
|
||||
|
||||
fun fastMakeJsonForId(
|
||||
pubKey: HexKey,
|
||||
createdAt: Long,
|
||||
kind: Int,
|
||||
tags: Array<Array<String>>,
|
||||
content: String,
|
||||
): ByteArray {
|
||||
val br: BufferRecycler = JsonMapper.mapper.factory._getBufferRecycler()
|
||||
try {
|
||||
ByteArrayBuilder(br).use { bb ->
|
||||
val generator = JsonMapper.mapper.createGenerator(bb, JsonEncoding.UTF8)
|
||||
generator.enable(JsonGenerator.Feature.COMBINE_UNICODE_SURROGATES_IN_UTF8)
|
||||
generator.use {
|
||||
it.writeStartArray()
|
||||
it.writeNumber(0)
|
||||
it.writeString(pubKey)
|
||||
it.writeNumber(createdAt)
|
||||
it.writeNumber(kind)
|
||||
it.writeStartArray()
|
||||
tags.forEach { tag ->
|
||||
it.writeStartArray()
|
||||
tag.forEach { value ->
|
||||
it.writeString(value)
|
||||
}
|
||||
it.writeEndArray()
|
||||
}
|
||||
it.writeEndArray()
|
||||
it.writeString(content)
|
||||
it.writeEndArray()
|
||||
}
|
||||
|
||||
val result = bb.toByteArray()
|
||||
bb.release()
|
||||
return result
|
||||
}
|
||||
} catch (e: JsonProcessingException) {
|
||||
throw e
|
||||
} catch (e: IOException) {
|
||||
// shouldn't really happen, but is declared as possibility so:
|
||||
throw JsonMappingException.fromUnexpectedIOE(e)
|
||||
} finally {
|
||||
br.releaseToPool()
|
||||
}
|
||||
}
|
||||
|
||||
fun hashIdBytes(
|
||||
pubKey: HexKey,
|
||||
createdAt: Long,
|
||||
kind: Int,
|
||||
tags: Array<Array<String>>,
|
||||
content: String,
|
||||
): ByteArray = sha256(makeJsonForId(pubKey, createdAt, kind, tags, content).toByteArray())
|
||||
): ByteArray = sha256(fastMakeJsonForId(pubKey, createdAt, kind, tags, content))
|
||||
|
||||
fun hashId(serializedJsonAsBytes: ByteArray): String = sha256(serializedJsonAsBytes).toHexKey()
|
||||
|
||||
@@ -94,4 +146,4 @@ class EventHasher {
|
||||
return Hex.isEqual(id, outId)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -79,13 +79,13 @@ class PoWMiner(
|
||||
|
||||
val bytes =
|
||||
EventHasher
|
||||
.makeJsonForId(
|
||||
pubKey,
|
||||
template.createdAt,
|
||||
template.kind,
|
||||
template.tags + PoWTag.assemble(initialNonce, desiredPoW),
|
||||
template.content,
|
||||
).toByteArray()
|
||||
.fastMakeJsonForId(
|
||||
pubKey = pubKey,
|
||||
createdAt = template.createdAt,
|
||||
kind = template.kind,
|
||||
tags = template.tags + PoWTag.assemble(initialNonce, desiredPoW),
|
||||
content = template.content,
|
||||
)
|
||||
|
||||
val startIndex = bytes.indexOf(initialNonce.toByteArray())
|
||||
|
||||
|
||||
Reference in New Issue
Block a user