From 491c31d625d65819cfcf90d30e4725243faceae7 Mon Sep 17 00:00:00 2001 From: Claude Date: Wed, 8 Apr 2026 03:06:46 +0000 Subject: [PATCH] perf: increase cache sizes from 256 to 1024 for ~1000 followed pubkeys MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit With 256 slots and ~1000 followed users, the direct-mapped caches had ~75% collision rate (multiple pubkeys mapping to the same slot, causing constant eviction and recomputation). Increasing to 1024 slots covers most follow lists with minimal collisions. Memory impact: pubkeyCache: 1024 × ~96 bytes = ~96KB (was ~24KB) pTableCache: 1024 × ~1KB = ~1MB (was ~256KB) Total ~1.1MB — acceptable for mobile. https://claude.ai/code/session_017UbWduFi1sLUsgVUMUH2nx --- .../com/vitorpamplona/quartz/utils/secp256k1/ECPoint.kt | 4 +++- .../com/vitorpamplona/quartz/utils/secp256k1/Secp256k1.kt | 5 +++-- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/quartz/src/commonMain/kotlin/com/vitorpamplona/quartz/utils/secp256k1/ECPoint.kt b/quartz/src/commonMain/kotlin/com/vitorpamplona/quartz/utils/secp256k1/ECPoint.kt index 8a7a8c439..ab7968f31 100644 --- a/quartz/src/commonMain/kotlin/com/vitorpamplona/quartz/utils/secp256k1/ECPoint.kt +++ b/quartz/src/commonMain/kotlin/com/vitorpamplona/quartz/utils/secp256k1/ECPoint.kt @@ -105,7 +105,9 @@ internal object ECPoint { // // 256 entries × 16 AffinePoints × 64 bytes = ~256KB total cache. - private const val P_TABLE_CACHE_SIZE = 256 + // 1024 entries to cover ~1000 followed pubkeys with minimal collisions. + // Memory: 1024 × 16 AffinePoints × 64 bytes = ~1MB. Acceptable for mobile. + private const val P_TABLE_CACHE_SIZE = 1024 private const val P_TABLE_CACHE_MASK = P_TABLE_CACHE_SIZE - 1 private class CachedPTable( diff --git a/quartz/src/commonMain/kotlin/com/vitorpamplona/quartz/utils/secp256k1/Secp256k1.kt b/quartz/src/commonMain/kotlin/com/vitorpamplona/quartz/utils/secp256k1/Secp256k1.kt index 3a4cdb13c..7bec8c134 100644 --- a/quartz/src/commonMain/kotlin/com/vitorpamplona/quartz/utils/secp256k1/Secp256k1.kt +++ b/quartz/src/commonMain/kotlin/com/vitorpamplona/quartz/utils/secp256k1/Secp256k1.kt @@ -85,8 +85,9 @@ object Secp256k1 { // saving the sqrt for repeated pubkeys (~13% of verify cost per cache hit). // // Simple fixed-size direct-mapped cache (no LRU overhead). Size must be power of 2. - // 256 entries × (32 + 32 + 32) bytes = ~24KB. Cache collisions just evict silently. - private const val PUBKEY_CACHE_SIZE = 256 // power of 2 + // 1024 entries covers most follow lists (~1000 users) with few collisions. + // Memory: 1024 × ~96 bytes = ~96KB. + private const val PUBKEY_CACHE_SIZE = 1024 // power of 2 private const val PUBKEY_CACHE_MASK = PUBKEY_CACHE_SIZE - 1 private class CachedPubkey(