mirror of
https://github.com/markedjs/marked.git
synced 2026-11-30 16:48:05 +01:00
perf: close residual O(n²) parse time on unmatched emphasis/strikethrough delimiter runs (follow-up to #4099 / #4103) (#4196)
* perf: skip dead emphasis/strikethrough delim runs in inline pass Follow-up to #4099 / #4103. When an input line contains a long run of `*`, `_` or `~` with no closer anywhere ahead in maskedSrc, every opener position in Tokenizer.emStrong / Tokenizer.del was correctly early-returning via the lastPossibleCloserIndex guard introduced by #4103, but the outer inline loop still advanced one character at a time through the run. The `^\*+` greedy anchor in emStrongLDelim re-scans the whole run on each call, so the inline pass remains O(n^2) even though the inner closer scan is now O(1). Measured on master before this change: ``` **` x 20000 + 'x' 8.73 s __` x 20000 + 'x' 11.30 s *` x 40000 + 'x' 8.73 s _` x 40000 + 'x' 11.30 s ``` With this change the same inputs render in ~0.001 s. The CommonMark spec test suite (1983 cases) and the project's unit tests (242 cases) continue to pass. A new fixture `em_strong_unmatched_delim_run` documents the behavior for closer-less `*`, `_`, and `~` runs. The fix checks the cached last-possible-closer index that #4103 already maintains; when no closer exists after the current position, the entire run of same-delim characters is consumed as literal text in one step. Behavior is unchanged (runs without a closer have always rendered as literal text); only the time to reach that result changes. * test(redos): regression test for closer-less delimiter run quadratic Mirrors the quadratic_em_mask.cjs / quadratic_br.cjs style. Each of the four cases fails on 18.1.3 baseline (times out around 2.3-3.0s per case in test:specs) and passes in 5-7ms with the fix. - '**' x 10000 + 'x' -> <p>**...**x</p> - '__' x 10000 + 'x' -> <p>__...__x</p> - '*' x 20000 + 'x' -> <p>*...*x</p> - '_' x 20000 + 'x' -> <p>_..._x</p> Requested by @UziTech in review of #4196.
This commit is contained in:
+46
-1
@@ -1,7 +1,7 @@
|
||||
import { _Tokenizer } from './Tokenizer.ts';
|
||||
import { _defaults } from './defaults.ts';
|
||||
import { other, block, inline } from './rules.ts';
|
||||
import { beginEmStrongDelCloserScan, endEmStrongDelCloserScan, normalizeLabel } from './helpers.ts';
|
||||
import { beginEmStrongDelCloserScan, cachedLastPossibleDelCloserIndex, cachedLastPossibleEmStrongCloserIndex, endEmStrongDelCloserScan, normalizeLabel } from './helpers.ts';
|
||||
import type { Token, TokensList, Tokens } from './Tokens.ts';
|
||||
import type { MarkedOptions } from './MarkedOptions.ts';
|
||||
|
||||
@@ -465,6 +465,51 @@ export class _Lexer<ParserOutput = string, RendererOutput = string> {
|
||||
continue;
|
||||
}
|
||||
|
||||
// #4099 follow-up: for a run of `*` or `_` with no possible closer
|
||||
// anywhere ahead in maskedSrc, every opener position fails identically
|
||||
// and the outer loop walks the run char-by-char, giving O(n^2) time.
|
||||
// Consume the whole run as literal text in one step.
|
||||
const firstChar = src[0];
|
||||
if (firstChar === '*' || firstChar === '_') {
|
||||
const pos = maskedSrc.length - src.length;
|
||||
const lastCloser = cachedLastPossibleEmStrongCloserIndex(this.tokenizer, maskedSrc, firstChar);
|
||||
if (lastCloser <= pos) {
|
||||
let runEnd = 1;
|
||||
while (runEnd < src.length && src.charCodeAt(runEnd) === firstChar.charCodeAt(0)) runEnd++;
|
||||
const raw = src.substring(0, runEnd);
|
||||
const lastToken = tokens.at(-1);
|
||||
if (lastToken?.type === 'text' && !lastToken.escaped) {
|
||||
lastToken.raw += raw;
|
||||
lastToken.text += raw;
|
||||
} else {
|
||||
tokens.push({ type: 'text', raw, text: raw, escaped: false } as Tokens.Text);
|
||||
}
|
||||
src = src.substring(runEnd);
|
||||
prevChar = firstChar;
|
||||
keepPrevChar = true;
|
||||
continue;
|
||||
}
|
||||
} else if (firstChar === '~') {
|
||||
const pos = maskedSrc.length - src.length;
|
||||
const lastCloser = cachedLastPossibleDelCloserIndex(this.tokenizer, maskedSrc);
|
||||
if (lastCloser <= pos) {
|
||||
let runEnd = 1;
|
||||
while (runEnd < src.length && src.charCodeAt(runEnd) === 126 /* ~ */) runEnd++;
|
||||
const raw = src.substring(0, runEnd);
|
||||
const lastToken = tokens.at(-1);
|
||||
if (lastToken?.type === 'text' && !lastToken.escaped) {
|
||||
lastToken.raw += raw;
|
||||
lastToken.text += raw;
|
||||
} else {
|
||||
tokens.push({ type: 'text', raw, text: raw, escaped: false } as Tokens.Text);
|
||||
}
|
||||
src = src.substring(runEnd);
|
||||
prevChar = firstChar;
|
||||
keepPrevChar = true;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
// em & strong
|
||||
if (token = this.tokenizer.emStrong(src, maskedSrc, prevChar)) {
|
||||
src = src.substring(token.raw.length);
|
||||
|
||||
@@ -0,0 +1,6 @@
|
||||
<p>****************x</p>
|
||||
<p>__________________x</p>
|
||||
<p>**********x</p>
|
||||
<p>__________x</p>
|
||||
<p>a ~~~~~~~~x</p>
|
||||
<p>a~~~~~~~b</p>
|
||||
@@ -0,0 +1,11 @@
|
||||
****************x
|
||||
|
||||
__________________x
|
||||
|
||||
**********x
|
||||
|
||||
__________x
|
||||
|
||||
a ~~~~~~~~x
|
||||
|
||||
a~~~~~~~b
|
||||
@@ -0,0 +1,18 @@
|
||||
module.exports = [
|
||||
{
|
||||
markdown: '**'.repeat(10000) + 'x',
|
||||
html: `<p>${'**'.repeat(10000)}x</p>`,
|
||||
},
|
||||
{
|
||||
markdown: '__'.repeat(10000) + 'x',
|
||||
html: `<p>${'__'.repeat(10000)}x</p>`,
|
||||
},
|
||||
{
|
||||
markdown: '*'.repeat(20000) + 'x',
|
||||
html: `<p>${'*'.repeat(20000)}x</p>`,
|
||||
},
|
||||
{
|
||||
markdown: '_'.repeat(20000) + 'x',
|
||||
html: `<p>${'_'.repeat(20000)}x</p>`,
|
||||
},
|
||||
];
|
||||
Reference in New Issue
Block a user