soku-perf — Zero-Regression, Profile-First Performance Tuning
Never optimize by guesswork. Follow this 4-phase loop until the target latency (e.g., sub-300 ms warm boot, <1 ms warm search) is reached with 0 functional regressions.
Phase 1: Phase-Split Millisecond Profiling (Measure First)
Never rely on a single wall-clock total. Instrument the critical path into 5–8 monotonic timestamps (e.g., process_start -> window_shown -> runtime_env -> controller_ready -> html_req -> js_req -> js_exec_start -> init_start -> app_ready) and capture 1 cold + 3–5 warm runs:
- Compute Cold, Warm Median, and Warm Min; compare against project baseline history.
- Always target the single largest delta interval first.
- If a single function (e.g.,
initApp()) takes >10 ms, bisect it with temporary sub-phase markers (cfg_done,sess_done,tab_done) before touching any code, then remove temporary markers once fixed.
Phase 2: 6 High-Leverage Optimization Patterns (Low-Risk First)
Apply in order of impact-to-risk ratio:
P1. Eliminate Boot-Time Forced Synchronous Layouts (Highest UI ROI)
During initial boot (bootInitializing = true until app_ready), audit and defer any call that forces browser style/layout recalculation:
el.focus(),el.selectionStart = ...,el.setSelectionRange(...)el.offsetWidth,el.clientWidth,el.scrollHeight,el.getBoundingClientRect(),getComputedStyle(el)- Fix: Keep a single
focusEditorAtEnd()right afterapp_ready, and defer secondary measurements (scrollbar probes, tab overflow checks, gutter/status-bar redraws) to the nextrequestAnimationFrame.
P2. Split Non-Critical CSS/JS & Enable immutable Caching
- Move hidden-at-boot CSS (
modals.css,panels.css,print.css) and non-critical JS modules into deferred bundles loaded afterapp_ready. - Serve content-hashed boot CSS/JS (
?v=<hash>) withCache-Control: public, max-age=31536000, immutableso browser HTTP cache and V8 Code Cache eliminate re-parse cost on warm starts. - Add
<link rel="preload" as="style|script">at the top of<head>for parallel fetching.
P3. Overlap Idle Wait Time & Offload Synchronous OS IPC
- While waiting for heavy runtime initialization (e.g., WebView2/Chromium controller startup ~200 ms), run independent work in parallel on background threads:
- Pre-decompress embedded gzip boot assets into an
RwLock<HashMap<_, Arc<Vec<u8>>>>(zero-copyArcreturn, no.clone()). - Pre-spawn worker thread pools (RPC / I/O workers).
- Pre-decompress embedded gzip boot assets into an
- Offload blocking OS shell IPC (e.g., Win32
Shell_NotifyIconWtray icon registration) to a background thread so Explorer IPC never stalls UI-thread resource callbacks.
P4. Adaptive (mtime, len) In-Memory Cache for Slow / Cloud I/O
For file search/scan on potentially slow mounts (Google Drive, OneDrive, SMB):
- Maintain an in-memory
HashMap<PathBuf, DirCache>storing pre-lowercased text and extracted metadata. - Track all scanned directory mtimes (
dir_mtimes). Apply a short scan TTL (e.g., 3 s) only when the previous scan was slow (>= 25 ms); on fast local SSDs, check(mtime_ns, len)every time and only re-read modified files in parallel. - Immediately invalidate the cache on every write path (
saveFile,saveFileAs,createNote,append_entry).
P5. Isolated A/B Test Before Changing Runtime Flags
- Test flags (e.g.,
--in-process-gpu,--disable-extensions) in isolation (config/env only, 3–5 runs) before changing code or default contracts. - Drop any flag that shows no statistically clear win.
P6. Audit Top-Level DOM Refs Before Lazy-Mounting Large HTML
- Before moving large hidden modals into
<template>or deferred HTML fragments, run a static audit counting top-leveldocument.getElementById(...)references to inner IDs. - If dozens of top-level refs exist, exhaust P1–P4 first; only refactor DOM mounting when P1–P4 are already saturated and a dedicated lazy-ref design is justified.
Phase 3: Multi-Run Statistical Verification
- Re-run the exact same benchmark harness (1 cold + 3–5 warm runs) after each change.
- Verify both internal trace (
app_ready) and end-to-end harness (warmAppReadyMedianMs,coldAppReadyMs,reattachWakeMs, memory/RSS, binary size). - Keep a change only if it measurably improves or holds latency with zero instability.
Phase 4: Zero-Regression Gate (Mandatory)
Speed optimizations must never drop features, weaken security, or silently break default config contracts:
- Run real-browser / runtime E2E tests (DOM/SVG rendering, focus/keyboard, modals, settings, zero
Uncaughtconsole errors). - Run full unit & static contract test suites.
- Rebuild release artifacts and confirm clean
git diff(no leftover temporary profiling markers).