mitchellh / libxev

libxev is a cross-platform, high-performance event loop that provides abstractions for non-blocking IO, timers, events, and more and works on Linux (io_uring or epoll), macOS (kqueue), and Wasm + WASI. Available as both a Zig and C API.
MIT License
2.15k stars 77 forks source link

Don't use SEND_ONCE mach rights for async, use a queue limit of 1 #102

Closed mitchellh closed 5 months ago

mitchellh commented 5 months ago

SEND_ONCE allocates a mach port with a SEND right exactly once and deallocates it automatically once the message is read. This is totally wasteful because we have a perfectly good mach port already... This commit adds the send right to our existing mach port and reuses it for each send. This avoids any port allocations.

We additionally call mach_port_set_attributes to set the queue limit for the mach port to 1. We don't need to allocate and queue a bunch of messages when all we need is 1 to wake up. This should speed up notifications AND waiting because draining will only ever drain 1.

Related bug for those with access: https://github.com/ghostty-org/ghostty/issues/1836