Skip to content

Optimize do_string_sub() by skipping vim_regsub() for literal substitutions#19707

Open
mattn wants to merge 1 commit intovim:masterfrom
mattn:perf/do_string_sub-literal-copy
Open

Optimize do_string_sub() by skipping vim_regsub() for literal substitutions#19707
mattn wants to merge 1 commit intovim:masterfrom
mattn:perf/do_string_sub-literal-copy

Conversation

@mattn
Copy link
Member

@mattn mattn commented Mar 16, 2026

Skip vim_regsub() in do_string_sub() when the substitution string contains no special characters (& or \). In that case, use mch_memmove() directly instead of calling vim_regsub() twice (once for length calculation, once for copy). The new substitute_literal_copy() function detects literal substitution strings upfront.

Benchmark with 100,000 substitute() calls (average of 3 runs):

Case Before After Speedup
Simple literal ('b''XYZ') 0.318 sec 0.303 sec 1.05x
Empty literal ('b''') 0.289 sec 0.283 sec 1.02x
Multibyte literal ('う''カキク') 0.328 sec 0.312 sec 1.05x
Long string literal (repeat('abcdefg',100), 'cd''XXXX') 1.505 sec 0.745 sec 2.02x
Backref \0 (not optimized) 0.313 sec 0.312 sec 1.00x

Short strings see modest improvement. Long strings with many matches get ~2x speedup. Substitutions using backreferences still go through vim_regsub() and are unaffected.

…utions

When the substitution string contains no special characters ('&' or '\'),
use mch_memmove() directly instead of calling vim_regsub(). This avoids
unnecessary regex substitution processing for plain string replacements.
@chrisbra
Copy link
Member

Hm, I think you need to also consider the ~ and possibly also literal key characters?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants