Simplify the input wake path

These three changes are worth doing because they shorten the wake
decision and remove a second opinion about what is small.

A child exit no longer wakes on its own. The next iteration queues the
child and wakes the main thread, so the old child_closed branch was a
second wake and a second copy of the rate-limit check.

vt_parser_commit_write() no longer returns whether the pending bytes
are small. Nothing read that bool, and the real check is
vt_parser_input_wake().

A small write that already used its early wake, and a signal, share one
rate-limit deadline. The sooner of that slot and a large chunk's own
deadline still wins.
This commit is contained in:
Jesse Miller 2026-10-10 16:10:51 -06:00
parent 17dd8e47ea
commit 2bcb49aa04
3 changed files with 14 additions and 26 deletions

View file

@ -1876,7 +1876,7 @@ io_loop(void *data) {
// The I/O thread loop
size_t i;
int ret;
bool has_more, child_data_received, child_closed, has_pending_wakeups = false, last_wakeup_was_early = false;
bool has_more, child_data_received, has_pending_wakeups = false, last_wakeup_was_early = false;
// Latched until WAKEUP. signalfd is drained on receipt, so a later poll cannot see it again.
bool signal_pending = false;
monotonic_t last_main_loop_wakeup_at = -1, now = -1, pending_wake_at = 0;
@ -1911,7 +1911,6 @@ io_loop(void *data) {
WAKEUP;
}
child_data_received = false;
child_closed = false;
for (i = 0; i < self->count + EXTRA_FDS; i++) children_fds[i].revents = 0;
for (i = 0; i < self->count; i++) {
screen = children[i].screen;
@ -1949,8 +1948,7 @@ io_loop(void *data) {
child_data_received = true;
has_more = read_bytes(children_fds[EXTRA_FDS + i].fd, children[i].screen);
if (!has_more) {
child_closed = true;
// child is dead
// child is dead. The next iteration queues it and wakes the main thread.
children_mutex(lock);
children[i].needs_removal = true;
children_mutex(unlock);
@ -1985,7 +1983,7 @@ io_loop(void *data) {
// on some platforms, such as cocoa. Small pending input, typically the echo of typed
// characters, wakes immediately, but at most once per input_delay so that continuous
// streams of small writes are still coalesced.
if (child_data_received || signal_pending || child_closed || has_pending_wakeups) {
if (child_data_received || signal_pending || has_pending_wakeups) {
now = monotonic();
ParserInputWake pending = pending_input_wake_info(self);
// rate_open is the gap since the previous wakeup. A large chunk the
@ -1995,6 +1993,7 @@ io_loop(void *data) {
bool rate_open = now - last_main_loop_wakeup_at > OPT(input_delay);
bool wake_now = pending.large_ready;
bool early_small = false;
bool need_rate_slot = false;
monotonic_t wait_until = pending.large_held_until > now ? pending.large_held_until : 0;
if (pending.small_pending) {
if (rate_open) wake_now = true;
@ -2003,21 +2002,16 @@ io_loop(void *data) {
// This wake is the one early small wake unless a large chunk
// was ready anyway. Marking that would swallow the next echo.
early_small = !pending.large_ready;
} else {
// The early wake was already used. Take whichever comes first,
// the coalesce slot or a large chunk's own deadline.
monotonic_t rate_at = last_main_loop_wakeup_at + OPT(input_delay);
if (!wait_until || rate_at < wait_until) wait_until = rate_at;
}
} else need_rate_slot = true;
}
// A signal, or a child that exited with nothing left to hold, uses the
// same rate limit as a small write. The queued child is woken separately.
if (signal_pending || (child_closed && !pending.large_held_until)) {
// A signal uses the same rate limit as a small write that already woke early.
if (signal_pending) {
if (rate_open) wake_now = true;
else {
monotonic_t rate_at = last_main_loop_wakeup_at + OPT(input_delay);
if (!wait_until || rate_at < wait_until) wait_until = rate_at;
}
else need_rate_slot = true;
}
if (need_rate_slot) {
monotonic_t rate_at = last_main_loop_wakeup_at + OPT(input_delay);
if (!wait_until || rate_at < wait_until) wait_until = rate_at;
}
if (wake_now) {
WAKEUP;

View file

@ -1647,13 +1647,9 @@ vt_parser_create_write_buffer(Parser *p, size_t *sz) {
return ans;
}
bool
void
vt_parser_commit_write(Parser *p, size_t sz) {
// Returns true if the pending input is small. Only uses fields modified
// with the lock held, so it overestimates while a parse is in progress,
// since read.sz includes input that is being parsed.
PS *self = (PS *)p->state;
bool pending_is_small;
with_lock {
size_t off = self->read.sz + self->write.pending;
// 0 means no unparsed input is waiting. monotonic() can return 0 at startup.
@ -1665,10 +1661,8 @@ vt_parser_commit_write(Parser *p, size_t sz) {
if (self->write.offset > off) memmove(self->buf + off, self->buf + self->write.offset, sz);
self->write.pending += sz;
self->write.sz = 0;
pending_is_small = self->read.sz + self->write.pending < SMALL_PENDING_INPUT_THRESHOLD;
}
end_with_lock;
return pending_is_small;
}
bool

View file

@ -45,7 +45,7 @@ void reset_vt_parser(Parser *);
// The following are thread safe, using an internal lock
uint8_t *vt_parser_create_write_buffer(Parser *, size_t *);
bool vt_parser_commit_write(Parser *, size_t);
void vt_parser_commit_write(Parser *, size_t);
bool vt_parser_has_space_for_input(const Parser *);
ParserInputWake vt_parser_input_wake(const Parser *);
void parse_worker(void *p, ParseData *data, bool flush);