Repository navigation
🐛 fix(xsd): stop validation rescanning padded schema elements - #2113
Conversation
Validation looked up the children of a schema element by name with a scan over all of them. Every instance element that reached a complex type, a group, an attribute group, an attribute or an element declaration scanned all children of that declaration, including the children that are not XSD and declare nothing. The model group loops (content matching and the element declaration collection) walked every child of a model group for each parent element. A schema with many such children made validation time grow with the product of the schema size and the number of instance elements. Compilation now enters each XSD child that validation reads under its parent while it walks the schema: the first child of each name, the first model group, the particles in document order and the attribute declarations in document order. The entries are sorted by node once the walk ends, and validation finds them with a binary search. Children of parents validation never reads, such as simple types, are not entered. The compile steps that run before the sort keep scanning children. Results are unchanged. The first child of each name, the content model preference (sequence, choice, all, group) and the order in which attributes and particles are read match the scans they replace.
Merging this PR will improve performance by 20.46%2 warnings
Performance Changes
Tip Curious why performance improved? Comment Comparing Footnotes
|
# Conflicts: # src/turbohtml/_c/validate/xsd.h
The compile walk classified every child of an indexed parent from the parent's visit and again on the child's own visit, then sorted the child entries by node. Each element is now classified once and entered under its parent from its own visit, and the entries are reached through the qname cache index, so no sort is needed.
# Conflicts: # src/turbohtml/_c/validate/xsd.h
Validation now resolves a group ref's model group from the child index, so the branch for a group without one needs a validation-time test.
An element with no namespace inside xs:all is not an XSD particle, so the group declares nothing. CI's 100% C line gate found the no-namespace branch of is_schema_el() uncovered on this branch.
The merge with tox-dev#2113 moved xsd_check_names to a switch on the child kind, so the xs:list and xs:union branches read locals that no longer exist. Names outside the kind table reach the XSD_CHILD_KINDS branch, so the two checks run there after the identity constraint check, in the same order.
Schema validation scanned every child of a schema element to find the few it needs. A schema element with many unrelated children therefore cost a full scan for each instance element validated against it, so validation time grew with the product of the schema size and the instance size. ⚡ After this change compilation records the children validation reads under their parent, and validation reads that record instead of scanning.
Architecture
Under XML Schema Part 1, a complex type's content is one model group, so validation has no use for the other children of a type. Compilation keeps one record per schema element and, for each parent whose children validation reads, an entry holding the first child of each name, the first model group, the particles in document order and the attribute children. Content lookups, attribute walks, element declarations and model group matching read that entry.
Compile-time checks that run while the entries are still being filled scan the children directly, as before. Simple types and the schema root get no entry, since validation never reads their children.
Behavior changes
Verdicts, error messages and their order are unchanged, and schemas compile or fail as before. Memory grows with the schema size, since the schema keeps one record per element and one entry per parent whose children validation reads.