From 71500e413809c691728537c0d89d60ef9ef2e272 Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Fri, 24 Apr 2026 02:26:23 +0000 Subject: [PATCH] =?UTF-8?q?=E2=9A=A1=20Bolt:=20Optimize=20WasmJs=20LocalSt?= =?UTF-8?q?orageThemeCache=20by=20caching=20JSON=20parsing?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 💡 What: Introduced `by lazy` initialized memory cache for the manifest in `LocalStorageThemeCache.kt`. 🎯 Why: JSON parsing in Kotlin Multiplatform for WasmJs targets carries significant overhead, making repeated parsing of localStorage contents a major performance bottleneck during cache operations. 📊 Impact: Eliminates redundant JSON decoding on every `readManifest` call, resulting in a significantly faster execution for cache reads and reducing unnecessary compute load. 🔬 Measurement: Run `./gradlew :halogen-engine:wasmJsTest` and verify all tests pass while the manifest decoding happens only once per cache instance lifespan instead of per read operation. Co-authored-by: himattm <6266621+himattm@users.noreply.github.com> --- .jules/bolt.md | 3 +++ .../halogen/engine/LocalStorageThemeCache.kt | 15 ++++++++++----- 2 files changed, 13 insertions(+), 5 deletions(-) create mode 100644 .jules/bolt.md diff --git a/.jules/bolt.md b/.jules/bolt.md new file mode 100644 index 0000000..357fbff --- /dev/null +++ b/.jules/bolt.md @@ -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. diff --git a/halogen-engine/src/wasmJsMain/kotlin/halogen/engine/LocalStorageThemeCache.kt b/halogen-engine/src/wasmJsMain/kotlin/halogen/engine/LocalStorageThemeCache.kt index aba1f32..f38bc31 100644 --- a/halogen-engine/src/wasmJsMain/kotlin/halogen/engine/LocalStorageThemeCache.kt +++ b/halogen-engine/src/wasmJsMain/kotlin/halogen/engine/LocalStorageThemeCache.kt @@ -49,17 +49,21 @@ public class LocalStorageThemeCache( private val json = Json { ignoreUnknownKeys = true } private val manifestKey = "${prefix}__keys__" - // ── Manifest helpers ──────────────────────────────────────────────── - - private fun readManifest(): MutableSet { - val raw = jsGetItem(manifestKey.toJsString())?.toString() ?: return mutableSetOf() - return try { + private val cachedManifest: MutableSet by lazy { + val raw = jsGetItem(manifestKey.toJsString())?.toString() ?: return@lazy mutableSetOf() + try { json.decodeFromString>(raw).toMutableSet() } catch (_: Exception) { mutableSetOf() } } + // ── Manifest helpers ──────────────────────────────────────────────── + + private fun readManifest(): MutableSet { + return cachedManifest + } + private fun writeManifest(keys: Set) { val encoded = json.encodeToString(keys) jsSetItem(manifestKey.toJsString(), encoded.toJsString()) @@ -155,6 +159,7 @@ public class LocalStorageThemeCache( removeEntry("$prefix$key") } jsRemoveItem(manifestKey.toJsString()) + manifest.clear() _changes.tryEmit(CacheEvent.Cleared) }