Skip to content

perf(sync-service): pre-compile cached parachain runtime during relay chain fetch#3226

Closed
replghost wants to merge 4 commits intoperf/parachain-warm-skip-downloadfrom
worktree-perf-precompile-parachain-runtime
Closed

perf(sync-service): pre-compile cached parachain runtime during relay chain fetch#3226
replghost wants to merge 4 commits intoperf/parachain-warm-skip-downloadfrom
worktree-perf-precompile-parachain-runtime

Conversation

@replghost
Copy link
Copy Markdown
Contributor

@replghost replghost commented Apr 22, 2026

Summary

  • Spawns a background task to compile the cached WASM runtime immediately on parachain start, before Phase 1's relay chain head fetch
  • Compilation now overlaps with network I/O instead of blocking after it, removing it from the warm-start critical path
  • Falls back gracefully to cold bootstrap if the pre-compilation task fails or is cancelled
  • 3 integration tests for the precompile mechanism

Changes

  • light-base/src/sync_service/parachain.rs: Spawn platform.spawn_task() for WASM compilation before Phase 1, deliver (HostVmPrototype, Vec<u8>) via oneshot channel. try_warm_start_from_cached_code awaits the precompiled VM before peer wait — fails fast on compilation errors without wasting time on peer discovery.

A/B Benchmarks

Warm-start "para after relay" — time from relay chain ready to parachain initialized.
Baseline is #3214 (inline compilation), treatment is this PR (background precompile).

Network Runtime Baseline (A) Treatment (B) Notes
Paseo Asset Hub 2.3 MB 1005ms, 650ms 1056ms, 677ms Compilation fully hidden; delta dominated by peer/Aura latency variance
Polkadot Asset Hub 2.0 MB 1217ms, 212ms 1113ms, 532ms Same — compilation hidden behind relay fetch
Kusama Asset Hub 2.6 MB 5059ms, 3272ms 8466ms, 1258ms High peer latency variance; run 2 shows clear win

The "para after relay" metric has high variance because it includes peer discovery + Aura call proof latency which depends on network conditions. The key signal is in the sync-service timeline:

Baseline timeline (Polkadot, run 1):

[5750ms] Compiling cached parachain runtime (2040296 bytes)...   ← blocks here
[6656ms] Warm start: Aura consensus verified                     ← 906ms gap (compile + proofs)

Treatment timeline (Polkadot, run 1):

[131ms]  Pre-compiling cached parachain runtime (2040296 bytes)  ← fires at startup
[5209ms] Warm start ... compiled 2040296 bytes                   ← VM already ready
[6115ms] Warm start: Aura consensus verified                     ← 906ms is now just proofs

Compilation (~500-700ms in debug builds) runs entirely during Phase 1's ~5s relay chain fetch. The VM is ready by the time Phase 2 starts.

Relates to #3221

… chain fetch

Spawn a background task to compile the cached WASM runtime immediately
on parachain start, before Phase 1's relay chain head fetch. The
compilation (~200-300ms) now overlaps with network I/O instead of
blocking after it, removing it from the critical warm-start path.
Tests the spawn_task + oneshot pattern used for background WASM
pre-compilation:
- Valid WASM compiles and delivers a HostVmPrototype via oneshot
- Dropped sender (cancelled task) yields cancellation error
- Invalid WASM delivers compilation error through oneshot
- Double-? unwrap pattern handles both error cases correctly
- Await precompiled VM before peer wait so compilation failures bail
  out immediately without wasting time on peer discovery
- Send (HostVmPrototype, Vec<u8>) through oneshot to avoid cloning
  the 2+ MB runtime code bytes
- Remove double_question_mark_pattern test (tests Rust, not our code)
- Strip verbose comments
These tested spawn_task + oneshot behavior, not actual code.
@replghost
Copy link
Copy Markdown
Contributor Author

Closing — benchmarks across Paseo, Polkadot, and Kusama Asset Hubs showed no measurable end-to-end improvement. Compilation (~200-300ms) is already fully hidden behind the 4-6s relay chain fetch. If relay fetch latency drops significantly in the future, a simpler approach (reorder compilation before peer wait, no spawn_task/oneshot needed) would suffice.

@replghost replghost closed this Apr 23, 2026
@replghost replghost deleted the worktree-perf-precompile-parachain-runtime branch April 23, 2026 03:14
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