Skip to content

perf(cli): reduce dynamic sketch-edit latency from 3.62s toward 1.6s #190

Closed

Description

@zackees

Context

Dynamic mode now correctly caches the FastLED runtime separately from sketch.wasm, but a sketch-only edit still takes substantially longer than the actual C++ compile and WASM link work require.

Representative quick-mode benchmark:

Scenario Static Dynamic
Warm no-op 1.70s 1.46s
Sketch edit 5.91s 3.62s

A phase-level investigation found that most of the dynamic edit latency comes from serial fingerprint scanning and Emscripten Python-driver startup rather than Clang or wasm-ld execution.

Measured phase costs

Phase Current Optimized prototype
FastLED/toolchain fingerprints ~1.0-1.3s ~0.31-0.33s
Sketch compile through em++.py ~1.0s ~0.21s direct Clang
Side-module link through em++.py ~0.96s ~0.07-0.10s direct pipeline
Miscellaneous install/output work ~0.1s ~0.05s

Updating the fingerprint implementation alone produced five fresh dynamic sketch-edit samples of 3.36s, 2.42s, 2.50s, 2.40s, and 2.43s, for a 2.43s median. The first sample was a warm-up outlier. Warm no-op builds fell to approximately 0.43-0.47s.

The faster fingerprint prototype used zccache-fingerprint revision 1bd5b6387eff54695e37adbfacbb977079d6f174, which parallelizes directory traversal and content hashing while retaining content-authoritative invalidation:

forge-ci/zccache@1bd5b63

Directly replaying the Clang invocation emitted by Emscripten produced an object file with identical size and SHA-256 hash while reducing compile time from approximately 1.0s to 0.20-0.23s. Emscripten officially supports obtaining direct Clang flags with emcc --cflags:

https://emscripten.org/docs/tools_reference/emcc.html

Directly replaying the side-module wasm-ld / llvm-objcopy / wasm-emscripten-finalize pipeline also produced byte-identical output in 0.07-0.10s, but that pipeline is an Emscripten implementation detail and needs stronger compatibility safeguards.

Related: #188 introduced independent runtime and sketch caching. This issue is the performance follow-up after that architecture landed.

Proposal

Implement the optimizations in risk-ordered stages.

Stage 1: parallel content-authoritative fingerprints

  • Upgrade to the tested parallel zccache-fingerprint revision, or backport its parallel traversal/hash implementation.
  • Preserve detection of same-size content changes with restored mtimes.
  • Review the additional dependency footprint before selecting upgrade versus backport.
  • Compute independent FastLED-source and toolchain fingerprints concurrently where practical.

Expected sketch-edit result: approximately 2.4s median.

Stage 2: compile sketches with direct Clang

  • Obtain the toolchain's direct compiler flags through em++ --cflags.
  • Cache the normalized flags by Emscripten/toolchain/build-mode fingerprint.
  • Invoke the bundled clang++ directly for sketch translation units.
  • Continue to use em++ for the supported side-module link path.
  • Preserve the existing object cache and independently cached translation units.

Expected sketch-edit result: approximately 1.5-1.7s.

Stage 3: remove small fixed overheads

  • Cache the selected installed Emscripten version/manifest with an appropriate TTL instead of performing remote-manifest work before every installed-marker check.
  • Do not rewrite .emscripten when its contents are unchanged.
  • On a sketch-only edit, copy/publish only changed sketch.wasm; do not recopy unchanged fastled.js or the approximately 5.4 MB fastled.wasm.
  • Record phase timings for source fingerprint, toolchain fingerprint, compile, side link, and output publication.
  • Record cache hit/miss and rebuild reasons.

Stage 4: experimental quick-mode direct side link

Prototype a quick-mode-only direct side-module link path using the tested Emscripten toolchain's wasm-ld, llvm-objcopy, and wasm-emscripten-finalize commands.

This path must:

  • Be version-gated to explicitly tested Emscripten releases, initially 4.0.19.
  • Cache a normalized link plan by toolchain and link flags.
  • Fall back automatically to em++ when the version or link configuration is unknown.
  • Never become the only supported path until compatibility coverage is sufficient.
  • Preserve SIDE_MODULE=1; this work must not explore or introduce SIDE_MODULE=2.

