From 0886af43865b8bb8a77ff149baae4e5439c61870 Mon Sep 17 00:00:00 2001 From: Vitor Pamplona Date: Fri, 12 Sep 2025 11:11:16 -0400 Subject: [PATCH] Migrates memory counters from Long to Int --- .../com/vitorpamplona/amethyst/AppModules.kt | 1 - .../com/vitorpamplona/amethyst/DebugUtils.kt | 42 +++++++++++++++++-- .../service/okhttp/OkHttpWebSocket.kt | 2 +- .../relayClient/speedLogger/FrameStat.kt | 2 +- .../relayClient/speedLogger/KindGroup.kt | 9 ++-- .../amethyst/ui/note/RelayListRow.kt | 2 +- .../interactiveStories/tags/RootSceneTag.kt | 4 +- .../quartz/nip01Core/core/Event.kt | 4 +- .../quartz/nip01Core/hints/EventHintBundle.kt | 2 +- .../client/single/basic/BasicRelayClient.kt | 6 +-- .../nip01Core/relay/client/stats/RelayStat.kt | 14 +++---- .../relay/client/stats/RelayStats.kt | 8 ++-- .../relay/sockets/WebSocketListener.kt | 2 +- .../sockets/okhttp/BasicOkHttpWebSocket.kt | 2 +- .../nip01Core/tags/addressables/ATag.kt | 4 +- .../nip01Core/tags/addressables/Address.kt | 4 +- .../quartz/nip01Core/tags/dTags/DTag.kt | 2 +- .../quartz/nip01Core/tags/events/ETag.kt | 2 +- .../quartz/nip01Core/tags/people/PTag.kt | 2 +- .../quartz/nip02FollowList/tags/ContactTag.kt | 2 +- .../quartz/nip13Pow/tags/PoWTag.kt | 2 +- .../nip18Reposts/quotes/QAddressableTag.kt | 2 +- .../quartz/nip18Reposts/quotes/QEventTag.kt | 2 +- .../nip28PublicChat/list/tags/ChannelTag.kt | 2 +- .../quartz/nip46RemoteSigner/BunkerMessage.kt | 2 +- .../quartz/nip46RemoteSigner/BunkerRequest.kt | 2 +- .../nip46RemoteSigner/BunkerResponse.kt | 2 +- .../quartz/nip47WalletConnect/Response.kt | 10 ++--- .../quartz/nip48ProxyTags/ProxyTag.kt | 2 +- .../bookmarkList/tags/AddressBookmark.kt | 2 +- .../bookmarkList/tags/EventBookmark.kt | 2 +- .../nip51Lists/muteList/tags/UserTag.kt | 2 +- .../nip51Lists/muteList/tags/WordTag.kt | 2 +- .../meetingSpaces/tags/MeetingSpaceTag.kt | 2 +- .../presence/tags/MeetingRoomTag.kt | 2 +- .../quartz/nip57Zaps/LnZapEvent.kt | 2 +- .../nip57Zaps/zapraiser/ZapRaiserTag.kt | 2 +- .../nip59Giftwrap/seals/SealedRumorEvent.kt | 2 +- .../nip59Giftwrap/wraps/GiftWrapEvent.kt | 2 +- .../quartz/nip71Video/tags/TextTrackTag.kt | 2 +- .../approval/tags/ApprovedAddressTag.kt | 2 +- .../follow/tags/CommunityTag.kt | 2 +- .../definition/AppDefinitionEvent.kt | 2 +- .../definition/AppMetadata.kt | 36 ++++++++-------- .../NIP90ContentDiscoveryResponseEvent.kt | 2 +- .../vitorpamplona/quartz/utils/StringUtils.kt | 8 ++-- 46 files changed, 125 insertions(+), 91 deletions(-) diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/AppModules.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/AppModules.kt index 6f445a26a..29c888d45 100644 --- a/amethyst/src/main/java/com/vitorpamplona/amethyst/AppModules.kt +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/AppModules.kt @@ -69,7 +69,6 @@ import kotlinx.coroutines.SupervisorJob import kotlinx.coroutines.cancel import kotlinx.coroutines.launch import java.io.File -import kotlin.run class AppModules( val appContext: Context, diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/DebugUtils.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/DebugUtils.kt index 7d3d0719b..9224b615d 100644 --- a/amethyst/src/main/java/com/vitorpamplona/amethyst/DebugUtils.kt +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/DebugUtils.kt @@ -106,6 +106,42 @@ fun debugState(context: Context) { " / " + LocalCache.users.size(), ) + Log.d( + "STATE DUMP", + "Public Chat Channels: " + + LocalCache.publicChatChannels.filter { _, it -> it.flowSet != null }.size + + " / " + + LocalCache.publicChatChannels.size() + + " / " + + LocalCache.publicChatChannels.values().sumOf { it.notes.size() }, + ) + Log.d( + "STATE DUMP", + "Live Chat Channels: " + + LocalCache.liveChatChannels.filter { _, it -> it.flowSet != null }.size + + " / " + + LocalCache.liveChatChannels.size() + + " / " + + LocalCache.liveChatChannels.values().sumOf { it.notes.size() }, + ) + Log.d( + "STATE DUMP", + "Ephemeral Chat Channels: " + + LocalCache.ephemeralChannels.filter { _, it -> it.flowSet != null }.size + + " / " + + LocalCache.ephemeralChannels.size() + + " / " + + LocalCache.ephemeralChannels.values().sumOf { it.notes.size() }, + ) + LocalCache.chatroomList.forEach { key, room -> + Log.d( + "STATE DUMP", + "Private Chats $key: " + + room.rooms.size() + + " / " + + room.rooms.sumOf { key, value -> value.messages.size }, + ) + } Log.d( "STATE DUMP", "Deletion Events: " + @@ -128,7 +164,7 @@ fun debugState(context: Context) { Log.d( "STATE DUMP", "Memory used by Events: " + - LocalCache.notes.sumOfLong { _, note -> note.event?.countMemory() ?: 0L } / (1024 * 1024) + + LocalCache.notes.sumOf { _, note -> note.event?.countMemory() ?: 0 } / (1024 * 1024) + " MB", ) @@ -137,10 +173,10 @@ fun debugState(context: Context) { val bytesNotes = LocalCache.notes - .sumByGroup(groupMap = { _, it -> it.event?.kind }, sumOf = { _, it -> it.event?.countMemory() ?: 0L }) + .sumByGroup(groupMap = { _, it -> it.event?.kind }, sumOf = { _, it -> it.event?.countMemory()?.toLong() ?: 0L }) val bytesAddressables = LocalCache.addressables - .sumByGroup(groupMap = { _, it -> it.event?.kind }, sumOf = { _, it -> it.event?.countMemory() ?: 0L }) + .sumByGroup(groupMap = { _, it -> it.event?.kind }, sumOf = { _, it -> it.event?.countMemory()?.toLong() ?: 0L }) qttNotes.toList().sortedByDescending { bytesNotes[it.first] }.forEach { (kind, qtt) -> Log.d("STATE DUMP", "Kind ${kind.toString().padStart(5,' ')}:\t${qtt.toString().padStart(6,' ')} elements\t${bytesNotes[kind]?.div((1024 * 1024))}MB ") diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/service/okhttp/OkHttpWebSocket.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/service/okhttp/OkHttpWebSocket.kt index e139631dd..f570d9b57 100644 --- a/amethyst/src/main/java/com/vitorpamplona/amethyst/service/okhttp/OkHttpWebSocket.kt +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/service/okhttp/OkHttpWebSocket.kt @@ -72,7 +72,7 @@ class OkHttpWebSocket( webSocket: okhttp3.WebSocket, response: Response, ) = out.onOpen( - response.receivedResponseAtMillis - response.sentRequestAtMillis, + (response.receivedResponseAtMillis - response.sentRequestAtMillis).toInt(), response.headers["Sec-WebSocket-Extensions"]?.contains("permessage-deflate") ?: false, ) diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/service/relayClient/speedLogger/FrameStat.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/service/relayClient/speedLogger/FrameStat.kt index 6be8af457..745bc3f80 100644 --- a/amethyst/src/main/java/com/vitorpamplona/amethyst/service/relayClient/speedLogger/FrameStat.kt +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/service/relayClient/speedLogger/FrameStat.kt @@ -35,7 +35,7 @@ class FrameStat { kind: Int, subId: String, relayUrl: NormalizedRelayUrl, - memory: Long, + memory: Int, ) { eventCount.incrementAndGet() diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/service/relayClient/speedLogger/KindGroup.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/service/relayClient/speedLogger/KindGroup.kt index 4fa495f17..b648047f3 100644 --- a/amethyst/src/main/java/com/vitorpamplona/amethyst/service/relayClient/speedLogger/KindGroup.kt +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/service/relayClient/speedLogger/KindGroup.kt @@ -24,22 +24,21 @@ import com.vitorpamplona.quartz.nip01Core.relay.normalizer.NormalizedRelayUrl import com.vitorpamplona.quartz.nip01Core.relay.normalizer.displayUrl import com.vitorpamplona.quartz.utils.LargeCache import java.util.concurrent.atomic.AtomicInteger -import java.util.concurrent.atomic.AtomicLong import kotlin.concurrent.atomics.ExperimentalAtomicApi @OptIn(ExperimentalAtomicApi::class) class KindGroup( var count: AtomicInteger = AtomicInteger(0), - var memory: AtomicLong = AtomicLong(0L), + var memory: AtomicInteger = AtomicInteger(0), val subs: LargeCache = LargeCache(), val relays: LargeCache = LargeCache(), ) { companion object { - const val MB: Long = 1024 + const val MB: Int = 1024 } fun increment( - mem: Long, + mem: Int, subId: String, relayUrl: NormalizedRelayUrl, ) { @@ -63,7 +62,7 @@ class KindGroup( fun reset() { count.set(0) - memory.set(0L) + memory.set(0) subs.forEach { key, value -> value.set(0) } relays.forEach { key, value -> value.set(0) } } diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/note/RelayListRow.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/note/RelayListRow.kt index d39042564..fa72f5264 100644 --- a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/note/RelayListRow.kt +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/note/RelayListRow.kt @@ -195,7 +195,7 @@ fun RenderRelayIcon( iconUrl: String?, loadProfilePicture: Boolean, loadRobohash: Boolean, - pingInMs: Long, + pingInMs: Int, iconModifier: Modifier = MaterialTheme.colorScheme.relayIconModifier, ) { val green = MaterialTheme.colorScheme.allGoodColor diff --git a/quartz/src/androidMain/kotlin/com/vitorpamplona/quartz/experimental/interactiveStories/tags/RootSceneTag.kt b/quartz/src/androidMain/kotlin/com/vitorpamplona/quartz/experimental/interactiveStories/tags/RootSceneTag.kt index ab50b9071..7586b058d 100644 --- a/quartz/src/androidMain/kotlin/com/vitorpamplona/quartz/experimental/interactiveStories/tags/RootSceneTag.kt +++ b/quartz/src/androidMain/kotlin/com/vitorpamplona/quartz/experimental/interactiveStories/tags/RootSceneTag.kt @@ -48,9 +48,9 @@ data class RootSceneTag( this.relay = relayHint } - fun countMemory(): Long = + fun countMemory(): Int = 5 * pointerSizeInBytes + // 7 fields, 4 bytes each reference (32bit) - 8L + // kind + 8 + // kind pubKeyHex.bytesUsedInMemory() + dTag.bytesUsedInMemory() + (relay?.url?.bytesUsedInMemory() ?: 0) diff --git a/quartz/src/androidMain/kotlin/com/vitorpamplona/quartz/nip01Core/core/Event.kt b/quartz/src/androidMain/kotlin/com/vitorpamplona/quartz/nip01Core/core/Event.kt index 09ea417cf..7a6a2f8d6 100644 --- a/quartz/src/androidMain/kotlin/com/vitorpamplona/quartz/nip01Core/core/Event.kt +++ b/quartz/src/androidMain/kotlin/com/vitorpamplona/quartz/nip01Core/core/Event.kt @@ -47,9 +47,9 @@ open class Event( open fun extraIndexableTagNames() = emptySet() - open fun countMemory(): Long = + open fun countMemory(): Int = 7 * pointerSizeInBytes + // 7 fields, 4 bytes each reference (32bit) - 12L + // createdAt + kind + 12 + // createdAt + kind id.bytesUsedInMemory() + pubKey.bytesUsedInMemory() + tags.sumOf { pointerSizeInBytes + it.sumOf { pointerSizeInBytes + it.bytesUsedInMemory() } } + diff --git a/quartz/src/androidMain/kotlin/com/vitorpamplona/quartz/nip01Core/hints/EventHintBundle.kt b/quartz/src/androidMain/kotlin/com/vitorpamplona/quartz/nip01Core/hints/EventHintBundle.kt index 56961814c..61a95097c 100644 --- a/quartz/src/androidMain/kotlin/com/vitorpamplona/quartz/nip01Core/hints/EventHintBundle.kt +++ b/quartz/src/androidMain/kotlin/com/vitorpamplona/quartz/nip01Core/hints/EventHintBundle.kt @@ -45,7 +45,7 @@ data class EventHintBundle( this.authorHomeRelay = authorHomeRelay } - fun countMemory(): Long = + fun countMemory(): Int = 2 * pointerSizeInBytes + // 2 fields, 4 bytes each reference (32bit) event.countMemory() + (relay?.url?.bytesUsedInMemory() ?: 0) diff --git a/quartz/src/androidMain/kotlin/com/vitorpamplona/quartz/nip01Core/relay/client/single/basic/BasicRelayClient.kt b/quartz/src/androidMain/kotlin/com/vitorpamplona/quartz/nip01Core/relay/client/single/basic/BasicRelayClient.kt index 54c1f7e5e..e80d553db 100644 --- a/quartz/src/androidMain/kotlin/com/vitorpamplona/quartz/nip01Core/relay/client/single/basic/BasicRelayClient.kt +++ b/quartz/src/androidMain/kotlin/com/vitorpamplona/quartz/nip01Core/relay/client/single/basic/BasicRelayClient.kt @@ -157,7 +157,7 @@ open class BasicRelayClient( val onConnected: () -> Unit, ) : WebSocketListener { override fun onOpen( - pingMillis: Long, + pingMillis: Int, compression: Boolean, ) { Log.d(logTag, "OnOpen (ping: ${pingMillis}ms${if (compression) ", using compression" else ""})") @@ -260,7 +260,7 @@ open class BasicRelayClient( } fun markConnectionAsReady( - pingInMs: Long, + pingInMs: Int, usingCompression: Boolean, ) { this.resetEOSEStatuses() @@ -459,7 +459,7 @@ open class BasicRelayClient( ) } socket?.let { - // Log.d(logTag, "Sending: $str") + Log.d(logTag, "Sending: $str") val result = it.send(str) listener.onSend(this@BasicRelayClient, str, result) stats.addBytesSent(str.bytesUsedInMemory()) diff --git a/quartz/src/androidMain/kotlin/com/vitorpamplona/quartz/nip01Core/relay/client/stats/RelayStat.kt b/quartz/src/androidMain/kotlin/com/vitorpamplona/quartz/nip01Core/relay/client/stats/RelayStat.kt index fd266e0ff..13be36066 100644 --- a/quartz/src/androidMain/kotlin/com/vitorpamplona/quartz/nip01Core/relay/client/stats/RelayStat.kt +++ b/quartz/src/androidMain/kotlin/com/vitorpamplona/quartz/nip01Core/relay/client/stats/RelayStat.kt @@ -24,11 +24,11 @@ import androidx.collection.LruCache import com.vitorpamplona.quartz.utils.TimeUtils class RelayStat( - var receivedBytes: Long = 0L, - var sentBytes: Long = 0L, - var spamCounter: Long = 0L, - var errorCounter: Long = 0L, - var pingInMs: Long = 0L, + var receivedBytes: Int = 0, + var sentBytes: Int = 0, + var spamCounter: Int = 0, + var errorCounter: Int = 0, + var pingInMs: Int = 0, ) { val messages = LruCache(100) @@ -54,11 +54,11 @@ class RelayStat( messages.put(debugMessage, debugMessage) } - fun addBytesReceived(bytesUsedInMemory: Long) { + fun addBytesReceived(bytesUsedInMemory: Int) { receivedBytes += bytesUsedInMemory } - fun addBytesSent(bytesUsedInMemory: Long) { + fun addBytesSent(bytesUsedInMemory: Int) { sentBytes += bytesUsedInMemory } diff --git a/quartz/src/androidMain/kotlin/com/vitorpamplona/quartz/nip01Core/relay/client/stats/RelayStats.kt b/quartz/src/androidMain/kotlin/com/vitorpamplona/quartz/nip01Core/relay/client/stats/RelayStats.kt index b12e95e2c..a282c9e49 100644 --- a/quartz/src/androidMain/kotlin/com/vitorpamplona/quartz/nip01Core/relay/client/stats/RelayStats.kt +++ b/quartz/src/androidMain/kotlin/com/vitorpamplona/quartz/nip01Core/relay/client/stats/RelayStats.kt @@ -25,7 +25,7 @@ import com.vitorpamplona.quartz.nip01Core.relay.normalizer.NormalizedRelayUrl object RelayStats { private val innerCache = - object : LruCache(1000) { + object : LruCache(100) { override fun create(key: NormalizedRelayUrl?) = RelayStat() } @@ -33,14 +33,14 @@ object RelayStats { fun addBytesReceived( url: NormalizedRelayUrl, - bytesUsedInMemory: Long, + bytesUsedInMemory: Int, ) { get(url).addBytesReceived(bytesUsedInMemory) } fun addBytesSent( url: NormalizedRelayUrl, - bytesUsedInMemory: Long, + bytesUsedInMemory: Int, ) { get(url).addBytesSent(bytesUsedInMemory) } @@ -61,7 +61,7 @@ object RelayStats { fun setPing( url: NormalizedRelayUrl, - pingInMs: Long, + pingInMs: Int, ) { get(url).pingInMs = pingInMs } diff --git a/quartz/src/androidMain/kotlin/com/vitorpamplona/quartz/nip01Core/relay/sockets/WebSocketListener.kt b/quartz/src/androidMain/kotlin/com/vitorpamplona/quartz/nip01Core/relay/sockets/WebSocketListener.kt index acebf8d6b..99a099464 100644 --- a/quartz/src/androidMain/kotlin/com/vitorpamplona/quartz/nip01Core/relay/sockets/WebSocketListener.kt +++ b/quartz/src/androidMain/kotlin/com/vitorpamplona/quartz/nip01Core/relay/sockets/WebSocketListener.kt @@ -22,7 +22,7 @@ package com.vitorpamplona.quartz.nip01Core.relay.sockets interface WebSocketListener { fun onOpen( - pingMillis: Long, + pingMillis: Int, compression: Boolean, ) diff --git a/quartz/src/androidMain/kotlin/com/vitorpamplona/quartz/nip01Core/relay/sockets/okhttp/BasicOkHttpWebSocket.kt b/quartz/src/androidMain/kotlin/com/vitorpamplona/quartz/nip01Core/relay/sockets/okhttp/BasicOkHttpWebSocket.kt index f61060469..12b1f07fa 100644 --- a/quartz/src/androidMain/kotlin/com/vitorpamplona/quartz/nip01Core/relay/sockets/okhttp/BasicOkHttpWebSocket.kt +++ b/quartz/src/androidMain/kotlin/com/vitorpamplona/quartz/nip01Core/relay/sockets/okhttp/BasicOkHttpWebSocket.kt @@ -46,7 +46,7 @@ class BasicOkHttpWebSocket( webSocket: okhttp3.WebSocket, response: Response, ) = out.onOpen( - response.receivedResponseAtMillis - response.sentRequestAtMillis, + (response.receivedResponseAtMillis - response.sentRequestAtMillis).toInt(), response.headers["Sec-WebSocket-Extensions"]?.contains("permessage-deflate") ?: false, ) diff --git a/quartz/src/androidMain/kotlin/com/vitorpamplona/quartz/nip01Core/tags/addressables/ATag.kt b/quartz/src/androidMain/kotlin/com/vitorpamplona/quartz/nip01Core/tags/addressables/ATag.kt index 74a0ac49b..f4ad7a243 100644 --- a/quartz/src/androidMain/kotlin/com/vitorpamplona/quartz/nip01Core/tags/addressables/ATag.kt +++ b/quartz/src/androidMain/kotlin/com/vitorpamplona/quartz/nip01Core/tags/addressables/ATag.kt @@ -40,9 +40,9 @@ data class ATag( ) { constructor(address: Address, relayHint: NormalizedRelayUrl? = null) : this(address.kind, address.pubKeyHex, address.dTag, relayHint) - fun countMemory(): Long = + fun countMemory(): Int = 5 * pointerSizeInBytes + // 7 fields, 4 bytes each reference (32bit) - 8L + // kind + 8 + // kind pubKeyHex.bytesUsedInMemory() + dTag.bytesUsedInMemory() + (relay?.url?.bytesUsedInMemory() ?: 0) diff --git a/quartz/src/androidMain/kotlin/com/vitorpamplona/quartz/nip01Core/tags/addressables/Address.kt b/quartz/src/androidMain/kotlin/com/vitorpamplona/quartz/nip01Core/tags/addressables/Address.kt index b055aab09..0f24ffd4f 100644 --- a/quartz/src/androidMain/kotlin/com/vitorpamplona/quartz/nip01Core/tags/addressables/Address.kt +++ b/quartz/src/androidMain/kotlin/com/vitorpamplona/quartz/nip01Core/tags/addressables/Address.kt @@ -39,9 +39,9 @@ data class Address( Parcelable { fun toValue() = assemble(kind, pubKeyHex, dTag) - fun countMemory(): Long = + fun countMemory(): Int = 3 * pointerSizeInBytes + - 8L + // kind + 8 + // kind pubKeyHex.bytesUsedInMemory() + dTag.bytesUsedInMemory() diff --git a/quartz/src/androidMain/kotlin/com/vitorpamplona/quartz/nip01Core/tags/dTags/DTag.kt b/quartz/src/androidMain/kotlin/com/vitorpamplona/quartz/nip01Core/tags/dTags/DTag.kt index e439d00d7..b92452f38 100644 --- a/quartz/src/androidMain/kotlin/com/vitorpamplona/quartz/nip01Core/tags/dTags/DTag.kt +++ b/quartz/src/androidMain/kotlin/com/vitorpamplona/quartz/nip01Core/tags/dTags/DTag.kt @@ -26,7 +26,7 @@ import com.vitorpamplona.quartz.utils.pointerSizeInBytes class DTag( val dId: String, ) { - fun countMemory(): Long = 1 * pointerSizeInBytes + dId.bytesUsedInMemory() + fun countMemory(): Int = 1 * pointerSizeInBytes + dId.bytesUsedInMemory() fun toTagArray() = assemble(dId) diff --git a/quartz/src/androidMain/kotlin/com/vitorpamplona/quartz/nip01Core/tags/events/ETag.kt b/quartz/src/androidMain/kotlin/com/vitorpamplona/quartz/nip01Core/tags/events/ETag.kt index 1a793e857..fe4672f1f 100644 --- a/quartz/src/androidMain/kotlin/com/vitorpamplona/quartz/nip01Core/tags/events/ETag.kt +++ b/quartz/src/androidMain/kotlin/com/vitorpamplona/quartz/nip01Core/tags/events/ETag.kt @@ -44,7 +44,7 @@ data class ETag( this.author = authorPubKeyHex } - fun countMemory(): Long = + fun countMemory(): Int = 3 * pointerSizeInBytes + // 3 fields, 4 bytes each reference (32bit) eventId.bytesUsedInMemory() + (relay?.url?.bytesUsedInMemory() ?: 0) + diff --git a/quartz/src/androidMain/kotlin/com/vitorpamplona/quartz/nip01Core/tags/people/PTag.kt b/quartz/src/androidMain/kotlin/com/vitorpamplona/quartz/nip01Core/tags/people/PTag.kt index 1b712af7a..92e0f48ed 100644 --- a/quartz/src/androidMain/kotlin/com/vitorpamplona/quartz/nip01Core/tags/people/PTag.kt +++ b/quartz/src/androidMain/kotlin/com/vitorpamplona/quartz/nip01Core/tags/people/PTag.kt @@ -40,7 +40,7 @@ data class PTag( override val pubKey: HexKey, override val relayHint: NormalizedRelayUrl? = null, ) : PubKeyReferenceTag { - fun countMemory(): Long = + fun countMemory(): Int = 2 * pointerSizeInBytes + // 2 fields, 4 bytes each reference (32bit) pubKey.bytesUsedInMemory() + (relayHint?.url?.bytesUsedInMemory() ?: 0) diff --git a/quartz/src/androidMain/kotlin/com/vitorpamplona/quartz/nip02FollowList/tags/ContactTag.kt b/quartz/src/androidMain/kotlin/com/vitorpamplona/quartz/nip02FollowList/tags/ContactTag.kt index b67f628b7..548b020a3 100644 --- a/quartz/src/androidMain/kotlin/com/vitorpamplona/quartz/nip02FollowList/tags/ContactTag.kt +++ b/quartz/src/androidMain/kotlin/com/vitorpamplona/quartz/nip02FollowList/tags/ContactTag.kt @@ -50,7 +50,7 @@ data class ContactTag( this.petname = petname } - fun countMemory(): Long = + fun countMemory(): Int = 3 * pointerSizeInBytes + pubKey.bytesUsedInMemory() + (relayUri?.url?.bytesUsedInMemory() ?: 0) + diff --git a/quartz/src/androidMain/kotlin/com/vitorpamplona/quartz/nip13Pow/tags/PoWTag.kt b/quartz/src/androidMain/kotlin/com/vitorpamplona/quartz/nip13Pow/tags/PoWTag.kt index e19494610..f8f241b5f 100644 --- a/quartz/src/androidMain/kotlin/com/vitorpamplona/quartz/nip13Pow/tags/PoWTag.kt +++ b/quartz/src/androidMain/kotlin/com/vitorpamplona/quartz/nip13Pow/tags/PoWTag.kt @@ -30,7 +30,7 @@ class PoWTag( val nonce: String, val commitment: Int?, ) { - fun countMemory(): Long = 2 * pointerSizeInBytes + nonce.bytesUsedInMemory() + (commitment?.bytesUsedInMemory() ?: 0) + fun countMemory(): Int = 2 * pointerSizeInBytes + nonce.bytesUsedInMemory() + (commitment?.bytesUsedInMemory() ?: 0) fun toTagArray() = assemble(nonce, commitment) diff --git a/quartz/src/androidMain/kotlin/com/vitorpamplona/quartz/nip18Reposts/quotes/QAddressableTag.kt b/quartz/src/androidMain/kotlin/com/vitorpamplona/quartz/nip18Reposts/quotes/QAddressableTag.kt index b25b6ebfa..db1b5eb14 100644 --- a/quartz/src/androidMain/kotlin/com/vitorpamplona/quartz/nip18Reposts/quotes/QAddressableTag.kt +++ b/quartz/src/androidMain/kotlin/com/vitorpamplona/quartz/nip18Reposts/quotes/QAddressableTag.kt @@ -53,7 +53,7 @@ data class QAddressableTag( this.relay = relayHint } - fun countMemory(): Long = + fun countMemory(): Int = 2 * pointerSizeInBytes + address.countMemory() + (relay?.url?.bytesUsedInMemory() ?: 0) diff --git a/quartz/src/androidMain/kotlin/com/vitorpamplona/quartz/nip18Reposts/quotes/QEventTag.kt b/quartz/src/androidMain/kotlin/com/vitorpamplona/quartz/nip18Reposts/quotes/QEventTag.kt index 662c6f9b9..aa8344b74 100644 --- a/quartz/src/androidMain/kotlin/com/vitorpamplona/quartz/nip18Reposts/quotes/QEventTag.kt +++ b/quartz/src/androidMain/kotlin/com/vitorpamplona/quartz/nip18Reposts/quotes/QEventTag.kt @@ -42,7 +42,7 @@ data class QEventTag( this.author = authorPubKeyHex } - fun countMemory(): Long = + fun countMemory(): Int = 3 * pointerSizeInBytes + // 3 fields, 4 bytes each reference (32bit) eventId.bytesUsedInMemory() + (relay?.url?.bytesUsedInMemory() ?: 0) + diff --git a/quartz/src/androidMain/kotlin/com/vitorpamplona/quartz/nip28PublicChat/list/tags/ChannelTag.kt b/quartz/src/androidMain/kotlin/com/vitorpamplona/quartz/nip28PublicChat/list/tags/ChannelTag.kt index 88aafdff8..122a9d7b0 100644 --- a/quartz/src/androidMain/kotlin/com/vitorpamplona/quartz/nip28PublicChat/list/tags/ChannelTag.kt +++ b/quartz/src/androidMain/kotlin/com/vitorpamplona/quartz/nip28PublicChat/list/tags/ChannelTag.kt @@ -38,7 +38,7 @@ class ChannelTag( val relay: NormalizedRelayUrl? = null, val author: HexKey? = null, ) { - fun countMemory(): Long = + fun countMemory(): Int = 3 * pointerSizeInBytes + // 3 fields, 4 bytes each reference (32bit) eventId.bytesUsedInMemory() + (relay?.url?.bytesUsedInMemory() ?: 0) + diff --git a/quartz/src/androidMain/kotlin/com/vitorpamplona/quartz/nip46RemoteSigner/BunkerMessage.kt b/quartz/src/androidMain/kotlin/com/vitorpamplona/quartz/nip46RemoteSigner/BunkerMessage.kt index bb2823521..fe7007f79 100644 --- a/quartz/src/androidMain/kotlin/com/vitorpamplona/quartz/nip46RemoteSigner/BunkerMessage.kt +++ b/quartz/src/androidMain/kotlin/com/vitorpamplona/quartz/nip46RemoteSigner/BunkerMessage.kt @@ -26,7 +26,7 @@ import com.fasterxml.jackson.databind.JsonNode import com.fasterxml.jackson.databind.deser.std.StdDeserializer abstract class BunkerMessage { - abstract fun countMemory(): Long + abstract fun countMemory(): Int class BunkerMessageDeserializer : StdDeserializer(BunkerMessage::class.java) { override fun deserialize( diff --git a/quartz/src/androidMain/kotlin/com/vitorpamplona/quartz/nip46RemoteSigner/BunkerRequest.kt b/quartz/src/androidMain/kotlin/com/vitorpamplona/quartz/nip46RemoteSigner/BunkerRequest.kt index 7e84eb24d..8b5a4bbe6 100644 --- a/quartz/src/androidMain/kotlin/com/vitorpamplona/quartz/nip46RemoteSigner/BunkerRequest.kt +++ b/quartz/src/androidMain/kotlin/com/vitorpamplona/quartz/nip46RemoteSigner/BunkerRequest.kt @@ -36,7 +36,7 @@ open class BunkerRequest( val method: String, val params: Array = emptyArray(), ) : BunkerMessage() { - override fun countMemory(): Long = + override fun countMemory(): Int = 3 * pointerSizeInBytes + // 3 fields, 4 bytes each reference (32bit) id.bytesUsedInMemory() + method.bytesUsedInMemory() + diff --git a/quartz/src/androidMain/kotlin/com/vitorpamplona/quartz/nip46RemoteSigner/BunkerResponse.kt b/quartz/src/androidMain/kotlin/com/vitorpamplona/quartz/nip46RemoteSigner/BunkerResponse.kt index 6d04c807b..d6272a6aa 100644 --- a/quartz/src/androidMain/kotlin/com/vitorpamplona/quartz/nip46RemoteSigner/BunkerResponse.kt +++ b/quartz/src/androidMain/kotlin/com/vitorpamplona/quartz/nip46RemoteSigner/BunkerResponse.kt @@ -36,7 +36,7 @@ open class BunkerResponse( val result: String?, val error: String?, ) : BunkerMessage() { - override fun countMemory(): Long = + override fun countMemory(): Int = 3 * pointerSizeInBytes + // 3 fields, 4 bytes each reference (32bit) id.bytesUsedInMemory() + (result?.bytesUsedInMemory() ?: 0) + diff --git a/quartz/src/androidMain/kotlin/com/vitorpamplona/quartz/nip47WalletConnect/Response.kt b/quartz/src/androidMain/kotlin/com/vitorpamplona/quartz/nip47WalletConnect/Response.kt index b653d897b..f1a148f24 100644 --- a/quartz/src/androidMain/kotlin/com/vitorpamplona/quartz/nip47WalletConnect/Response.kt +++ b/quartz/src/androidMain/kotlin/com/vitorpamplona/quartz/nip47WalletConnect/Response.kt @@ -28,7 +28,7 @@ import com.vitorpamplona.quartz.utils.pointerSizeInBytes abstract class Response( @field:JsonProperty("result_type") val resultType: String, ) { - abstract fun countMemory(): Long + abstract fun countMemory(): Int } // PayInvoice Call @@ -39,10 +39,10 @@ class PayInvoiceSuccessResponse( class PayInvoiceResultParams( val preimage: String? = null, ) { - fun countMemory(): Long = pointerSizeInBytes + (preimage?.bytesUsedInMemory() ?: 0) + fun countMemory(): Int = pointerSizeInBytes + (preimage?.bytesUsedInMemory() ?: 0) } - override fun countMemory(): Long = pointerSizeInBytes + (result?.countMemory() ?: 0) + override fun countMemory(): Int = pointerSizeInBytes + (result?.countMemory() ?: 0) } class PayInvoiceErrorResponse( @@ -52,10 +52,10 @@ class PayInvoiceErrorResponse( val code: ErrorType? = null, val message: String? = null, ) { - fun countMemory(): Long = pointerSizeInBytes + pointerSizeInBytes + (message?.bytesUsedInMemory() ?: 0) + fun countMemory(): Int = pointerSizeInBytes + pointerSizeInBytes + (message?.bytesUsedInMemory() ?: 0) } - override fun countMemory(): Long = pointerSizeInBytes + (error?.countMemory() ?: 0) + override fun countMemory(): Int = pointerSizeInBytes + (error?.countMemory() ?: 0) enum class ErrorType { @JsonProperty(value = "RATE_LIMITED") diff --git a/quartz/src/androidMain/kotlin/com/vitorpamplona/quartz/nip48ProxyTags/ProxyTag.kt b/quartz/src/androidMain/kotlin/com/vitorpamplona/quartz/nip48ProxyTags/ProxyTag.kt index 44496d1ea..e2bb5591f 100644 --- a/quartz/src/androidMain/kotlin/com/vitorpamplona/quartz/nip48ProxyTags/ProxyTag.kt +++ b/quartz/src/androidMain/kotlin/com/vitorpamplona/quartz/nip48ProxyTags/ProxyTag.kt @@ -31,7 +31,7 @@ data class ProxyTag( val id: String, val protocol: String, ) { - fun countMemory(): Long = 2 * pointerSizeInBytes + id.bytesUsedInMemory() + protocol.bytesUsedInMemory() + fun countMemory(): Int = 2 * pointerSizeInBytes + id.bytesUsedInMemory() + protocol.bytesUsedInMemory() fun toTagArray() = assemble(id, protocol) diff --git a/quartz/src/androidMain/kotlin/com/vitorpamplona/quartz/nip51Lists/bookmarkList/tags/AddressBookmark.kt b/quartz/src/androidMain/kotlin/com/vitorpamplona/quartz/nip51Lists/bookmarkList/tags/AddressBookmark.kt index ede0ece45..02e0fbd10 100644 --- a/quartz/src/androidMain/kotlin/com/vitorpamplona/quartz/nip51Lists/bookmarkList/tags/AddressBookmark.kt +++ b/quartz/src/androidMain/kotlin/com/vitorpamplona/quartz/nip51Lists/bookmarkList/tags/AddressBookmark.kt @@ -35,7 +35,7 @@ class AddressBookmark( val address: Address, val relayHint: NormalizedRelayUrl? = null, ) : BookmarkIdTag { - fun countMemory(): Long = 2 * pointerSizeInBytes + address.countMemory() + (relayHint?.url?.bytesUsedInMemory() ?: 0) + fun countMemory(): Int = 2 * pointerSizeInBytes + address.countMemory() + (relayHint?.url?.bytesUsedInMemory() ?: 0) fun toTag() = Address.assemble(address.kind, address.pubKeyHex, address.dTag) diff --git a/quartz/src/androidMain/kotlin/com/vitorpamplona/quartz/nip51Lists/bookmarkList/tags/EventBookmark.kt b/quartz/src/androidMain/kotlin/com/vitorpamplona/quartz/nip51Lists/bookmarkList/tags/EventBookmark.kt index 8bfeb5a59..82de81214 100644 --- a/quartz/src/androidMain/kotlin/com/vitorpamplona/quartz/nip51Lists/bookmarkList/tags/EventBookmark.kt +++ b/quartz/src/androidMain/kotlin/com/vitorpamplona/quartz/nip51Lists/bookmarkList/tags/EventBookmark.kt @@ -38,7 +38,7 @@ class EventBookmark( val relay: NormalizedRelayUrl? = null, val author: HexKey? = null, ) : BookmarkIdTag { - fun countMemory(): Long = + fun countMemory(): Int = 3 * pointerSizeInBytes + // 3 fields, 4 bytes each reference (32bit) eventId.bytesUsedInMemory() + (relay?.url?.bytesUsedInMemory() ?: 0) + diff --git a/quartz/src/androidMain/kotlin/com/vitorpamplona/quartz/nip51Lists/muteList/tags/UserTag.kt b/quartz/src/androidMain/kotlin/com/vitorpamplona/quartz/nip51Lists/muteList/tags/UserTag.kt index 2558e6d54..69fd21af1 100644 --- a/quartz/src/androidMain/kotlin/com/vitorpamplona/quartz/nip51Lists/muteList/tags/UserTag.kt +++ b/quartz/src/androidMain/kotlin/com/vitorpamplona/quartz/nip51Lists/muteList/tags/UserTag.kt @@ -38,7 +38,7 @@ class UserTag( val pubKey: HexKey, val relayHint: NormalizedRelayUrl? = null, ) : MuteTag { - fun countMemory(): Long = + fun countMemory(): Int = 2 * pointerSizeInBytes + // 2 fields, 4 bytes each reference (32bit) pubKey.bytesUsedInMemory() + (relayHint?.url?.bytesUsedInMemory() ?: 0) diff --git a/quartz/src/androidMain/kotlin/com/vitorpamplona/quartz/nip51Lists/muteList/tags/WordTag.kt b/quartz/src/androidMain/kotlin/com/vitorpamplona/quartz/nip51Lists/muteList/tags/WordTag.kt index 1204b5d42..9a1010c89 100644 --- a/quartz/src/androidMain/kotlin/com/vitorpamplona/quartz/nip51Lists/muteList/tags/WordTag.kt +++ b/quartz/src/androidMain/kotlin/com/vitorpamplona/quartz/nip51Lists/muteList/tags/WordTag.kt @@ -31,7 +31,7 @@ import com.vitorpamplona.quartz.utils.pointerSizeInBytes class WordTag( val word: String, ) : MuteTag { - fun countMemory(): Long = + fun countMemory(): Int = 1 * pointerSizeInBytes + // 1 fields, 4 bytes each reference (32bit) word.bytesUsedInMemory() diff --git a/quartz/src/androidMain/kotlin/com/vitorpamplona/quartz/nip53LiveActivities/meetingSpaces/tags/MeetingSpaceTag.kt b/quartz/src/androidMain/kotlin/com/vitorpamplona/quartz/nip53LiveActivities/meetingSpaces/tags/MeetingSpaceTag.kt index e7237ca0c..cfe346311 100644 --- a/quartz/src/androidMain/kotlin/com/vitorpamplona/quartz/nip53LiveActivities/meetingSpaces/tags/MeetingSpaceTag.kt +++ b/quartz/src/androidMain/kotlin/com/vitorpamplona/quartz/nip53LiveActivities/meetingSpaces/tags/MeetingSpaceTag.kt @@ -35,7 +35,7 @@ class MeetingSpaceTag( val address: Address, val relayHint: NormalizedRelayUrl? = null, ) { - fun countMemory(): Long = 2 * pointerSizeInBytes + address.countMemory() + (relayHint?.url?.bytesUsedInMemory() ?: 0) + fun countMemory(): Int = 2 * pointerSizeInBytes + address.countMemory() + (relayHint?.url?.bytesUsedInMemory() ?: 0) fun toTag() = Address.assemble(address.kind, address.pubKeyHex, address.dTag) diff --git a/quartz/src/androidMain/kotlin/com/vitorpamplona/quartz/nip53LiveActivities/presence/tags/MeetingRoomTag.kt b/quartz/src/androidMain/kotlin/com/vitorpamplona/quartz/nip53LiveActivities/presence/tags/MeetingRoomTag.kt index c59d1d714..c5c9062c7 100644 --- a/quartz/src/androidMain/kotlin/com/vitorpamplona/quartz/nip53LiveActivities/presence/tags/MeetingRoomTag.kt +++ b/quartz/src/androidMain/kotlin/com/vitorpamplona/quartz/nip53LiveActivities/presence/tags/MeetingRoomTag.kt @@ -35,7 +35,7 @@ class MeetingRoomTag( val address: Address, val relayHint: NormalizedRelayUrl? = null, ) { - fun countMemory(): Long = 2 * pointerSizeInBytes + address.countMemory() + (relayHint?.url?.bytesUsedInMemory() ?: 0) + fun countMemory(): Int = 2 * pointerSizeInBytes + address.countMemory() + (relayHint?.url?.bytesUsedInMemory() ?: 0) fun toTag() = Address.assemble(address.kind, address.pubKeyHex, address.dTag) diff --git a/quartz/src/androidMain/kotlin/com/vitorpamplona/quartz/nip57Zaps/LnZapEvent.kt b/quartz/src/androidMain/kotlin/com/vitorpamplona/quartz/nip57Zaps/LnZapEvent.kt index 5393d2b4d..bd7299f9f 100644 --- a/quartz/src/androidMain/kotlin/com/vitorpamplona/quartz/nip57Zaps/LnZapEvent.kt +++ b/quartz/src/androidMain/kotlin/com/vitorpamplona/quartz/nip57Zaps/LnZapEvent.kt @@ -72,7 +72,7 @@ class LnZapEvent( } } - override fun countMemory(): Long = + override fun countMemory(): Int = super.countMemory() + pointerSizeInBytes + (zapRequest?.countMemory() ?: 0) + // rough calculation pointerSizeInBytes + 36 // bigdecimal size diff --git a/quartz/src/androidMain/kotlin/com/vitorpamplona/quartz/nip57Zaps/zapraiser/ZapRaiserTag.kt b/quartz/src/androidMain/kotlin/com/vitorpamplona/quartz/nip57Zaps/zapraiser/ZapRaiserTag.kt index fdc831fb6..a86d4215c 100644 --- a/quartz/src/androidMain/kotlin/com/vitorpamplona/quartz/nip57Zaps/zapraiser/ZapRaiserTag.kt +++ b/quartz/src/androidMain/kotlin/com/vitorpamplona/quartz/nip57Zaps/zapraiser/ZapRaiserTag.kt @@ -26,7 +26,7 @@ import com.vitorpamplona.quartz.utils.pointerSizeInBytes class ZapRaiserTag( val amountInSats: Long, ) { - fun countMemory(): Long = 1 * pointerSizeInBytes + amountInSats.bytesUsedInMemory() + fun countMemory(): Int = 1 * pointerSizeInBytes + amountInSats.bytesUsedInMemory() fun toTagArray() = assemble(amountInSats) diff --git a/quartz/src/androidMain/kotlin/com/vitorpamplona/quartz/nip59Giftwrap/seals/SealedRumorEvent.kt b/quartz/src/androidMain/kotlin/com/vitorpamplona/quartz/nip59Giftwrap/seals/SealedRumorEvent.kt index a2def5540..210cc13f9 100644 --- a/quartz/src/androidMain/kotlin/com/vitorpamplona/quartz/nip59Giftwrap/seals/SealedRumorEvent.kt +++ b/quartz/src/androidMain/kotlin/com/vitorpamplona/quartz/nip59Giftwrap/seals/SealedRumorEvent.kt @@ -44,7 +44,7 @@ class SealedRumorEvent( ) : WrappedEvent(id, pubKey, createdAt, KIND, tags, content, sig) { @Transient var innerEventId: HexKey? = null - override fun countMemory(): Long = + override fun countMemory(): Int = super.countMemory() + pointerSizeInBytes + (innerEventId?.bytesUsedInMemory() ?: 0) diff --git a/quartz/src/androidMain/kotlin/com/vitorpamplona/quartz/nip59Giftwrap/wraps/GiftWrapEvent.kt b/quartz/src/androidMain/kotlin/com/vitorpamplona/quartz/nip59Giftwrap/wraps/GiftWrapEvent.kt index 01a72f03d..8f2e65cf3 100644 --- a/quartz/src/androidMain/kotlin/com/vitorpamplona/quartz/nip59Giftwrap/wraps/GiftWrapEvent.kt +++ b/quartz/src/androidMain/kotlin/com/vitorpamplona/quartz/nip59Giftwrap/wraps/GiftWrapEvent.kt @@ -48,7 +48,7 @@ class GiftWrapEvent( ) : Event(id, pubKey, createdAt, KIND, tags, content, sig) { @Transient var innerEventId: HexKey? = null - override fun countMemory(): Long = + override fun countMemory(): Int = super.countMemory() + pointerSizeInBytes + (innerEventId?.bytesUsedInMemory() ?: 0) diff --git a/quartz/src/androidMain/kotlin/com/vitorpamplona/quartz/nip71Video/tags/TextTrackTag.kt b/quartz/src/androidMain/kotlin/com/vitorpamplona/quartz/nip71Video/tags/TextTrackTag.kt index 671c77ca1..d1cd9cf23 100644 --- a/quartz/src/androidMain/kotlin/com/vitorpamplona/quartz/nip71Video/tags/TextTrackTag.kt +++ b/quartz/src/androidMain/kotlin/com/vitorpamplona/quartz/nip71Video/tags/TextTrackTag.kt @@ -31,7 +31,7 @@ data class TextTrackTag( val eventId: HexKey, var relay: String? = null, ) { - fun countMemory(): Long = + fun countMemory(): Int = 2 * pointerSizeInBytes + // 3 fields, 4 bytes each reference (32bit) eventId.bytesUsedInMemory() + (relay?.bytesUsedInMemory() ?: 0) diff --git a/quartz/src/androidMain/kotlin/com/vitorpamplona/quartz/nip72ModCommunities/approval/tags/ApprovedAddressTag.kt b/quartz/src/androidMain/kotlin/com/vitorpamplona/quartz/nip72ModCommunities/approval/tags/ApprovedAddressTag.kt index 727993df6..1e2a4582f 100644 --- a/quartz/src/androidMain/kotlin/com/vitorpamplona/quartz/nip72ModCommunities/approval/tags/ApprovedAddressTag.kt +++ b/quartz/src/androidMain/kotlin/com/vitorpamplona/quartz/nip72ModCommunities/approval/tags/ApprovedAddressTag.kt @@ -36,7 +36,7 @@ class ApprovedAddressTag( val address: Address, val relayHint: NormalizedRelayUrl? = null, ) { - fun countMemory(): Long = 2 * pointerSizeInBytes + address.countMemory() + (relayHint?.url?.bytesUsedInMemory() ?: 0) + fun countMemory(): Int = 2 * pointerSizeInBytes + address.countMemory() + (relayHint?.url?.bytesUsedInMemory() ?: 0) fun toTag() = Address.assemble(address.kind, address.pubKeyHex, address.dTag) diff --git a/quartz/src/androidMain/kotlin/com/vitorpamplona/quartz/nip72ModCommunities/follow/tags/CommunityTag.kt b/quartz/src/androidMain/kotlin/com/vitorpamplona/quartz/nip72ModCommunities/follow/tags/CommunityTag.kt index 153482351..262d201e2 100644 --- a/quartz/src/androidMain/kotlin/com/vitorpamplona/quartz/nip72ModCommunities/follow/tags/CommunityTag.kt +++ b/quartz/src/androidMain/kotlin/com/vitorpamplona/quartz/nip72ModCommunities/follow/tags/CommunityTag.kt @@ -36,7 +36,7 @@ class CommunityTag( val address: Address, val relayHint: NormalizedRelayUrl? = null, ) { - fun countMemory(): Long = 2 * pointerSizeInBytes + address.countMemory() + (relayHint?.url?.bytesUsedInMemory() ?: 0) + fun countMemory(): Int = 2 * pointerSizeInBytes + address.countMemory() + (relayHint?.url?.bytesUsedInMemory() ?: 0) fun toTag() = Address.assemble(address.kind, address.pubKeyHex, address.dTag) diff --git a/quartz/src/androidMain/kotlin/com/vitorpamplona/quartz/nip89AppHandlers/definition/AppDefinitionEvent.kt b/quartz/src/androidMain/kotlin/com/vitorpamplona/quartz/nip89AppHandlers/definition/AppDefinitionEvent.kt index 76db75a2a..b6bee3c05 100644 --- a/quartz/src/androidMain/kotlin/com/vitorpamplona/quartz/nip89AppHandlers/definition/AppDefinitionEvent.kt +++ b/quartz/src/androidMain/kotlin/com/vitorpamplona/quartz/nip89AppHandlers/definition/AppDefinitionEvent.kt @@ -49,7 +49,7 @@ class AppDefinitionEvent( sig: HexKey, ) : BaseAddressableEvent(id, pubKey, createdAt, KIND, tags, content, sig), PublishedAtProvider { - override fun countMemory(): Long = super.countMemory() + (cachedMetadata?.countMemory() ?: 8L) + override fun countMemory(): Int = super.countMemory() + (cachedMetadata?.countMemory() ?: 8) @Transient private var cachedMetadata: AppMetadata? = null diff --git a/quartz/src/androidMain/kotlin/com/vitorpamplona/quartz/nip89AppHandlers/definition/AppMetadata.kt b/quartz/src/androidMain/kotlin/com/vitorpamplona/quartz/nip89AppHandlers/definition/AppMetadata.kt index 9ee0072ce..f47606412 100644 --- a/quartz/src/androidMain/kotlin/com/vitorpamplona/quartz/nip89AppHandlers/definition/AppMetadata.kt +++ b/quartz/src/androidMain/kotlin/com/vitorpamplona/quartz/nip89AppHandlers/definition/AppMetadata.kt @@ -50,25 +50,25 @@ class AppMetadata { var lud06: String? = null var lud16: String? = null - fun countMemory(): Long = + fun countMemory(): Int = 20 * pointerSizeInBytes + // 20 fields, 4 bytes for each reference - (name?.bytesUsedInMemory() ?: 0L) + - (username?.bytesUsedInMemory() ?: 0L) + - (displayName?.bytesUsedInMemory() ?: 0L) + - (picture?.bytesUsedInMemory() ?: 0L) + - (banner?.bytesUsedInMemory() ?: 0L) + - (image?.bytesUsedInMemory() ?: 0L) + - (website?.bytesUsedInMemory() ?: 0L) + - (about?.bytesUsedInMemory() ?: 0L) + - (subscription?.bytesUsedInMemory() ?: 0L) + - (acceptsNutZaps?.bytesUsedInMemory() ?: 0L) + - (supportsEncryption?.bytesUsedInMemory() ?: 0L) + - (personalized?.bytesUsedInMemory() ?: 0L) + // A Boolean has 8 bytes of header, plus 1 byte of payload, for a total of 9 bytes of information. The JVM then rounds it up to the next multiple of 8. so the one instance of java.lang.Boolean takes up 16 bytes of memory. - (amount?.bytesUsedInMemory() ?: 0L) + - (nip05?.bytesUsedInMemory() ?: 0L) + - (domain?.bytesUsedInMemory() ?: 0L) + - (lud06?.bytesUsedInMemory() ?: 0L) + - (lud16?.bytesUsedInMemory() ?: 0L) + (name?.bytesUsedInMemory() ?: 0) + + (username?.bytesUsedInMemory() ?: 0) + + (displayName?.bytesUsedInMemory() ?: 0) + + (picture?.bytesUsedInMemory() ?: 0) + + (banner?.bytesUsedInMemory() ?: 0) + + (image?.bytesUsedInMemory() ?: 0) + + (website?.bytesUsedInMemory() ?: 0) + + (about?.bytesUsedInMemory() ?: 0) + + (subscription?.bytesUsedInMemory() ?: 0) + + (acceptsNutZaps?.bytesUsedInMemory() ?: 0) + + (supportsEncryption?.bytesUsedInMemory() ?: 0) + + (personalized?.bytesUsedInMemory() ?: 0) + // A Boolean has 8 bytes of header, plus 1 byte of payload, for a total of 9 bytes of information. The JVM then rounds it up to the next multiple of 8. so the one instance of java.lang.Boolean takes up 16 bytes of memory. + (amount?.bytesUsedInMemory() ?: 0) + + (nip05?.bytesUsedInMemory() ?: 0) + + (domain?.bytesUsedInMemory() ?: 0) + + (lud06?.bytesUsedInMemory() ?: 0) + + (lud16?.bytesUsedInMemory() ?: 0) fun anyName(): String? = displayName ?: name ?: username diff --git a/quartz/src/androidMain/kotlin/com/vitorpamplona/quartz/nip90Dvms/NIP90ContentDiscoveryResponseEvent.kt b/quartz/src/androidMain/kotlin/com/vitorpamplona/quartz/nip90Dvms/NIP90ContentDiscoveryResponseEvent.kt index d7ca0d2ac..ea94b7466 100644 --- a/quartz/src/androidMain/kotlin/com/vitorpamplona/quartz/nip90Dvms/NIP90ContentDiscoveryResponseEvent.kt +++ b/quartz/src/androidMain/kotlin/com/vitorpamplona/quartz/nip90Dvms/NIP90ContentDiscoveryResponseEvent.kt @@ -43,7 +43,7 @@ class NIP90ContentDiscoveryResponseEvent( ) : Event(id, pubKey, createdAt, KIND, tags, content, sig) { @Transient var events: List? = null - override fun countMemory(): Long = + override fun countMemory(): Int = super.countMemory() + pointerSizeInBytes + (events?.sumOf { it.bytesUsedInMemory() } ?: 0) diff --git a/quartz/src/androidMain/kotlin/com/vitorpamplona/quartz/utils/StringUtils.kt b/quartz/src/androidMain/kotlin/com/vitorpamplona/quartz/utils/StringUtils.kt index 557ca3fdb..c88a46dfd 100644 --- a/quartz/src/androidMain/kotlin/com/vitorpamplona/quartz/utils/StringUtils.kt +++ b/quartz/src/androidMain/kotlin/com/vitorpamplona/quartz/utils/StringUtils.kt @@ -24,13 +24,13 @@ import kotlin.math.min val pointerSizeInBytes = 4 -fun String.bytesUsedInMemory(): Long = (8 * (((this.length * 2L) + 45) / 8)) +fun String.bytesUsedInMemory(): Int = (8 * (((this.length * 2) + 45) / 8)) -fun Long.bytesUsedInMemory(): Long = 8 +fun Long.bytesUsedInMemory(): Int = 8 -fun Int.bytesUsedInMemory(): Long = 4 +fun Int.bytesUsedInMemory(): Int = 4 -fun Boolean.bytesUsedInMemory(): Long = 8 +fun Boolean.bytesUsedInMemory(): Int = 8 fun String.containsIgnoreCase(term: String): Boolean { if (term.isEmpty()) return true // Empty string is contained