Skip to content

⚡️ perf(xml): copy plain attribute values once - #2464

Merged
gaborbernat merged 3 commits into
tox-dev:mainfrom
gaborbernat:perf/xml-attr-values
Nov 18, 2026
Merged

gaborbernat merged 3 commits into
tox-dev:mainfrom
gaborbernat:perf/xml-attr-values

Conversation

@gaborbernat

Copy link
Copy Markdown
Member

The XML parser widened every attribute value into a scratch buffer and then copied it again into the arena. For large values both allocations cross glibc's mmap threshold, so every parse mapped and faulted fresh pages: 180 minor faults per parse on a 107k-character value, where lxml has none. That made parse-xml-values the one XML parse row where lxml kept up with turbohtml.

A 1-byte value with no reference and no whitespace to normalize is now widened straight into its final arena allocation, and th_node_attr_adopt takes that buffer instead of copying it. Values that need reference expansion or attribute-value normalization still go through the scratch buffer.

Release build, best of 15:

row before after
parse-xml-values, 107k value characters 108 µs 26–28 µs
parse-xml-values, 64 KiB clean value 60 µs 30 µs
parse-xml-values, 64 KiB references 71–74 µs 70–79 µs
parse-xml-attrs, 1000 attributes 45 µs 43 µs

A differential run over 60,000 random documents (whitespace, references, control and wide characters, namespace declarations, duplicates) gave identical values, serializations, errors and exceptions against main.

The XML parser copied every attribute value twice: it widened the value
into a scratch buffer, then th_node_attr_append copied that into the
arena. Both allocations exceed glibc's mmap threshold, so each parse
mapped and faulted fresh pages: 180 minor faults per parse for a
107k-character value.

A 1-byte value with no reference and no whitespace to normalize is now
widened straight into its arena allocation. The scratch buffer is used
only for values that need reference expansion or attribute-value
normalization. th_node_attr_append becomes th_node_attr_adopt, which
takes the arena buffer instead of copying it. Namespace declarations
read the value they are given rather than the scratch buffer.

Release build, best of 15:

  parse-xml-values, 107k value characters  108 us -> 26-28 us
  parse-xml-values, 64 KiB clean value      60 us -> 30 us
  parse-xml-values, 64 KiB references       71-74 us -> 70-79 us
  parse-xml-attrs, 1000 attributes          45 us -> 43 us
  parse-xml-text, 64 KiB clean text         10.2 us -> 10.2 us

Minor faults per parse on the 107k row drop from 180 to 0.3. A
differential run of 60,000 random documents (whitespace, references,
control and wide characters, namespace declarations, duplicates) gave
identical values, serializations, errors and exceptions.
@gaborbernat gaborbernat added the enhancement New feature or request label Nov 18, 2026
@codspeed

codspeed Bot commented Nov 18, 2026 •

Copy link
Copy Markdown

Merging this PR will regress 6 benchmarks

2 warnings

⚠️ 11 benchmarks with a significant change ran on different hardware

Their base and head results were measured on machines with a different CPU, core count or memory, so the changes might not come from the code. Run base and head on the same runner type.

Learn more about Macro Runners

See affected benchmarks

⚠️ 11 benchmarks with a significant change ran in a different software environment

Their base and head results were measured with a different OS or toolchain, such as compiler, runtime or library versions, so the changes might come from this difference rather than from the code. Open an affected benchmark to see what changed.

See affected benchmarks

⚡ 5 improved benchmarks
❌ 6 regressed benchmarks
✅ 569 untouched benchmarks
⏩ 91 skipped benchmarks1

Warning

Please fix the performance issues or acknowledge them on CodSpeed.

Performance Changes

Benchmark BASE HEAD Efficiency
❌ test_feature[canonicalize] 6 ms 6.5 ms -7.19%
❌ test_feature[encode-inner] 6.7 ms 7.2 ms -6.42%
❌ test_feature[encode-inner-indent] 7.1 ms 7.5 ms -6.21%
❌ test_feature[serialize-inner] 5.5 ms 5.8 ms -5.26%
❌ test_feature[parse-xml-text-references] 1.8 ms 1.9 ms -5.19%
❌ test_feature[serialize-inner-indent] 5.9 ms 6.2 ms -5.03%
⚡ test_feature[parse-xml-values-tiny] 1,323.8 µs 840.6 µs +57.5%
⚡ test_feature[parse-xml-values] 826.8 µs 530.7 µs +55.79%
⚡ test_feature[serialize-named-small] 861.2 µs 816 µs +5.54%
⚡ test_mutating_feature[observe-registrations] 1.2 ms 1.1 ms +5.26%
⚡ test_feature[transform-text-literal] 5.8 ms 5.6 ms +5.05%

Tip

Investigate this regression by commenting @codspeedbot fix this regression on this PR, or directly use the CodSpeed MCP with your agent.


Comparing gaborbernat:perf/xml-attr-values (aaa53cb) with main (01b4635)

Open in CodSpeed

Footnotes

  1. 91 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports. ↩

Moving the 1-byte value scan out of consume_attribute made the quote an
opaque parameter, so the compiler kept a quote test on every byte above
'<' that it had dropped while the loop was inlined after the quote check.
The scan now derives the quote from a flag, and parse-xml-values falls
from 607k to 477k instructions under callgrind, 4% below main.
@gaborbernat
gaborbernat merged commit bed1cb8 into tox-dev:main Nov 18, 2026
60 of 65 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant