Repository navigation
A deadline or a timer fires while computations keep the loop busy (#1122) - #1245
Merged
Merged
Conversation
…e loop glances at its waits when a timer is due and every 10 ms (bendlang#1122) Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Context
io_looponly callsio_wait, which checks parked timers and sockets, whenio_runsis empty. Computations that wake each other over a channel keepio_runsfrom ever emptying. So every parked wait (IO.sleep,IO.within's deadline, a socket's readiness) waits for the whole chain to end.In the issue's program,
within(10)and a 10 ms timer both fire only when a 2M-round channel pipeline ends: at ~100–130 ms in C and ~510 ms in JS. The JS lane has the same loop. Fixes #1122.Change
The loop glances at its waits while it is busy. It reads the clock every 64 steps and calls
io_waitwithout blocking when either:io_parkkeeps deadlines sorted, so this check is O(1); orselectwalks every parked fd.io_waittakes back ablockflag, which bf745e3 removed; with it unset, the poll's timeout is 0. The JS lane does the same withio.waits[0].at. The rate limit is there because of bf745e3: polling every 64 steps cost 650 ms over 64,000 steps beside 2,000 parked sockets. This is the shape the reporter proposed in the issue, with the timer check added so a timer isn't up to 10 ms late.Other runtimes bound starvation the same way: Go's
sysmonpolls the network when nothing has for 10 ms, and Tokio checks timers and I/O every 61 scheduler ticks (event_interval). Ordering runnable steps and timers in one priority queue would be exact for timers, but it needs a clock read per step; the check every 64 steps gives the same order to within 64 steps. A queue can't order sockets at all: their readiness is only known by polling, so they need a rate either way.tests/io/within_busy.bendracesIO.within's 1 ms deadline against a 500k-round channel pipeline and expects the deadline to answer first. Onmainthe interpreter, JS and C all print the pipeline first.Checks
On an Apple M-series Mac, all on this PR's head against its base (607a095). For the benchmarks the machine was quiet (load 1.0–3.8), and each run alternated base and branch.
The issue's program:
within(10)and the 10 ms timer answer at 10–11 ms in C (base: 122–138 ms) and 10–14 ms in JS (base: 502–506 ms). The pipeline itself takes the same time: 124–130 ms against 122–137 ms in C, and 503–509 ms against 496–500 ms in JS.Echo server (
demos/io_tcp_echos, a C load client):Cost of the glance: a 1M-round pipeline beside 2000 parked TCP receives runs in 47–49 ms, against 47–50 ms on base. Base never glances there.
Tests: all 1,530 tests under
tests/ran on both trees. Each went through the interpreter, and the 688 that build also ran in C and JS.#|lines. The other 8 are the same on base:gfx/clicksandgfx/windowexpect no display, and this Mac has one;select_eintr,tcp_connect_black_holeandtcp_send_slow_peerend with an extra blank line, which the gate trims.Size:
comp.tsgoes from 63,213 to 63,406 ttok (+193).Static checks:
bun gates/repo.tspasses (50/50).tscreports no errors incomp.ts, and the same 12 elsewhere as on base.Not run: the cluster gates, and the Linux lane.
Alternative measured
kqueue/epoll instead of
select, with the same glance, removes the O(parked) cost of each wait:fstatbecause kqueue misses a FIFO's close on macOS (macOS: poll() does not report a FIFO close, so a parked read never sees EOF #928).That is a separate decision about Bend as a server for many idle clients, so it is not part of this PR.
🤖 Generated with Claude Code