Skip to content

Optimize string.gsub for patterns without special characters - #2988

Open
gemtool wants to merge 1 commit into
luau-lang:masterfrom
gemtool:optimize-string-gsub
Open

gemtool wants to merge 1 commit into
luau-lang:masterfrom
gemtool:optimize-string-gsub

Conversation

@gemtool

@gemtool gemtool commented Sep 25, 2026

Copy link
Copy Markdown
Contributor

string.gsub runs the pattern matcher at every position, even when the pattern has no special characters and can only match its own text. This adds a fast path for that case (non-anchored, non-empty patterns without special characters; ) is excluded as it is still an error in a pattern):

  • occurrences are found directly (memchr for single characters, first/last character checks before memcmp otherwise), and the text between matches is copied at once
  • replacement strings without % escapes are appended directly; other replacements go through add_value as before

The change is behind DFFlag::LuauOptimizeStringGsub and the flag-off branch is the original loop verbatim. Output is unchanged: a differential fuzz of 40k random calls (including error cases, replacement limits and string/table/function replacements) matches the original implementation exactly.

bench/micro_tests/test_string_gsub.lua before after speedup
remove spaces 97.1 ms 23.2 ms 4.2x
replace newlines 69.5 ms 6.9 ms 10.1x
replace word 87.0 ms 12.6 ms 6.9x
no match 86.3 ms 9.1 ms 9.4x
function replacement 98.8 ms 24.3 ms 4.1x
short string 147.0 ms 72.6 ms 2.0x
every character matches 126.5 ms 56.7 ms 2.2x
separator prefix repeats 102.5 ms 10.6 ms 9.7x

Patterns with special characters take the original path and are unaffected. Added conformance tests for patterns without special characters.

@gemtool
gemtool requested a review from a team as a code owner September 25, 2026 00:37
@gemtool
gemtool requested a review from annieetang September 25, 2026 00:37

This branch has not been deployed

No deployments
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.

1 participant