Expected aggressive quick-mode result: approximately 0.65-0.75s per sketch edit.

Stage 5: persistent Replit/watch mode

For a long-lived compiler process:

  • Perform one full, content-authoritative FastLED/toolchain scan at startup.
  • Watch relevant source and toolchain directories for changes.
  • Invalidate affected fingerprints from filesystem events.
  • Force a full rescan after watcher overflow, restart, uncertainty, or branch/repository replacement.

This may remove the remaining approximately 0.32s fingerprint scan from each edit and target approximately 0.35-0.45s in a persistent session.

Non-goals

  • Do not treat WASM_BIGINT=1 as a compile-speed optimization. It changes ABI/output behavior but did not materially remove Python-driver wall time.
  • Do not weaken content-authoritative cache correctness to gain speed.
  • Do not make machine-specific wall-clock thresholds the primary correctness tests.
  • Do not explore SIDE_MODULE=2.

Acceptance criteria

  • Parallel fingerprinting detects additions, deletions, renames, normal edits, and same-size/restored-mtime content changes.
  • With only Stage 1 enabled, a representative dynamic sketch edit has a benchmark median near the measured ~2.4s rather than ~3.6s on the reference machine.
  • Direct sketch compilation derives flags from the active Emscripten toolchain rather than maintaining a manually duplicated static flag list.
  • Direct and em++ sketch compilation produce equivalent object files across quick, debug, and release configurations.
  • Object-cache behavior remains correct for repeated source states and multi-file sketches.
  • Sketch-only edits do not recopy unchanged fastled.js or fastled.wasm.
  • Installed-toolchain checks do not require a remote manifest request on every warm compilation.
  • Logs expose phase timings and cache/rebuild reasons.
  • Existing static mode behavior remains unchanged.
  • Existing dynamic runtime cache invalidation from perf(cli): cache dynamic fastled.wasm independently with fingerprinted runtime side loading #188 remains correct.
  • The experimental direct-link path is quick-mode-only, version-gated, and automatically falls back to em++.
  • SIDE_MODULE=2 is not evaluated or implemented.

Test plan

Fingerprint and cache correctness

  1. No-op rebuild invokes neither sketch compiler nor linker.
  2. Sketch edit invokes only sketch compile and side link.
  3. Same-content touch does not rebuild.
  4. Same-size content change with restored mtime does rebuild.
  5. FastLED source/header, toolchain, build-mode, ABI flag, and source-set changes invalidate the proper caches.
  6. Watcher overflow and compiler restart trigger a safe full scan.

Direct compiler equivalence

For quick, debug, and release modes:

  1. Capture flags through em++ --cflags.
  2. Compile representative sketches through both em++ and direct clang++.
  3. Compare object structure, imports/exports, symbols, and runtime behavior.
  4. Cover multiple translation units, PCH/header-unit inputs, EM_JS, EM_ASM, exceptions/settings in supported configurations, and compile failures.

Experimental direct-link equivalence

Compare the direct quick-mode pipeline with em++ for:

  • Constructors and static initialization.
  • Unresolved main-module imports.
  • EM_JS and EM_ASM.
  • TLS and pthread configurations where supported.
  • WASM_BIGINT=0 and applicable ABI settings.
  • Debug information and release stripping behavior.
  • Invalid or unsupported Emscripten versions and fallback behavior.
  • Browser startup and execution using the resulting sketch.wasm.

Assert compiler/linker invocation counts, artifact hashes/structure, and browser behavior. Keep wall-clock measurements as benchmark evidence rather than brittle pass/fail criteria.

Target performance

Milestone Dynamic sketch-edit target Confidence
Current 3.62s Measured baseline
Parallel fingerprints ~2.4s Measured prototype
Direct Clang + supported em++ link ~1.5-1.7s Projected from measured phases
Version-gated direct quick link ~0.65-0.75s Projected from measured phases
Persistent watcher/compiler service ~0.35-0.45s Longer-term target

Activity

  1. zackees commented on Jul 12, 2026

    @zackees
    MemberAuthor

    Final post-merge acceptance audit found that debug-mode direct compilation falls back to em++ because -gsource-map is driver-only. Reopening to complete the explicit quick/debug/release direct-compile equivalence criterion.

  2. added a commit that references this issue on Jul 12, 2026
    68ec7f9
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions