patch 9.2.1034: NFA regexp matching is slow for ASCII text
Commit:
https://github.com/vim/vim/commit/58390ca285d8dae3f15134396894e0343fa4399e
Author: Julien Voisin <
julien...@dustri.org>
Date: Wed Sep 2 21:09:03 2026 +0000
patch 9.2.1034: NFA regexp matching is slow for ASCII text
Problem: NFA regexp matching is slower than necessary for ASCII text
because two indirect function calls are made for every
character.
Solution: Add an inline fast path for an ASCII byte that is not followed
by a composing character (Julien Voisin).
The main loop of nfa_regmatch() fetched the current character and its
byte length with two calls through the mb_ptr2char and mb_ptr2len
function pointers on every character. These pointers cannot be inlined,
yet for ASCII text, which is the common case, both merely return the byte
and a length of one.
Handle that case inline. NUL is checked first so that reading the next
byte cannot go past the end of the line, and the "next byte is ASCII"
condition matches the check in utfc_ptr2len(), so a base character
followed by a composing character still falls through to the original
calls.
A "perf stat -e instructions" on a full scroll of a 60000 line C file with
syntax highlighting enabled shows an instructions count reduction of 4%.
closes: #21179
Signed-off-by: Julien Voisin <
julien...@dustri.org>
Signed-off-by: Christian Brabandt <
c...@256bit.org>
diff --git a/src/regexp_nfa.c b/src/regexp_nfa.c
index f6b13a5d9..adbde61dc 100644
--- a/src/regexp_nfa.c
+++ b/src/regexp_nfa.c
@@ -5891,8 +5891,19 @@ nfa_regmatch(
if (has_mbyte)
{
- curc = (*mb_ptr2char)(rex.input);
- clen = (*mb_ptr2len)(rex.input);
+ // Fast path for an ASCII byte not followed by a composing
+ // character, matching the check in utfc_ptr2len(). Avoids two
+ // indirect calls for the common case.
+ if (rex.input[0] != NUL && rex.input[0] < 0x80 && rex.input[1] < 0x80)
+ {
+ curc = rex.input[0];
+ clen = 1;
+ }
+ else
+ {
+ curc = (*mb_ptr2char)(rex.input);
+ clen = (*mb_ptr2len)(rex.input);
+ }
}
else
{
diff --git a/src/version.c b/src/version.c
index 3ad277e3d..a8e412993 100644
--- a/src/version.c
+++ b/src/version.c
@@ -763,6 +763,8 @@ static char *(features[]) =
static int included_patches[] =
{ /* Add new patch number below this line */
+/**/
+ 1034,
/**/
1033,
/**/