Skip to content

perf(wasm): run functions without Asyncify when no import can suspend - #1087

Merged
wolfy-j merged 8 commits into
mainfrom
perf/wasm-function-speed
Nov 15, 2026
Merged

wolfy-j merged 8 commits into
mainfrom
perf/wasm-function-speed

Conversation

@skhaz

@skhaz skhaz commented Nov 15, 2026 •

Copy link
Copy Markdown
Contributor

Reason for This PR

WASM functions that can never suspend still ran with Asyncify instrumentation. For the PDF rasterizer component this made each call about 1.5x slower, and the first load of the module cost about 5 s of transform work.

Description of Changes

Speedup

Every WASM function was compiled with the Asyncify transform as soon as one registered host function was async. One example is output-stream.blocking-flush, which Rust std calls on its print and panic paths. Stdio streams never suspend: blockingOutput/blockingInput suspend only for buffered streams, which come from sockets, HTTP bodies and files. So a function that imports only wasi:io, wasi:cli, wasi:clocks and wasi:random paid for instrumentation it can never use.

  • HostProfile.NeverSuspends marks wasi:io, wasi:cli, wasi:clocks and wasi:random.
  • HostRegistry.MaySuspend(imports) is true when any import can suspend or is unknown.
  • go.mod updates wasm-runtime (synchronous compile, SIMD Asyncify fix, schema validation once per type) and the wazero fork (context watch without a goroutine per call, geometric memory growth).
  • The function manager compiles isolated modules with CompileConfig{Synchronous: true} when MaySuspend is false. Shared runtimes keep normal Asyncify derivation because sibling entries may have registered additional hosts. Such an instance has no Asyncify scheduler, so Process takes the existing synchronous step path.
  • Functions that import poll, filesystem, sockets, HTTP, actor, funcs or an unknown profile keep the transform.

This needs wippyai/wasm-runtime#22 and wippyai/wazero#2. go.mod points at their branch heads. I will update it to the merge commits after those PRs merge.

Measurements

The measurements use kickside/pdf2image (Rust component, imports wasi:io, wasi:cli, wasi:random), one A4 page at 150 DPI, Apple M-series, wippy test with the same module and the same SDK.

before after
Guest step time, PNG page (instrumented build) 798 ms 525 ms
Render, PNG / JPEG / WEBP page 790 / 356 / 270 ms 525 / 254 / 192 ms
Render, 5 PNG pages 3980 ms 2640 ms
kickside/doc2md convert, 2 test PDFs 36 / 105 ms 26 / 71 ms
kickside/doc2md page_resources 32 / 55 ms 24 / 28 ms
First load of the 3.8 MB module (transform + compile) 5.18 s, 5925 MB allocated, 19.1 M allocs 0.68 s, 127 MB allocated, 186 k allocs
Go allocations per render 281-283 265-269
Linear memory per instance 19.6-24.9 MB unchanged

kickside/html2pdf imports wasi:filesystem, so it keeps the transform and has no change (34 ms).

Tests

  • TestHostRegistryMaySuspend.
  • go test ./runtime/wasm/... ./boot/components/runtime/wasm/... passes.
  • Smoke test with wippy test on the pdf2image (99 tests), html2pdf (33 tests) and doc2md suites: all pass.

Review corrections

  • Limit the synchronous shortcut to isolated function runtimes. Shared runtimes retain instrumentation even when the next entry omits a host registered by a sibling. A regression test fails under the original shortcut and passes with this restriction. The default retained-memory policy uses isolated component runtimes.
  • Simplify the registry-ID test helper to satisfy lint.

Dependency follow-up

The wazero update replaces per-call watcher goroutines with a reusable context.AfterFunc watch and doubles linear-memory backing capacity on growth, capped by the declared maximum. Review fixed nested background calls disabling the outer cancellation watch and serialized callback closure with watch reuse. Spare backing capacity can increase compared with Go’s append growth; guest-visible memory and admission limits are unchanged.

Validation includes the runtime WASM and boot WASM suites with the race detector against these exact production changes; all passed.

Every WASM function was compiled with the Asyncify transform as soon as
one registered host function was async, for example the blocking
output-stream flush that Rust std calls on its print and panic paths.
Stdio streams never suspend, so a function that imports only wasi:io,
wasi:cli, wasi:clocks and wasi:random paid for instrumentation it can
never use: about 1.5x slower calls and seconds of transform work on the
first load of a large module.

