Skip to content

perf: exit instead of unwinding after a successful run - #1388

Merged
dsherret merged 1 commit into
dprint:mainfrom
zachbornheimer:perf-skip-wasm-teardown
Oct 22, 2026
Merged

dsherret merged 1 commit into
dprint:mainfrom
zachbornheimer:perf-skip-wasm-teardown

Conversation

@zachbornheimer

Copy link
Copy Markdown
Contributor

Closes #1387

Exits the process once run() succeeds instead of returning from main. The error arm and the language server already do this.

Returning from main frees every loaded Wasm module. On macOS wasmtime deregisters each function's unwind info from libunwind one at a time while doing that, so with 12 plugins it took 435 ms of a 570 ms single-file run (0, 9, 56 and 435 ms at 1, 3, 6 and 12 plugins). Everything a run has to do is finished inside run() before this point: files are written as they are formatted, the incremental cache is saved by the command, output is written and plugins are shut down by clear_and_shutdown_initialized().

Measurements

macOS arm64, 12 plugins, release builds, CPU time of the process (user + sys) so a busy machine doesn't skew it, interleaved rounds, medians:

run before after
fmt --stdin, one file (25 rounds) 1081 ms 114 ms
fmt whole repo, 3217 files, already formatted (6 rounds) 7336 ms 6319 ms

On an idle machine, wall time, medians of 4 interleaved rounds: whole repo 4.81 s -> 3.82 s (unformatted) and 3.07 s -> 2.04 s (formatted); single file 643-923 ms -> 58-180 ms.

Linux (aarch64, glibc, in Docker) shows no difference (single file 60 ms -> 62 ms wall, whole repo 2850 ms -> 2831 ms). Windows wasn't measured.

Test

tests/fast_exit.rs runs the built binary and checks what has to be finished before the process exits: the formatted file and an incremental cache entry are on disk, --stdin output without a trailing newline is complete, and a run with nothing to format exits 0. Exiting just before the incremental cache is written (incremental_file.write()) makes the first test fail, so it fails if that work ever moves into a destructor.

Checked

  • stdout, stderr, exit code and --log-level debug output of a single-file run are the same before and after.
  • With the exec process plugin the plugin process is gone after the run, and the final terminal output on a TTY is byte-identical.
  • cargo fmt --check and cargo test -p dprint --test fast_exit pass. cargo clippy -p dprint --all-targets reports nothing in the changed files; it fails on main already at commands/editor/mod.rs:1249 (reversed_empty_ranges).

Not checked

  • Windows.
  • A progress bar mid-run on a TTY.
  • Only the dprint fmt/check paths were run; other successful commands take the same exit.

On macOS, returning from `main` frees every loaded Wasm module, and wasmtime
deregisters each function's unwind info from libunwind one at a time while
doing it. With 12 plugins that took 435 ms of a 570 ms single-file run, and it
grows faster than linearly with the plugin count (0, 9, 56 and 435 ms at 1, 3,
6 and 12 plugins).

The error arm and the language server already end the process with
`std::process::exit`. Do the same on success. Everything a run has to do is
finished inside `run()`: files are written as they are formatted, the
incremental cache is saved by the command, output is written and the plugins
are shut down. Only the process teardown is skipped.

Adds tests/fast_exit.rs, which runs the built binary: the formatted file and an
incremental cache entry exist after the process exits, stdout without a
trailing newline is complete, and a run with nothing to format exits 0.
Exiting just before the incremental cache is written makes the first test
fail.

@dsherret dsherret left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Nice catch! Thanks!

I'll look into ways of catching this in the future to ensure it doesn't regress. This previously used to happen on Windows too.

@dsherret
dsherret merged commit 57d3f6d into dprint:main Oct 22, 2026
zachbornheimer added a commit to zachbornheimer/dev-profile that referenced this pull request Oct 23, 2026
dprint/dprint#1388 (our exit-after-success patch) was merged and then reverted by #1391, which fixes the same macOS cost at its source by not generating native unwind info for wasm plugins (0.64.2). Measured on this machine: single-file stdin format 19 ms on 0.64.3 against 72 ms on the fork, whole-repo fmt equal. Upstream ships Linux and Windows assets, so the per-OS pin (cc4fb6e) is no longer needed and is reverted with the fork pin.
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.

perf: successful runs spend most of their time freeing Wasm modules on macOS

2 participants