This is an automated email from the git hooks/post-receive script.
git pushed a commit to branch tymux-11-parser-speedup
in repository terminology.
View the commit online.
commit 9d02b270c1890309c769628a7cf6c4429fd1358f
Author: Cedric BAIL <[email protected]>
AuthorDate: Sun Nov 22 14:38:41 2026 -0600
termptyesc: keep a short CSI argument buffer on the stack
The 16KB argument buffer made _handle_esc_csi probe the stack page by
page for every sequence. 128 codepoints serve nearly all of them; a
longer one continues in a noinline function with the full buffer,
resuming where the first scan stopped so the C0 controls inside the
sequence still run once. The limit of 4096 is unchanged.
tybench, cycles per input byte, 7 interleaved rounds: csi-motion
27.98 -> 27.06 (-3.3%), sgr -2.7%, insert-mode -3.1%, insert -2.7%,
truecolor-cells -0.9%. hyperlink (+2.9%) and long-lines (+3.4%) read
slower, but the same binary moves by 6% and 11% on them between runs.
csi-long-args.sh walks the edges of both buffers. Its checksum is the
one the code before this change gives; an off-by-one in the buffer
shows only under ASan.
---
src/bin/termptyesc.c | 54 +++++++++++++++++++++++++++++++++++++----
tests/csi-long-args.sh | 65 ++++++++++++++++++++++++++++++++++++++++++++++++++
tests/tests.results | 1 +
3 files changed, 115 insertions(+), 5 deletions(-)
diff --git a/src/bin/termptyesc.c b/src/bin/termptyesc.c
index 8b4cf736..ab21a53e 100644
--- a/src/bin/termptyesc.c
+++ b/src/bin/termptyesc.c
@@ -3798,14 +3798,27 @@ _handle_esc_csi_kitty_kbd(Termpty *ty, Eina_Unicode **ptr)
}
}
+#define CSI_MAX_LEN 4096
+/* Nearly every CSI is shorter than this. A buffer of CSI_MAX_LEN codepoints
+ * is 16KB, which makes the compiler probe the stack page by page on every
+ * call (-fstack-clash-protection). */
+#define CSI_SHORT_LEN 128
+#define CSI_NEEDS_LONG_BUFFER (-2)
+
/* Returns the codepoints consumed after the CSI introducer, 0 when the CSI
- * is incomplete, or -1 when an ESC right after the introducer aborts it. */
+ * is incomplete, -1 when an ESC right after the introducer aborts it, or
+ * CSI_NEEDS_LONG_BUFFER when the arguments do not fit in buf. The latter
+ * leaves in ty->seq_scanned how far the scan went, as for an incomplete
+ * sequence, so that calling again with a larger buf continues from there
+ * without running the C0 controls a second time. */
static int
-_handle_esc_csi(Termpty *ty, const Eina_Unicode *c, const Eina_Unicode *ce)
+_handle_esc_csi_buf(Termpty *ty, const Eina_Unicode *c, const Eina_Unicode *ce,
+ Eina_Unicode *buf, unsigned int buf_len)
{
const Eina_Unicode *cc, *be;
- Eina_Unicode buf[4096], *b;
- const Eina_Unicode *ccmax = c + sizeof(buf) / sizeof(buf[0]);
+ Eina_Unicode *b;
+ const Eina_Unicode *ccmax = c + CSI_MAX_LEN;
+ const Eina_Unicode *buf_last = buf + buf_len - 1;
unsigned int scanned = ty->seq_scanned;
ty->seq_scanned = 0;
@@ -3826,7 +3839,14 @@ _handle_esc_csi(Termpty *ty, const Eina_Unicode *c, const Eina_Unicode *ce)
_handle_cursor_control(ty, cc);
}
else if (!scanned)
- *b++ = *cc;
+ {
+ if (b == buf_last)
+ {
+ ty->seq_scanned = cc - c;
+ return CSI_NEEDS_LONG_BUFFER;
+ }
+ *b++ = *cc;
+ }
cc++;
}
if (cc == ccmax)
@@ -3840,6 +3860,11 @@ _handle_esc_csi(Termpty *ty, const Eina_Unicode *c, const Eina_Unicode *ce)
{
const Eina_Unicode *p;
+ if ((unsigned int)(cc - c) > buf_len - 1)
+ {
+ ty->seq_scanned = cc - c;
+ return CSI_NEEDS_LONG_BUFFER;
+ }
for (p = c; p < cc; p++)
if (*p >= ' ')
*b++ = *p;
@@ -4137,6 +4162,25 @@ unhandled:
return cc - c;
}
+static int __attribute__((noinline))
+_handle_esc_csi_long(Termpty *ty, const Eina_Unicode *c, const Eina_Unicode *ce)
+{
+ Eina_Unicode buf[CSI_MAX_LEN];
+
+ return _handle_esc_csi_buf(ty, c, ce, buf, CSI_MAX_LEN);
+}
+
+static int
+_handle_esc_csi(Termpty *ty, const Eina_Unicode *c, const Eina_Unicode *ce)
+{
+ Eina_Unicode buf[CSI_SHORT_LEN];
+ int len = _handle_esc_csi_buf(ty, c, ce, buf, CSI_SHORT_LEN);
+
+ if (EINA_UNLIKELY(len == CSI_NEEDS_LONG_BUFFER))
+ return _handle_esc_csi_long(ty, c, ce);
+ return len;
+}
+
static int
_osc_arg_get(Termpty *ty, Eina_Unicode **ptr)
{
diff --git a/tests/csi-long-args.sh b/tests/csi-long-args.sh
new file mode 100644
index 00000000..d0e31c72
--- /dev/null
+++ b/tests/csi-long-args.sh
@@ -0,0 +1,65 @@
+#!/bin/sh
+
+# Argument strings around the sizes where the parser changes buffer: a short
+# one, one past 128 codepoints, one just under the 4096 limit and one past it.
+zeros() {
+ n=$1
+ while [ "$n" -gt 0 ]; do
+ printf '0;'
+ n=$((n - 1))
+ done
+}
+
+printf '\033[2J\033[1;1H'
+printf '\033[1;'; zeros 5; printf '31mshort '
+printf '\033[1;'; zeros 70; printf '32mlong '
+printf '\033[1;'; zeros 2040; printf '33mlonger'
+
+# C0 controls inside a sequence run once, whether they come before or after
+# the point where the buffer grows.
+printf '\033[3;1H'
+printf '\033[1;'; zeros 10; printf '\n'; zeros 70; printf '\r'; zeros 10; printf '34mctl'
+printf '\033[5;1H'
+printf '\033[1;'; zeros 70; printf '\n'; zeros 10; printf '\t'; zeros 10; printf '35mctl'
+
+# Past the limit the whole sequence is dropped.
+printf '\033[7;1H'
+printf '\033[1;'; zeros 2100; printf '36mdropped'
+printf '\033[8;1Hend'
+
+# The edges of the two buffers, 128 and 4096 codepoints. An argument run of n
+# codepoints ends in an underline (";4"), which only takes effect if the last
+# codepoint made it through. A C0 control, here a backspace, is not part of
+# the arguments but counts for the length limit; it goes at the offset 'at',
+# which is just before or just after the codepoint that does not fit.
+arg_run() {
+ n=$1
+ at=$2
+ i=0
+ while [ "$i" -lt "$n" ]; do
+ [ "$i" -eq "$at" ] && printf '\b'
+ if [ "$i" -eq $((n - 2)) ]; then
+ printf ';'
+ elif [ "$i" -eq $((n - 1)) ]; then
+ printf '4'
+ elif [ $((i % 2)) -eq 0 ]; then
+ printf '0'
+ else
+ printf ';'
+ fi
+ i=$((i + 1))
+ done
+ [ "$at" -ge "$n" ] && printf '\b'
+}
+
+row=1
+for edge in 127 4095; do
+ for n in $((edge - 1)) "$edge" $((edge + 1)) $((edge + 2)); do
+ for at in -1 "$edge" $((edge + 1)); do
+ printf '\033[0m\033[%d;1H' "$row"
+ printf '\033['; arg_run "$n" "$at"; printf 'mX'
+ row=$((row % 24 + 1))
+ done
+ done
+done
+printf '\033[0m\033[1;1HEND'
diff --git a/tests/tests.results b/tests/tests.results
index f2ef215c..800a8d6c 100644
--- a/tests/tests.results
+++ b/tests/tests.results
@@ -250,3 +250,4 @@ kitty-kbd-unsupported.sh 63cfb663085848fd972e9ba667a2901b
kitty-kbd-malformed.sh 70f115545d4cda821177c842c2f96a7f
ind_nel_blank_line.sh 0824297732e75e03e7c37de4efd225a5
tymux_attach_escape.sh 983ab3dfbb930a91fb8d5cbdb46648eb
+csi-long-args.sh 2ed2e135643219617a277bd866ba8f85
--
To stop receiving notification emails like this one, please contact
the administrator of this repository.