· via dev.to (home feed)
Patch fixes quadratic slowdown in libxml2 streaming XSD validation of base64 attachments
A dev.to post documents a quadratic slowdown in libxml2's streaming XSD validator that makes a 30 MB e-invoice attachment take about 16 seconds to validate, and a patch that cuts it to 0.16 s.

Electronic invoices often embed their attachments directly in the XML — the invoice PDF, a delivery note, a timesheet — each base64-encoded into a single element such as UBL's cbc:EmbeddedDocumentBinaryObject (business term BT-125 in EN 16931), so a 20 MB PDF becomes a 27 MB text node. According to a post on dev.to, validating one of these documents against its XSD with libxml2's streaming mode can take minutes, and the bottleneck is not the document as a whole but that one element.
The slowdown
The post's author benchmarked xmllint with streaming schema validation from libxml2 2.15.4 — run with --huge, which lifts the 10 MB cap on a single text node — on an Apple M4, using a minimal schema with one xs:base64Binary element and documents containing only base64 in 76-character lines. A 1 MB attachment validated in 0.03 seconds, 10 MB took 1.77 seconds, 30 MB took 15.93 seconds, and 100 MB took 187.27 seconds. Each doubling of the input roughly quadruples the runtime, the pattern of a quadratic algorithm: an invoice with a 30 MB attachment validates more slowly than a 1 GB document without one.
Where the time goes
libxml2 can validate by building a full tree and checking it against the schema, or by validating while reading through the streaming xmlTextReader interface — the option chosen when a document may not fit in memory. In streaming mode the parser passes text to the schema validator in small pieces; an instrumented build cited in the post shows those pieces are at most 512 bytes, meaning a 30 MB text node arrives as roughly 62,000 fragments.
The validator needs the whole value to check it against its type, so xmlSchemaVPushText glues the fragments together with xmlStrncat. That function measures the existing string with strlen, reallocates the buffer to exactly the new size, then copies the new fragment in. Across 62,000 appends the length scans collectively touch on the order of a terabyte of memory to assemble a 30 MB string, and each exact-size reallocation forces an additional copy when the allocator cannot extend the block in place.
The tree-based path does not suffer this, because the text node already exists as one piece; the same 30 MB document validates in 0.17 seconds there. Most validating tools build a tree, which the post offers as a likely reason the underlying issue — filed by libxml2's maintainer in 2023 — stayed open: it only appears in the streaming mode people use for large documents.
The fix
The remedy is the standard one for repeated string concatenation: keep the used length and allocated capacity beside the buffer, copy only the incoming fragment on each append, and double the capacity when it fills, so total copying stays proportional to the final size. The patch adds two size_t fields to the validator's per-element node-info structure and rewrites the append accordingly, touching about fifty lines of xmlschemas.c. It applies to libxml2 2.13 and 2.15.
Two edge cases receive specific care, per the post. The first fragment of a value can point into memory the validator does not own, so the first append copies it into an owned buffer — the sole remaining strlen call. And lengths are checked against INT_MAX before adding, because the rest of libxml2 measures strings in int, and a validator fed untrusted documents should not be where a length wraps.
On the same machine and files, the patched build validates a 10 MB attachment in 0.05 seconds, 30 MB in 0.16 seconds, and 100 MB in 0.53 seconds — linear behaviour, roughly five milliseconds per megabyte.
Going upstream
The author's validator, xmlvalid, compiles its own libxml2 for both a native engine and a WebAssembly build that runs in the browser, so it already carries the patch everywhere. The change has also been submitted to libxml2 as merge request !471, which would close issue #516; at the time of writing it passes CI and is awaiting review.
If you stream large documents
Until a release includes the fix, the post advises anyone streaming XML through libxml2 against an XSD — directly or via a binding — to benchmark against the largest text node, not the largest file: a 2 GB file of small elements is fast, while a 30 MB file with one huge element is not. Base64 content is the typical offender, from embedded PDFs and images in invoices to SOAP attachments and inline certificates in signed documents. Tree-based validation of the same file is fast if the document fits in memory, at the cost of holding that memory.
Why it matters
libxml2 sits under an enormous amount of software: it ships in essentially every Linux distribution and backs PHP's XML extensions, Python's lxml, and Ruby's Nokogiri. Any of those users streaming documents with large base64 text nodes — a routine case in UBL and EN 16931 e-invoicing, not an edge case — currently pays the quadratic penalty. The fix is small, confined to xmlschemas.c, and already passing CI upstream, so for affected systems the open question is mostly when the next libxml2 release lands, and what to do in the meantime.
- #libxml2
- #xml
- #xsd
- #performance
- #e-invoicing