Migrates memory counters from Long to Int

This commit is contained in:
Vitor Pamplona
2025-09-12 11:11:16 -04:00
parent 3628091511
commit 0886af4386
46 changed files with 125 additions and 91 deletions
@@ -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)
@@ -47,9 +47,9 @@ open class Event(
open fun extraIndexableTagNames() = emptySet<String>()
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() } } +
@@ -45,7 +45,7 @@ data class EventHintBundle<T : Event>(
this.authorHomeRelay = authorHomeRelay
}
fun countMemory(): Long =
fun countMemory(): Int =
2 * pointerSizeInBytes + // 2 fields, 4 bytes each reference (32bit)
event.countMemory() +
(relay?.url?.bytesUsedInMemory() ?: 0)
@@ -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())
@@ -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<RelayDebugMessage, RelayDebugMessage>(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
}
@@ -25,7 +25,7 @@ import com.vitorpamplona.quartz.nip01Core.relay.normalizer.NormalizedRelayUrl
object RelayStats {
private val innerCache =
object : LruCache<NormalizedRelayUrl, RelayStat>(1000) {
object : LruCache<NormalizedRelayUrl, RelayStat>(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
}
@@ -22,7 +22,7 @@ package com.vitorpamplona.quartz.nip01Core.relay.sockets
interface WebSocketListener {
fun onOpen(
pingMillis: Long,
pingMillis: Int,
compression: Boolean,
)
@@ -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,
)
@@ -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)
@@ -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()
@@ -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)
@@ -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) +
@@ -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)
@@ -50,7 +50,7 @@ data class ContactTag(
this.petname = petname
}
fun countMemory(): Long =
fun countMemory(): Int =
3 * pointerSizeInBytes +
pubKey.bytesUsedInMemory() +
(relayUri?.url?.bytesUsedInMemory() ?: 0) +
@@ -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)
@@ -53,7 +53,7 @@ data class QAddressableTag(
this.relay = relayHint
}
fun countMemory(): Long =
fun countMemory(): Int =
2 * pointerSizeInBytes +
address.countMemory() +
(relay?.url?.bytesUsedInMemory() ?: 0)
@@ -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) +
@@ -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) +
@@ -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>(BunkerMessage::class.java) {
override fun deserialize(
@@ -36,7 +36,7 @@ open class BunkerRequest(
val method: String,
val params: Array<String> = emptyArray(),
) : BunkerMessage() {
override fun countMemory(): Long =
override fun countMemory(): Int =
3 * pointerSizeInBytes + // 3 fields, 4 bytes each reference (32bit)
id.bytesUsedInMemory() +
method.bytesUsedInMemory() +
@@ -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) +
@@ -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")
@@ -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)
@@ -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)
@@ -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) +
@@ -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)
@@ -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()
@@ -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)
@@ -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)
@@ -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
@@ -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)
@@ -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)
@@ -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)
@@ -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)
@@ -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)
@@ -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)
@@ -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
@@ -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
@@ -43,7 +43,7 @@ class NIP90ContentDiscoveryResponseEvent(
) : Event(id, pubKey, createdAt, KIND, tags, content, sig) {
@Transient var events: List<HexKey>? = null
override fun countMemory(): Long =
override fun countMemory(): Int =
super.countMemory() +
pointerSizeInBytes + (events?.sumOf { it.bytesUsedInMemory() } ?: 0)
@@ -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