Host profiles now declare NeverSuspends, and the function manager
compiles a module synchronously when none of its imports can suspend.
Imports that can suspend (poll, filesystem, sockets, http, actor, funcs,
unknown) keep the transform.
skhaz added a commit to kicksideai/converters that referenced this pull request Nov 15, 2026
Rendering and encoding use WebAssembly SIMD: 1.30-1.45x faster per page,
with the same memory and allocations.

BREAKING CHANGE: the component needs a Wippy runtime whose Asyncify
transform supports SIMD (wippyai/wasm-runtime#22) and that runs
functions without Asyncify when no import can suspend
(wippyai/runtime#1087). Earlier runtimes fail to load it.
skhaz and others added 7 commits November 15, 2026 15:19
- wasm-runtime validates each signature TypeDef once per module instead
  of on every call: 266 -> 45 Go allocations per pdf2image page render
  together with the wazero change below.
- wazero watches call contexts with context.AfterFunc and a reused watch
  instead of a goroutine per call, and doubles linear memory capacity on
  growth instead of reallocating the whole memory about every 1.25x.

@wolfy-j wolfy-j left a comment

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.

Reviewed the synchronous WASM selection and final dependency composition. Restricted the shortcut to isolated runtimes; a red/green regression proves shared runtimes retain async imports registered by siblings. Pins merged wasm-runtime #22 and wazero #2, including SIMD suspend/rewind coverage and the repaired cancellation-watch lifetime. Runtime WASM and boot WASM suites pass under the race detector against these production changes; scoped vet passes. Final CI must be green before merge.

skhaz added a commit to kicksideai/converters that referenced this pull request Nov 15, 2026
…need

- page_resources counts the images each page paints without decoding
  them (unpdf ParseOptions::with_resource_counts).
- unpdf keeps simple ToUnicode bfrange entries as ranges. An identity CMap
  over the BMP, as html2pdf writes, was 65,536 strings per font.
- unpdf keeps image metadata on the page without a second copy of the
  image bytes.
- Pages that paint the same PDF object encode the image file once.
- Encoded image streams move into the output instead of being copied, and
  8-bit gray/RGB samples go to the PNG encoder without a copy.
- PNG uses zlib level 2 with the Up filter instead of level 6.
- Build the component with SIMD128.

BREAKING CHANGE: the component needs a Wippy runtime whose Asyncify
transform supports SIMD (wippyai/wasm-runtime#22) and that runs
functions without Asyncify when no import can suspend
(wippyai/runtime#1087). Earlier runtimes fail to load it.
skhaz added a commit to kicksideai/converters that referenced this pull request Nov 15, 2026
7148e3f removed SIMD128 so the component loads on released runtimes, and
17309bf replaced the document cache with a parse per request. Both are
restored, keeping the request bounds, the JPEG chroma fidelity test and
full-resolution chroma from 17309bf:

- SIMD128 renders and encodes 1.30-1.37x faster per page and per batch
  (8-page JPEG batch 262 -> 191 ms, 8-page PNG 317 -> 244 ms).
- The cache keeps only the last document of each pool instance, so the
  per-page and per-group calls of one document parse it and load its
  fonts once. Tests now check that repeated, failed and different
  requests never retain more than one document.

Add the SPDX headers the legal check requires on the files 17309bf added.

BREAKING CHANGE: the component needs a Wippy runtime whose Asyncify
transform supports SIMD (wippyai/wasm-runtime#22) and that runs
functions without Asyncify when no import can suspend
(wippyai/runtime#1087). Earlier runtimes fail to load it.
skhaz added a commit to kicksideai/converters that referenced this pull request Nov 15, 2026
c287682 removed SIMD128 so the converters load on released runtimes. SIMD
is restored to keep the faster builds; the -O3 html2pdf build and the
wippy/test lock update from c287682 stay.

BREAKING CHANGE: the components need a Wippy runtime whose Asyncify
transform supports SIMD (wippyai/wasm-runtime#22) and that runs functions
without Asyncify when no import can suspend (wippyai/runtime#1087).
Earlier runtimes fail to load them.
@wolfy-j
wolfy-j merged commit f8e2a57 into main Nov 15, 2026
21 checks passed
@wolfy-j
wolfy-j deleted the perf/wasm-function-speed branch November 15, 2026 20:04
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