Skip to content

🐛 fix(xsd): stop validation rescanning padded schema elements - #2113

Merged
gaborbernat merged 11 commits into
tox-dev:mainfrom
gaborbernat:sec-fix/xonbjtt4
Nov 15, 2026
Merged

gaborbernat merged 11 commits into
tox-dev:mainfrom
gaborbernat:sec-fix/xonbjtt4

Conversation

@gaborbernat

@gaborbernat gaborbernat commented Nov 15, 2026 •

Copy link
Copy Markdown
Member

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.

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.
@gaborbernat gaborbernat added bug Something isn't working security Security vulnerability fix labels Nov 15, 2026
@codspeed

codspeed Bot commented Nov 15, 2026 •

Copy link
Copy Markdown

Merging this PR will improve performance by 20.46%

2 warnings

⚠️ 10 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

⚠️ 10 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

⚡ 10 improved benchmarks
✅ 572 untouched benchmarks
⏩ 89 skipped benchmarks1

Performance Changes

Benchmark BASE HEAD Efficiency
⚡ test_feature[validate-attributes-sparse] 960.6 µs 419.9 µs ×2.3
⚡ test_feature[validate-attributes] 1.9 ms 1.4 ms +32.71%
⚡ test_mutating_feature[path-xpath-one-cold] 143 µs 124.6 µs +14.78%
⚡ test_feature[is-valid-one-error] 13.1 ms 11.8 ms +11.3%
⚡ test_feature[validate] 13.1 ms 11.8 ms +11.27%
⚡ test_feature[is-valid-valid] 13.1 ms 11.8 ms +11.26%
⚡ test_feature[is-valid] 13.2 ms 11.8 ms +11.21%
⚡ test_feature[validate-facets-attributes] 6.1 ms 5.6 ms +8.31%
⚡ test_feature[validate-numeric-facets-string] 283.8 µs 268.2 µs +5.82%
⚡ test_feature[validate-numeric-facets] 305.1 µs 290.2 µs +5.11%

Tip

Curious why performance improved? Comment @codspeedbot explain why performance improved on this PR, or directly use the CodSpeed MCP with your agent.


Comparing gaborbernat:sec-fix/xonbjtt4 (207115a) with main (659fcc5)

Open in CodSpeed

Footnotes

  1. 89 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. ↩

# 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.
@gaborbernat
gaborbernat merged commit 5de6956 into tox-dev:main Nov 15, 2026
30 checks passed
gaborbernat added a commit to gaborbernat/turbohtml that referenced this pull request Nov 15, 2026
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working security Security vulnerability fix

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant