Reduces JC usage by creating arrays with the exact amount of bytes when deserializing events.

This commit is contained in:
Vitor Pamplona 2023-11-28 09:37:00 -05:00
parent 98d29ac6dc
commit d87542a949

View File

@ -272,13 +272,14 @@ open class Event(
private class GossipDeserializer : StdDeserializer<Gossip>(Gossip::class.java) {
override fun deserialize(jp: JsonParser, ctxt: DeserializationContext): Gossip {
val jsonObject: JsonNode = jp.codec.readTree(jp)
val tagList = jsonObject.get("tags")
return Gossip(
id = jsonObject.get("id")?.asText()?.intern(),
pubKey = jsonObject.get("pubkey")?.asText()?.intern(),
createdAt = jsonObject.get("created_at")?.asLong(),
kind = jsonObject.get("kind")?.asInt(),
tags = jsonObject.get("tags")?.map {
it.mapNotNull { s -> if (s?.isNull ?: true) null else s.asText().intern() }
tags = tagList.mapTo(ArrayList<List<String>>(tagList.size())) {
it.mapNotNullTo(ArrayList<String>(it.size())) { s -> if (s.isNull) null else s.asText().intern() }
},
content = jsonObject.get("content")?.asText()
)
@ -360,13 +361,14 @@ open class Event(
)
fun fromJson(jsonObject: JsonNode): Event {
val tagList = jsonObject.get("tags")
return EventFactory.create(
id = jsonObject.get("id").asText().intern(),
pubKey = jsonObject.get("pubkey").asText().intern(),
createdAt = jsonObject.get("created_at").asLong(),
kind = jsonObject.get("kind").asInt(),
tags = jsonObject.get("tags").map {
it.mapNotNull { s -> if (s.isNull) null else s.asText().intern() }
tags = tagList.mapTo(ArrayList<List<String>>(tagList.size())) {
it.mapNotNullTo(ArrayList<String>(it.size())) { s -> if (s.isNull) null else s.asText().intern() }
},
content = jsonObject.get("content").asText(),
sig = jsonObject.get("sig").asText()