Repository navigation
fix(valid): memoise RelaxNG matching once it backtracks (#56) - #59
Merged
Merged
Conversation
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #56.
choice,optionalandzeroOrMoretry 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).Results
Twin-choice schema from the issue (failing document; Windows wall time, callgrind Ir):
Cases that never backtrack, plus the real schemas (Ir):
zeroOrMore(element), 32,000 siblingszeroOrMore(choice of 3), 16,000 siblingsdocbook_0.xmlChecks
wasm32builds, and semver-checks requires no update.Not in scope.
zeroOrMorecopies 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