Skip to content

perf: memory map cached wasm plugins on unix - #1318

Merged
dsherret merged 2 commits into
mainfrom
perf-mmap-cached-wasm-plugins
Oct 10, 2026
Merged

dsherret merged 2 commits into
mainfrom
perf-mmap-cached-wasm-plugins

Conversation

@dsherret

@dsherret dsherret commented Oct 10, 2026 •

Copy link
Copy Markdown
Member

On unix, loads a cached compiled wasm plugin with wasmtime's Module::deserialize_file, which memory maps the .cwasm and pages it in lazily, instead of reading the whole file and copying it into executable memory. If mapping fails (for example a cache directory on a noexec mount), it falls back to reading the file as before.

Windows keeps reading the file. It doesn't allow replacing or deleting a file that another process has mapped, so mapping there would let a running dprint lsp/editor-service (or any concurrent dprint run) block clear-cache and recompiling a plugin. An earlier revision of this PR enabled it on Windows and killed those processes when a delete or replace failed; that was removed because the gain there is smaller (about 7 ms of 22) and not worth the complexity.

Measurements

Warm plugin cache, hyperfine means, Ubuntu under WSL2 on a 32-core machine. "5 plugins" is typescript, json, markdown, toml and dockerfile.

Scenario Before After
typescript only, check one file 15.6 ms 4.1 ms
5 plugins, check four small files 19 ms 5.5 ms
5 plugins, output-resolved-config 19 ms 4.4 ms
check this repo, no incremental 909 ms 946 ms

The first row is from the final revision. The other rows were taken with an earlier revision that had the same load path on Linux. The whole-repo run is within noise, so this is a fixed saving per invocation.

Safety

deserialize_file requires that the file not change while the module is alive. The cache only replaces a .cwasm by writing a temp file and renaming it over, or deletes it, and on unix both leave an existing mapping of the old file intact. Checked on Linux:

  • renaming over the artifacts while another dprint process had them mapped succeeded, and that process finished normally
  • a truncated, empty or random-bytes artifact is recompiled and the run exits 0

Not covered

  • The mapped load path isn't exercised by unit tests because the test environment's file system is in memory.
  • The noexec fallback was not tested on a real noexec mount.
  • macOS is untested.

Loads a cached compiled wasm plugin by memory mapping the file instead of reading it, falling back to reading when mapping fails. Windows doesn't allow replacing or deleting a mapped file, so when that fails for a cached plugin, kill the dprint lsp and editor-service processes using the same cache directory and try again.
Windows doesn't allow replacing or deleting a mapped file, and working around that by killing other dprint processes isn't worth it for the smaller gain there.
@dsherret dsherret changed the title perf: memory map cached wasm plugins perf: memory map cached wasm plugins on unix Oct 10, 2026
@dsherret
dsherret merged commit 4d93776 into main Oct 10, 2026
18 checks passed
@dsherret
dsherret deleted the perf-mmap-cached-wasm-plugins branch October 10, 2026 19:16
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