Skip to content

Index the rules by pointer instead of copying them for every node (~150x faster validation) - #9

Merged
danielepanozzo merged 1 commit into
mainfrom
perf/verify-without-copies
Nov 17, 2026
Merged

danielepanozzo merged 1 commit into
mainfrom
perf/verify-without-copies

Conversation

@danielepanozzo

Copy link
Copy Markdown
Contributor

Summary

verify_json and inject_defaults 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;
  • comparing a rule's pointer with the node's (i.at("pointer") == pointer) built a json from 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::create showed 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_defaults call (JSE::RuleIndex: pointers into rules, 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, 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 are the same, including the order of the rules listed in an error.
  • 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 (no data members).

Timings (Release, Apple M-series)

before after
verify + inject_defaults, polysolve nonlinear spec 13.1 ms 0.078 ms
verify + inject_defaults, polysolve linear spec 13.0 ms 0.074 ms
verify + inject_defaults, an application spec of 53 rules on a large input 252 ms 1.7 ms
polysolve Solver::create (DenseNewton + Eigen::LDLT; validates the linear spec 3 times) 43 ms 0.48 ms
unit_tests 2.05 s 0.04 s

Identical results

A harness ran the old and the new library on the same cases and compared everything observable: the verify_json result, log2str(), the inject_defaults dump (or the exception), and the inject_include output. Each case ran with strict on and off and boxing_primitive on and off. The inputs:

  • polysolve's linear and nonlinear specs with valid and invalid solver parameters;
  • every wildmeshing-toolkit component spec, with the documentation's example configurations;
  • this repository's data/ (rules_0* × input_0* with includes, default_rules.json with default.json and the 67 polyfem-data-jsons);
  • a 53-rule application spec with 27 inputs;
  • deterministic mutations of every input: a node removed, retyped, set to null, given an unknown key, boxed or unboxed.

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

  • The existing 21 tests pass.
  • 4 new tests cover what the index must keep: the rules of a pointer in spec order (ambiguity and no-match messages), "skip" against other defaults (including the string "skip" inside a list default), non-string pointers and rules without one, and inconsistent defaults.
  • The new tests pass against the old library too.

🤖 Generated with Claude Code

https://claude.ai/code/session_01ULwCy3KGG3sBouB7ZLaXwF

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
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.

1 participant