Repository navigation
perf(string): zero-copy string.find, Lua 5.4 semantics, and pattern class-prefix scan - #71
Merged
Merged
Conversation
…lass-prefix scan Remove the unused subject-string copy on the string.find pattern path and use string_view::find for plain searches. Honor PUC-Rio nospecials (so a pattern like "10)" is a literal), treat the plain flag as a Lua truth value, accept string.gmatch's optional init, and map %1 to the whole match in gsub when there are no captures. Search now precompiles a first-byte class/set bitmap (after skipping leading captures) so patterns such as %d+ and [0-9]+ skip non-matching bytes before entering the matcher. gmatch and unanchored gsub reuse that Search fast path. FlStringFindPlain uses memchr+memcmp, and TCC JIT lower/upper/reverse call host -O3 helpers instead of unoptimized loops.
|
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #71 +/- ##
==========================================
+ Coverage 94.55% 94.56% +0.01%
==========================================
Files 130 130
Lines 32425 32496 +71
==========================================
+ Hits 30659 30731 +72
+ Misses 1766 1765 -1 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
This was referenced Dec 2, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What changed
P2 follow-up after PR #68 and the P1 call-dispatch work: close the remaining
string.find/ pattern-match gaps and a few Lua 5.4 semantic holes. Editable C runtime source remainssrc/compile/c_runtime_cov.h(CMake embeds it; this PR does not invent a second copy).string.findzero-copy and Lua 5.4 semantics (src/native/string/native_string.cpp)std::string sub = sv.substr(...)copy that ran before theplainbranch and was unused on the pattern path.sv.substr(init_pos - 1).find(pat_view)with no heap copy.SPECIALS(^$*+?.([%-, not)), find uses the plain path. Fixesstring.find("abc10)def", "10)")→4, 6instead ofinvalid pattern capture.plainis a Lua truth value (inter::CVarToBool, onlynil/falseare false). Fixesstring.find("a.b", ".", 1, 1).string.gmatch(s, pattern [, init])is vararg and accepts the Lua 5.4 optional 1-basedinit(explicitnilequals omit; same position normalization as find).string.gsubreplacement%1with no captures (level == 0) is the whole match, like%0.%2and higher still error.JIT / TCC string fast paths (
src/compile/c_runtime_cov.h)FlStringFindPlainusesmemchr+memcmp, same idea as PR perf(string): literal-prefix memchr scan and tighter dispatch in the Lua pattern engine #68's literal-prefixSearch.FAKELUA_JIT_TYPE == 0(TCC, no-O3),FlStringLower/FlStringUpper/FlStringReversecall host-O3helpersFlHostStringLower/Upper/Reversedefined insrc/native/string/native_string.cppand registered insrc/jit/tcc_handle.cpp. GCC keeps the inlined loops (already compiled with-O3).Pattern
Searchclass-prefix scan; gmatch/gsub reuse it (src/native/string/lua_pattern.cpp,native_string.cpp)Searchskips leading captures and, if the first required item is a character class/set (or a literal only visible after those captures), precompiles anItemBitsfirst-byte bitmap (ormemchrfor a literal) and only callsRunAtat candidate positions. Covers[0-9]+,%d+,%w+,(%d+).Searchtakesleading_caret_is_anchor(default true). gmatch and unanchored gsub call it withfalseso a leftover^is a literal (after gsub has already stripped a leading anchor).string.gsub("^abc", "^^", "X")(Lua 5.4:Xabc), which previously stripped a second^viaMatchAt's default anchor behavior.Tests
New fixtures in
test/lua/string/, wired throughtest/test_string.cppon INTERP / TCC / GCC:test_string_find_nospecialstest_string_find_plain_truthinesstest_string_gmatch_inittest_string_gsub_percent1_whole_match(includes%1/%0/%2and the^^anchor/literal case)C++ regressions in
test_string_lua_pattern_cpp_regressionscover class-prefixSearchandleading_caret_is_anchor=false.Local run (Release
unit_tests, cwd = buildbin/,--gtest_filter='test_string.*:common.c_runtime_cov_matches_kCRuntimeHeader'):common.c_runtime_cov_matches_kCRuntimeHeaderpassed.test_string_{len,sub,rep,case,byte_char,format,dump,lua_pattern,find,match,gmatch,gsub}plustest_string_lua_pattern_cpp_regressionsand the new nospecials / plain-truthiness / gmatch-init tests.test_string_gsub_percent1_whole_match(after the first two backends had already run the expected%2pcall), the entirebuild-cpm-fixtree was deleted from outside this work, so the remainingtest_string.*cases were not finished locally. Please treat CI as the complete run.luaon this machine; 5.4.8 was not installed).Plan
No merge and no branch deletion from this PR.