Skip to content

Throwing and catching an exception is ~44× slower than php (20 µs vs 0.47 µs) #648

Closed

Description

@blacktrs

Problem

Throwing and catching an exception costs ~44× what php pays.

function f(int $i): int { if ($i >= 0) { throw new RuntimeException('x'); } return $i; }
function g(int $i): int { return f($i) + 1; }
$n = 200000; $c = 0;
$t = hrtime(true);
for ($i = 0; $i < $n; $i++) { try { g($i); } catch (RuntimeException $e) { $c++; } }
printf("%.2f us/throw\n", (hrtime(true) - $t) / 1e3 / $n);
per throw
php 8.5 0.47 µs
manticore (-O2, macOS arm64, shared-generics b451934) 20.5 µs

Why it matters

Exceptions are ordinary control flow in real applications: Symfony routing throws NotFoundHttpException for every 404, Laravel validation throws ValidationException, Doctrine and Guzzle throw on expected failures. A request path that throws a handful of times pays tens of microseconds each.

It is also the single largest cost in the AOT suite's serial phase: the throw-path leak tests spend 9–14 s each in 200k throws.

Expected

A throw/catch within a few frames costs on the order of php's (single-digit µs at most).

Notes

  • Zero-cost exceptions unwind through the platform unwinder (two-phase search + cleanup per frame); every frame between throw and catch with a landing pad is visited twice, and the personality runs per frame. Backtrace capture on construction may add to it.
  • Linux not measured yet.

Activity

  1. added
    bug
    on Nov 11, 2026
  2. blacktrs commented on Nov 11, 2026

    @blacktrs
    ContributorAuthor

    Measured on Linux arm64 (Ubuntu LTS VM, manticore 0.18.0 release): 1.35 µs per throw, flat from a same-function catch to five frames deep — 2-4× php, not 44×. The 20 µs is macOS-only.

    macOS cause (sample): every throw starts unwinding inside the system _Unwind_RaiseException, i.e. at a frame of libunwind.dylib in the dyld shared cache. libunwind asks _dyld_find_unwind_sections for that address and dyld4 resolves shared-cache addresses with findImageMappedAt → forEachImageTextSegment, a linear walk over the cache's images. ~70% of the time is that lookup; it happens again in every _Unwind_Resume a cleanup pad calls.

    Fix direction for macOS: raise and resume through libunwind's unw_getcontext/unw_init_local/unw_step API from our own image, calling __mc_personality for both phases ourselves, so no step ever starts inside the shared cache; cleanup pads end in our resume instead of _Unwind_Resume. Linux keeps the libgcc path.

  3. added 2 commits that reference this issue on Nov 11, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions