From b6694dd51c7be36637f0af9effdd507c6abc391c Mon Sep 17 00:00:00 2001 From: Jesse Miller Date: Sat, 10 Oct 2026 16:02:17 -0600 Subject: [PATCH] Wake once per large chunk, and do not count a scanned tail as new input A large chunk stayed large_ready until the parser cleared its stamp, so every later read woke the main loop again. Record the stamp on wake and ignore that chunk until the parser replaces it. Bytes already scanned and left in the buffer were counted with the next write. A short write after a large tail waited out input_delay, while the parser would have taken it immediately. The stamp now stores the buffer size at that moment, and only bytes committed after it count. --- kitty/child-monitor.c | 17 ++++++++++++++++- kitty/vt-parser.c | 14 ++++++++++---- kitty/vt-parser.h | 4 +++- 3 files changed, 29 insertions(+), 6 deletions(-) diff --git a/kitty/child-monitor.c b/kitty/child-monitor.c index d8b6fd65e..1bf722b02 100644 --- a/kitty/child-monitor.c +++ b/kitty/child-monitor.c @@ -75,6 +75,8 @@ typedef struct { unsigned long id; pid_t pid; int exit_status; + // new_input_at last woken for. A later read of the same chunk must not wake again. + monotonic_t woken_for_input_at; } Child; static const Child EMPTY_CHILD = {0}; @@ -1847,12 +1849,24 @@ pending_input_wake_info(ChildMonitor *self) { for (size_t i = 0; i < self->count; i++) { ParserInputWake one = vt_parser_input_wake(children[i].screen->vt_parser); if (one.small_pending) info.small_pending = true; - if (one.large_ready) info.large_ready = true; + // The main thread clears input_at only when it finishes this chunk. + if (one.large_ready && one.input_at != children[i].woken_for_input_at) info.large_ready = true; if (one.large_held_until && (!info.large_held_until || one.large_held_until < info.large_held_until)) info.large_held_until = one.large_held_until; } return info; } +static void +remember_woken_large_input(ChildMonitor *self) { + for (size_t i = 0; i < self->count; i++) { + ParserInputWake one = vt_parser_input_wake(children[i].screen->vt_parser); + if (!(one.large_ready && one.input_at)) continue; + children_mutex(lock); + children[i].woken_for_input_at = one.input_at; + children_mutex(unlock); + } +} + static void * io_loop(void *data) { // The I/O thread loop @@ -1875,6 +1889,7 @@ io_loop(void *data) { has_pending_wakeups = false; \ last_wakeup_was_early = false; \ signal_pending = false; \ + remember_woken_large_input(self); \ } while (LIKELY(!self->shutting_down)) { diff --git a/kitty/vt-parser.c b/kitty/vt-parser.c index 88698e1c6..4a8872cf0 100644 --- a/kitty/vt-parser.c +++ b/kitty/vt-parser.c @@ -242,6 +242,8 @@ typedef struct PS { PyObject *dump_callback; Screen *screen; monotonic_t now, new_input_at; + // Buffer size when new_input_at was set. Bytes before this are a scanned tail. + size_t fresh_input_at; pthread_mutex_t lock; // The buffer @@ -1615,6 +1617,7 @@ run_worker(void *p, ParseData *pd, bool flush) { self->write.pending = 0; } while (self->read.pos < self->read.sz); self->new_input_at = 0; + self->fresh_input_at = 0; if (self->read.consumed) { pd->write_space_created = self->read.sz >= BUF_SZ; self->read.pos -= MIN(self->read.pos, self->read.consumed); @@ -1657,6 +1660,7 @@ vt_parser_commit_write(Parser *p, size_t sz) { if (self->new_input_at == 0) { monotonic_t stamp = monotonic(); self->new_input_at = stamp ? stamp : 1; + self->fresh_input_at = off; } if (self->write.offset > off) memmove(self->buf + off, self->buf + self->write.offset, sz); self->write.pending += sz; @@ -1682,11 +1686,13 @@ vt_parser_input_wake(const Parser *p) { ParserInputWake ans = {0}; with_lock { size_t pending = self->read.sz + self->write.pending; + size_t fresh = pending > self->fresh_input_at ? pending - self->fresh_input_at : 0; // A cleared timestamp is a scanned tail the parser left in the buffer. - // It is not input waiting out input_delay. - if (pending && self->new_input_at) { - if (pending < SMALL_PENDING_INPUT_THRESHOLD) ans.small_pending = true; - else if (pending + INPUT_PARSE_FORCE_MARGIN > BUF_SZ) ans.large_ready = true; + // fresh excludes that tail, matching pending_input_is_small(). + if (fresh && self->new_input_at) { + ans.input_at = self->new_input_at; + if (pending + INPUT_PARSE_FORCE_MARGIN > BUF_SZ) ans.large_ready = true; + else if (fresh < SMALL_PENDING_INPUT_THRESHOLD) ans.small_pending = true; else { monotonic_t ready_at = self->new_input_at + OPT(input_delay); if (ready_at <= monotonic()) ans.large_ready = true; diff --git a/kitty/vt-parser.h b/kitty/vt-parser.h index f093574f9..54b0d7764 100644 --- a/kitty/vt-parser.h +++ b/kitty/vt-parser.h @@ -27,12 +27,14 @@ typedef struct ParseData { } ParseData; // How the I/O thread should wake the main loop for this parser's pending bytes. -// small_pending: under 1 KB, parsed immediately, but the I/O thread still coalesces wakes. +// small_pending: under 1 KB of bytes since input_at, parsed immediately, but the I/O thread still coalesces wakes. // large_ready: a large chunk the parser will accept on the next wake. // large_held_until: a large chunk the parser will refuse until this time. Zero if none. +// input_at: new_input_at for this chunk. Zero if the parser is not waiting on fresh bytes. typedef struct ParserInputWake { bool small_pending, large_ready; monotonic_t large_held_until; + monotonic_t input_at; } ParserInputWake; // The must only be called on the main thread