Skip to content

Commit fcce48d

Browse files
fb55claude
andcommitted
perf(parser): find open elements without scanning a deep stack
Searches of the stack of open elements walked it from the top: the scope checks, the search for a list item to close, generic end tags in HTML and in foreign content, insertion-mode resets, foster parenting and the adoption agency. On a deep stack every such token paid for the depth, so `<p><button>` followed by N `<div>`s, or `<b>` followed by N `<span><div>` pairs and N `</b>`, took quadratic time. Once 64 elements are open, the stack indexes them: elements with the same tag in the same namespace are chained, so the topmost is found without a scan, and each slot counts the special elements, list item boundaries and scope boundaries at or below it, so whether one lies between two elements is a comparison. Open formatting elements are found by element. Stacks that never reach 64 elements are scanned, which costs less than keeping the index on every push and pop. On an indexed stack, removing an element from the middle leaves its slot free instead of moving every element above it down, and the adoption agency moves only the furthest block and the formatting elements it kept below it, so each of its steps costs the elements it passes, not the depth of the stack. `replaceFormattingElement` replaces `insertAfter`, which only the adoption agency used. The stack tests count reads of the stack's arrays for each kind of search behind a deep stack, with the target missing or behind a boundary; each is constant per token, and the reset rows that counted the entries a reset passed are among them. The stack's unit tests check the searches after removals from the middle and the adoption agency's step, on a scanned and on an indexed stack, and a parser test parses inputs that use each search behind 64 open elements as behind none. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent e2901d0 commit fcce48d

6 files changed

Lines changed: 813 additions & 310 deletions

File tree

‎packages/parse5/lib/parser/index.test.ts‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import {
44
parse,
55
InsertionMode,
66
serialize,
7+
defaultTreeAdapter,
8+
html,
79
type ElementCreationContext,
810
type TreeAdapter,
911
type DefaultTreeAdapterMap,
@@ -322,3 +324,35 @@ describe('InsertionMode', () => {
322324
assert.strictEqual(InsertionMode.AFTER_AFTER_FRAMESET, 22);
323325
});
324326
});
327+
328+
// Behind 64 open elements, the stack of open elements is indexed, and the adoption agency leaves free slots.
329+
const deepOpen = '<x-p>'.repeat(64);
330+
const deepClose = '</x-p>'.repeat(64);
331+
332+
it.each([
333+
'<a><b><big><em><strong><div>X</a>Y</div>Z',
334+
'<b><em><foo><foo><foo><aside></b></em>Y</aside>Z',
335+
'<u><i><bar><b><center></i></u>x</b>y',
336+
'<cite><b><cite><i><cite><i><cite><i><div>X</b>TEST',
337+
'<a href=a>aa<marquee>aa<a href=b>bb</marquee>aa',
338+
'<a><p><a>x',
339+
'<li><div><li>x',
340+
'<dd><div><dt>x',
341+
'<ul><li><ol></li>x',
342+
'<p><button></p>x',
343+
'<h1><div><h2>x</h1>y',
344+
'<x-a><span></x-a>x',
345+
'<table><b><tr><td>aaa</td></tr>bbb</table>ccc',
346+
'<table><tr><td><select><td>x',
347+
'<svg><g><foo></g>x',
348+
'<x-a><form><span></form></x-a>x',
349+
])('parses %s behind a deep stack as behind a shallow one', (input) => {
350+
expect(serialize(parseFragment(deepOpen + input))).toBe(deepOpen + serialize(parseFragment(input)) + deepClose);
351+
});
352+
353+
it.each([0, 64])('closes elements in a fragment context outside HTML, SVG and MathML behind %i elements', (depth) => {
354+
const context = defaultTreeAdapter.createElement('c', html.NS.XML, []);
355+
const open = '<y>'.repeat(depth);
356+
357+
expect(serialize(parseFragment(context, `${open}<x></x>after`))).toBe(`${open}<x></x>after${'</y>'.repeat(depth)}`);
358+
});

0 commit comments

Comments
 (0)