Repository navigation
⚡️ perf(xml): copy plain attribute values once - #2464
Conversation
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.
Merging this PR will regress 6 benchmarks2 warnings
Warning Please fix the performance issues or acknowledge them on CodSpeed. Performance Changes
Tip Investigate this regression by commenting Comparing Footnotes
|
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.
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-valuesthe 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_adopttakes 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:
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.