Skip to content

epoll/kqueue: deferred-send path does ~1 epoll_wait per chunk, capping single-stream throughput (fairness vs batching) #525

Closed

Description

@lalinsky

Summary

With the single-owner socket registration backend (PR #511 / the persistent edge-triggered path), optimistic reads complete inline but optimistic sends are deferred (via the deferred-completion queue). Deferring the send turns every successful writeAll into a yield point — the fiber parks, the loop ticks, and the tick does a (zero-timeout) epoll_wait before processing the deferred completion. So a send-bound fiber pays ~1 epoll_wait per chunk.

This is the right default for request/response and for concurrent servers, but it caps throughput for a single bulk stream on one executor. It's a fairness-vs-batching trade, not a correctness bug — filing to track the options.

Evidence

One-directional stream, 1 executor, 1 connection, 16 KB chunks, 128 MB (tcp-pingpong-bench ... stream):

variant GiB/s
defer-send (current) 1.2
inline-send (experiment) 3.0

strace -c, 8192 chunks:

defer-send inline-send
sendto 8192 8254
epoll_pwait2 8199 (~1/chunk) 69 (total)

inline-send doesn't return to the loop per send: it runs send → send → … until one hits WouldBlock (socket buffer full), so a whole batch costs one epoll_wait. defer-send ticks the loop (→ epoll_wait) after every chunk.

The gap is bounded to the low-concurrency single-stream case. With 8+ connections or multiple executors it disappears, and inline-send actually regresses there (it monopolizes the executor during a send burst, starving other connections — the loop never ticks to service them). 64 KB-chunk streaming, current vs io_uring: ~2.9 vs 3.3 GiB/s at 1 conn, ~8.0 vs 7.7 GiB/s at 4 exec / 32 conn (already on par). So the default is correct under load; only the lone bulk stream pays.

Root cause

  • defer-send: a yield point per send → fair (loop services every other fd promptly) but a epoll_wait syscall per chunk.
  • inline-send: batches until backpressure → fast for one stream but monopolizes the loop (unfair), which is why it loses under concurrency and breaks the cooperative-fairness model.

Static op-type policy can't win both: ping-pong / concurrent servers want the per-send yield (fairness, and the sender is about to wait anyway); a lone stream wants to batch.

Options

  1. Skip the redundant zero-timeout poll when deferred completions are already pending (extend the should_poll = wait or loadInflight() > 0 gate in Loop.tick): if state.completions is non-empty, process them this tick and poll next tick — but poll at least every N ticks so a fiber that keeps producing ready completions can't starve I/O readiness. Keeps defer-send's fairness, removes the per-send epoll_wait. No NetSendFile change. Lowest risk; likely closes most of the gap.

  2. Count the already-woken / inline-completed waitForIo path toward the cooperative yield quota (Executor.maybeYield / yield_ready_threshold). Today an op that completes without blocking returns without ever yielding, so a burst of inline-completing ops never hits a yield point. If the already-ready path counted toward the quota, a send-bound fiber using the inline path would batch for throughput yet be forced to yield after N, bounding monopolization. Enables safely switching sends to inline. Requires NetSendFile made re-entrancy-safe (its child sends currently rely on deferral to avoid recursing/double-finishing).

  3. Adaptive per-fiber send budget: inline sends up to a budget, then defer/yield — explicit batching + fairness (Go goroutine-preemption / Tokio coop-budget style). Most general; also needs the NetSendFile fix.

  4. Do nothing. It's a bounded corner case (single/few bulk streams on one executor); concurrent servers, ping-pong, and bulk file→socket (NetSendFile, a separate op) are unaffected.

Notes

  • Applies to both epoll and kqueue (same defer-send-for-NetSendFile rationale, same per-send tick).
  • Related: the inline-vs-deferred completion choice for reads (completing optimistic reads inline was a ~20% ping-pong win); this is the mirror question for sends.
  • tcp-pingpong-bench has a stream mode for reproducing (-- <chunks> <chunk_bytes> <executors> <conns> stream).

Activity

  1. lalinsky commented on Jul 9, 2026

    @lalinsky
    OwnerAuthor

    Key part of this:

    • we need to always directly call callbacks

    • if needed to avoid re-entrancy, we can defer submissions that come from inside the event loop

    • we need to count already woken and yield as if the weather ask yielded, and count it towards the 61 limit, the same would apply for maybeYield

    • ideally, we have a thread doing ticks, so we have something like Go time windows, which would allow us quickly decide whether to yield or not

  2. lalinsky commented on Jul 16, 2026

    @lalinsky
    OwnerAuthor

    Already addresses

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions