From 282d4614c0b000c82806105dd54c68a89ac4b4ec Mon Sep 17 00:00:00 2001 From: Vitor Pamplona Date: Thu, 8 Aug 2024 18:19:48 -0400 Subject: [PATCH] Adds zap amount cache for the memory space calculations --- .../vitorpamplona/quartz/events/LnZapEvent.kt | 23 ++++++++++--------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/quartz/src/main/java/com/vitorpamplona/quartz/events/LnZapEvent.kt b/quartz/src/main/java/com/vitorpamplona/quartz/events/LnZapEvent.kt index e0dff9863..568fc4d87 100644 --- a/quartz/src/main/java/com/vitorpamplona/quartz/events/LnZapEvent.kt +++ b/quartz/src/main/java/com/vitorpamplona/quartz/events/LnZapEvent.kt @@ -39,9 +39,20 @@ class LnZapEvent( // This event is also kept in LocalCache (same object) @Transient val zapRequest: LnZapRequestEvent? + // Keeps this as a field because it's a heavier function used everywhere. + val amount by lazy { + try { + lnInvoice()?.let { LnInvoiceUtil.getAmountInSats(it) } + } catch (e: Exception) { + Log.e("LnZapEvent", "Failed to Parse LnInvoice ${lnInvoice()}", e) + null + } + } + override fun countMemory(): Long = super.countMemory() + - pointerSizeInBytes + (zapRequest?.countMemory() ?: 0) // rough calculation + pointerSizeInBytes + (zapRequest?.countMemory() ?: 0) + // rough calculation + pointerSizeInBytes + 36 // bigdecimal size override fun containedPost(): LnZapRequestEvent? = try { @@ -75,16 +86,6 @@ class LnZapEvent( override fun amount() = amount - // Keeps this as a field because it's a heavier function used everywhere. - val amount by lazy { - try { - lnInvoice()?.let { LnInvoiceUtil.getAmountInSats(it) } - } catch (e: Exception) { - Log.e("LnZapEvent", "Failed to Parse LnInvoice ${lnInvoice()}", e) - null - } - } - override fun content(): String = content fun lnInvoice() = tags.firstOrNull { it.size > 1 && it[0] == "bolt11" }?.get(1)