Skip input_delay when pending program output is under 1 KiB

Interactive pending input wakes the main thread and is parsed immediately.
Larger reads still wait and coalesce based on input_delay.
This commit is contained in:
Jesse Miller 2026-09-27 13:33:00 -06:00
parent becd8ed74c
commit b0d3d1b4c0
4 changed files with 26 additions and 8 deletions

View file

@ -535,9 +535,7 @@ do_parse(ChildMonitor *self, Screen *screen, monotonic_t now, bool flush) {
self->parse_func(screen, &pd, flush);
if (pd.input_read) {
if (pd.write_space_created) wakeup_io_loop(self, false);
if (screen->paused_rendering.expires_at) {
set_maximum_wait(MAX(0, screen->paused_rendering.expires_at - now));
} else set_maximum_wait(OPT(input_delay) - pd.time_since_new_input);
if (screen->paused_rendering.expires_at) set_maximum_wait(MAX(0, screen->paused_rendering.expires_at - now));
} else if (pd.has_pending_input) set_maximum_wait(OPT(input_delay) - pd.time_since_new_input);
return pd.input_read;
}
@ -1813,7 +1811,7 @@ io_loop(void *data) {
// The I/O thread loop
size_t i;
int ret;
bool has_more, data_received, has_pending_wakeups = false;
bool has_more, data_received, pending_input_is_interactive, has_pending_wakeups = false;
monotonic_t last_main_loop_wakeup_at = -1, now = -1;
Screen *screen;
ChildMonitor *self = (ChildMonitor *)data;
@ -1825,6 +1823,7 @@ io_loop(void *data) {
add_children(self);
children_mutex(unlock);
data_received = false;
pending_input_is_interactive = 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;
@ -1861,6 +1860,7 @@ io_loop(void *data) {
if (children_fds[EXTRA_FDS + i].revents & (POLLIN | POLLHUP)) {
data_received = true;
has_more = read_bytes(children_fds[EXTRA_FDS + i].fd, children[i].screen);
if (vt_parser_pending_input_is_interactive(children[i].screen->vt_parser)) pending_input_is_interactive = true;
if (!has_more) {
// child is dead
children_mutex(lock);
@ -1903,9 +1903,9 @@ io_loop(void *data) {
has_pending_wakeups = false; \
}
// we only wakeup the main loop after input_delay as wakeup is an expensive operation
// on some platforms, such as cocoa
// on some platforms, such as cocoa. interactive pending input wakes immediately.
if (data_received) {
if ((now = monotonic()) - last_main_loop_wakeup_at > OPT(input_delay)) WAKEUP
if ((now = monotonic()) - last_main_loop_wakeup_at > OPT(input_delay) || pending_input_is_interactive) WAKEUP
else has_pending_wakeups = true;
} else {
if (has_pending_wakeups && (now = monotonic()) - last_main_loop_wakeup_at > OPT(input_delay)) WAKEUP

View file

@ -1446,7 +1446,8 @@ Delay before input from the program running in the terminal is processed (in
milliseconds). Note that decreasing it will increase responsiveness, but also
increase CPU usage and might cause flicker in full screen programs that redraw
the entire screen on each loop, because kitty is so fast that partial screen
updates will be drawn. This setting is ignored when the input buffer is almost full.
updates will be drawn. This setting is ignored for interactive pending input
and when the input buffer is almost full.
""",
)

View file

@ -1569,6 +1569,13 @@ consume_input(PS *self, PyObject *dump_callback UNUSED, id_type window_id UNUSED
#define with_lock pthread_mutex_lock(&self->lock);
#define end_with_lock pthread_mutex_unlock(&self->lock);
static bool
pending_input_is_interactive(const PS *self) {
size_t pending = self->write.pending;
if (self->read.sz > self->read.pos) pending += self->read.sz - self->read.pos;
return pending < 1024u;
}
static void
run_worker(void *p, ParseData *pd, bool flush) {
Screen *screen = (Screen *)p;
@ -1580,7 +1587,7 @@ run_worker(void *p, ParseData *pd, bool flush) {
pd->has_pending_input = self->read.pos < self->read.sz;
if (pd->has_pending_input) {
pd->time_since_new_input = pd->now - self->new_input_at;
if (flush || pd->time_since_new_input >= OPT(input_delay) || self->read.sz + 16 * 1024 > BUF_SZ) {
if (flush || pending_input_is_interactive(self) || pd->time_since_new_input >= OPT(input_delay) || self->read.sz + 16 * 1024 > BUF_SZ) {
pd->input_read = true;
self->dump_callback = pd->dump_callback;
self->now = pd->now;
@ -1647,6 +1654,15 @@ vt_parser_has_space_for_input(const Parser *p) {
end_with_lock;
return ans;
}
bool
vt_parser_pending_input_is_interactive(const Parser *p) {
PS *self = (PS *)p->state;
bool ans;
with_lock { ans = pending_input_is_interactive(self); }
end_with_lock;
return ans;
}
#endif
// }}}

View file

@ -36,5 +36,6 @@ void reset_vt_parser(Parser *);
uint8_t *vt_parser_create_write_buffer(Parser *, size_t *);
void vt_parser_commit_write(Parser *, size_t);
bool vt_parser_has_space_for_input(const Parser *);
bool vt_parser_pending_input_is_interactive(const Parser *);
void parse_worker(void *p, ParseData *data, bool flush);
void parse_worker_dump(void *p, ParseData *data, bool flush);