From 39fc561cec2fe0c2984824a424a28f51fa26363a Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Fri, 6 Nov 2026 14:49:47 +0530 Subject: [PATCH] Remove input_delay and parse output as soon as it is received Holding large chunks of input for input_delay left the parser idle about 30% of the time during bulk output: the parser started only when the buffer was nearly full, and freed space only at the end of a pass, so the I/O thread could not read while the parser worked. Now the I/O thread wakes the main loop whenever new input arrives and no wakeup is already outstanding, and a parse pass compacts the buffer as it goes, waking the I/O thread if the buffer had filled up. A pass parses at most 1MB of input so that a continuous stream cannot starve rendering and event handling. This raises parser utilization during kitten __benchmark__ from ~70% to ~97% and throughput by 12% to 30% depending on the kind of output. --- docs/performance.rst | 12 +-- kitty/child-monitor.c | 190 +++++---------------------------- kitty/options/definition.py | 18 +--- kitty/options/parse.py | 32 +++--- kitty/options/to-c-generated.h | 15 --- kitty/options/types.py | 2 - kitty/options/utils.py | 6 ++ kitty/screen.h | 3 - kitty/state.h | 4 +- kitty/vt-parser.c | 132 ++++++++--------------- kitty/vt-parser.h | 22 ++-- 11 files changed, 112 insertions(+), 324 deletions(-) diff --git a/docs/performance.rst b/docs/performance.rst index 6912a6ab3..ce555f5d8 100644 --- a/docs/performance.rst +++ b/docs/performance.rst @@ -12,10 +12,9 @@ rendering, to improve smoothness. Parsing of the byte stream is done using maximum performance. Updates to the screen typically require sending just a few bytes to the GPU. -There are two config options you can tune to adjust the performance, -:opt:`repaint_delay` and :opt:`input_delay`. These control the artificial delays -introduced into the render loop to reduce CPU usage. See -:ref:`conf-kitty-performance` for details. See also the :opt:`sync_to_monitor` +Output from programs is parsed as soon as it is received. The config option +:opt:`repaint_delay` controls the artificial delay introduced into the render +loop to reduce CPU usage. See :ref:`conf-kitty-performance` for details. See also the :opt:`sync_to_monitor` option to further decrease latency at the cost of some `screen tearing `__ while scrolling. @@ -37,7 +36,6 @@ class keyboard to screen latency. Note that to minimize latency at the expense of more energy usage, use the following settings in kitty.conf:: - input_delay 0 repaint_delay 2 sync_to_monitor no wayland_enable_ime no @@ -45,8 +43,8 @@ following settings in kitty.conf:: `Hardware based measurement on macOS `__ show that kitty and Apple's Terminal.app share the crown for best latency. These -measurements were done with :opt:`input_delay` at its default value of ``3 ms`` -which means kitty's actual numbers would be even lower. +measurements were done with the now removed ``input_delay`` option at its +default value of ``3 ms`` which means kitty's actual numbers would be even lower. `Typometer based measurements on Linux `__ diff --git a/kitty/child-monitor.c b/kitty/child-monitor.c index 8e1fac212..547f10386 100644 --- a/kitty/child-monitor.c +++ b/kitty/child-monitor.c @@ -87,17 +87,10 @@ static const Child EMPTY_CHILD = {0}; static Child children[MAX_CHILDREN] = {{0}}; static Child scratch[MAX_CHILDREN] = {{0}}; static Child add_queue[MAX_CHILDREN] = {{0}}, remove_queue[MAX_CHILDREN] = {{0}}, remove_notify[MAX_CHILDREN] = {{0}}; -// Pending input of each child, as the I/O thread last saw it. Parallel to children and -// only used by the I/O thread, so it needs no lock. -typedef struct { - ParserInputWake pending; - // The chunk the main loop was last woken for. Its next parse accepts that chunk, so - // more reads of it must not wake it again. - uint64_t woken_chunk; - // Bytes were committed since pending was queried - bool stale; -} ChildInputWake; -static ChildInputWake child_input_wake[MAX_CHILDREN] = {{{0}}}; +// Set by the I/O thread when it wakes the main loop to parse input, cleared by the main +// thread before it parses. While set, newly read input needs no further wakeup, since +// the parse the main loop was woken for will consume it. +static atomic_bool input_wakeup_pending = false; static size_t add_queue_count = 0, remove_queue_count = 0; static struct pollfd children_fds[MAX_CHILDREN + EXTRA_FDS] = {{0}}; static pthread_mutex_t children_lock, talk_lock; @@ -549,9 +542,14 @@ parse_input_once(ChildMonitor *self, PyObject *a UNUSED) { Py_RETURN_FALSE; } +static void +wakeup_io_loop_for_write_space(void *self) { + wakeup_io_loop((ChildMonitor *)self, false); +} + static bool do_parse(ChildMonitor *self, Screen *screen, monotonic_t now, bool flush) { - ParseData pd = {.dump_callback = self->dump_callback, .now = now}; + ParseData pd = {.dump_callback = self->dump_callback, .now = now, .write_space_created = wakeup_io_loop_for_write_space, .write_space_created_data = self}; const bool had_url_highlight = screen->url_ranges.count > 0; const MouseShape pointer_shape_before = screen_pointer_shape(screen); self->parse_func(screen, &pd, flush); @@ -562,43 +560,21 @@ do_parse(ChildMonitor *self, Screen *screen, monotonic_t now, bool flush) { // screen, which has its own pointer shape stack. if (screen_pointer_shape(screen) != pointer_shape_before) update_pointer_shape_for_kitty_window(screen->window_id); if (had_url_highlight && !screen->url_ranges.count) link_hover_cleared_by_output(screen); - 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)); - } - // Bytes held until input_delay has passed. The I/O thread wakes the main loop when they are ready. - screen->input_held = !pd.input_read && pd.has_pending_input; - screen->input_parsed = pd.input_read; + if (pd.input_read && screen->paused_rendering.expires_at) set_maximum_wait(MAX(0, screen->paused_rendering.expires_at - now)); + // A pass parses a bounded amount of input, so that rendering and event handling + // happen during a continuous stream. Parse the rest on the next loop iteration. + if (pd.has_pending_input) set_maximum_wait(0); return pd.input_read; } -static void -mark_os_windows_with_held_input(void) { - // Painting an OS window whose only new output is held draws the old frame, and the - // turn that parses the held bytes paints again because it ignores repaint_delay. - // A window that parsed new output this turn is painted anyway. - for (size_t i = 0; i < global_state.num_os_windows; i++) { - OSWindow *w = global_state.os_windows + i; - bool held = false, parsed = false; - for (size_t t = 0; t < w->num_tabs; t++) { - Tab *tab = w->tabs + t; - for (size_t c = 0; c < tab->num_windows; c++) { - Screen *screen = tab->windows[c].render_data.screen; - if (!screen) continue; - held |= screen->input_held; - parsed |= screen->input_parsed; - } - } - w->input_held = held && !parsed; - } -} - static bool parse_input(ChildMonitor *self) { // Parse all available input that was read in the I/O thread. size_t count = 0, remove_count = 0; - bool input_read = false, reload_config_called = false, input_held = false; + bool input_read = false, reload_config_called = false; monotonic_t now = monotonic(); + // Must happen before the input is parsed, see input_wakeup_pending + atomic_store(&input_wakeup_pending, false); children_mutex(lock); while (remove_queue_count) { remove_queue_count--; @@ -688,15 +664,9 @@ parse_input(ChildMonitor *self) { } for (size_t i = 0; i < count; i++) { - if (!scratch[i].needs_removal) { - if (do_parse(self, scratch[i].screen, now, false)) input_read = true; - else if (scratch[i].screen->input_held) input_held = true; - } else scratch[i].screen->input_held = scratch[i].screen->input_parsed = false; + if (!scratch[i].needs_removal && do_parse(self, scratch[i].screen, now, false)) input_read = true; DECREF_CHILD(scratch[i]); } - if (input_held) mark_os_windows_with_held_input(); - else - for (size_t i = 0; i < global_state.num_os_windows; i++) global_state.os_windows[i].input_held = false; if (reload_config_called) { call_boss(load_config_file, NULL); } return input_read; } @@ -1265,12 +1235,6 @@ render(monotonic_t now, bool input_read) { // rendering is done in cocoa_os_window_resized() if (w->live_resize.in_progress) continue; #endif - // Held bytes are not on this window yet. Painting it draws the old frame, and the - // turn that parses them paints again because it ignores repaint_delay. - if (w->input_held && w->id != global_state.thumbnail_callback.os_window) { - if (scan_for_animated_images) global_state.check_for_active_animated_images = true; - continue; - } // A window waiting for a render frame cannot paint now, so it must not delay the // paint when the frame arrives, otherwise the next output paints it mid-burst if (!w->keep_rendering_till_swap && USE_RENDER_FRAMES && w->render_state != RENDER_FRAME_READY && should_os_window_be_rendered(w)) @@ -1744,7 +1708,6 @@ add_children(ChildMonitor *self) { for (; add_queue_count > 0 && self->count < MAX_CHILDREN;) { add_queue_count--; children[self->count] = add_queue[add_queue_count]; - child_input_wake[self->count] = (ChildInputWake){0}; add_queue[add_queue_count] = EMPTY_CHILD; children_fds[EXTRA_FDS + self->count].fd = children[self->count].fd; children_fds[EXTRA_FDS + self->count].events = POLLIN; @@ -1794,7 +1757,6 @@ remove_children(ChildMonitor *self) { if (num_to_right > 0) { memmove(children + i, children + i + 1, num_to_right * sizeof(Child)); memmove(children_fds + EXTRA_FDS + i, children_fds + EXTRA_FDS + i + 1, num_to_right * sizeof(struct pollfd)); - memmove(child_input_wake + i, child_input_wake + i + 1, num_to_right * sizeof(ChildInputWake)); } } } @@ -1971,80 +1933,28 @@ write_to_child(int fd, Screen *screen) { screen_mutex(unlock, write); } -static bool -input_needs_wake(const ChildInputWake *c) { - return c->pending.chunk && c->pending.chunk != c->woken_chunk && (c->pending.small_pending || c->pending.large_ready); -} - -static ParserInputWake -pending_input_wake_info(ChildMonitor *self, monotonic_t now) { - ParserInputWake info = {0}; - for (size_t i = 0; i < self->count; i++) { - ChildInputWake *c = child_input_wake + i; - // Only this thread commits bytes, so a child with nothing pending at its last - // query and no reads since still has nothing pending. - if (c->stale || c->pending.chunk) { - c->pending = vt_parser_input_wake(children[i].screen->vt_parser, now); - c->stale = false; - } - if (input_needs_wake(c)) { - if (c->pending.small_pending) info.small_pending = true; - if (c->pending.large_ready) info.large_ready = true; - } - monotonic_t held_until = c->pending.large_held_until; - if (held_until && (!info.large_held_until || held_until < info.large_held_until)) info.large_held_until = held_until; - } - return info; -} - -static void -remember_woken_input(ChildMonitor *self) { - // Uses the state the wake was decided on, so a chunk that arrives after the - // main thread has parsed is not marked as woken. Must run before the main loop - // is woken, so that a small chunk which grows in the meantime is still accepted. - for (size_t i = 0; i < self->count; i++) { - ChildInputWake *c = child_input_wake + i; - if (!input_needs_wake(c)) continue; - if (c->pending.small_pending) vt_parser_accept_input(children[i].screen->vt_parser, c->pending.chunk); - c->woken_chunk = c->pending.chunk; - } -} - static void * io_loop(void *data) { // The I/O thread loop size_t i; int ret; - bool has_more, child_data_received, has_pending_wakeups = false, last_wakeup_was_early = false; - // A signal or a removed child that needs the main loop. Latched until WAKEUP, since - // signalfd is drained on receipt and a removed child is no longer polled. - bool wake_latched = false; - monotonic_t last_main_loop_wakeup_at = -1, now = -1, pending_wake_at = 0; + bool has_more, child_data_received, wake_needed; Screen *screen; ChildMonitor *self = (ChildMonitor *)data; set_thread_name("KittyChildMon"); #define WAKEUP \ { \ - remember_woken_input(self); \ if (self->benchmark_wakeup_fd >= 0) { \ static const char _wakeup_byte = 1; \ ssize_t _wakeup_ret UNUSED = write(self->benchmark_wakeup_fd, &_wakeup_byte, 1); \ } else wakeup_main_loop(); \ - last_main_loop_wakeup_at = now; \ - last_wakeup_was_early = false; \ - wake_latched = false; \ } while (LIKELY(!self->shutting_down)) { children_mutex(lock); - // A child that exited is no longer polled, so its held output has no deadline here. // The main thread flush-parses the remove queue. It does not need a wake for a // child it closed itself, the SIGCHLD from the hangup wakes it. - if (remove_children(self)) { - wake_latched = true; - has_pending_wakeups = true; - pending_wake_at = 0; - } + wake_needed = remove_children(self) > 0; add_children(self); children_mutex(unlock); child_data_received = false; @@ -2058,19 +1968,14 @@ io_loop(void *data) { children_fds[EXTRA_FDS + i].events |= (screen->write_buf_used ? POLLOUT : 0); screen_mutex(unlock, write); } - if (has_pending_wakeups) { - now = monotonic(); - monotonic_t time_delta = pending_wake_at - now; - if (time_delta > 0) ret = poll(children_fds, self->count + EXTRA_FDS, monotonic_t_to_ms_ceil(time_delta)); - else ret = 0; - } else { - ret = poll(children_fds, self->count + EXTRA_FDS, -1); - } + // A removed child needs the main loop now, as it is no longer polled + if (wake_needed) ret = 0; + else ret = poll(children_fds, self->count + EXTRA_FDS, -1); if (ret > 0) { if (children_fds[0].revents & POLLIN) drain_fd(children_fds[0].fd); // wakeup if (children_fds[1].revents & POLLIN) { SignalSet ss = {0}; - wake_latched = true; + wake_needed = true; read_signals(children_fds[1].fd, handle_signal, &ss); if (ss.kill_signal || ss.reload_config) { children_mutex(lock); @@ -2083,7 +1988,6 @@ io_loop(void *data) { for (i = 0; i < self->count; i++) { if (children_fds[EXTRA_FDS + i].revents & (POLLIN | POLLHUP)) { child_data_received = true; - child_input_wake[i].stale = true; has_more = read_bytes(children_fds[EXTRA_FDS + i].fd, children[i].screen); if (!has_more) { // child is dead. The next iteration queues it and wakes the main thread. @@ -2117,47 +2021,11 @@ io_loop(void *data) { } else if (ret < 0) { if (errno != EAGAIN && errno != EINTR) { perror("Call to poll() failed"); } } - // we only wakeup the main loop after input_delay as wakeup is an expensive operation - // 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 || wake_latched || has_pending_wakeups) { - now = monotonic(); - ParserInputWake pending = pending_input_wake_info(self, now); - // rate_open is the gap since the previous wakeup. A large chunk the - // parser will accept now (large_ready) wakes inside that gap, once per chunk. - // A large chunk still inside input_delay waits until new_input_at + input_delay, - // stored in wait_until, rather than until the next rate-limit slot. - 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; - // The main thread does not arm a timer for a held chunk, this deadline is the only one - monotonic_t wait_until = pending.large_held_until; - if (pending.small_pending) { - if (rate_open) wake_now = true; - else if (!last_wakeup_was_early) { - wake_now = true; - // 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 need_rate_slot = true; - } - // A latched wake uses the same rate limit as a small write that already woke early. - if (wake_latched) { - if (rate_open) wake_now = true; - else need_rate_slot = true; - } - if (wake_now) { - WAKEUP; - if (early_small) last_wakeup_was_early = true; - } else 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; - } - has_pending_wakeups = wait_until != 0; - pending_wake_at = wait_until; - } + // Input is parsed as soon as it is read. Waking the main loop is expensive on some + // platforms, such as cocoa, so it is woken only if it is not already due to parse, + // which coalesces the wakeups of a continuous stream of output into one per parse. + if (child_data_received && !atomic_exchange(&input_wakeup_pending, true)) wake_needed = true; + if (wake_needed) WAKEUP; } #undef WAKEUP children_mutex(lock); diff --git a/kitty/options/definition.py b/kitty/options/definition.py index aaa3de8e5..15b9c240c 100644 --- a/kitty/options/definition.py +++ b/kitty/options/definition.py @@ -25,6 +25,7 @@ definition.add_deprecation('deprecated_macos_show_window_title_in_menubar_alias' definition.add_deprecation('deprecated_send_text', 'send_text') definition.add_deprecation('deprecated_adjust_line_height', 'adjust_line_height', 'adjust_column_width', 'adjust_baseline') definition.add_deprecation('deprecated_scrollback_indicator_opacity', 'scrollback_indicator_opacity') +definition.add_deprecation('deprecated_input_delay', 'input_delay') agr = definition.add_group egr = definition.end_group @@ -1539,23 +1540,6 @@ pending input to be processed, this option is ignored. """, ) -opt( - 'input_delay', - '3', - option_type='positive_int', - ctype='time-ms', - long_text=""" -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. It is also ignored when less than 1 KB of input is pending, such as when -the program echoes typed characters, but at most once per delay period, so -continuous streams of small writes are still coalesced. -""", -) - opt( 'sync_to_monitor', 'yes', diff --git a/kitty/options/parse.py b/kitty/options/parse.py index 40311ff30..90416895d 100644 --- a/kitty/options/parse.py +++ b/kitty/options/parse.py @@ -13,19 +13,19 @@ from kitty.options.utils import ( clone_source_strategies, config_or_absolute_path, confirm_close, copy_on_select, cursor_blink_interval, cursor_text_color, cursor_trail_decay, cursor_trail_start_threshold, custom_shaders, deprecated_adjust_line_height, deprecated_hide_window_decorations_aliases, - deprecated_macos_show_window_title_in_menubar_alias, deprecated_scrollback_indicator_opacity, - deprecated_send_text, disable_ligatures, edge_width, env, filter_notification, font_features, - gpu_driver, hide_window_decorations, macos_option_as_alt, macos_titlebar_color, menu_map, - modify_font, mouse_hide_wait, narrow_symbols, notify_on_cmd_finish, optional_edge_width, - parse_font_spec, parse_map, parse_mouse_map, paste_actions, pointer_shape_when_dragging, - remap_modifiers, remote_control_password, resize_debounce_time, scrollback_lines, - scrollback_pager_history_size, scrollbar_color, shell_integration, show_hyperlink_targets, - store_multiple, symbol_map, tab_activity_symbol, tab_bar_edge, tab_bar_margin_height, - tab_bar_min_tabs, tab_fade, tab_font_style, tab_separator, tab_title_template, tab_title_wrap, - text_fg_override_threshold, titlebar_color, to_cursor_shape, to_cursor_unfocused_shape, - to_font_size, to_layout_names, to_modifiers, transparent_background_colors, underline_exclusion, - url_prefixes, url_style, visual_bell_duration, visual_window_select_characters, window_border_width, - window_logo_scale, window_size + deprecated_input_delay, deprecated_macos_show_window_title_in_menubar_alias, + deprecated_scrollback_indicator_opacity, deprecated_send_text, disable_ligatures, edge_width, env, + filter_notification, font_features, gpu_driver, hide_window_decorations, macos_option_as_alt, + macos_titlebar_color, menu_map, modify_font, mouse_hide_wait, narrow_symbols, notify_on_cmd_finish, + optional_edge_width, parse_font_spec, parse_map, parse_mouse_map, paste_actions, + pointer_shape_when_dragging, remap_modifiers, remote_control_password, resize_debounce_time, + scrollback_lines, scrollback_pager_history_size, scrollbar_color, shell_integration, + show_hyperlink_targets, store_multiple, symbol_map, tab_activity_symbol, tab_bar_edge, + tab_bar_margin_height, tab_bar_min_tabs, tab_fade, tab_font_style, tab_separator, + tab_title_template, tab_title_wrap, text_fg_override_threshold, titlebar_color, to_cursor_shape, + to_cursor_unfocused_shape, to_font_size, to_layout_names, to_modifiers, + transparent_background_colors, underline_exclusion, url_prefixes, url_style, visual_bell_duration, + visual_window_select_characters, window_border_width, window_logo_scale, window_size ) @@ -1084,9 +1084,6 @@ class Parser: def initial_window_width(self, val: str, ans: dict[str, typing.Any]) -> None: ans['initial_window_width'] = window_size(val) - def input_delay(self, val: str, ans: dict[str, typing.Any]) -> None: - ans['input_delay'] = positive_int(val) - def italic_font(self, val: str, ans: dict[str, typing.Any]) -> None: ans['italic_font'] = parse_font_spec(val) @@ -1647,6 +1644,9 @@ class Parser: def scrollback_indicator_opacity(self, val: str, ans: dict[str, typing.Any]) -> None: deprecated_scrollback_indicator_opacity('scrollback_indicator_opacity', val, ans) + def input_delay(self, val: str, ans: dict[str, typing.Any]) -> None: + deprecated_input_delay('input_delay', val, ans) + def map(self, val: str, ans: dict[str, typing.Any]) -> None: for k in parse_map(val): ans['map'].append(k) diff --git a/kitty/options/to-c-generated.h b/kitty/options/to-c-generated.h index 7154bf15d..9bbaa6507 100644 --- a/kitty/options/to-c-generated.h +++ b/kitty/options/to-c-generated.h @@ -797,19 +797,6 @@ convert_from_opts_repaint_delay(PyObject *py_opts, Options *opts) { Py_DECREF(ret); } -static void -convert_from_python_input_delay(PyObject *val, Options *opts) { - opts->input_delay = parse_ms_long_to_monotonic_t(val); -} - -static void -convert_from_opts_input_delay(PyObject *py_opts, Options *opts) { - PyObject *ret = PyObject_GetAttrString(py_opts, "input_delay"); - if (ret == NULL) return; - convert_from_python_input_delay(ret, opts); - Py_DECREF(ret); -} - static void convert_from_python_sync_to_monitor(PyObject *val, Options *opts) { opts->sync_to_monitor = PyObject_IsTrue(val); @@ -1714,8 +1701,6 @@ convert_opts_from_python_opts(PyObject *py_opts, Options *opts) { if (PyErr_Occurred()) return false; convert_from_opts_repaint_delay(py_opts, opts); if (PyErr_Occurred()) return false; - convert_from_opts_input_delay(py_opts, opts); - if (PyErr_Occurred()) return false; convert_from_opts_sync_to_monitor(py_opts, opts); if (PyErr_Occurred()) return false; convert_from_opts_enable_audio_bell(py_opts, opts); diff --git a/kitty/options/types.py b/kitty/options/types.py index 8fc711e60..da3633c7a 100644 --- a/kitty/options/types.py +++ b/kitty/options/types.py @@ -385,7 +385,6 @@ option_names = ( 'inactive_text_alpha', 'initial_window_height', 'initial_window_width', - 'input_delay', 'italic_font', 'kitten_alias', 'kitty_mod', @@ -612,7 +611,6 @@ class Options: inactive_text_alpha: float = 1.0 initial_window_height: tuple[int, str] = (400, 'px') initial_window_width: tuple[int, str] = (640, 'px') - input_delay: int = 3 italic_font: FontSpec = FontSpec(family=None, style=None, postscript_name=None, full_name=None, system='auto', axes=(), variable_name=None, features=(), created_from_string='auto') kitty_mod: int = 5 linux_bell_theme: str = '__custom' diff --git a/kitty/options/utils.py b/kitty/options/utils.py index e8b3b493a..9907745c4 100644 --- a/kitty/options/utils.py +++ b/kitty/options/utils.py @@ -2001,6 +2001,12 @@ def deprecated_adjust_line_height(key: str, x: str, opts_dict: dict[str, Any]) - opts_dict['modify_font'][fm] = FontModification(mtype, ModificationValue(int(x), ModificationUnit.pixel)) +def deprecated_input_delay(key: str, val: str, ans: dict[str, Any]) -> None: + if not hasattr(deprecated_input_delay, key): + setattr(deprecated_input_delay, key, True) + log_error(f'The option {key} has been removed, output from programs is now always processed immediately.') + + def deprecated_scrollback_indicator_opacity(key: str, val: str, ans: dict[str, Any]) -> None: if not hasattr(deprecated_scrollback_indicator_opacity, key): setattr(deprecated_scrollback_indicator_opacity, key, True) diff --git a/kitty/screen.h b/kitty/screen.h index 2555aff98..f9489688b 100644 --- a/kitty/screen.h +++ b/kitty/screen.h @@ -249,9 +249,6 @@ typedef struct { char_type idx_plus_1[16]; } tab_cache; monotonic_t parsing_at; - // Set by the main thread on every parse: input_held when bytes are waiting out - // input_delay, input_parsed when new input was parsed - bool input_held, input_parsed; ExtraCursors extra_cursors; struct { bool active; diff --git a/kitty/state.h b/kitty/state.h index 5eb4a5f4b..a450ccb32 100644 --- a/kitty/state.h +++ b/kitty/state.h @@ -84,7 +84,7 @@ typedef struct Options { char_type *select_by_word_characters_forward; color_type url_color, background, foreground, active_border_color, inactive_border_color, bell_border_color, tab_bar_background, tab_bar_margin_color, window_title_bar_active_foreground, window_title_bar_active_background, window_title_bar_inactive_foreground, window_title_bar_inactive_background; - monotonic_t repaint_delay, input_delay; + monotonic_t repaint_delay; struct { bool on_cross, on_drop; } focus_follows_mouse; @@ -616,7 +616,7 @@ typedef struct OSWindow { gpu_render_target_t persist_fbo_id; uint32_t persist_texture_generation; unsigned int active_tab, num_tabs, capacity, last_active_tab, last_num_tabs, last_active_window_id; - bool focused_at_last_render, needs_render, needs_layers, input_held; + bool focused_at_last_render, needs_render, needs_layers; unsigned keep_rendering_till_swap; WindowRenderData tab_bar_render_data; struct { diff --git a/kitty/vt-parser.c b/kitty/vt-parser.c index 149b71a68..f85a5e16f 100644 --- a/kitty/vt-parser.c +++ b/kitty/vt-parser.c @@ -20,11 +20,12 @@ #define BUF_EXTRA (512u / 8u) #define MAX_ESCAPE_CODE_LENGTH (BUF_SZ / 4u) #define MAX_CSI_PARAMS 256u -// Pending input smaller than this, typically the echo of typed characters, is -// parsed without waiting for input_delay -#define SMALL_PENDING_INPUT_THRESHOLD 1024u -// See classify_pending_input() -#define INPUT_PARSE_FORCE_MARGIN (16u * 1024u) +// The most input a single non-flushing parse pass consumes, so that a continuous +// stream of output cannot starve the main thread of rendering and event handling. +#define MAX_INPUT_PER_PARSE_PASS BUF_SZ +// Once this much of the buffer is in use, a parse pass compacts it as it goes, so +// the I/O thread always has space to read into while the parser is busy. +#define COMPACT_BUFFER_THRESHOLD (BUF_SZ / 2u) // Macros {{{ @@ -240,12 +241,7 @@ typedef struct PS { // these are temporary variables set only for duration of a parse call 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; - // input_chunk identifies the bytes stamped by new_input_at. accepted_chunk is a chunk - // the I/O thread woke the main loop for as small input, parsed even if it has grown since. - uint64_t input_chunk, accepted_chunk; + monotonic_t now; pthread_mutex_t lock; // The buffer @@ -1599,20 +1595,19 @@ 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); -typedef enum { PENDING_INPUT_NONE, PENDING_INPUT_SMALL, PENDING_INPUT_READY, PENDING_INPUT_HELD } PendingInput; - -static PendingInput -classify_pending_input(const PS *self, monotonic_t now) { - // Must be called with the lock held. Both the parser and the I/O thread use this, - // so the I/O thread wakes the main loop exactly when the parser will accept the input. - size_t pending = self->read.sz + self->write.pending; - // A cleared timestamp or bytes before fresh_input_at are a scanned tail the parser left in the buffer - if (!self->new_input_at || pending <= self->fresh_input_at) return PENDING_INPUT_NONE; - // A chunk this close to the end of the buffer is parsed immediately so a fast - // producer cannot fill the buffer and block while input_delay is waiting. - if (pending + INPUT_PARSE_FORCE_MARGIN > BUF_SZ) return PENDING_INPUT_READY; - if (pending - self->fresh_input_at < SMALL_PENDING_INPUT_THRESHOLD || self->input_chunk == self->accepted_chunk) return PENDING_INPUT_SMALL; - return now - self->new_input_at >= OPT(input_delay) ? PENDING_INPUT_READY : PENDING_INPUT_HELD; +static bool +compact_buffer(PS *self) { + // Must be called with the lock held and with no unmerged pending input. Moves the + // unconsumed bytes to the start of the buffer. Bytes the I/O thread is writing + // lie beyond read.sz, and vt_parser_commit_write() moves them down when they are + // committed. Returns true if the buffer was full, so the I/O thread had stopped reading. + if (!self->read.consumed) return false; + const bool was_full = self->read.sz >= BUF_SZ; + self->read.pos -= MIN(self->read.pos, self->read.consumed); + self->read.sz -= MIN(self->read.sz, self->read.consumed); + if (self->read.sz) memmove(self->buf, self->buf + self->read.consumed, self->read.sz); + self->read.consumed = 0; + return was_full; } static void @@ -1620,39 +1615,38 @@ run_worker(void *p, ParseData *pd, bool flush) { Screen *screen = (Screen *)p; PS *self = (PS *)screen->vt_parser->state; screen->parsing_at = pd->now; + bool write_space_created = false; with_lock { self->read.sz += self->write.pending; self->write.pending = 0; - pd->has_pending_input = self->read.pos < self->read.sz; - if (pd->has_pending_input) { - if (flush || classify_pending_input(self, pd->now) != PENDING_INPUT_HELD) { - pd->input_read = true; - self->dump_callback = pd->dump_callback; - self->now = pd->now; - self->screen = screen; - self->read.consumed = 0; - do { - end_with_lock; - // consume all already read input without lock cycling, the - // writer thread only modifies write.pending and buffer space - // beyond read.sz + write.pending - do { consume_input(self, pd->dump_callback, screen->window_id); } while (self->read.pos < self->read.sz); - with_lock; - self->read.sz += self->write.pending; - 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); - self->read.sz -= MIN(self->read.sz, self->read.consumed); - if (self->read.sz) memmove(self->buf, self->buf + self->read.consumed, self->read.sz); - } - } + if (self->read.pos < self->read.sz) { + pd->input_read = true; + self->dump_callback = pd->dump_callback; + self->now = pd->now; + self->screen = screen; + self->read.consumed = 0; + size_t parsed = 0; + do { + const size_t start = self->read.pos; + end_with_lock; + // consume all already read input without lock cycling, the + // writer thread only modifies write.pending and buffer space + // beyond read.sz + write.pending + do { consume_input(self, pd->dump_callback, screen->window_id); } while (self->read.pos < self->read.sz); + with_lock; + parsed += self->read.pos - start; + self->read.sz += self->write.pending; + self->write.pending = 0; + // Free the consumed space now rather than at the end of the pass, so that + // the I/O thread can keep reading while the rest of the input is parsed. + if (self->read.sz >= COMPACT_BUFFER_THRESHOLD && compact_buffer(self)) write_space_created = true; + } while (self->read.pos < self->read.sz && (flush || parsed < MAX_INPUT_PER_PARSE_PASS)); + if (compact_buffer(self)) write_space_created = true; } + pd->has_pending_input = self->read.pos < self->read.sz; } end_with_lock; + if (write_space_created && pd->write_space_created) pd->write_space_created(pd->write_space_created_data); } #ifndef DUMP_COMMANDS @@ -1690,14 +1684,6 @@ vt_parser_commit_write(Parser *p, size_t sz) { PS *self = (PS *)p->state; with_lock { size_t off = self->read.sz + self->write.pending; - // 0 means no unparsed input is waiting. monotonic() can return 0 at startup. - // A zero-length commit must not start the clock before any byte arrives. - if (sz && self->new_input_at == 0) { - monotonic_t stamp = monotonic(); - self->new_input_at = stamp ? stamp : 1; - self->fresh_input_at = off; - self->input_chunk++; - } if (self->write.offset > off) memmove(self->buf + off, self->buf + self->write.offset, sz); self->write.pending += sz; self->write.sz = 0; @@ -1714,32 +1700,6 @@ vt_parser_has_space_for_input(const Parser *p) { return ans; } -ParserInputWake -vt_parser_input_wake(const Parser *p, monotonic_t now) { - PS *self = (PS *)p->state; - ParserInputWake ans = {0}; - with_lock { - switch (classify_pending_input(self, now)) { - case PENDING_INPUT_NONE: break; - case PENDING_INPUT_SMALL: ans.small_pending = true; break; - case PENDING_INPUT_READY: ans.large_ready = true; break; - case PENDING_INPUT_HELD: ans.large_held_until = self->new_input_at + OPT(input_delay); break; - } - if (ans.small_pending || ans.large_ready || ans.large_held_until) ans.chunk = self->input_chunk; - } - end_with_lock; - return ans; -} - -void -vt_parser_accept_input(Parser *p, uint64_t chunk) { - PS *self = (PS *)p->state; - with_lock { - // The chunk may have been parsed already, in which case a later chunk must not be accepted early - if (self->new_input_at && self->input_chunk == chunk) self->accepted_chunk = chunk; - } - end_with_lock; -} #endif // }}} diff --git a/kitty/vt-parser.h b/kitty/vt-parser.h index 932129951..9c892f85f 100644 --- a/kitty/vt-parser.h +++ b/kitty/vt-parser.h @@ -21,21 +21,16 @@ typedef struct Parser { typedef struct ParseData { PyObject *dump_callback; monotonic_t now; + // Called, from the thread doing the parsing, when the parser frees space in its + // buffer after it had become full, so that whoever fills the buffer can resume. + void (*write_space_created)(void *); + void *write_space_created_data; - bool input_read, write_space_created, has_pending_input; + // input_read: some input was parsed. has_pending_input: input remains unparsed + // because a single pass parses at most a bounded amount of input. + bool input_read, has_pending_input; } ParseData; -// How the I/O thread should wake the main loop for this parser's pending bytes. -// small_pending: under 1 KB of fresh bytes, or a chunk accepted with vt_parser_accept_input(). 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. -// chunk: identifies the pending bytes, it changes once they are parsed. Zero if nothing is pending. -typedef struct ParserInputWake { - bool small_pending, large_ready; - monotonic_t large_held_until; - uint64_t chunk; -} ParserInputWake; // The must only be called on the main thread Parser *alloc_vt_parser(id_type window_id); @@ -49,8 +44,5 @@ uint8_t *vt_parser_create_write_buffer(Parser *, size_t *); uint8_t *vt_parser_create_write_buffer_if_none(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 *, monotonic_t now); -// Make the parser accept chunk on its next run even if it has grown past the small input threshold -void vt_parser_accept_input(Parser *, uint64_t chunk); void parse_worker(void *p, ParseData *data, bool flush); void parse_worker_dump(void *p, ParseData *data, bool flush);