# E17b canonical row deltas

<!-- packages: egraph -->

## Canonical tables and accepted searches

Plain pattern rules use canonical node rows grouped by operator, arity and owning
class. Rows retain node creation positions, canonical children and their last changed
revision. The graph owns these tables, so several rules can share their immutable
rows. Class stamps detect membership changes. A merge counter and canonical child
checks allow tables to survive unrelated graph changes. Rebuild changes are checked
before a table is used again.

Each rule retains the revision of its last accepted search. A direct nested search
joins new outer rows with all inner rows, then old outer rows with new inner rows.
Emission follows the original outer row order, so the partition of the join changes
no application order. Larger patterns with fresh variables keep the flat interpreter and
emit matches containing a changed row. This avoids expanding a deep tree pattern
into repeated relations over every operator row.

The complete match count is separate from the emitted delta. Direct joins add the
inner table size to that count even when an old pair is skipped. General joins count
the complete query before searching its delta relations. Backoff tests the complete
count, and the rule accepts the new revision after that test passes. A rejected search
keeps its earlier revision and can apply the same delta on a later iteration.

## Joins for shared variables

A node occurrence becomes a relation over its owner and child class variables.
Repeated pattern variables use the same query variable. Local equalities filter a
relation before it is indexed. The smallest tables choose the common variable order,
and tries index every relation by that order. The search intersects all projections
that constrain a variable, starting with the smallest projection. It uses an explicit
stack and constructs no binary intermediate join.

Later searches partition matches by their first changed relation in preorder. Earlier
relations use old rows, the chosen relation uses new rows, and later relations use all
rows. This partition emits a match once even when several participating rows change.
Trie leaves retain node positions, so dirty congruent rows keep their existing match
multiplicity until rebuild removes them. Results are sorted by candidate root order
and preorder node positions before RHS construction begins.

## Callbacks, views and budgets

Callback searches still receive every live class. Conditions and callback builders
retain complete searches because they can observe unrelated graph changes. Their
invocation order and immutable match snapshots keep the existing contract.

Public node reads cache immutable node and class views by dense numeric positions.
A read reuses a class view within one graph revision and checks canonical children
before reusing an individual node view across revisions. Retained views keep their
earlier children. Reads retain the existing eager fuel charges, including allocated
entries that rebuild has removed, and charge stored cache cells.

Tables and joins charge growing work and storage through the existing work meter.
RHS construction and merging retain the existing atomic application
boundary. The Phase A performance test keeps five fresh runs per size and the same
25% headroom. Workload inputs, production budgets, dependencies and extraction
tie-breaks keep their existing values.

## Measurements

The retained engine is `06bd2dbe1`. Its comparison with the landed Phase A engine at
`ac1ced20c` alternates samples between the engines in one process for each workload.
A second window reverses the starting engine. Each window uses five fresh graphs per
AC size and seven complete grammar catalogue runs per engine. AC shapes and catalogue
output hashes agree in both windows. Each catalogue run includes all 75 drafts, with
73 successful results and the same two unsupported results.

<!-- src: maintainer locked forward and reverse-start alternating comparisons on 2026-11-27, ac1ced20c and 06bd2dbe1, test/ac-workload.ts and apps/web/test/support/catalogue-simplify.ts. -->

| Workload               | First Phase A median (ms) | First E17b median (ms) | Reverse Phase A median (ms) | Reverse E17b median (ms) |
| ---------------------- | ------------------------- | ---------------------- | --------------------------- | ------------------------ |
| AC with seven terms    | 34.53                     | 30.62                  | 37.84                       | 35.97                    |
| AC with eight terms    | 121.80                    | 103.46                 | 147.58                      | 114.27                   |
| AC with nine terms     | 619.77                    | 574.97                 | 729.24                      | 673.08                   |
| AC with ten terms      | 3,553.83                  | 3,421.60               | 3,818.71                    | 3,332.89                 |
| All catalogue Simplify | 632.03                    | 602.04                 | 569.30                      | 572.09                   |

AC medians improve in both windows. The grammar median increases in the reverse
window. Pooling the 14 grammar timings per engine gives medians of 623.55 ms for
Phase A and 576.46 ms for E17b. Of the 14 paired trials, 12 take less time with E17b.
The median paired change is a decrease of 32.92 ms.

A separate replay of the unchanged Phase A performance test passes at `06bd2dbe1`.
Five fresh graphs per size give medians of 32 ms, 127 ms, 720 ms and 3,535 ms for
seven through ten terms. All 20 samples satisfy the correctness assertions.

<!-- src: maintainer locked unchanged test/ac.perf.test.ts replay on 2026-11-27 at 06bd2dbe1, five fresh graphs per size and the original 25% timing headroom. -->

These AC medians stay above the stretch targets of 56 ms, 324 ms and 1,800 ms for
eight, nine and ten terms.

### Exploratory comparisons

The first checkpoint at `3a235b2c9` does not qualify. Its eight-term, nine-term and
grammar medians exceed Phase A in the same comparison. Canonical tables at this
checkpoint refresh on every graph revision and allocate a child subarray for each
row. The retained engine checks class and child dependencies and removes those
subarray allocations.

<!-- src: maintainer locked comparison on 2026-11-27, ac1ced20c and 3a235b2c9, AC medians of five and grammar median of seven. -->

| Workload               | Phase A median (ms) | `3a235b2c9` median (ms) |
| ---------------------- | ------------------- | ----------------------- |
| AC with seven terms    | 36.95               | 34.10                   |
| AC with eight terms    | 111.68              | 119.72                  |
| AC with nine terms     | 617.76              | 684.93                  |
| AC with ten terms      | 3,764.89            | 3,565.44                |
| All catalogue Simplify | 570.07              | 708.30                  |

The second checkpoint at `625223115` records lower medians in every workload.
Its Phase A eight-term, nine-term and grammar medians exceed the first comparison
while concurrent tests are active. That load difference motivates alternating
samples for the retained engine. A separate budget regression at this checkpoint
shows that relational preprocessing expands a deep pattern with fresh variables beyond its
allowance. The retained engine restores the flat matcher for those patterns and
uses dense array positions for its matching tables and cached node views.

<!-- src: maintainer second locked comparison on 2026-11-27, ac1ced20c and 625223115, AC medians of five and grammar median of seven. -->

| Workload               | Phase A median (ms) | `625223115` median (ms) |
| ---------------------- | ------------------- | ----------------------- |
| AC with seven terms    | 47.34               | 35.51                   |
| AC with eight terms    | 160.13              | 117.11                  |
| AC with nine terms     | 971.50              | 740.53                  |
| AC with ten terms      | 3,789.79            | 3,292.13                |
| All catalogue Simplify | 926.27              | 556.65                  |

## Verification

The original partition, cost and fixed-point oracle remains unchanged. Additional
tests compare shared-variable saturation against the naive oracle on 48 seeds and
compare exact matcher order on 80 seeded graphs, including a triangle query. Focused
cases cover new outer and inner rows, changed children before rebuild, dirty duplicate
multiplicity, rejected backoff searches, immutable retained views and bounded matching
of a deep pattern with fresh variables. The existing grammar catalogue snapshot
matches all 75 outcomes.

<!-- src: maintainer existing apps/web/test/domain/egraph-catalogue-simplify.test.ts snapshot check on 2026-11-27 with the retained E17b production sources. -->

## Counts

No procedure, certificate family, `Justifies` pair or translation is added or removed.
Each count changes by zero.
