Skip to content

perf: P0 performance optimizations#944

Merged
jackwener merged 2 commits intomainfrom
perf/p0-optimizations
Apr 10, 2026
Merged

perf: P0 performance optimizations#944
jackwener merged 2 commits intomainfrom
perf/p0-optimizations

Conversation

@jackwener
Copy link
Copy Markdown
Owner

Summary

  • VM context reuse (template.ts): Reuse vm.createContext() sandbox across pipeline expression evaluations instead of creating a new V8 context per call. In map/filter loops over large arrays, this eliminates the most expensive operation (~0.3ms/call → amortized to ~0).
  • sanitizeContext caching (template.ts): Cache JSON.parse(JSON.stringify()) results via WeakMap. args and data are the same object reference across loop iterations — deep copy now runs once per step instead of once per item.
  • Startup parallelization (main.ts): Run built-in CLI discovery concurrently with ensureUserCliCompatShims() and ensureUserAdapters() since they operate on independent paths. Saves ~30-50ms on cold start.
  • Stealth JS caching (stealth.ts): Cache the 350-line static stealth JS string after first generation. Every goto() call was regenerating the same string.

Test plan

  • npx tsc --noEmit passes
  • All 47 unit test files pass (584 tests)
  • Template-specific tests pass (51 tests)
  • Manual test: run opencli list and verify startup feels snappy
  • Manual test: run a pipeline-heavy adapter and verify correctness

…elization, stealth caching

1. Reuse VM sandbox context in pipeline template engine instead of creating
   a new vm.createContext() on every expression evaluation. This eliminates
   ~0.3ms per call in map/filter loops over large arrays.

2. Cache sanitizeContext() results via WeakMap keyed by object reference.
   In pipeline loops, `args` and `data` are the same object across all
   iterations — the expensive JSON round-trip now runs only once per step.

3. Parallelize independent startup I/O: built-in CLI discovery now runs
   concurrently with ensureUserCliCompatShims and ensureUserAdapters,
   saving ~30-50ms on cold start.

4. Cache the stealth JS string (350 lines, pure static) after first
   generation — every subsequent goto() reuses the cached string.
1. sanitizeContext: cache JSON string instead of parsed object to prevent
   sandbox mutation from polluting subsequent calls
2. VM sandbox: clean non-whitelisted properties before each execution to
   prevent cross-expression state leakage
3. Startup parallelization: document registry overwrite semantics and
   confirm no shared-state race between parallel tasks
@jackwener jackwener merged commit 575986c into main Apr 10, 2026
11 checks passed
@jackwener jackwener deleted the perf/p0-optimizations branch April 10, 2026 17:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant