Skip to content

perf: give closed computations of engine V to engine G - #358

Open
tamnd wants to merge 6 commits into
mainfrom
perf/nat-sparse
Open

tamnd wants to merge 6 commits into
mainfrom
perf/nat-sparse

Conversation

@tamnd

@tamnd tamnd commented Dec 2, 2026

Copy link
Copy Markdown
Owner

Part of #354.

This PR removes most of the cost of the closed computations that engine V met on the perf tests. It has three changes.

  1. A schoolbook Nat product (rulean-nat/src/limbs.rs) skips the zero limbs of the long operand when at most half of its limbs are not zero. In gf3-matrix-product, each step multiplies a masked row of a packed matrix where about 3% of the limbs are not zero.
  2. Engine V gives the source term of a binary Nat operation to engine G before it computes the arguments (v_ground_origin in v_nat_step). Engine G keeps no value of a step after the step, so the large results of gf3-matrix-product do not fill the arena of engine V.
  3. Engine V gives a closed compare with a Bool or Nat value to engine G (v_eq_ground in v_eq_core), as ground_defeq of engine R. The hook does not run in v_try_struct, where the compare is a guess. Engine V first computes the value with at most 2^20 work and 64 MiB of arena growth (v_probe), and only a value that engine V does not compute in these bounds goes to engine G.

The probe is necessary. The first form of change 3 gave each compare to engine G at once, and grind-field-kernel-cancel went from 5.94 G to 18.14 G instructions. A measure build showed 20 certificates of about 4.5 million steps each in engine G, which engine V computes with less than 2^19 work each. Spec perf/19.33.5 gives the measures.

Engine G gives a result only when it computes a constructor or a literal, so each result of the hooks is the result of the same steps in engine V. Rule 2 of spec 05.1 does not change.

Measures (instructions, build without PGO, server4):

Test Before After
gf3-matrix-product 20.05 G 4.18 G
magma-list-pair-n21 15.47 G 10.83 G
magma-list-pair-n7 2.27 G 2.21 G
grind-field-kernel-cancel 5.96 G 5.97 G
init 46.11 G 46.31 G
std 83.37 G 83.72 G
Sum of the 44 perf tests 76.96 G 56.39 G

The 238 Arena verdicts are correct, and --confirm all gives 0 disagreements on init, std and the Arena tests. cargo test --workspace, cargo clippy -D warnings and cargo xtask all pass. The sum of the perf tests is now 1.52 times less than kefir, 19.65 times less than nanoda and 23.55 times less than official. gf3-matrix-product, magma-list-pair-n21 and grind-field-kernel-cancel are now faster than each rival.

Changes:

  • mul_school and sparse_rows in crates/rulean-nat/src/limbs.rs, and the test sparse_rows_agree_with_num_bigint in crates/rulean-nat/tests/oracle.rs.
  • The new module crates/rulean-core/src/vground.rs, the memo origin and the counter tries of VState, and the two hooks in vreduce.rs and vdefeq.rs.
  • Spec section perf/19.33.
  • A changelog entry.

tamnd added 6 commits December 2, 2026 22:00
When at least half of the limbs of the long operand are zero, the product adds one row for each limb that is not zero. The masked rows of a packed matrix in gf3-matrix-product have about 3% of their limbs not zero.
Engine V records the source term of a binary Nat operation when the term has no loose bound variable. When engine V must compute the operation, it asks engine G for the value first. Engine G makes no values in the arena, so a large closed computation does not stop engine V on the arena bound and then run again in engine G.
Engine V reads back a closed value as a term when the other side of a compare is a Bool or Nat value, and engine G computes the term, as ground_defeq of engine R does. The origin step of the Nat operations moves to the new module vground.rs.
The compare hook gave each closed compare with a scalar to engine G at once. On grind-field-kernel-cancel engine G used about 13 G instructions on 20 certificates that engine V computes with less than 2^19 work each. Now engine V computes the value first with at most 2^20 work and 64 MiB of arena growth. Only a value that engine V does not compute in these bounds goes to engine G, and a stop on these bounds is not a stop of the check.
A compare in v_try_struct is a guess, and the probe of engine V there did work that the check did not need, which cost 0.83 G on std. Now v_eq_ground does not run in v_try_struct. A pair that the try does not decide comes back in the retry, where the hook runs.
The section gives the cost of gf3-matrix-product, the sparse product, why engine V stopped on the closed computations, the two sites that give them to engine G, why engine G is not always the faster engine, and the measures. The changelog has the entry.
@tamnd tamnd added this to the M14 Perf: closed computation milestone Dec 2, 2026
@tamnd tamnd added area/nat The big number type and the Nat operations area/core The type checker: engines F and R, declarations, profiles type/perf Fewer instructions, less memory or less wall time labels Dec 2, 2026
@tamnd
tamnd enabled auto-merge (squash) December 2, 2026 16:57
@tamnd tamnd mentioned this pull request Dec 2, 2026
2 of 9 tasks
@tamnd tamnd closed this Dec 2, 2026
auto-merge was automatically disabled December 2, 2026 17:00

Pull request was closed

@tamnd tamnd reopened this Dec 2, 2026
@tamnd
tamnd enabled auto-merge (squash) December 2, 2026 17:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/core The type checker: engines F and R, declarations, profiles area/nat The big number type and the Nat operations type/perf Fewer instructions, less memory or less wall time

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant