Repository navigation
Description
What you did
bend within.bend -o within && ./within
What happened
pipeline done after 209 ms
10 ms timer fired after 209 ms
within(10) answered None after 209 ms
Two computations pass 2M values over a channel, a third runs IO.within(Unit, 10, IO.sleep(5000)), and a fourth sleeps 10 ms. The deadline and the timer both fire only when the pipeline ends. The JS lane does the same, later: within(10) answers after ~1264 ms under bun, and after ~1562 ms interpreted.
As far as I can tell, the loop consults the poller only when no step is runnable (if (io_runs == NULL) { ... io_wait(e); } in io_loop), and computations that wake each other on a channel keep io_runs from ever emptying, so every parked wait, timers included, waits for the whole chain. This is not a long pure computation: every step is short and is an effect. The guide says IO.within "answers None{} if the deadline wins", and here the deadline cannot win while any two computations exchange messages, which is a common shape for a server built on IO.fork and channels.
What I tried
In the emitted C only, not as a patch: every 64 turns, at most once every 10 ms, the loop takes a glance at the parked waits through one wait that is already due, so select does not block, and then goes on.
if ((n & 63) == 0 && io_park != NULL && io_tick() > look) {
look = io_tick() + 10000000;
io_wait_on(&io_glance, 0, 0, 1, io_none); // io_none answers IO_PARK
io_wait(e);
}| C, Linux x86-64 | main | with the glance |
|---|---|---|
within(10) answers, beside the 2M-round pipeline (3 runs each) |
209-210 ms | 10 ms |
| the 2M-round pipeline itself (3 runs each) | 209-210 ms | 213-214 ms |
a 1M-round pipeline beside 2,000 idle parked sockets, where io_wait walks every parked wait (medians of 7 interleaved runs) |
110 ms | 117 ms (+6.4%) |
I know bf745e3 made the loop poll only when no step is runnable, for a good reason: a poll every 64 steps cost 650 ms over 64,000 steps beside 2,000 parked sockets. The glance above is rate-limited to once every 10 ms, which keeps that walk to at most 100 a second and is where the 6.4% comes from; epoll/kqueue, which that commit left open, would change the cost again. So how the loop should look is your call. I'd be glad to turn whichever shape you prefer into a PR for both lanes, with this program as its test.
The file
# IO.within(10, ..) and a 10 ms timer beside two computations that pass
# 2M values over a channel: when does each answer?
import Base
def produce(+c: Chan(Nat), n: Nat) -> IO(Unit):
match n:
case 0n:
Chan.close(Nat, c)
case 1n+p:
+q = p
do IO<Unit>:
Bool <- Chan.send(Nat, c, q)
produce(c, q)
def since(t0: Nat, what: String) -> IO(Unit):
do IO<Unit>:
t1 : Nat <- IO.now()
IO.print(what ++ " after " ++ Nat.show(Nat.sub(t1, t0)) ++ " ms")
def alive(m: Maybe<&1, Nat>) -> Bool:
match m:
case None{}:
False{}
case Some{value}:
True{}
def consume(f: Nat, +c: Chan(Nat), +t0: Nat, live: Bool) -> IO(Unit):
match f live:
case 0n _:
IO.pure(Unit, Unit{})
case 1n+g False{}:
since(t0, "pipeline done")
case 1n+g True{}:
IO.bind(Maybe<&1, Nat>, Unit, Chan.recv(Nat, c), got => consume(g, c, t0, alive(got)))
def timer(t0: Nat, +ms: U32) -> IO(Unit):
do IO<Unit>:
Unit <- IO.sleep(ms)
since(t0, U32.show(ms) ++ " ms timer fired")
def within.got(t0: Nat, r: Maybe<&1, Unit>) -> IO(Unit):
match r:
case None{}:
since(t0, "within(10) answered None")
case Some{u}:
since(t0, "within(10) answered Some")
def within(t0: Nat) -> IO(Unit):
IO.bind(Maybe<&1, Unit>, Unit, IO.within(Unit, 10, IO.sleep(5000)), r => within.got(t0, r))
def main.run(+ch: Chan(Nat), +t0: Nat) -> IO(U32):
do IO<U32>:
Unit <- IO.spawn(Unit, within(t0))
Unit <- IO.spawn(Unit, timer(t0, 10))
Unit <- IO.spawn(Unit, produce(ch, 2000000n))
Unit <- consume(2000002n, ch, t0, True{})
return 0
def main.go(ch: Chan(Nat)) -> IO(U32):
IO.bind(Nat, U32, IO.now(), t0 => main.run(ch, t0))
def main() -> IO(U32):
IO.bind(Chan(Nat), U32, Chan.new(Nat, 0), main.go)bend --version
bend 2.0.32
uname -sm
Linux x86_64
clang --version (the first line)
clang version 22.1.8
Activity
A deadline or a timer fires while computations keep the loop busy (#1122
Bend 2.0.35: M1 and M2 run the GPU again, a library's names stay its own