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-04-22 - WasmJs JSON Decoding Overhead
**Learning:** JSON decoding in Kotlin Multiplatform for WasmJs targets carries significant overhead when accessing localStorage. Re-parsing the same JSON structure repeatedly (e.g., manifest keys) negatively impacts performance on Web.
**Action:** Use an in-memory cache initialized when needed (or standard memory properties) to keep a parsed copy of frequently accessed data, thus minimizing redundant `localStorage` operations and `kotlinx.serialization` parsing penalties on WasmJs targets.
Original file line number Diff line number Diff line change
Expand Up @@ -49,18 +49,23 @@ public class LocalStorageThemeCache(
private val json = Json { ignoreUnknownKeys = true }
private val manifestKey = "${prefix}__keys__"

// Memory cache for manifest to avoid JSON decoding on every operation
private var manifestCache: MutableSet<String>? = null

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

private fun readManifest(): MutableSet<String> {
val raw = jsGetItem(manifestKey.toJsString())?.toString() ?: return mutableSetOf()
manifestCache?.let { return it }
val raw = jsGetItem(manifestKey.toJsString())?.toString() ?: return mutableSetOf<String>().also { manifestCache = it }
return try {
json.decodeFromString<Set<String>>(raw).toMutableSet()
json.decodeFromString<Set<String>>(raw).toMutableSet().also { manifestCache = it }
} catch (_: Exception) {
mutableSetOf()
mutableSetOf<String>().also { manifestCache = it }
}
}

private fun writeManifest(keys: Set<String>) {
manifestCache = keys.toMutableSet()
val encoded = json.encodeToString(keys)
jsSetItem(manifestKey.toJsString(), encoded.toJsString())
}
Expand Down
Loading