Skip to content

perf(java): pre-compile WASM module once and reuse across instances#279

Closed
nicklasl wants to merge 1 commit intomainfrom
nicklasl/pre-compile-wasm-module
Closed

perf(java): pre-compile WASM module once and reuse across instances#279
nicklasl wants to merge 1 commit intomainfrom
nicklasl/pre-compile-wasm-module

Conversation

@nicklasl
Copy link
Copy Markdown
Member

Summary

  • Pre-compile the WASM module once in SwapWasmResolverApi constructor using MachineFactoryCompiler.compile(WasmModule) and reuse the compiled machine factory across all WasmResolveApi instances.
  • Previously, every updateStateAndFlushLogs call created a new WasmResolveApi, triggering full AOT WASM-to-JVM bytecode compilation each time via MachineFactoryCompiler::compile(Instance).

Test plan

  • mvn compile passes
  • Verified all test failures are pre-existing (missing deps, missing env vars)
  • Verify in staging that state updates no longer cause CPU spikes from WASM recompilation

🤖 Generated with Claude Code

@nicklasl nicklasl marked this pull request as ready for review February 16, 2026 15:31
Use MachineFactoryCompiler.compile(WasmModule) to compile once in the
SwapWasmResolverApi constructor and pass the pre-compiled factory to all
WasmResolveApi instances.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@nicklasl nicklasl force-pushed the nicklasl/pre-compile-wasm-module branch from 3cd6aad to 3b51d57 Compare February 16, 2026 17:56
@nicklasl
Copy link
Copy Markdown
Member Author

Benchmark Results

Ran a 2-minute CPU benchmark comparing this branch against main, simulating production load: ~1 flag evaluation every 60ms (~2000 total) with state updates every 30s (3 updates total). CPU time measured via OperatingSystemMXBean.getProcessCpuTime().

Metric This branch (optimized) main (baseline)
CPU time 3.94s 8.41s
Evaluations 2001 2000
State updates 3 3
CPU/eval 1.97ms 4.21ms

~53% CPU reduction — the savings come from eliminating MachineFactoryCompiler.compile() on each state update, since the compiled WASM module is now reused across instances.

How the benchmark works

A JUnit test creates a SwapWasmResolverApi with a real resolver state and uses a ScheduledExecutorService to run flag evaluations every 60ms and trigger updateStateAndFlushLogs every 30s, measuring process CPU time over 2 minutes. A shell script automates running the benchmark on both branches and prints a side-by-side comparison.

@nicklasl nicklasl changed the title feat(java): pre-compile WASM module once and reuse across instances perf(java): pre-compile WASM module once and reuse across instances Feb 17, 2026
private final MaterializationStore materializationStore;
private final WasmFlagLogger flagLogger;
private final WasmModule wasmModule;
private final Function<Instance, Machine> machineFactory;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually feels like this should be static for this to be any speedup?

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.

2 participants