Repository navigation
perf(string): literal-prefix memchr scan and tighter dispatch in the Lua pattern engine - #68
Merged
Merged
Conversation
…Lua pattern engine - Search: memchr on the first literal byte plus a memcmp prefix filter before entering the matcher; pure-literal patterns resolve to a single memcmp (MatchAt and Search), mirroring PUC-Rio's memchr-based plain scan while staying in pattern semantics. - Class/set dispatch: startup-built 256-bit membership bitmaps for the %a..%z classes (same ctype results as before), and quantifier loops (max/min_expand) compile the repeated item once into a bitmap instead of re-parsing the set per byte. - Capture snapshots copy only the first level_ entries instead of the full 32-entry array, and MatchResult/Capture drop redundant zero init on the hot path. Semantics are byte-exact with the previous engine: verified with a differential harness that runs the old and new engine side by side on ~18.5M randomized cases (0 mismatches, clean under ASan/UBSan), plus a 200k-case Lua-level differential against PUC-Rio Lua 5.4.8 over find/match/gmatch/gsub. Co-authored-by: esrrhs <esrrhs@163.com>
|
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #68 +/- ##
==========================================
- Coverage 94.56% 94.55% -0.01%
==========================================
Files 129 130 +1
Lines 32349 32425 +76
==========================================
+ Hits 30590 30661 +71
- Misses 1759 1764 +5 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
This was referenced Dec 1, 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
Optimizes the shared Lua 5.4 pattern engine (
src/native/string/lua_pattern.cpp,.h) that backsstring.find/string.match/string.gmatch/string.gsubon all three backends. Three changes, per the iteration plan:Searchcomputes the length of the pattern's leading fixed literal prefix (LiteralPrefixLen), then usesmemchron the first literal byte plus amemcmpprefix filter before entering the matcher, instead of stepping every subject byte through the full match routine. Pure-literal patterns resolve to a singlememcmpin bothSearchandMatchAt(the latter coversgmatch/gsub's per-position calls). Quantifier rules matchlstrlib.c(a+keeps a 1-byte prefix,a*/a?/a-don't; leading*+?-are literals).%a..%zmembership is now a startup-built 256-bit bitmap per class (same ctype results as the previous per-byteswitch), and the quantifier loops (MaxExpand/MinExpand) compile the repeated item once into anItemBitsbitmap instead of re-parsing the set/class per byte.CompileItemmirrorsSingleMatchByte's set parsing exactly (leading-]member, ranges,%-escapes, negation).SaveCaps/RestoreCaps) copy only the firstlevel_entries instead of the full 32-entry array, andCapture/MatchResultdrop redundant zero-initialization on the hot path (only the firstlevelentries are ever read; all external readers arelevel-bounded).Also adds a
bench_string_find_literalmicro-benchmark (pattern-modefindof a literal, correctness-verified each iteration against the C++ result) and fixes a stale "ECMAScript" comment inbenchmark/benchmark_string.cpp.Semantics: byte-exact, verified three ways
%f[set],%bxy, lazy-, position captures): 0 mismatches; 1.5M of those under ASan+UBSan, clean.find,find+init,match,match+init,gmatch,gsub) = 1.2M ops, output compared line-by-line. Only 3 lines differ, all one pre-existing deviation: fakelua'sstring.findlacks PUC'snospecialsplain-search shortcut (PUC'sSPECIALSexcludes)), so a pattern like10)errors in fakelua ("invalid pattern capture") where PUC plain-searches. Present identically in master; the engine itself agrees with PUC everywhere including erroring on bare)inmatch/gmatch/gsub. Left unchanged (out of this slice); flagging as a possible follow-up.test_string.*gtests pass on all three backends (incl.test_string_lua_pattern.lua,test_gsub_negative_n.lua). Full suite: 1835/1858 pass; the 23 failures are alltest_mysql/test_redisintegration tests that need live MySQL/Redis services (unavailable in this VM: "Can't connect to server on 127.0.0.1"). The differential fuzz target was not run:fuzz/build_fuzz.shrequires a GCC 15 toolchain at/root/GCC-15that this environment doesn't have; the ASan/UBSan differential harness above covered the same ground.Benchmarks (measured on this VM, 4-core, core-pinned, Release)
Branch vs master baseline, same machine, both runs back-to-back; PUC-Lua rows stable within 1.00–1.07x between runs.
fakelua branch vs master (speedup):
[0-9]+)[0-9]+loop)(plain=true find doesn't touch the pattern engine; 0.95–1.05x is run noise.)
vs PUC-Rio Lua 5.4.8 on the branch (time ratio, <1 means fakelua faster):
StringFindPattern(astring.find-with-init loop) is improved but still slower than PUC Lua — per-call engine entry overhead dominates; noted for a future slice.New
StringFindLiteralbenchmark (pattern-mode find ofneedlein ana-haystack, no master equivalent): INTERP 290→524 ns and GCC 269→493 ns across sizes 10→10000 (near-flat thanks tomemchr), vs PUC Lua 109→893 ns — fakelua is faster than PUC at 10000 (524 vs 893 ns). In the isolated -O3 engine harness, the literal find in a 10KB subject is ~448x vs the old engine; end-to-end it's diluted by VM call overhead.Isolated engine-harness (old vs new engine, -O3, no VM):
find [0-9]+1.71x, gmatch-style[0-9]+2.69x, gsub-style literalMatchAt2.53x.