From 60b3ccc64115fb7e4b303eb1b93ba16d10fff1b5 Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Sun, 14 Mar 2021 19:19:10 +0100 Subject: term: runtime switch between a ‘fast’ and a ‘generic’ ASCII print function MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit term_print() is called whenever the client application “prints” something to the grid. It is called for both ASCII and UTF-8 characters, and needs to handle sixels, insert mode and ASCII vs. graphical charsets. Since it’s on the hot path, this becomes unnecessarily slow. This patch adds a “fast” version of term_print(), tailored for the common case: ASCII characters in non-insert mode, without any sixels and non-graphical charsets. A new function, term_update_ascii_printer(), has been added, and must be called whenever: * The currently selected charset *index* changes * The currently selected charset changes (from ASCII to graphical, or vice verse) * Sixels are added to the grid * Sixels are removed from the grid * Insert mode is enabled/disabled --- terminal.c | 64 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) (limited to 'terminal.c') diff --git a/terminal.c b/terminal.c index 85f267e1..0fe53ac1 100644 --- a/terminal.c +++ b/terminal.c @@ -1187,6 +1187,8 @@ term_init(const struct config *conf, struct fdm *fdm, struct reaper *reaper, #endif }; + term_update_ascii_printer(term); + for (size_t i = 0; i < 4; i++) { size_t j = 0; tll_foreach(conf->fonts[i], it) { @@ -1702,6 +1704,8 @@ term_reset(struct terminal *term, bool hard) term_ime_enable(term); #endif + term_update_ascii_printer(term); + if (!hard) return; @@ -2335,6 +2339,7 @@ term_restore_cursor(struct terminal *term, const struct cursor *cursor) term->vt.attrs = term->vt.saved_attrs; term->charsets = term->saved_charsets; + term_update_ascii_printer(term); } void @@ -2813,6 +2818,21 @@ term_print(struct terminal *term, wchar_t wc, int width) { xassert(width > 0); + if (unlikely(term->charsets.set[term->charsets.selected] == CHARSET_GRAPHIC) && + wc >= 0x60 && wc <= 0x7e) + { + /* 0x60 - 0x7e */ + static const wchar_t vt100_0[] = { + L'◆', L'▒', L'␉', L'␌', L'␍', L'␊', L'°', L'±', /* ` - g */ + L'␤', L'␋', L'┘', L'┐', L'┌', L'└', L'┼', L'⎺', /* h - o */ + L'⎻', L'─', L'⎼', L'⎽', L'├', L'┤', L'┴', L'┬', /* p - w */ + L'│', L'≤', L'≥', L'π', L'≠', L'£', L'·', /* x - ~ */ + }; + + xassert(width == 1); + wc = vt100_0[wc - 0x60]; + } + print_linewrap(term); print_insert(term, width); @@ -2855,6 +2875,50 @@ term_print(struct terminal *term, wchar_t wc, int width) xassert(!term->grid->cursor.lcf); } +static void +ascii_printer_generic(struct terminal *term, wchar_t wc) +{ + term_print(term, wc, 1); +} + +static void +ascii_printer_fast(struct terminal *term, wchar_t wc) +{ + xassert(term->charsets.set[term->charsets.selected] == CHARSET_ASCII); + xassert(!term->insert_mode); + xassert(tll_length(term->grid->sixel_images) == 0); + + print_linewrap(term); + + /* *Must* get current cell *after* linewrap+insert */ + struct row *row = term->grid->cur_row; + struct cell *cell = &row->cells[term->grid->cursor.point.col]; + + cell->wc = term->vt.last_printed = wc; + cell->attrs = term->vt.attrs; + + row->dirty = true; + cell->attrs.clean = 0; + + /* Advance cursor */ + if (unlikely(++term->grid->cursor.point.col >= term->cols)) { + term->grid->cursor.lcf = true; + term->grid->cursor.point.col--; + } else + xassert(!term->grid->cursor.lcf); +} + +void +term_update_ascii_printer(struct terminal *term) +{ + term->ascii_printer = + unlikely(tll_length(term->grid->sixel_images) > 0 || + term->charsets.set[term->charsets.selected] == CHARSET_GRAPHIC || + term->insert_mode) + ? &ascii_printer_generic + : &ascii_printer_fast; +} + enum term_surface term_surface_kind(const struct terminal *term, const struct wl_surface *surface) { -- cgit v1.3 From b601307faeb47374485e5990db5177556bce7715 Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Tue, 16 Mar 2021 12:57:25 +0100 Subject: term: update_ascii_printer(): log when we (actually) switch printer --- terminal.c | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) (limited to 'terminal.c') diff --git a/terminal.c b/terminal.c index 0fe53ac1..68a7a138 100644 --- a/terminal.c +++ b/terminal.c @@ -2911,12 +2911,22 @@ ascii_printer_fast(struct terminal *term, wchar_t wc) void term_update_ascii_printer(struct terminal *term) { - term->ascii_printer = + void (*new_printer)(struct terminal *term, wchar_t wc) = unlikely(tll_length(term->grid->sixel_images) > 0 || term->charsets.set[term->charsets.selected] == CHARSET_GRAPHIC || term->insert_mode) ? &ascii_printer_generic : &ascii_printer_fast; + +#if defined(_DEBUG) && LOG_ENABLE_DBG + if (term->ascii_printer != new_printer) { + LOG_DBG("switching ASCII printer %s -> %s", + term->ascii_printer == &ascii_printer_fast ? "fast" : "generic", + new_printer == &ascii_printer_fast ? "fast" : "generic"); + } +#endif + + term->ascii_printer = new_printer; } enum term_surface -- cgit v1.3