Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .jules/bolt.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## 2024-05-24 - WasmJs JSON Parsing Overhead
**Learning:** JSON decoding in Kotlin Multiplatform for WasmJs targets carries significant overhead, making repeated parsing of localStorage contents a performance bottleneck.
**Action:** Use an in-memory cache (e.g., via `by lazy`) to store parsed JSON structures and only sync to localStorage when necessary.
Original file line number Diff line number Diff line change
Expand Up @@ -49,17 +49,21 @@ public class LocalStorageThemeCache(
private val json = Json { ignoreUnknownKeys = true }
private val manifestKey = "${prefix}__keys__"

// ── Manifest helpers ────────────────────────────────────────────────

private fun readManifest(): MutableSet<String> {
val raw = jsGetItem(manifestKey.toJsString())?.toString() ?: return mutableSetOf()
return try {
private val cachedManifest: MutableSet<String> by lazy {
val raw = jsGetItem(manifestKey.toJsString())?.toString() ?: return@lazy mutableSetOf()
try {
json.decodeFromString<Set<String>>(raw).toMutableSet()
} catch (_: Exception) {
mutableSetOf()
}
}

// ── Manifest helpers ────────────────────────────────────────────────

private fun readManifest(): MutableSet<String> {
return cachedManifest
}

private fun writeManifest(keys: Set<String>) {
val encoded = json.encodeToString(keys)
jsSetItem(manifestKey.toJsString(), encoded.toJsString())
Expand Down Expand Up @@ -155,6 +159,7 @@ public class LocalStorageThemeCache(
removeEntry("$prefix$key")
}
jsRemoveItem(manifestKey.toJsString())
manifest.clear()
_changes.tryEmit(CacheEvent.Cleared)
}

Expand Down
Loading