Repository navigation
Closed
Description
ContextDynamic mode currently passes sketch.wasm to the fastled.wasm link command for Emscripten load-time dynamic linking. Consequently, every sketch change invalidates and relinks the large main module.Measured quick-mode costs on the representative WASM sketch:- side-module link: approximately 0.8 seconds- dynamic main-module link: approximately 6.9 seconds- static main link: approximately 3.0 seconds- dynamic fastled.wasm: approximately 5.4 MB- static fastled.wasm: approximately 538 KBThe expensive main link is unnecessary when only sketch code changes. Dynamic mode should treat the FastLED runtime and sketch side module as independently cached artifacts.Related: #184 established the dynamic main/side-module output contract.## ProposalChange dynamic mode to produce and cache two independently built units:1. FastLED runtime: - fastled.js - fastled.wasm - Built from FastLED library sources and runtime configuration. - Shared across sketches when their FastLED/toolchain/build configuration is identical. - Loaded first.2. Sketch side module: - sketch.wasm - Built from sketch code against the selected FastLED runtime ABI. - Loaded at runtime by fastled.js. - Rebuilt independently when only sketch code changes.Do not pass sketch.wasm to the fastled.wasm link command. fastled.js should load it after main-module initialization through Emscripten's supported runtime dynamic-library API.### Emscripten module-mode decisionBegin with MAIN_MODULE=1 for the independently cached runtime. Because the main module is linked without seeing a particular sketch, it must retain every FastLED and system symbol an arbitrary future sketch.wasm may import.Hard non-goal: use SIDE_MODULE=1 exclusively. SIDE_MODULE=2 must not be explored, benchmarked, prototyped, evaluated, or implemented as part of this work. The sketch side module must preserve the broad linkable surface rather than introducing dead-code-elimination and retention-list complexity.Treat MAIN_MODULE=2 as a later optimization only after FastLED has a generated, versioned, exhaustive ABI export manifest. Using mode 2 without such a manifest can dead-code-eliminate symbols required by runtime-loaded sketches. Deriving retained symbols from each sketch would make fastled.wasm sketch-specific again and defeat this cache design.The implementation must verify whether MAIN_MODULE=1 requires --whole-archive or another retention mechanism when no side module is present during the main link. INCLUDE_FULL_LIBRARY covers Emscripten's JS library, not necessarily every C/C++ member of libfastled.a.## Fingerprint modelIntegrate zccache-fingerprint directly as a Rust library rather than requiring an external zccache executable.Use a content-authoritative aggregate fingerprint for cache identity. A same-size content change with a preserved mtime must still invalidate the cache. Two-layer mtime acceleration may be used only if correctness remains content-backed.### FastLED runtime fingerprintInclude every input capable of changing fastled.js, fastled.wasm, or their ABI:- FastLED C/C++ sources and headers under src/- source additions, deletions, and renames- build_flags.toml- js_library.js- WASM PCH/header-unit inputs- Meson files and WASM source-discovery/build scripts- generated cross-file contents- build mode: quick, debug, or release- dynamic-link settings, including MAIN_MODULE, pthreads, and WASM_BIGINT- normalized compile and link flags- Emscripten, compiler, linker, and archiver identity/version- target architecture- environment values that affect emitted code- fingerprint schema version### Sketch fingerprintInclude:- sketch .ino, .cpp, .c, .h, and .hpp inputs- generated wrapper contents- sketch compile and side-module link flags- build mode and dynamic ABI settings- toolchain identity- the FastLED runtime fingerprintIncluding the runtime fingerprint ensures that a FastLED library or ABI change rebuilds both fastled.wasm and sketch.wasm.### Separately cached inputsThese must not invalidate either WASM module:- default app/UI source changes- --no-app toggles- sketch asset changes- sketch_assets.json regeneration- legacy files.json handling## Required state transitions| Change | FastLED runtime | Sketch module | App/assets ||---|---:|---:|---:|| Nothing | reuse | reuse | reuse || Sketch source only | reuse | rebuild | reuse || FastLED source/header | rebuild | rebuild | reuse || FastLED build/link flags | rebuild | rebuild | reuse || Toolchain/Emscripten version | rebuild | rebuild | reuse || Dynamic ABI setting | rebuild | rebuild | reuse || App/UI source only | reuse | reuse | rebuild app || Sketch assets only | reuse | reuse | regenerate assets || Toggle --no-app | reuse | reuse | add/remove app || Static/dynamic switch | isolated caches | rebuild appropriate sketch output | reconcile app || Different sketch, same runtime key | reuse shared runtime | build sketch | build app as needed |## Cache ownership and publication- Store runtime artifacts in a FastLED/toolchain/build-mode-level cache, not under a single sketch's cache directory.- Share a valid runtime cache entry across sketches.- Key entries by the complete runtime fingerprint.- Use a per-key interprocess lock.- Build into temporary paths.- Validate all artifacts before atomically publishing them.- Mark the zccache fingerprint successful only after publication succeeds.- A failed or interrupted build must not replace the previous known-good entry.- A waiter must recheck the cache after acquiring the lock.- --purge and force-clean behavior must explicitly cover runtime and sketch caches.- Log cache hit/miss/rebuild reasons and phase timings.## Degenerate and corrupt statesThe implementation must detect and recover from:- missing fastled.js- missing fastled.wasm- missing sketch.wasm- zero-length artifacts- truncated or invalid WASM magic/version- corrupt or unreadable fingerprint metadata- metadata schema mismatch- metadata referencing a different build mode, ABI, or toolchain- mismatched fastled.js and fastled.wasm- side module built against a different runtime fingerprint- stale artifacts with newer timestamps- changed content with preserved timestamps and file sizes- source deletion or rename- branch switches that restore older mtimes- partial temporary files from interruption- previous failed fingerprint state- concurrent builds targeting the same cache key- stale dynamic artifacts after switching to static mode- stale app artifacts after switching to --no-appRecovery must rebuild the smallest safe unit:- corrupt runtime artifact: rebuild runtime- corrupt sketch artifact: rebuild sketch- ABI/runtime fingerprint mismatch: rebuild both- unknown or ambiguous metadata: fail safe and rebuild## Acceptance criteria- [ ] A sketch-only edit compiles and links only sketch.wasm.- [ ] A sketch-only edit does not invoke the main-module linker.- [ ] fastled.js and fastled.wasm remain byte-for-byte unchanged after a sketch-only edit.- [ ] Runtime startup loads the newly rebuilt sketch.wasm successfully.- [ ] Dynamic sketches use SIDE_MODULE=1; SIDE_MODULE=2 is not evaluated or implemented.- [ ] A FastLED source, header, build-flag, or toolchain change rebuilds both modules.- [ ] Two different sketches with the same runtime fingerprint share one runtime cache entry.- [ ] Quick, debug, and release modes use isolated runtime keys.- [ ] Static and dynamic artifacts cannot contaminate one another.- [ ] App-only and asset-only changes do not rebuild WASM.- [ ] Cache publication is atomic and safe across interruption and concurrency.- [ ] Logs clearly state which fingerprint changed and why each artifact was rebuilt.- [ ] Existing static mode and --no-app output contracts remain unchanged.Do not use a machine-specific wall-time threshold as the primary performance test. Assert linker/compiler invocation counts and artifact hashes; record timing only as benchmark data.## Test matrixAdd unit and integration tests using temporary repositories and instrumented fake compiler/linker commands:1. Cold dynamic build invokes library build, main link, sketch compile, and side link.2. No-op rebuild invokes none of them.3. Sketch-only edit invokes sketch compile and side link only.4. FastLED .cpp edit rebuilds both modules.5. FastLED header edit rebuilds both modules.6. Source addition, deletion, and rename rebuild both modules.7. build_flags.toml edit rebuilds both modules.8. js_library.js edit rebuilds the runtime and sketch.9. Toolchain/version change rebuilds both modules.10. Quick/debug/release transitions use separate entries.11. Static/dynamic transitions do not leak stale artifacts.12. App-only edit leaves all WASM hashes unchanged.13. Asset-only edit leaves all WASM hashes unchanged.14. --no-app toggle leaves all WASM hashes unchanged.15. A second sketch reuses the first sketch's runtime entry.16. Same-content touch does not rebuild.17. Same-size content change with restored mtime does rebuild.18. Corrupt runtime metadata rebuilds safely.19. Missing or truncated runtime artifacts rebuild safely.20. Missing or truncated side module rebuilds only the side module when the ABI remains valid.21. Interrupted build preserves the prior known-good cache.22. Concurrent builds perform one main link and publish one valid entry.23. Failed main or side link marks the pending fingerprint failed.24. Real-browser smoke tests verify static and runtime-loaded dynamic sketches.25. Browser tests cover app and --no-app hosts plus sketch_assets.json and legacy files.json.## Open questions- Which supported Emscripten runtime loading API and options should fastled.js use?- Does the unstripped MAIN_MODULE=1 runtime require --whole-archive for libfastled.a, or can a generated retention manifest provide the same guarantee?- What cache eviction policy and maximum disk usage should be implemented separately from cache correctness?## Related issues- #184
Metadata
Metadata
Assignees
Labels
No labels
Activity
perf(cli): cache dynamic WASM runtime independently (#189)