Repository navigation
Index the rules by pointer instead of copying them for every node (~150x faster validation) - #9
Merged
Merged
Conversation
verify_json looked up the rules of every node of the input by scanning the
whole spec, and copied every rule while doing it: collect_pointer and
collect_default_rules iterated with `for (auto i : rules)` and returned
copies, the matching loop copied each matching rule again, and comparing a
rule's pointer with the node's (`i.at("pointer") == pointer`) built a json
from the string for every rule. A spec of N rules on an input of M nodes
cost M x N rule copies.
The rules are now indexed by pointer once per verify_json / inject_defaults
call (RuleIndex: pointers into `rules`, in spec order, and the subset with a
default), and the recursion looks nodes up in it and works on references.
Smaller copies go too: rule options, required keys and extensions are
iterated by reference, and contained_in_list compares strings without
building a json per element.
Nothing observable changes: the same rules match in the same order, so the
results, the injected defaults and the log (including the order of the
rules listed in an error) are the same; a rule without "pointer" still
throws from at(), and a non-string pointer still matches nothing. The
public API is unchanged; only private members of JSE changed.
Verify + inject_defaults: polysolve's nonlinear spec 13.1 ms -> 0.078 ms,
its linear spec 13.0 ms -> 0.074 ms, an application spec of 53 rules on a large input
252 ms -> 1.7 ms; polysolve's Solver::create (DenseNewton, which validates
the linear spec 3 times) 43 ms -> 0.48 ms. The unit tests run in 0.04 s
instead of 2 s. New tests cover the behaviour the index must keep: the
rules of a pointer in spec order, "skip" and other defaults, non-string
pointers and rules without one, inconsistent defaults.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01ULwCy3KGG3sBouB7ZLaXwF
This was referenced Nov 17, 2026
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.
Summary
verify_jsonandinject_defaultslooked up the rules of every node of the input by scanning the whole spec, and copied every rule while doing it:collect_pointerandcollect_default_rulesiterated withfor (auto i : rules)and returned copies;i.at("pointer") == pointer) built ajsonfrom the string for every rule.So a spec of N rules on an input of M nodes cost M × N rule copies. A profile of polysolve's
Solver::createshowed 99% of its time in these copies (json copy-construction and destruction, malloc).This PR indexes the rules by pointer once per
verify_json/inject_defaultscall (JSE::RuleIndex: pointers intorules, in spec order, plus the subset with a default). The recursion looks nodes up in that index and works on references. A few smaller copies go too: rule options, required keys and extensions are now iterated by reference, andcontained_in_listcompares strings without building ajsonper element.Nothing observable changes.
"pointer"still throws fromat(), and a non-string pointer still matches nothing.JSEchanged (no data members).Timings (Release, Apple M-series)
Solver::create(DenseNewton + Eigen::LDLT; validates the linear spec 3 times)unit_testsIdentical results
A harness ran the old and the new library on the same cases and compared everything observable: the
verify_jsonresult,log2str(), theinject_defaultsdump (or the exception), and theinject_includeoutput. Each case ran withstricton and off andboxing_primitiveon and off. The inputs:data/(rules_0*×input_0*with includes,default_rules.jsonwithdefault.jsonand the 67polyfem-data-jsons);That is 1,492 inputs, 5,968 verify/inject pairs and a 36 MB transcript. The output is byte-identical between the two libraries, and identical again under AddressSanitizer + UBSan (old: 100 s, new: 0.97 s).
Tests
"skip"against other defaults (including the string"skip"inside a list default), non-string pointers and rules without one, and inconsistent defaults.🤖 Generated with Claude Code
https://claude.ai/code/session_01ULwCy3KGG3sBouB7ZLaXwF