From aa8902a6ef88ae3eab4d6d039618d7fb4b383409 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Ekl=C3=B6f?= Date: Sun, 14 Mar 2021 14:32:00 +0100 Subject: [PATCH] term: cache currently selected charset MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit vt.c::action_print(), which is in the hot path, needs to lookup the currently selected charset. This currently requires two loads; first to load the current charset *index*, and then another to get _which_ charset is currently mapped to that index. By caching the current charset, we only need one load. We need to update the cache when: * A different charset index is selected (i.e. SO/SI) * The charset for the current index is changed (e.g. ‘\E(0’, ‘\E(B’ etc) * We restore saved charsets --- terminal.c | 1 + terminal.h | 1 + vt.c | 16 ++++++++++------ 3 files changed, 12 insertions(+), 6 deletions(-) diff --git a/terminal.c b/terminal.c index 88599094..9b1eb159 100644 --- a/terminal.c +++ b/terminal.c @@ -2335,6 +2335,7 @@ term_restore_cursor(struct terminal *term, const struct cursor *cursor) term->vt.attrs = term->vt.saved_attrs; term->charsets = term->saved_charsets; + term->charset = term->charsets.set[term->charsets.selected]; } void diff --git a/terminal.h b/terminal.h index 0fcd97ea..f112376a 100644 --- a/terminal.h +++ b/terminal.h @@ -280,6 +280,7 @@ struct terminal { struct charsets charsets; struct charsets saved_charsets; /* For save/restore cursor + attributes */ + enum charset charset; /* cached copy of charsets.set[charsets.selected] */ bool auto_margin; bool insert_mode; diff --git a/vt.c b/vt.c index d94420da..17cc6a3e 100644 --- a/vt.c +++ b/vt.c @@ -184,11 +184,13 @@ action_execute(struct terminal *term, uint8_t c) case '\x0e': /* SO - shift out */ term->charsets.selected = 1; /* G1 */ + term->charset = term->charsets.set[1]; break; case '\x0f': /* SI - shift in */ term->charsets.selected = 0; /* G0 */ + term->charset = term->charsets.set[0]; break; /* @@ -238,13 +240,10 @@ action_print(struct terminal *term, uint8_t c) L'│', L'≤', L'≥', L'π', L'≠', L'£', L'·', /* x - ~ */ }; - if (unlikely(term->charsets.set[term->charsets.selected] == CHARSET_GRAPHIC) && - c >= 0x60 && c <= 0x7e) - { + if (unlikely(term->charset == CHARSET_GRAPHIC) && c >= 0x60 && c <= 0x7e) term_print(term, vt100_0[c - 0x60], 1); - } else { + else term_print(term, c, 1); - } } static void @@ -414,11 +413,13 @@ action_esc_dispatch(struct terminal *term, uint8_t final) case 'N': /* SS2 - Single Shift 2 */ term->charsets.selected = 2; /* G2 */ + term->charset = term->charsets.set[2]; break; case 'O': /* SS3 - Single Shift 3 */ term->charsets.selected = 3; /* G3 */ + term->charset = term->charsets.set[3]; break; case '\\': @@ -453,6 +454,8 @@ action_esc_dispatch(struct terminal *term, uint8_t final) '+' ? 3 : -1; xassert(idx != -1); term->charsets.set[idx] = CHARSET_GRAPHIC; + if (idx == term->charsets.selected) + term->charset = CHARSET_GRAPHIC; break; } @@ -465,7 +468,8 @@ action_esc_dispatch(struct terminal *term, uint8_t final) '+' ? 3 : -1; xassert(idx != -1); term->charsets.set[idx] = CHARSET_ASCII; - + if (idx == term->charsets.selected) + term->charset = CHARSET_ASCII; break; } }