Skip to content

fix(valid): memoise RelaxNG matching once it backtracks (#56) - #59

Merged
Ttimmahlax merged 1 commit into
mainfrom
fix/issue-56
Nov 15, 2026
Merged

Ttimmahlax merged 1 commit into
mainfrom
fix/issue-56

Conversation

@Ttimmahlax

Copy link
Copy Markdown
Contributor

Closes #56.

choice, optional and zeroOrMore try each alternative on copies and discard the work when it fails. Nothing remembered that a pattern had already failed on an element. Alternatives that reach the same definition therefore walked the same subtree again, and n nested two-way choices over a failing document cost 2^n.

Fix

The issue's proposal, with one refinement: match_content's outcome is memoised by (pattern, element, depth).

  • Why depth is in the key: the schema and document never change during a validation. Depth decides whether the nesting bound is reached, so keying on it keeps results exact even next to the bound.
  • When the table is used: only after the first non-fatal failure of a choice, option or repetition. A document that never backtracks pays nothing.
  • What is never stored: interrupts and the depth bound end the run and are not outcomes of the pair.
  • Size: the table stops growing at 2^20 entries.

Results

Twin-choice schema from the issue (failing document; Windows wall time, callgrind Ir):

depth 0.9.3 this PR
12 11.12M Ir 0.75M Ir
22 1,361 ms 0.1 ms
40 does not finish 0.1 ms
200 does not finish 0.6 ms

Cases that never backtrack, plus the real schemas (Ir):

case 0.9.3 this PR
matching document, depth 400 10,936,213 10,944,819 (+0.08%)
zeroOrMore(element), 32,000 siblings 4,146,924,696 4,147,404,757 (+0.01%)
zeroOrMore(choice of 3), 16,000 siblings 5,440,578,182 5,452,533,681 (+0.22%)
DocBook schema, docbook_0.xml 43,180,176 39,950,364 (−7.5%)
OASIS spectest 13,591,829 13,603,262 (+0.08%)

Checks

  • Verdicts and messages are identical on all 159 schema/instance pairs in libxml2's RelaxNG tests (93 valid, 66 invalid).
  • Tests: the workspace passes. A new test covers depths 10–200, failing and matching documents, and an interrupt during backtracking.
  • Builds: clippy is clean for the change, wasm32 builds, and semver-checks requires no update.

Not in scope. zeroOrMore copies the remaining children on every iteration, which makes long sibling runs quadratic: 32,000 siblings take 232 ms. That needs a different data structure and gets its own change.

🤖 Generated with Claude Code

A choice, option or repetition tries each alternative on copies and
throws the work away when it fails. Nothing remembered that a pattern
had already failed on an element, so alternatives reaching the same
definition walked the same subtree again: n nested two-way choices over
a failing document cost 2^n (depth 22: 1.4 s, depth 40: never finished).

match_content's outcome depends only on the pattern, the element and the
depth (which decides whether the nesting bound is reached); the schema
and document never change during a validation. Cx now keeps a table of
those outcomes, consulted only after the first non-fatal failure, so a
document that never backtracks pays nothing. Interrupts and the depth
bound end the run and are never stored; the table stops growing at 2^20
entries.

Failing twin-choice document: depth 12 11.12M -> 0.75M Ir, depth 22
1,361 ms -> 0.1 ms, depth 200 0.6 ms. DocBook schema on docbook_0.xml:
43.18M -> 39.95M Ir (-7.5%). No-backtracking cases: +0.01% to +0.22%
Ir. Verdicts and messages are identical on all 159 schema/instance
pairs of libxml2's RelaxNG tests.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Ttimmahlax
Ttimmahlax merged commit da44a0c into main Nov 15, 2026
8 checks passed
@Ttimmahlax
Ttimmahlax deleted the fix/issue-56 branch November 15, 2026 06:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Enhancement] RelaxNG matcher: remove exponential backtracking on documents that fail validation

2 participants