From ee2d2caeaa86ec2c1141ed7edd2812a011d54e18 Mon Sep 17 00:00:00 2001 From: Fabian Homborg Date: Thu, 26 Aug 2021 21:01:55 +0200 Subject: [PATCH] escape_code_length: Test colors last We have a *lot* of color sequences to try and tparm is slow (on the whole, when you do this thousands of times). So let's just check colors last, which makes everything else (which is comparatively nothing) faster, while barely impacting colors (benchmarking confirms no measurable difference). Fixes #8253. --- src/screen.cpp | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/screen.cpp b/src/screen.cpp index 84693eee8..23ccd5108 100644 --- a/src/screen.cpp +++ b/src/screen.cpp @@ -252,13 +252,15 @@ maybe_t escape_code_length(const wchar_t *code) { if (*code != L'\x1B') return none(); size_t esc_seq_len = 0; - bool found = is_color_escape_seq(code, &esc_seq_len); - if (!found) found = is_visual_escape_seq(code, &esc_seq_len); + bool found = is_visual_escape_seq(code, &esc_seq_len); if (!found) found = is_screen_name_escape_seq(code, &esc_seq_len); if (!found) found = is_osc_escape_seq(code, &esc_seq_len); if (!found) found = is_three_byte_escape_seq(code, &esc_seq_len); if (!found) found = is_csi_style_escape_seq(code, &esc_seq_len); if (!found) found = is_two_byte_escape_seq(code, &esc_seq_len); + // Colors are the hardest to match, so we try last. + // (also tparm is *slow*, we should try to find a better replacement) + if (!found) found = is_color_escape_seq(code, &esc_seq_len); return found ? maybe_t{esc_seq_len} : none(); }