Skip to content

perf: keep long attribute values and character runs at their own size - #1870

Closed
auscaster wants to merge 1 commit into
inikulin:masterfrom
auscaster:gather-appended-strings
Closed

auscaster wants to merge 1 commit into
inikulin:masterfrom
auscaster:gather-appended-strings

Conversation

@auscaster

Copy link
Copy Markdown

The tokenizer builds attribute values and character tokens by appending one code point at a time (value += ch, chars += ch). In V8 every += leaves a rope node behind until the string is read, so a long run holds tens of bytes per character while it is being built. Real pages have long runs: an inline data: image in an attribute, or a large inline script.

We hit this parsing a vendor page that embeds a 7.3 MB base64 PNG in an SVG xlink:href. Parsing it held about 222 MB of heap for that one value, enough to exhaust a 384 MB worker.

This keeps short runs as native strings, so the common path only adds a length check. A run that reaches 1,024 characters moves into a small list of pieces, and every 32 pieces are joined into one flat string. The value is settled when the next attribute starts or the tag is emitted, and a character token is settled when it is emitted. The tokens the tree builder receives are unchanged.

Measured against master, built locally:

master this PR
4 MB attribute value (held after parse) 131 MB 24 MB
4 MB inline script (held after parse) 116 MB 35 MB
1,281 real vendor pages, mean time per pass over 6 interleaved rounds 8,807 ms 8,686 ms

All existing tests pass (19,327). One test is added for long attribute values and character runs with character references at the spill boundary.

@43081j

43081j commented Oct 10, 2026

Copy link
Copy Markdown
Collaborator

i had a dig through this as im not a huge fan of having to add a bunch of logic around "spilled strings".

all we're talking about here is appending chunks rather than characters when we're dealing with large strings. so i wonder if this can be much simpler...

  • keep track of the start position of an attribute value (or character data)
  • if the current character is not a terminator (quotes, EOF, etc), increment an end position from that
  • once you hit a terminator or a transformed character (i.e. source[i] !== cp), append the slice and reset

i tried this in a branch and it seems faster since we're not accumulating or joining arrays, and uses the same or less memory.

once i clean the branch up a bit ill push it so you can take a look and tell me if you still think this approach is better

@fb55

fb55 commented Oct 25, 2026

Copy link
Copy Markdown
Collaborator

Opened #1897 with Jame's solution; ended up working quite well.

@fb55

fb55 commented Oct 28, 2026

Copy link
Copy Markdown
Collaborator

Thanks @auscaster! Merged the other PR instead, with you as a co-author for the tests that were adopted

@fb55 fb55 closed this Oct 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants