Skip to content

Darwin: 6x faster throw/catch; @serial test cases build in parallel - #649

Merged
blacktrs merged 2 commits into
mainfrom
macos-fast-unwind
Nov 11, 2026
Merged

blacktrs merged 2 commits into
mainfrom
macos-fast-unwind

Conversation

@blacktrs

@blacktrs blacktrs commented Nov 11, 2026 •

Copy link
Copy Markdown
Contributor

Part of #648.

Problem

On macOS a caught PHP throw cost ~20 µs (php: 0.3–0.6 µs, Linux build: 1.35 µs). sample shows ~70% inside dyld: the system _Unwind_RaiseException / _Unwind_Resume start from a frame in libunwind.dylib, and dyld resolves shared-cache addresses by walking every cached image — once per throw and again per cleanup pad.

Change (Darwin only; Linux untouched)

  • @__mc_unwind runs the unwind through libunwind's unw_getcontext / unw_step / unw_resume starting in our image.
  • One pass instead of Itanium's two: per frame the personality is asked to search, then immediately installs a handler (CLEANUP|HANDLER_FRAME) or the frame's cleanup pad. A PHP throw runs every unwound frame's cleanups caught or not, so the search phase only chose where to stop. End of stack = the uncaught fatal.
  • Cleanup pads end in @__mc_resume (foreign or forced unwinds fall back to _Unwind_Resume).
  • The cursor is zeroed before unw_init_local: unw_resume pops the cursor's walked-frame count off the return-address stack, and stack garbage there spun for billions of iterations.

Result

throw+catch, macOS arm64: 19.6 µs → 3.1 µs. The remaining cost is dyld's lookup of our own image per step; the error-return model (design on branch exc-error-return) removes it on both platforms.

Testing

  • 38 cases that hung before the cursor fix (catch/unwind/dtor/async/intl/…) pass.
  • Compiler rebuilt by itself (its own throws go through the new path).
  • Full suite left to CI (local machine was saturated).

Also: faster test runner

  • tests/aot/run.sh: @serial cases are now compiled in the parallel pool (MC_PHASE=compile); only their measurement runs alone (MC_PHASE=run). Full macOS suite 472 s → 458 s, same verdicts.
  • The four throw-path leak tests added for A spread into a variadic function that throws leaks the pack built for the call. #647 drop from 100–200k to 20k iterations (threshold 5 MB); verified they still report LEAK on 0.18.0, which leaks on those paths.

…image, one pass

The system _Unwind_RaiseException/_Unwind_Resume start from a frame inside
libunwind.dylib, an address dyld resolves by walking every image of the shared
cache: ~20 us per throw and again per cleanup pad. The runtime now drives the
unwind itself (unw_getcontext/unw_step/unw_resume), asking each personality to
search and then to install a handler or a cleanup pad in the same pass; cleanup
pads end in @__mc_resume. One pass is enough because a PHP throw runs every
unwound frame's cleanups whether or not it is caught. The cursor is zeroed
first: unw_resume pops the cursor's walked-frame count off the return stack.
Throw+catch on macOS arm64: 19.6 us -> 3.1 us. Linux is unchanged.
…nt alone; cut the throw-leak tests to 20k iterations

A @serial case needs a quiet machine for its RUN, not for its build: the runner
now compiles it with the others (`@c:<name>`, MC_PHASE=compile) and the serial
loop only runs the binary (MC_PHASE=run). The four throw-path leak tests added
for #647 did 100-200k throws (9-13 s each at today's cost of a throw, #648);
20k iterations with a 5 MB threshold still catch the leak they guard.
@blacktrs blacktrs changed the title Darwin: 6x faster throw/catch — drive the unwind from our own image Darwin: 6x faster throw/catch; @serial test cases build in parallel Nov 11, 2026
@blacktrs
blacktrs merged commit bc10008 into main Nov 11, 2026
49 checks passed
@blacktrs
blacktrs deleted the macos-fast-unwind branch November 11, 2026 13:40
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