Repository navigation
Conversation
Scan each inline pass once for valid bare GFM email autolink ranges instead of re-running the email look-ahead in inlineText and bare email scan in url from every token position. Fixes markedjs#4272
|
@yxshee is attempting to deploy a commit to the MarkedJS Team on Vercel. A member of the Team first needs to authorize it. |
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
|
|
||
| // Sticky matcher for the domain after `@` in a bare GFM email autolink, | ||
| // shared with the per-pass email scan in helpers.ts. | ||
| export const emailDomainSticky = edit(_emailDomain, 'y').getRegex(); |
There was a problem hiding this comment.
Add this regex to other if you want to export it.
There was a problem hiding this comment.
The other tests should cover the changes so we don't need the lexer unit tests
| const cap = noEmailRule.exec(src); | ||
| if (cap) { | ||
| raw = cap[0]; | ||
| if (!this.lexer.state.inLink && frame.emails.length > 0) { | ||
| const nextStart = findNextBareEmailStart(frame, frame.pos, frame.pos + raw.length, frame.pos + src.length); | ||
| if (nextStart !== -1) { | ||
| raw = raw.slice(0, nextStart - frame.pos); | ||
| } | ||
| } | ||
| } |
There was a problem hiding this comment.
This makes a list of plain emails quadratic, which is linear on main. noEmailRule has no email stop, so it runs to the next delimiter (often the end of the paragraph) and only then is the match cut at the next email. Every gap between two emails rescans the rest:
marked.parse('a@b.co '.repeat(n))
// n main this PR
// 5000 4 ms 0.6 s
// 10000 6 ms 2.3 s
// 20000 10 ms 9.6 s
// 40000 18 ms 38 s (280 KB)The start of the next email is known before the regex runs, so the input can be cut first:
| const cap = noEmailRule.exec(src); | |
| if (cap) { | |
| raw = cap[0]; | |
| if (!this.lexer.state.inLink && frame.emails.length > 0) { | |
| const nextStart = findNextBareEmailStart(frame, frame.pos, frame.pos + raw.length, frame.pos + src.length); | |
| if (nextStart !== -1) { | |
| raw = raw.slice(0, nextStart - frame.pos); | |
| } | |
| } | |
| } | |
| const end = frame.pos + src.length; | |
| const nextStart = !this.lexer.state.inLink && frame.emails.length > 0 | |
| ? findNextBareEmailStart(frame, frame.pos, end, end) | |
| : -1; | |
| raw = noEmailRule.exec(nextStart === -1 ? src : src.slice(0, nextStart - frame.pos))?.[0]; |
With that 40000 emails take 22 ms, and 150k random inputs (emails, links, emphasis, html, breaks, pedantic, an inline extension) render the same as main (inputs without www.; with it the PR already differs, e.g. +www.x.co/@u is no longer a link, same as GitHub). findNextBareEmailStart then no longer needs its rawEnd argument. A redos spec with 'a@b.co '.repeat(40000) would guard it.
There was a problem hiding this comment.
Great catch, thank you @bardrop! Applied the pre-slice fix in Tokenizer.inlineText, removed the unused rawEnd parameter from findNextBareEmailStart, and added 'a@b.co '.repeat(40000) to test/specs/redos/quadratic_inline_text_email.cjs in 45e83f2 ('a@b.co '.repeat(40000) dropped from ~36.5 s to ~25 ms locally on Node v26.7.0, while the original *a* / a_ / _a_ cases stay ~8–22 ms).
|
Tested 45e83f2 against another input that hits the same email lookahead: intraword underscores, since
A CPU profile on master puts nearly all of the time in the |
|
Thanks for profiling and sharing the intraword underscore numbers, @shubham21155102! Added |
Marked version: 18.2.1 (
master@b20f69f2)Markdown flavor: GitHub Flavored Markdown
Description
textrule: its email look-ahead rereads the rest of the paragraph #4272Cause
In GFM mode,
inlineGfm.text(andinlineBreaks.text) ran an email look-ahead ((?=[a-zA-Z0-9.!#$%&'*+\/=?_{|}~-]+@)) at the start and in the lazy tail of every text token, andinlineGfm.urlre-scanned[A-Za-z0-9.+-]+(@)...at every position. On paragraphs of repeated emphasis spans (a,a), underscore-joined words (a), or punctuation runs (a!) — both with and without an@later in the paragraph — each token rescanned to the end of the run, resulting inO(n^2)` time.Fix
inlineTokenspass once inO(n)time via a per-passWeakMapframe (beginInlineEmailScan/endInlineEmailScaninsrc/helpers.ts, matching the#4099closer-scan lifecycle so source strings are never retained andTokenizergains no public methods).@skip the scan entirely.@locate every@once, walk backward over[A-Za-z0-9._+-](pairwise disjoint because@is excluded), validate the domain with stickyemailDomainSticky, and record sorted disjoint[start, at)bare email ranges.Tokenizer.urlchecksurlGfmNoEmail(guarded by anO(1)first-character check for protocol/www.starters) and resolves bare email autolinks inO(1)viafindBareEmailAt.Tokenizer.inlineTextmatchesinlineTextGfmNoEmail/inlineTextBreaksNoEmail(without the bare email look-aheads) and clips atfindNextBareEmailStartinO(1), respectingstate.inLinkandstartInlineextension clipping.rules.inline.url/rules.inline.textoverrides and directtokenizer.url()/tokenizer.inlineText()calls outsideinlineTokensfall back to the existing regexes. The fallbackinlineGfm.textlook-ahead class is also tightened to[A-Za-z0-9._+-]to matchinlineGfm.url.(Note: like #4225, non-boundary
www.sequences such aswww.foo_@b.co_ `` before an invalid email domain now remain plain text rather than accidentally matchingwww.mid-token, consistent with GFM §6.5.)Benchmarks (Node v26.7.0)
5k / 10k / 20kreps)'*a*''a_''_a_''a_'+'@''a_'+' foo@bar.com''*a*'+'b@example.com'Spec suite throughput (100x all 1,332 CommonMark + GFM specs):
gfm: false325 ms -> 287 ms,gfm: true346 ms -> 343 ms.Contributor
test/specs/redos/quadratic_inline_text_email.cjs(9 pathological cases, all >1 s before and <25 ms after) and edge-case unit tests intest/unit/Lexer.test.js. Fullnpm test(test:specs2059 passing,test:unit276 passing,test:umd,test:cjs,test:types,test:lint) passes locally.Committer
In most cases, this should be a different person than the contributor.