From dfa4a0e5a28b6f0081f973cfa254c2b31a66edf7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mattias=20Engdeg=C3=A5rd?= Date: Tue, 3 Mar 2026 17:13:36 +0100 Subject: [PATCH] Faster JSON string serialisation (bug#80529) * src/json.c (json_out_string): Optimise for runs of non-special ASCII chars. This seems to be a a substantial performance gain for long strings but a smaller regression for short strings (up to 6 chars or thereabouts, depending on cpu, compiler, and/or C library). Still likely worth it. Suggested by Pavel . --- src/json.c | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/src/json.c b/src/json.c index d308ee9b391..ccbbae615d0 100644 --- a/src/json.c +++ b/src/json.c @@ -332,13 +332,17 @@ json_out_string (json_out_t *jo, Lisp_Object str, int skip) p += skip; while (p < end) { - unsigned char c = *p; - if (json_plain_char[c]) + unsigned char *run = p; + while (p < end && json_plain_char[*p]) + p++; + if (p > run) { - json_out_byte (jo, c); - p++; + json_out_str (jo, (const char *)run, p - run); + continue; } - else if (c > 0x7f) + + unsigned char c = *p; + if (c > 0x7f) { if (STRING_MULTIBYTE (str)) {