Semantics and tuning
Details behind the short list in the README: threading, backpressure, scheduling and placement on each platform, and the measurements that set the defaults.
- Threads. Handlers run on the context's I/O threads.
on_messagefor one socket never runs concurrently with itself. Messages from one sending thread arrive in order; messages from different threads arrive whole, never interleaved. - Receiving. Reads start when you first call
on_message. Until then, incoming data waits in the kernel. - Backpressure.
send()never blocks. It returnsTooLargeabove the connection's message size limit (options::max_message_size, default 64 MiB; the smaller of both peers' values applies). It returnsWouldBlockwhen more thanoptions::max_queued_bytes(default 64 MiB) is waiting. A message is either queued whole or rejected. Split very large payloads yourself. - Closing.
close()drops unsent messages and fireson_closeonce, on an I/O thread. Destroying your lastshared_ptrto a socket or server closes it too. That waits for a handler that is currently running on another thread, and no handler of that object runs afterwards (on_closedoesn't fire either). So it's safe to destroy what your handlers capture right after the handle. - Contexts. All sockets share one process-wide context (I/O threads) by default. Create your own with
datapath::context::create({spin, io_threads})and pass it toconnect/hostfor separate tuning. Destroying a context closes everything on it. - Handshake timeout. A server gives each connecting client
options::handshake_timeout(default 5 s, 0 = no limit) to send its hello. A client that stays silent, or sends only part of it, is disconnected, so it cannot hold amax_clientsslot or a pipe/socket handle forever. The clock stops once the hello is in: a client waiting foron_accept(no handler set yet) is not affected. The deadlines live on one thread per context that starts with the first connection and sleeps until the next deadline, so the I/O threads never check a clock. - Windows scheduling. I/O threads run at
THREAD_PRIORITY_HIGHEST(context_options::priority, 0–3). Under full CPU load, normal priority let single round trips stall for up to 12 ms; highest kept p99.9 under 100 µs.context_options::mmcss(registering with the Multimedia Class Scheduler) is off: on a busy system MMCSS throttles threads that use their CPU share, which measured as a fixed ~2 ms p99.9 stall, and on an idle system it made no difference.yieldandalertableshowed no consistent gain and are off. - Linux scheduling. The same
context_options::priority(default 2) lowers each I/O thread's nice value by 0, 4, 12 or 19 from the one it inherited. Unprivileged processes only get as far asRLIMIT_NICEallows, which is often not at all;CAP_SYS_NICElifts the limit. Under full CPU load, a 12-step gap took 64 B p99.9 from about 8 ms to about 200 µs. No real-time scheduling policy is used. - macOS scheduling. macOS picks threads, performance or efficiency cores and timer coalescing by quality-of-service class, so
priority1 setsQOS_CLASS_USER_INITIATEDand 2 and 3 setQOS_CLASS_USER_INTERACTIVE(the class of a UI's main thread). No privileges are needed. Real-time (time constraint) threads are not used: the kernel demotes one that spins past its computation budget.io_threads = 0starts one I/O thread per physical core (hw.physicalcpu, which counts both core types on Apple silicon). - Core placement (Windows). By default (
context_options::ideal_core = -1) each I/O thread gets its own physical core as a soft hint (ideal processor, not hard affinity), never two on the hyper-threads of one core, and core 0, which handles most interrupts, is skipped when there are more than two cores. Core 0 is avoided with real thread affinity (within the process's own affinity), because under full CPU load Windows ignored the hint and ran I/O threads there, at about 1.1 ms instead of 0.2 ms p99.9 for 16 KiB.context_options::pair_peer(opt-in) goes further for a 1:1 link: when both the client's and the server's context set it (each withio_threads = 1), the handshake puts the two I/O threads on the two hyper-threads of one physical core, pinned. They then share that core's L1/L2 and no other thread runs there; under full CPU load (--stress 0 --pair, 4 unpinned runs, each landing on one core's two hyper-threads) 16 KiB p99.9 measured 13–14.5 µs and 64 KiB 22–33 µs, against 229 and 457 µs on separate cores, about the same as idle. 256 KiB still suffers under load (p99.9 0.57–2 ms vs 87 µs idle). Only the first such connection per context is paired, other connections on that context share the pinned core, and the pinning ends when that connection closes. A peer without the option, or with an older library, connects as before. A busy hyper-thread sibling roughly halves the speed of the other one; on the test machine this cut 64 KiB p99.9 from 72 to 52 µs and 8 B from 12 to 7 µs.-2leaves placement to the OS,Nstarts at physical core N.io_threads = 0starts exactly one I/O thread per physical core (on Windows, the cores this placement uses), so no two I/O threads ever share a core's hyper-threads. - Core placement (Linux, macOS). Not managed yet: the scheduler places the I/O threads. For large messages placement is the main factor; on a two-CCD EPYC, 1 MiB over shared memory measured 329 µs p99.9 with client and server on different CCDs and 102 µs on one CCD (memory bandwidth and placement).
- Shared memory (Windows, Linux). On by default since 1.1.0:
options::shared_memory(both ends must have it) moves messages through two shared-memory rings (options::shared_memory_sizeper direction, default 4 MiB) instead of the pipe or socket, which then only carries the handshake and notices when the peer is gone. Only the two processes can map the rings. An idle receiver spins forspin, then waits on an event (Windows) or eventfd (Linux). A peer with it turned off (including 1.0.x, where it was off by default), an older library or macOS connects over the pipe or socket.DATAPATH_SHARED_MEMORY=1or0in the environment overrides the setting for every connection in a process. On Windows the rings are created as global objects when the server may (services can), else in its logon session; a client that can't open them (another session, say) connects again over the pipe without them. - Each handler gets a private copy. By default a message is copied out of the ring before
on_messagesees it, so a hostile or broken peer can't change the bytes while the handler reads them (a peer writing during the copy can only produce a message it could also have sent).options::shared_memory_zero_copyskips that copy for same-user peers you trust, andsocket::set_zero_copy(true)switches it on for a live connection, e.g. once your own handshake has authenticated the peer; messages from another user are always copied. Each side decides for what it receives. The Node.js and Python bindings copy every message into aBuffer/bytesanyway and turn it on themselves. - Cost. Each connection holds 2 ×
shared_memory_sizeof rings. On Windows the section is committed when the connection is made, so a server with 1,000 clients at the default size adds about 8 GB of commit charge; lowershared_memory_sizeor turn shared memory off for servers with many mostly idle clients. On Linux pages are only used once touched, and each connection has 5 more file descriptors (a memfd and four eventfds). - Buffers.
options::receive_buffer(256 KiB) holds incoming frames; reads adapt their size to the traffic, so a large buffer doesn't slow down small messages.options::pipe_buffer(1 MiB) is the Windows kernel pipe buffer, and on macOS the kernel socket buffer of each connection (the macOS default is 8 KiB). Linux keeps its default socket buffers. - Tuning.
context_options::spin(default 50 µs) is how long an I/O thread busy-polls after the last event before sleeping in the kernel. Higher values cut tail latency for bursty traffic and cost CPU only while traffic flows.0disables spinning. Spinning only helps while spare CPUs exist: when spinning I/O threads cover every CPU (for example client and server on one machine, both withio_threads = 0), a woken thread waits for a spinner's budget to run out, and on Linux 64 B p99.9 measured about 116 µs with the default spin versus 23 µs with0.context_options::yieldalso fixes that case (24 µs) but gives a timeslice away on every yield under full CPU load. On Windows the I/O threads also opt out of EcoQoS power throttling. - Paths. A bare name maps to
\\.\pipe\<name>on Windows, to the abstract socketdatapath.<name>on Linux, and to the socket file/tmp/datapath.<name>on macOS, which has no abstract namespace (/tmprather than the per-user$TMPDIR, so other users can still reach aGrouporWorldserver). On Linux and macOS, a path starting with/is a filesystem socket, created with mode0600/0660/0666forUser/Group/World(Groupis the server's group) and removed on close. On macOS a server whose backlog is full refuses connections like a missing server does, soconnect()keeps retrying while the socket file exists and returnsTimedOutatconnect_timeout.