Repository navigation
Conversation
|
Preview build of ec18152: |
robobun
added a commit
to oven-sh/bun
that referenced
this pull request
Oct 1, 2026
This is the preview build of oven-sh/WebKit#750: f20ce77445 plus the matcher. It becomes the merged commit once that pull request lands.
robobun
force-pushed
the
robobun/96d7caac/yarr-linear-engine
branch
3 times, most recently
from
October 2, 2026 06:35
36f2026 to
6830e12
Compare
robobun
added a commit
to oven-sh/bun
that referenced
this pull request
Oct 2, 2026
--experimental-linear-regexp and BUN_FEATURE_FLAG_EXPERIMENTAL_LINEAR_REGEXP=1 set JavaScriptCore's useRegExpLinearEngine before it starts. A RegExp then runs on JavaScriptCore's non-backtracking matcher when the matcher accepts its pattern, and one match takes time linear in the length of its subject. A pattern the matcher refuses is compiled and run as it is without the switch. The switch is the process's. bun_jsc::initialize resolves it, bun test --parallel forwards it to its workers, and a Worker whose execArgv has it throws ERR_WORKER_INVALID_EXEC_ARGV in a process that does not have it. WEBKIT_VERSION is the preview build of oven-sh/WebKit#750, which has the matcher.
robobun
force-pushed
the
robobun/96d7caac/yarr-linear-engine
branch
3 times, most recently
from
October 7, 2026 20:17
33a20ad to
ec05001
Compare
robobun
added a commit
to oven-sh/bun
that referenced
this pull request
Oct 8, 2026
--experimental-linear-regexp and BUN_FEATURE_FLAG_EXPERIMENTAL_LINEAR_REGEXP=1 set JavaScriptCore's useRegExpLinearEngine before it starts. A RegExp then runs on JavaScriptCore's non-backtracking matcher when the matcher accepts its pattern, and one match takes time linear in the length of its subject. A pattern the matcher refuses is compiled and run as it is without the switch. The switch is the process's. bun_jsc::initialize resolves it, bun test --parallel forwards it to its workers, and a Worker whose execArgv has it throws ERR_WORKER_INVALID_EXEC_ARGV in a process that does not have it. WEBKIT_VERSION is the preview build of oven-sh/WebKit#750, which has the matcher.
robobun
force-pushed
the
robobun/96d7caac/yarr-linear-engine
branch
2 times, most recently
from
October 8, 2026 15:41
e896397 to
809b157
Compare
The YARR JIT and the bytecode interpreter match by backtracking. A pattern such as /(a*)*b/ costs them a number of steps that is exponential in the length of the subject, and /a*a*a*a*a*a*a*a*b/ a polynomial one. With --useRegExpLinearEngine=1 (default false), RegExp::compile() and RegExp::compileMatchOnly() first ask Yarr::byteCompileLinear() for a program for a nondeterministic automaton. When the matcher accepts the pattern, the RegExp has bytecode that carries the program and no JIT code, and Yarr::interpret() runs the program. The matcher advances every state of the automaton over the subject together and enters no state twice at one position, so the steps of a match grow with the length of the subject and with nothing else. It keeps the states in the order backtracking tries them, so the match and the captures are the ones the backtracking engines report. The compiler takes two bounds from the program. The first is the most steps a position of the subject can cost (LinearProgram::m_maximumStepsPerPosition). Without a lookaround that is at most four steps per instruction. A lookaround is run to its end at every position that asserts it, so lookarounds inside one another multiply it, and the compiler refuses a program that can cost a position more than four steps per instruction of the largest program. The second is the most memory a match can hold (m_maximumScratchBytes). The compiler counts the states and the frames of each code, and the matcher lets none of its vectors grow past those counts. The matcher refuses a backreference, a lookaround that can match a string of any length, lookarounds that are too costly, groups more than 32 levels deep, and a pattern whose program or working memory passes the limits (maximumRegExpLinearProgramSize, maximumRegExpLinearWorkingMemory). A refused pattern is compiled and run as it is without the option, by the JIT or by the bytecode interpreter, and --reportRegExpLinearRefusals=1 prints a line for each one. A refusal is about the pattern and about nothing else, and the RegExp remembers it. A RegExp asks the matcher once, before the JIT: Yarr::jitCompile() reorders the terms of the pattern it is given. The compiler also refuses a pattern whose terms are out of order, whoever asks with it. When the stack has too little room left for the compiler, the compile fails with the error of the other stages of a compile (TooManyDisjunctions): the match throws, nothing is remembered, and the next match asks again. Yarr::RegularExpression has the interpreter and no other engine, and asks in its own compile. Yarr::byteCompile() does not ask. With --verifyRegExpLinearEngine=1 (for tests), the bytecode interpreter matches again from the same start after each match of the matcher, and the process ends when the two do not agree on the match or on the groups. $vm.regExpMatchStatistics(regExp, string, start) reports the engine a RegExp is compiled for, how many times the RegExps of the VM asked the matcher, and the bounds, the steps and the working memory of one match, for tests that assert how the cost grows without a clock. Its mode "backtracking" matches with the bytecode interpreter, and its mode "outOfStack" makes the compiler of the matcher find no stack. $vm.regExpMatchOnBothEngines(regExp, string, start) matches one RegExp with the engine it is compiled for and with the bytecode interpreter, and compares the two output vectors. The interpreter has a low match limit there and reports when it gives up, because its answer is then "no match" whatever the subject has. $vm.regExpLinearRefusalAfterJITCompile() asks the matcher with a pattern the JIT has compiled. JSTests/stress/regexp-linear-engine-*.js are the tests of the matcher: regexp-linear-engine-differential.js compares it with the interpreter on 1.4 million matches. JSTests/regexpLinearEngine.yaml runs the RegExp tests of JSTests/stress once more with the option and the verification, so that a change of YarrPattern is checked against the matcher. Tools/yarr-fuzz has the configurations "linear", "linear-interp" and "linear-verify", and reuses group names across alternatives.
robobun
force-pushed
the
robobun/96d7caac/yarr-linear-engine
branch
from
October 8, 2026 16:03
809b157 to
ec18152
Compare
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.
For oven-sh/bun#44384, requested by @alii.
On hold. #777 (
useMemoizedRegExp) bounds the same searches in the existing engines at the JIT's speed. I suggest #777 as the switch, and this matcher at most as a fallback for what #777 does not memoize.Problem
/(a*)*b/on 28aplus!takes 0.7 s./(a*)*b|a*!/.test("a".repeat(64) + "!")isfalse(Yarr.h:62).Fix
useRegExpLinearEngine(default false):RegExp::compileasks a compiler (yarr/YarrLinear.cpp) for an automaton program, andYarr::interpretruns it.JSTests/stress/regexp-linear-engine-*.js(step counts, no clock),JSTests/regexpLinearEngine.yaml,Tools/yarr-fuzz.hasCodeForarm, a total-work limit, theoptimizeBOLfix.Background
Downsides
test()(3,418 times with a{1,500}group). [YARR] Match memoization, behind useMemoizedRegExp (off by default) #777: 0.9 to 1.2 times, one pattern 2.5.jsc), 1 to 9 more instructions per bytecode-interpreter match, no diff on the JIT path.Notes
Relation to #777 (added 16 October)
#777 (
useMemoizedRegExp, default off, opened on 8 October, after this PR) bounds backtracking inside the existing engines. The JIT keeps a budget and hands a match that passes it to the interpreter. The interpreter then remembers the (cache point, position) pairs that failed, and does not try them again. I ran the ltojscthat CI built for each PR (linux x64, this head and f45f06f) on the same cases.The first call of a hostile search, in ms. Every engine answers "no match", which is correct, except in the second row. There the subject matches, both options say so, and the engines without an option say "no match".
/(a*)*b/aand!/(a*)*b|a*!/aand!/a*a*a*a*a*a*a*a*b/a/\s+$/x(...)*around(...)+aand!/^(?=(a+)+b)/a/^(?=(?:.*\d)+x)(?=.*[a-z])\w{6,}/1/^(?:\w+\s?){1,500}$/aand!/^(?:a*){1000}b/a/^(?:a|a){1,2000}$/aand!/(?:\w+\s?){1,9000}!/a/(a*)*\1b/aand!A hot
test()on a subject that matches, in ns per call (the fastest of four runs, the JavaScript tiers on, a quiet machine). Each option is measured against its own binary without the option./\.json$//\s+$//i/^.{1,255}$//^[\w.+-]+@[\w-]+(?:\.[\w-]+)+$//(\d{4})-(\d{2})-(\d{2})//^v?(\d+)\.(\d+)\.(\d+)(?:-([\w.]+))?$//^\/users\/(\d+)\/posts\/(\d+)$//^[A-Za-z0-9+\/]{4,4096}={0,2}$//^.{1,10000}$//^\p{RGI_Emoji}$/v/^(?:\w+\s?){1,500}$//<(\w+)>[^<]*<\/\1>//(?=.*\d)(?=.*[a-z])\w{6,}/What the numbers say:
test()stays at the JIT's time: 0.94 to 1.16 times. The route pattern is the one exception, at 2.5 times (5.5 to 13.6 ns). [YARR] Match memoization, behind useMemoizedRegExp (off by default) #777 says that a pattern with a budget is no longer inlined into DFG code. With this PR the first eight patterns take 10 to 94 times, and the large programs more.a{0,2000}), which can stay quadratic there.{1,9000}shows the price of this matcher: a program near the cap is slower than the limit of the backtracking engines.resources/regexp-linear-engine-patterns.js) made 35,876 patterns, 28,443 of them with lookarounds. Each ranexecfrom every start offset of three subjects. This matcher accepts 27,495 of them. For every one, [YARR] Match memoization, behind useMemoizedRegExp (off by default) #777 gives the same result as this matcher, with--useMemoizedRegExp=1and with--forceMemoizedRegExp=1 --useRegExpJIT=0added. 9 patterns differ: this matcher refuses all 9, and the backtracking JIT then stops at its limit.yarr/YarrInterpreter.cppandyarr/YarrInterpreter.h.What I suggest:
RegExp::compile, to the place where [YARR] Match memoization, behind useMemoizedRegExp (off by default) #777 finds that a pattern cannot be memoized in full. Only those patterns then run on the matcher. This is the second shape of open decision 3 below, with [YARR] Match memoization, behind useMemoizedRegExp (off by default) #777's budget as the counter it needs.JSTests/regexpLinearEngine.yaml), and theTools/yarr-fuzzconfigurations.BUN_JSC_useMemoizedRegExp=1turns it on with no change in Bun. If a named flag is wanted, the plumbing of that PR can setuseMemoizedRegExp.Open decisions for a maintainer
These apply only if this matcher is kept (the section above).
--reportRegExpLinearRefusals=1names it. V8's--default-to-experimental-regexp-enginedoes the same: on node v26.3.0 a backreference, a lookahead,/iand/ustay on its backtracking engine with no error.lflag, and at construction ("Cannot be executed in linear time"). Here the ask is inRegExp::compile, so an error there (a newYarr::ErrorCode) comes out of the firsttest()orexec(). An error at construction needs the ask inRegExp::finishCreationwith the lazy construction of the pattern off, and an answer for a RegExp that comes from the bytecode cache.Yarr::RegularExpressionhas its own chooser and needs its own answer.matchLimitcounts only where a group is entered, so/a*a*a*a*a*a*a*a*b/has no limit at all (13 s on 48 characters, and 0.09 ms on the matcher). Those counters run with the option off too.maximumRegExpLinearProgramSizeinstructions (65,536), which allows 262,144 steps per position, andmaximumRegExpLinearWorkingMemorybytes (8 MB). Lower caps refuse more patterns, so this decision depends on the first one. The caps also decide the most one search can cost, because the matcher has no limit on the total work of a search ("What the option does not bound" below).--enable-experimental-regexp-engine-on-excessive-backtracksdoes. That needs the counters of decision 1.yarr/go from 0 to 14USE(BUN_JSC_ADDITIONS)blocks, andYarrLinear.cppis 1,700 lines. It readsYarrPattern, so a change of what the parser produces can need a change here. Upstream merges touchedyarr/4 times in 46 days. What lowers the cost is in "Upkeep" below. What stays: the guards at compile time catch a new kind of term, and a change of meaning is caught only by tests, on two test lanes that the release does not wait for.How it works
RegExp::compileandRegExp::compileMatchOnlycallYarr::byteCompileLinearbefore the JIT conditions.compileMatchOnlybuilds a pattern with every group for that, becauseexecshares what it compiles. It does not build one for a pattern with a backreference, which the matcher refuses first.Yarr::jitCompilereorders the terms of the pattern it is given (optimizeAlternative), and the program takes the terms in order. The compiler also refuses a pattern whose terms are out of order, whoever asks with it (LinearRefusal::PatternReorderedForJIT).RegExp(one byte, in padding), so that the other character size and the fallback to bytecode do not ask again.TooManyDisjunctions): the match throws aRangeError, nothing is remembered, and the next match asks again.Yarr::RegularExpressionhas the interpreter and no other engine. It asks in its own compile.Yarr::byteCompileis the upstream function and does not ask.optimizeDotStarWrappedExpressionsrewrites/^.*X.*$/toXand aDotStarEnclosureterm. The compiler of the matcher expands that term to the pattern as it was written, soYarrPattern.cpphas no diff and a refused pattern keeps the rewrite.$can only match at the end. The matcher starts that far from the end, as the other engines do, so/\.json$/does not read the subject.Vectorgrows.(a?)x 2,400 needed about 185 MB under the old limit (maxRegExpStackSize, 192 MB). It is now refused.dgimsuvy, groups and named groups (also duplicated names), greedy and lazy quantifiers, counted repeats (unrolled),^ $ \b \B, modifiers, class sets, and lookarounds whose body has a bounded length, with captures.LinearRefusal: a backreference, a lookaround of unbounded length, lookarounds more than 16 deep or too costly, groups more than 32 deep, a program over the size cap, working memory over its cap, a kind of term the compiler does not know.verifyRegExpLinearEngine(default false, for tests): after each match of the matcher, the bytecode interpreter matches the same bytecode from the same start, and the process ends when the two do not agree on the match or on the groups. The bytecode of an accepted RegExp has the interpreter's body beside the program for this.$vm.regExpMatchStatistics(regExp, string, start)returns{ index, captures, engine, refusal, jit, gaveUp, linearCompiles, programSize, maximumStepsPerPosition, maximumScratchBytes, steps, scratchBytes }for the engine the RegExp is compiled for.linearCompilescounts the asks of every RegExp of the VM. With the mode"backtracking"the bytecode interpreter matches the same RegExp. With the mode"outOfStack"a RegExp that asks in that call finds the compiler out of stack.$vm.regExpMatchOnBothEngines(regExp, string, start)matches on both and compares the output vectors. It returns the index of the match,falsewhen the two differ, andnullwhen the interpreter gave up. For these functions the interpreter has a match limit of 100,000 in place of 100,000,000 and reports when it gives up, so a test never compares with the "no match" of a limit, and never waits seconds for it.$vm.regExpLinearRefusalAfterJITCompile(regExp)asks the matcher with a pattern the JIT has compiled, which no RegExp does.Where the answer is not the backtracking engines' answer
matchLimit, or fills its pool of contexts, answers "no match". The matcher has neither limit and answers what the subject has.uorv. The matcher takes no match between the two halves of a pair, which is not a position of the subject. A RegExp never takes one either: where a backtracking engine finds one,RegExp::matchInlinesearches again after the pair. Two places show a difference./.*X.*/:/.*\B.*/u.test("a\u{1F600}a")istrueon the JIT and on the interpreter, andfalseon the matcher. The backtracking engines search forXalone (DotStarEnclosure) and find it between the halves.falseis the answer of the specification.regexp-linear-engine-semantics.jspins this case and/.*(?<=\uD83D).*/ufor the matcher only.Yarr::RegularExpressionhas no search after the pair. With the option,/\B/von"a\u{1F600}a"goes from a match at 2 to no match. No test runs that route with a pair.^in a lookahead inside a lookbehind, when another alternative begins with^:/(?<=(?=^|x)a)b|^2/.exec("ab")isnullon the JIT and on the interpreter, and index 1 on the matcher and on V8.optimizeBOLdrops that^from the copy of the pattern that the backtracking engines retry after the start. That is a bug of the backtracking engines without the option too, reported on its own. Its sibling/(?<=(?=^)a)b|^2/isnullon all three engines until that fix.What the option does not bound
/(?:\w+\s?){1,9000}!/(63,003 instructions) on 12,000atakes 5.2 s on the matcher. The JIT gives up atmatchLimitafter 1.0 s (its "no match" is the answer of the limit, and right here by chance). A lowermaximumRegExpLinearProgramSizelowers this, and refuses more patterns (open decision 2). A limit on the steps of one search needs a way to report that the search was given up, which is what [JSC] A RegExp search that Yarr abandons throws a RangeError #343 adds.replace,matchAll,split). It runs one match per result./a*b|a/gon nacharacters takes about 3n² steps, as it does for a backtracking engine, and the matcher's steps are slower.Relation to other open PRs
Yarr::interpret()andInterpreter::interpret()returnJSRegExpResult. This PR adds the matcher's branch tointerpret(), and test functions around it that take anunsignedfrom the interpreter. A merge of the two heads (8eade9e and this one) conflicts inyarr/YarrInterpreter.cpp, so the PR that lands second needs a rebase.YarrInterpreter.cpp,YarrInterpreter.handRegExp::matchForTesting: the matcher's branch returnsMatchorNoMatch, the test functions returnJSRegExpResult, and "gave up" comes fromisAbandoned()in place of the three flags this PR sets where the interpreter gives up. The tests and comments that say "no match at the limit" then sayRangeError, and one run is added: a refused pattern with a low--regExpMatchLimitthrows.--regExpMatchLimitor--maxRegExpStackSize. The seven tests that [JSC] A RegExp search that Yarr abandons throws a RangeError #343 adds all do, and the matcher has no such limit to reach.m_state != JITCodearm ofhasCodeForandhasMatchOnlyCodeFortoreturn m_workInInterpreter != workBeforeJIT;, so that a RegExp on bytecode is compiled again for the JIT after 256 units of work. An accepted RegExp stays on bytecode. After that merge it is compiled again for each match.regexp-linear-engine-compile-once.jsfails then, and so does anASSERTinRegExp::compileForLinearMatcher. The change isusesLinearMatcher() ||in that arm. They also put auint16_tin the padding ofRegExpwhere this PR has one byte.RegExp.cppnow asserts that aRegExpis at most 80 bytes, so that a merge that moves every RegExp to the next size class does not compile.Upkeep
PatternTerm::Typethat the compiler does not know is a refusal, so a new kind of term runs on the backtracking engines until the matcher has it.-Wswitchis an error inYarrLinear.cpp, so a new enumerator in a type its switches name stops the build there.JSTests/regexpLinearEngine.yamlis in the list ofrun-javascriptcore-tests. It runs the regexp tests ofJSTests/stressonce more with the option and withverifyRegExpLinearEngine: 428 runs, about one and a half minutes with 8 jobs. A test that comes with a change ofYarrPatternruns there too, and a difference between the matcher and the interpreter ends the process, also in a test that only prints what it finds.JSTests/BUN-TEST-DIFFERENCES.md(section E, step 4) says what to do with a test that fails only there: find the engine that is right first. Where the fix of the matcher is not plain, make the compiler refuse the construct.Refused patterns in real code
A corpus of 9,809 RegExp literals from 539 packages (the packages in bun's test tree, and bun's
src/js), classified with the build of an earlier head: 9,274 accepted, 535 refused (5.45%). 329 have a lookaround of unbounded length, 205 a backreference, 1 is too large. Outside syntax-highlighter packages it is 93 of 5,374 (1.73%). 65 packages have at least one refused literal. No literal has groups more than 8 deep, so the limit of 32 levels refuses none of them.Measurements (lto
jscfrom CI, linux x64, 0c06faa against this head)In the table, some instruction counts of the matcher are from the head before this one (e896397). The commit between the two changes only how the interpreter's entry is inlined. The ns columns are from a loaded machine. The table in "Relation to #777" has ns from a quiet one.
size: text 48,044,303 to 48,119,023 (+74,720), data +208. By section:.text+70,192,.rodata+4,288,.data.rel.ro+208. The 44 symbols named for the matcher and its test functions are 61,179 bytes. The test functions and the verification are 9 of them and 17,131 bytes. ABytecodePatternis 8 bytes larger (120 to 128). ARegExpis not (80), andRegExp.cppasserts that.test()call (qemu-x86_64 -one-insn-per-tb, the JavaScript tiers off, base to this head), for/\.json$/, the date, the UUID and the route pattern of the table below. With the RegExp JIT: 801 to 798, 885 to 876, 1,249 to 1,247, 894 to 881. With--useRegExpJIT=0: 1,257 to 1,264, 5,066 to 5,068, 3,426 to 3,427, 2,665 to 2,674. One cell measured twice on one binary differs by up to 1%. There is no diff inYarrJIT.cpp,YarrJIT.h,YarrPattern.cpp,dfg/,ftl/orRegExpInlines.h.Interpreter::interpret(), and the lto build no longer inlined that intoYarr::interpret(). It isALWAYS_INLINEnow, which is 2,160 of the bytes above.test()call. Instructions are counted as above and are exact. Nanoseconds are from a hot loop in a function of its own, with the JavaScript tiers on: the fastest of six runs, JIT and matcher in turn. The machine is shared, and one cell varies by up to 40% between runs (the two refused rows run the same code in both columns)./\.json$//\s+$//i/^.{1,255}$//^[\w.+-]+@[\w-]+(?:\.[\w-]+)+$//(\d{4})-(\d{2})-(\d{2})//^v?(\d+)\.(\d+)\.(\d+)(?:-([\w.]+))?$//^\/users\/(\d+)\/posts\/(\d+)$//^[A-Za-z0-9+\/]{4,4096}={0,2}$//^.{1,10000}$//^\p{RGI_Emoji}$/v/^(?:\w+\s?){1,500}$//<(\w+)>[^<]*<\/\1>//(?=.*\d)(?=.*[a-z])\w{6,}//^(?:\w+\s?){1,500}$/on a valid subject, and the matcher follows every copy of the group. On a hostile 29-character subject (28aand!) that pattern costs the backtracking engines 0.5 s, and the matcher 0.2 ms.test(), thenexec()./(a)\1b/): 29,030 to 29,864, 28,862 to 29,243. A longer pattern with a backreference: 66,531 to 66,367, 65,586 to 65,991. The compiler sees the backreference before it reads a term./(?=.*\d)\w{6,}x/): 57,839 to 62,923, 57,901 to 59,112.test()builds a second pattern, with every group, to ask with./^(?:\w+\s?){1,20000}x$/): 27,964 to 11,537,337, 30,121 to 11,532,286. The compiler fills a program up to the cap before it refuses: about 3 ms, once per RegExp.Tests (nine files in
JSTests/stress, 18 runs: the files with a second header also run with--useRegExpJIT=0)regexp-linear-engine-semantics.js: 228 cases on the matcher, on the JIT and with--useRegExpJIT=0, and 3 for the matcher only (the exceptions above). The expectations are what all three and V8 report. It starts with three RegExps against the option: with the option each asked the matcher once, and without it none did, whichever call compiled it first.-steps.js: steps at n, 2n and 3n are collinear and under the bound, for exponential, polynomial and lookaround patterns. A pattern that is anchored at the end takes the same steps at every length. It also pins the global loop as quadratic.-differential.js: the matcher against the bytecode interpreter, in one process, with$vm.regExpMatchOnBothEngines. 11,985 accepted patterns: 5,808 of the shape.*X.*(with and without^,$and each flag), 3,481 made from a fixed sequence of numbers (2,644 with a lookaround), 521 made with a name that alternatives share, and hand-written sources for case folding, surrogate pairs, groups in loops and in lookarounds, and duplicate names. 1,391,774 matches from every start offset have the same output vector on both engines (446,387 of them are a match). The interpreter gave up on 6. 7 s on a Release build with assertions.-step-bound.js: 2,000 patterns from the same generator (resources/regexp-linear-engine-patterns.js): 1,483 accepted, 1,070 of them with lookarounds, 35 too costly. No match passes its step bound or its memory bound.-refused.js: every refusal reason, that a refused pattern has the JIT's code and its usual result, and that the matcher refuses a pattern the JIT has compiled and reordered.-working-memory.js, with the default limit and with 65,536 bytes: the largest accepted member of two families, found by search, and the next one refused. One family is(?:(a)|(a)|...)*b, whose lists fill up: no match holds more than its bound.-stack.js: 32 levels of groups are accepted and 33 are refused, for five kinds of group. A compile that finds no stack throws aRangeError, and the next use runs on the matcher: with the mode"outOfStack", and for RegExps first used from the deepest frame of a recursion. One run lets JavaScript use the stack to its end, as a thread with a small stack does, and one also leaves native code 70,000 bytes there, where the compiler does run out.-compile-once.js: after 2,000 calls on 8-bit and 16-bit subjects and one match that reads 20,000 characters, three RegExps have asked the matcher three times, one of them a refused one. After the code is deleted an accepted RegExp asks once more.-tiers.js:test,exec,replace,split,searchandmatchin functions the DFG compiles, with a subject the compiler folds and one it cannot./(a*)*b|a*!/on 64aplus!says which engine ran: a backtracking engine answers "no match".LinearProgram::matchasserts both bounds in a build with assertions, andregex-fuzz.jschecks them in thelinearconfigurations.RegExp.cpp, each run against the 18 runs. Every one fails a run.JSTests/regexpLinearEngine.yamlwas run on three of them: a start one character too late fails 14 of its 428 runs, and a case-insensitive group read from the pattern's flags fails 3 runs with the verification and 1 without it (two of upstream's tests only print their failures).Tools/yarr-fuzzon the sources of this head, configurationsjit,interp,linear-verifyandlinear-interp, 10 profiles x 40 seeds x 250 cases: 152,109 cases, no crash and no verification abort. 3 cases differ between the JIT and a matcher configuration. In all three the JIT reaches its limit after 10 to 44 s and answers "no match", andlinear-triage.jsfinds no difference on a search that the interpreter finishes. 103 seeds ran into the time limit of a seed (jit21,interp34,linear-verify20,linear-interp28). In the matcher configurations every slow case but one is a refused pattern. The one accepted case takes 571 ms, and its firstexeccosts 42 to 54 ms on every engine (the classes of a/isvpattern).oven-sh/bunwith this matcher: build 123887 of Add --experimental-linear-regexp: a non-backtracking matcher for the patterns that allow it bun#44384, pinned to an earlier head of this PR (ec05001), passed all 181 jobs: Linux (glibc, musl, ASAN), macOS and Windows, x64 and arm64. Its tests run the matcher through--experimental-linear-regexpon every platform.--useRegExpLinearEngine=1 --verifyRegExpLinearEngine=1(2,645 pass, and the same 2 fail in both with "regular expression too large"). No verification abort.Self-review
DotStarEnclosureis expanded. The memory bound counts the frames and has its own cap. The tiers test. The statistics hook reports the engineexecuses. The semantics file runs on three engines. The global-loop wording and test. The bounds are asserted at run time and in the fuzzer. A refusal report exists outside$vm.Yarr::byteCompileasked the matcher a second time, after the JIT had reordered the pattern, and a RegExp could then get wrong answers. The memory bound assumed that aVectorgrows by a quarter, and it grows by a half. The rest: the order with [JSC] A RegExp search that Yarr abandons throws a RangeError #343, the compile count, the regexp tests under the option, the refusal of an unknown term, the tests and the generators for duplicated names, the runs with the RegExp JIT off, and the exception inside a surrogate pair.RegExp::reset): the stack no longer chooses the engine at all. The matcher read the whole subject for/\.json$/. The rest: the refusal of a reordered pattern, the compile count off theRegExpcell and the assertion on its size,verifyRegExpLinearEngineand its use in the collection, the pin of "option off asks nothing", the run with a small stack, the tests that [JSC] A RegExp search that Yarr abandons throws a RangeError #343 adds left out of the collection, the rule for a test that fails only in the collection, the cross-platform run in bun's CI, and the text of this body (V8's three modes, the open decisions sized, the total work of a search, the first compile of a refused RegExp, the third exception).m_state != JITCodeinhasCodeForandhasMatchOnlyCodeFor, so that the merge with [JSC] Ahead-of-time compilation #757 needs no change. It is one more compare on each match of every RegExp, with the option off too. The arm that [JSC] Ahead-of-time compilation #757 rewrites is on the slow side of that test, and the test and theASSERTnamed above fail until the change is there.optimizeBOLfix for the third exception. It is a bug of the backtracking engines without the option, so it is reported for a PR of its own. This PR pins what the matcher answers.Follow-ups, not in this PR
l). The option is read in three places, and a flag is an||in each.verifyRegExpLinearEnginerun. It can be built when one of them asks for it.