# std: make stack unwinding faster on macOS · gitcafe/zig

[View on GitCafe](https://git.cafe/gitcafe/zig/commit/65922a2d4319ca2e9f3e67650d018e269d018a77)

Repository: [gitcafe/zig](https://git.cafe/gitcafe/zig)

Visibility: public

Requested revision: 65922a2d4319ca2e9f3e67650d018e269d018a77

Requested commit: 65922a2d4319ca2e9f3e67650d018e269d018a77

Commit: 65922a2d4319ca2e9f3e67650d018e269d018a77

Tree: 41aa53f75c76a514500ac1bdea96c267487ac41d

Author: Matthew Lugg

Committer: Matthew Lugg

## Message

```
std: make stack unwinding faster on macOS

https://github.com/ziglang/zig/issues/26027#issuecomment-3571227050
tracked some bad performance in `DebugAllocator` on macOS down to a
function in dyld which `std.debug.SelfInfo` was calling into. It turns
out `dladdr`'s symbol lookup logic is horrendously slow (looking at its
source code, it appears to be doing a *linear scan* over all symbols in
the image?!). However, we don't actually need the symbol, so we want to
try and avoid this logic.

Luckily, dyld has more precise APIs for what we need! Unluckily, Apple,
in their infinite wisdom, decided they should be deprecated in favour of
`dladdr`, despite the latter being several times slower (and by "several
times", I have measured a 50x slowdown on repeated calls to `dladdr`
compared to the other API). But luckily again, the deprecated APIs are
still exposed.

So, after a careful analysis of the situation (reading dyld code and
cursing Apple engineers), I think it makes sense to just use these
deprecated APIs for now. If they ever go away, we can write our own
cache for this data to bypass Apple's awfully slow code, but I suspect
these functions will stick around for the foreseeable future.

Uh, and if `_dyld_get_image_header_containing_address` goes away,
there's also `dyld_image_header_containing_address`, which is a
seemingly identical function, exported by dyld just the same, but with a
separate (functionally identical) implementation, and not documented in
the public header file. Apple work in mysterious ways, I guess.

```

## Parents

- [621e1d7b1e560901e2a5cbdca39f173f6592a004](https://git.cafe/gitcafe/zig/commit/621e1d7b1e560901e2a5cbdca39f173f6592a004?format=markdown)

[Source at this commit](https://git.cafe/gitcafe/zig/tree/65922a2d4319ca2e9f3e67650d018e269d018a77?format=markdown)
