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