Skip to content

Commit ea24015

Browse files
fb55claude
andcommitted
perf(sax-parser): keep the namespace stack at its tail
The feedback simulator kept the current namespace at index 0 and used `unshift`/`shift`, moving the whole stack on every foreign element, which grows quadratically with the nesting depth of foreign content. It now pushes and pops at the end and reads the parent namespace from `length - 2`, guarding the case without a parent. The test counts stack writes for 400 nested `svg`/`foreignObject` pairs. Co-Authored-By: Claude Fable 5.1 <[email protected]>
1 parent ac2f60d commit ea24015

2 files changed

Lines changed: 42 additions & 9 deletions

File tree

‎packages/parse5-sax-parser/lib/parser-feedback-simulator.ts‎

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -86,14 +86,14 @@ export class ParserFeedbackSimulator implements TokenHandler {
8686

8787
//Namespace stack mutations
8888
private _enterNamespace(namespace: html.NS): void {
89-
this.namespaceStack.unshift(namespace);
89+
this.namespaceStack.push(namespace);
9090
this.inForeignContent = namespace !== html.NS.HTML;
9191
this.tokenizer.inForeignNode = this.inForeignContent;
9292
}
9393

9494
private _leaveCurrentNamespace(): void {
95-
this.namespaceStack.shift();
96-
this.inForeignContent = this.namespaceStack[0] !== html.NS.HTML;
95+
this.namespaceStack.pop();
96+
this.inForeignContent = this.namespaceStack[this.namespaceStack.length - 1] !== html.NS.HTML;
9797
this.tokenizer.inForeignNode = this.inForeignContent;
9898
}
9999

@@ -148,7 +148,7 @@ export class ParserFeedbackSimulator implements TokenHandler {
148148
if (foreignContent.causesExit(token)) {
149149
this._leaveCurrentNamespace();
150150
} else {
151-
const currentNs = this.namespaceStack[0];
151+
const currentNs = this.namespaceStack[this.namespaceStack.length - 1];
152152

153153
if (currentNs === html.NS.MATHML) {
154154
foreignContent.adjustTokenMathMLAttrs(token);
@@ -193,7 +193,8 @@ export class ParserFeedbackSimulator implements TokenHandler {
193193
let tn = token.tagID;
194194

195195
if (!this.inForeignContent) {
196-
const previousNs = this.namespaceStack[1];
196+
const previousNs =
197+
this.namespaceStack.length > 1 ? this.namespaceStack[this.namespaceStack.length - 2] : undefined;
197198

198199
if (previousNs === html.NS.SVG) {
199200
const adjustedTagName = foreignContent.SVG_TAG_NAMES_ADJUSTMENT_MAP.get(token.tagName);
@@ -204,18 +205,18 @@ export class ParserFeedbackSimulator implements TokenHandler {
204205
}
205206

206207
//NOTE: check for exit from integration point
207-
if (foreignContent.isIntegrationPoint(tn, previousNs, token.attrs)) {
208+
if (previousNs !== undefined && foreignContent.isIntegrationPoint(tn, previousNs, token.attrs)) {
208209
this._leaveCurrentNamespace();
209210
}
210211
} else if (
211-
(tn === $.SVG && this.namespaceStack[0] === html.NS.SVG) ||
212-
(tn === $.MATH && this.namespaceStack[0] === html.NS.MATHML)
212+
(tn === $.SVG && this.namespaceStack[this.namespaceStack.length - 1] === html.NS.SVG) ||
213+
(tn === $.MATH && this.namespaceStack[this.namespaceStack.length - 1] === html.NS.MATHML)
213214
) {
214215
this._leaveCurrentNamespace();
215216
}
216217

217218
// NOTE: adjust end tag name as well for consistency
218-
if (this.namespaceStack[0] === html.NS.SVG) {
219+
if (this.namespaceStack[this.namespaceStack.length - 1] === html.NS.SVG) {
219220
foreignContent.adjustTokenSVGTagName(token);
220221
}
221222

‎packages/parse5-sax-parser/test/parser-feedback-simulator.test.ts‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { it, assert } from 'vitest';
12
import { generateTokenizationTests } from 'parse5-test-utils/utils/generate-tokenization-tests.js';
23
import { ParserFeedbackSimulator } from '../lib/parser-feedback-simulator.js';
34

@@ -8,3 +9,34 @@ generateTokenizationTests(
89
feedbackPath.pathname,
910
(handler) => new ParserFeedbackSimulator({}, handler).tokenizer,
1011
);
12+
13+
it('does constant work per namespace push and pop', () => {
14+
const noop = (): void => {};
15+
const simulator = new ParserFeedbackSimulator(
16+
{},
17+
{
18+
onStartTag: noop,
19+
onEndTag: noop,
20+
onCharacter: noop,
21+
onWhitespaceCharacter: noop,
22+
onNullCharacter: noop,
23+
onComment: noop,
24+
onDoctype: noop,
25+
onEof: noop,
26+
},
27+
);
28+
let writes = 0;
29+
const stack = Reflect.get(simulator, 'namespaceStack') as unknown[];
30+
Reflect.set(
31+
simulator,
32+
'namespaceStack',
33+
new Proxy(stack, {
34+
set(target, key, value): boolean {
35+
writes++;
36+
return Reflect.set(target, key, value);
37+
},
38+
}),
39+
);
40+
simulator.tokenizer.write('<svg><foreignObject>'.repeat(400) + '</foreignObject></svg>'.repeat(400), true);
41+
assert.isBelow(writes, 400 * 10);
42+
});

0 commit comments

Comments
 (0)