Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@ All changes that a user can see are in this file. The format follows Keep a Chan

## [Unreleased]

### Changed

- Engine V compares a pair again when its compare stopped on the bound of `v_try_struct` and no side of the pair can unfold. Before this change, engine V gave false for two equal values, as in a `decide` proof, and engine R checked the declaration again. The retry has a bound of its own. On a build without PGO, the sum of the perf tests goes down by 24.3%, and `init` and `std` go up by 1.4% and 0.9% (#355).

### Documentation

- Spec perf/19.31 compares engine V with the rivals test by test (#355).

## [0.11.0] - 2026-12-02

This release closes the milestone M13 (#344). M13 measured the cost of each step of engine V and tried to make this cost smaller. The step count of engine V is near the least count for its keys, so the gap to the goal of 01 is the cost of one step in the verified subset. On the Arena run, `init` is about 41.3 G instructions against the goal of 22.4 G. No step of M13 gave 1% or more with a correct verdict, so this release has no change to the code.
Expand Down
1 change: 1 addition & 0 deletions crates/rulean-core/src/vcheck.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ impl<S: Source<B>, B: Base> Checker<S, B> {
self.vs.work_start = self.vs.stats.work();
self.vs.check_end = self.vs.work_start + V_WORK;
self.vs.work_end = self.vs.check_end;
self.vs.bytes_end = V_CHECK_BYTES;
self.vs.stk.truncate(0);
true
}
Expand Down
44 changes: 43 additions & 1 deletion crates/rulean-core/src/vdefeq.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ const DELTA_STEPS: u32 = 10_000;
/// The work count that one compare of the arguments before an unfold can use (`v_try_struct`).
const TRY_WORK: u64 = 1 << 14;

/// The work count and the arena growth that one compare again after a stopped try can use (`v_retry_struct`).
const RETRY_WORK: u64 = 1 << 23;
const RETRY_BYTES: usize = 256 << 20;

/// The largest number of steps that lazy delta unfolds the side of a taken branch (row O8 of spec 05.7.3), as `TAKEN_STEPS` of engine F.
const TAKEN_STEPS: u32 = 32;

Expand Down Expand Up @@ -82,9 +86,17 @@ impl<S: Source<B>, B: Base> Checker<S, B> {
return false;
}
// Two applications of the same head compare their arguments before lazy delta, as in the official algorithm.
let t0 = self.vs.stats.try_stops;
if x == y || self.v_try_struct(x, y, d) {
return true;
}
// The pair whose last compare stopped on the bound of `v_try_struct`, if any.
let mut gx = VId::NONE;
let mut gy = VId::NONE;
if self.vs.stats.try_stops != t0 {
gx = x;
gy = y;
}
// Proof irrelevance, before lazy delta as in engine R, so that two proofs of the same proposition do not unfold. A value that is not a proof by the form of its declared type skips the two type computations.
let pi = if self.v_not_proof(x) {
0
Expand Down Expand Up @@ -173,10 +185,15 @@ impl<S: Source<B>, B: Base> Checker<S, B> {
y = self.v_delta(y, nat);
}
}
let t1 = self.vs.stats.try_stops;
if !self.vs.bad && (x == y || self.v_try_struct(x, y, d)) {
found = true;
done = true;
}
if self.vs.stats.try_stops != t1 {
gx = x;
gy = y;
}
}
}
if found {
Expand All @@ -196,6 +213,13 @@ impl<S: Source<B>, B: Base> Checker<S, B> {
if x2 != x || y2 != y {
return self.v_eq(x2, y2, d);
}
// The last compare of this pair stopped on the bound of `v_try_struct`, and no side unfolds. So the false of that compare is not a result, and the compare runs again with the bound of `v_retry_struct` (spec perf/19.31).
if x == gx && y == gy && self.v_retry_struct(x, y, d) {
return !self.vs.bad;
}
if self.vs.bad {
return false;
}
self.v_eq_eta(x2, y2, d)
|| self.v_eq_eta(y2, x2, d)
|| self.v_eq_nat_ctor(x2, y2, d)
Expand Down Expand Up @@ -658,7 +682,7 @@ impl<S: Source<B>, B: Base> Checker<S, B> {
!self.vs.bad && self.v_eq(w, tb, d)
}

/// `v_eq_struct` before an unfold, with a work count of at most `TRY_WORK`. If a side can unfold, this compare is a guess: if it fails, the unfold finds the equality. So a stop of engine V on this bound gives false, and the check goes on. The work of the compare counts in the work of the check, so each check still ends. Without this bound, the compare of `eqA 600 x y` and `Nat.beq x y` in `false && _` on the Arena test `perf/short-circuit-and` uses the full budget, and engine V declines.
/// `v_eq_struct` before an unfold, with a work count of at most `TRY_WORK`. If a side can unfold, this compare is a guess: if it fails, the unfold finds the equality. So a stop of engine V on this bound gives false, and the check goes on. If no side unfolds after the stop, `v_eq_core` compares the pair again with `v_retry_struct`. The work of the compare counts in the work of the check, so each check still ends. Without this bound, the compare of `eqA 600 x y` and `Nat.beq x y` in `false && _` on the Arena test `perf/short-circuit-and` uses the full budget, and engine V declines.
fn v_try_struct(&mut self, a: VId, b: VId, d: u32) -> bool {
let end = self.vs.work_end;
let cap = self.vs.stats.work() + TRY_WORK;
Expand All @@ -683,6 +707,24 @@ impl<S: Source<B>, B: Base> Checker<S, B> {
r && !self.vs.bad
}

/// `v_eq_struct` again after a stop of `v_try_struct`, with a work count of at most `RETRY_WORK` and an arena growth of at most `RETRY_BYTES`. A stop on these bounds stops the check, because the false of the compare is not a result. Then engine R checks the declaration, which is faster than engine V on a long closed computation (spec perf/19.31).
fn v_retry_struct(&mut self, a: VId, b: VId, d: u32) -> bool {
let end = self.vs.work_end;
let cap = self.vs.stats.work() + RETRY_WORK;
if cap < end {
self.vs.work_end = cap;
}
let bytes = self.vs.bytes_end;
let bcap = self.vs.a.live_bytes() + RETRY_BYTES;
if bcap < bytes {
self.vs.bytes_end = bcap;
}
let r = self.v_eq_struct(a, b, d);
self.vs.work_end = end;
self.vs.bytes_end = bytes;
r && !self.vs.bad
}

/// The compare of the structure of two values in weak head normal form without delta.
fn v_eq_struct(&mut self, a: VId, b: VId, d: u32) -> bool {
match (self.vs.a.node(a), self.vs.a.node(b)) {
Expand Down
9 changes: 7 additions & 2 deletions crates/rulean-core/src/veval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use crate::env::ConstKind;
use crate::level;
use crate::source::Source;
use crate::tc::Checker;
use crate::vcheck::V_CHECK_BYTES;
use crate::vvalue::{Slot, VArena, VClo, VEnv, VHead, VId, VNode, VSub};

/// The log2 of the number of entries of the eval memo and the infer memo, at the start and at most. The memos start small, so a small export does not pay for large tables, and they grow when they fill (`v_grow`).
Expand Down Expand Up @@ -203,6 +204,8 @@ pub struct VState {
pub(crate) work_start: u64,
pub(crate) check_end: u64,
pub(crate) work_end: u64,
/// The bound of the arena size now. It is `V_CHECK_BYTES`, and it is lower in the retry of `v_eq_core`.
pub(crate) bytes_end: usize,
/// The log2 of the sizes of the eval and infer memos and of the equality cache.
pub(crate) eval_bits: u32,
pub(crate) eq_bits: u32,
Expand Down Expand Up @@ -458,9 +461,11 @@ impl<S: Source<B>, B: Base> Checker<S, B> {
VId::NONE
}

/// True if the work count is over the bound now (`V_WORK`, or the bound of `v_try_struct`). Engine V then stops.
/// True if the work count is over the bound now (`V_WORK`, or the bound of `v_try_struct`), or if the arena is over the lower bound of the retry of `v_eq_core`. Engine V then stops. The arena size is not checked here outside the retry, because `v_unfold` checks it.
pub(crate) fn v_spent(&mut self) -> bool {
if self.vs.stats.work() <= self.vs.work_end {
if self.vs.stats.work() <= self.vs.work_end
&& (self.vs.bytes_end == V_CHECK_BYTES || self.vs.a.live_bytes() <= self.vs.bytes_end)
{
return false;
}
if self.vs.work_end == self.vs.check_end {
Expand Down
3 changes: 1 addition & 2 deletions crates/rulean-core/src/vwhnf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ use rulean_store::Base;

use crate::source::Source;
use crate::tc::Checker;
use crate::vcheck::V_CHECK_BYTES;
use crate::veval::{V_MAX_DEPTH, V_RED_DEPTH};
use crate::vvalue::{Slot, VEnv, VHead, VId, VNode, VSpine, VSub};

Expand Down Expand Up @@ -119,7 +118,7 @@ impl<S: Source<B>, B: Base> Checker<S, B> {
if self.v_spent() {
return VId::NONE;
}
if self.vs.fuel == 0 || self.vs.a.live_bytes() > V_CHECK_BYTES {
if self.vs.fuel == 0 || self.vs.a.live_bytes() > self.vs.bytes_end {
self.vs.stats.fuel_stops += 1;
return self.v_stop();
}
Expand Down
128 changes: 128 additions & 0 deletions docs/spec/perf/19-engine-v.md
Original file line number Diff line number Diff line change
Expand Up @@ -817,3 +817,131 @@ The profile of CI runs with `--report`, so its count has the walk of `Axioms::of
The steps 19.22 and 19.24 to 19.28 each give less than 1%, or they give a wrong verdict, so they are not merged. 19.29 shows that engine V does near the least count of work steps for its keys. Thus the gap to the goal is the cost of one step. This cost comes from the reads of the store and of the arena in the verified subset, with the bounds checks and the decode of each node.

A change of this cost needs a hot path outside the verified subset, with pointers and a fixed node layout. This change is a change of the trust model of rulean, and it is not part of the plan. Thus the goal of 01 is not met. The rival counts of 18.2 and the count of about 41.3 G give these ratios on `init`: rulean is about 1.6 times slower than nano-lean, about 5.7 times faster than nanoda and about 10 times faster than official. The rival counts include the hash work of each rival, and the rulean count does not include `sha256::compress`, so these ratios are approximate.

## 19.31 Engine V against the rivals, test by test

This section compares rulean v0.11.0 with the rivals on each Arena test. It finds the declarations that engine V gives to engine R, and the cause of each one.

### 19.31.1 The counts

The rulean counts are `perf stat -e instructions:u` of the release binary of v0.11.0 (PGO) on server4 (x86_64), with `--profile v4.35`. The rival counts are the Arena results of 2026-11-30 (rev e38a7d3f). The 238 verdicts are correct.

| Kernel | Sum of the 44 perf tests | rulean is faster by | Perf tests where rulean is 10 times faster |
|---|---|---|---|
| rulean, 1 thread | 111.3 G | | |
| rulean, 4 threads | 158.7 G | | |
| nanoda | 1108.0 G | 9.96 | 14 |
| official v4.35.0 | 1327.7 G | 11.93 | 24 |
| sokonanoda | 192.3 G | 1.73 | 4 |
| rpylean (43 tests) | 194.5 G | 1.75 | 1 |
| mathgraph | 183.6 G | 1.65 | 4 |
| nano-lean | 100.8 G | 0.91 | 2 |
| gruszka | 97.7 G | 0.88 | 1 |
| kefir | 85.6 G | 0.77 | 0 |

Four tests use 81.1 G of the 111.3 G:

| Test | rulean | nanoda | official | nano-lean | gruszka | kefir |
|---|---|---|---|---|---|---|
| `magma-list-deep-n36` | 25.03 G | 225.99 G | 212.73 G | 2.55 G | 2.63 G | 2.43 G |
| `grind-field-kernel-cancel` | 20.15 G | 123.48 G | 154.56 G | 12.93 G | 14.67 G | 22.21 G |
| `short-circuit-and-bad` | 18.08 G | 22.37 G | 37.06 G | 3.22 G | 8.51 G | 2.73 G |
| `gf3-matrix-product` | 17.86 G | 17.34 G | 10.91 G | 5.00 G | 6.65 G | 4.62 G |

### 19.31.2 Where the time goes

In each of these four tests, engine V accepts each declaration except one to twenty. Engine R checks the declined declarations, and engine R uses almost all of the count. A run with `--engine R` gives about the same count as the default run (15.45 G on `gf3-matrix-product`, 25.04 G on `magma-list-deep-n36`, 17.98 G on `short-circuit-and-bad`). So the work of engine V before it declines is small, and the cost is the work of engine R.

A measure build records the first stop of each declined check. The table gives the causes on the perf tests and on `init` and `std`.

| Cause | Tests |
|---|---|
| The compare of a pair stopped on the bound of `v_try_struct`, and no side of the pair can unfold, so `v_eq` gave a false that is not a result. In an argument check this stops the check (site `vinfer.rs` of the argument type). | the four magma tests, `gf3-matrix-product`, 19 declarations of `init`, 79 of `std` |
| The same stop of `v_try_struct`, and the check then gave false with no other stop | `grind-field-kernel-cancel` (20 declarations), `short-circuit-and`, `loop-unfolded-once`, `discarded-argument-match`, `shared-subterm`, `proj-lazy-struct`, and the bad tests |
| The depth bound `V_RED_DEPTH` | 9 declarations of `init`, 5 of `std` |

In each declined check of the perf tests, the count `try_stops` of the report is the same as the count `declined`.

### 19.31.3 The gap in `v_eq_core`

`v_try_struct` compares two values as applications with a work bound of `TRY_WORK`. A stop on this bound gives false. This is correct when a side can unfold, because the lazy delta loop then finds the equality. But if both sides have height 0, as `Eq Bool (decide p) true` against `Eq Bool true true`, the loop ends at once, and no step compares the arguments again. So engine V gives false for two values that are equal, and it writes this false into its eq memo.

A `decide` proof has this form. The type of `Eq.refl true` is `Eq Bool true true`, and the argument check compares it with `Eq Bool (decide p) true`. `Eq` cannot unfold, and the compare of `decide p` with `true` is the full computation, which is much more than `TRY_WORK`.

The rivals do not have this gap. nano-lean does a bounded compare with no force step (`same`, with a fuel of 512) and then the full compare (19.20.2). The official kernel compares the arguments with no bound.

The fix: `v_eq_core` keeps the pair of the last stopped compare. If the loop ends with the same pair and no step after the loop changes it, `v_eq_core` compares the pair again with the bound of 19.31.6 (`v_retry_struct`).

### 19.31.4 The measures of the fix

The table gives the instruction counts of a build without PGO, on the tests that change by more than 0.1 G. The sum of the 44 perf tests goes from 127.72 G to 112.05 G (-12.3%).

| Test | Before | With the fix | Stop site with the fix |
|---|---|---|---|
| `magma-list-deep-n36` | 28.20 G | 7.94 G | none |
| `grind-field-kernel-cancel` | 23.70 G | 5.95 G | none |
| `magma-list-deep-n21` | 3.32 G | 0.71 G | none |
| `short-circuit-and` | 1.13 G | 0.61 G | none |
| `magma-list-pair-n21` | 9.71 G | 26.65 G | fuel or arena size |
| `gf3-matrix-product` | 19.12 G | 24.32 G | fuel or arena size |
| `short-circuit-and-bad` | 22.12 G | 23.94 G | depth |
| `magma-list-pair-n7` | 1.21 G | 2.27 G | none |
| `ite-taken-branch-bad` | 3.28 G | 3.75 G | depth |
| `init` | 45.48 G | 46.08 G | depth (19 declarations) |
| `std` | 82.64 G | 83.30 G | depth (29 declarations) |

A measure build counts the work of each retry. The retry of `magma-list-deep-n36` uses between 1M and 4M work and succeeds. The retry of `magma-list-pair-n21` uses between 4M and 16M work and stops on the fuel. The retry of `gf3-matrix-product` uses less than 65k work, but it stops on the arena size, because each step makes a `Nat` literal of about 9500 limbs.

A second measure build turns the retry on for one group of declarations of `init` at a time. The loss of 0.53 G has two parts of about the same size.

| Group | Declarations | Change |
|---|---|---|
| The retry succeeds, and engine V accepts the declaration | 9 (`String.fromUTF8Lossy` alone is +0.24 G) | +0.30 G |
| The retry stops on the depth bound `V_RED_DEPTH`, and engine R checks the declaration | 19 | +0.25 G |

A larger depth bound does not help. With `V_RED_DEPTH` at 8192, `init` declines 3 declarations and uses 46.24 G, `std` declines 3 and uses 86.93 G, and `short-circuit-and-bad` uses 27.69 G. On these declarations, engine V is slower than engine R.

The cycle counts of `init` on server2 show no change outside the noise (97.4 G and 88.1 G without the retry, 96.5 G and 93.6 G with it). So the instruction count is the measure that this section uses.

### 19.31.5 Where engine V is slower than engine R

Engine V is the faster engine when the work has much sharing. `magma-list-deep` builds terms that share subterms at each level, and the hash-consed values of engine V compare each shared pair once. Engine R has no sharing across lookups (19.16.3), so it does the same work again at each level.

Engine V is the slower engine on a long closed computation with no sharing, as a `decide` proof or a product of large numbers. Each step of engine V makes a value in the arena and finds it in the hash table, and each value stays until the end of the check. The Krivine machine of engine G (`ground.rs`) keeps no table of values, so a step has no hash and no table insert. With all bounds raised, engine V checks `magma-list-pair-n21` in 25.4 G, and engine G checks it in 8.2 G. nano-lean uses 13.38 G.

nano-lean and the official kernel have one engine, so they do not pay twice for a declaration. rulean pays twice when engine V does a long computation and then declines.

### 19.31.6 A bound for the retry

The retry has its own bound: at most `RETRY_WORK` (1<<23) work and at most `RETRY_BYTES` (256 MiB) of arena growth. A stop on this bound stops the check, and engine R checks the declaration. The arena size is checked in `v_spent` while a retry runs, because a `Nat` computation can grow the arena with few unfolds, and `v_unfold` is the only other site that checks the arena size. Outside a retry, `v_spent` does not read the arena size.

The bounds come from the measures of 19.31.4. The retry of `magma-list-deep-n36` uses less than 4M work and less than 256 MiB. A first try with 1<<22 work and 128 MiB stopped this retry, and the test went to 31.37 G.

| Test | Before | Fix with no bound | Fix with the bound |
|---|---|---|---|
| `magma-list-deep-n36` | 28.20 G | 7.94 G | 7.93 G |
| `grind-field-kernel-cancel` | 23.70 G | 5.95 G | 5.96 G |
| `magma-list-pair-n21` | 9.71 G | 26.65 G | 15.47 G |
| `gf3-matrix-product` | 19.12 G | 24.32 G | 20.05 G |
| `init` | 45.48 G | 46.08 G | 46.11 G |
| `std` | 82.64 G | 83.30 G | 83.37 G |
| Sum of the 44 perf tests | 127.72 G | 112.05 G | 96.63 G |

The 238 verdicts are correct, and the 452 tests of the workspace pass.

### 19.31.7 Decision

The fix with the bound is merged. It decreases the sum of the perf tests by 24.3%, and it increases `init` by 1.4% and `std` by 0.9%. The loss on `init` comes from declarations where engine V is slower than engine R (19.31.4), and a bound of work cannot find these declarations before the work is done.

The table gives the sums of the 44 perf tests of the fast rivals in the Arena results, and the ratio to rulean with the fix (96.63 G, no PGO). The PGO build of v0.11.0 used 13% less than the build without PGO, so the ratios of a PGO build are about 15% higher. `mini` (37.87 G) is not in the table, because it declines 27 perf tests and stops on its time bound on 12.

| Kernel | Sum | Ratio |
|---|---|---|
| kefir | 85.59 G | 0.89 |
| gruszka | 97.69 G | 1.01 |
| nano-lean | 100.78 G | 1.04 |
| nanoda | 1108.04 G | 11.47 |
| official | 1327.72 G | 13.74 |

The goal of 10 times the fastest rival is not met on the perf tests. The tests where rulean is behind each rival are `short-circuit-and-bad` (23.94 G against 2.73 G for kefir), `gf3-matrix-product` (20.05 G against 4.62 G), `magma-list-pair-n21` (15.47 G against 13.38 G for nano-lean) and `magma-list-deep-n36` (7.93 G against 2.43 G). In the first three, engine V declines and engine R does a long closed computation. So the next step is engine G, and not engine V: a memo of the iota steps on closed major premises (design 2 of 19.16.5), and a cost of a `Nat` product that is near the cost of the rivals.

Loading