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 localStorage JSON parsing overhead
**Learning:** JSON decoding in Kotlin Multiplatform for WasmJs targets carries significant overhead. Parsing the same JSON manifest from localStorage repeatedly causes a measurable performance bottleneck.
**Action:** Use an in-memory cache initialized via `by lazy` to avoid redundant localStorage parsing for frequently accessed metadata like manifests.
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 inMemoryManifest: 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 inMemoryManifest
}

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