Skip to content

Nat.min and Nat.max compile to one comparison, as Nat.cmp and Nat.is_lt do - #1037

Closed
aldeni wants to merge 1 commit into
bendlang:mainfrom
aldeni:nat-min-max
Closed

aldeni wants to merge 1 commit into
bendlang:mainfrom
aldeni:nat-min-max

Conversation

@aldeni

@aldeni aldeni commented Sep 24, 2026

Copy link
Copy Markdown
Contributor

What

Two rows in OPERATIONS (bend2/comp.ts): nat_min and nat_max, ($0 < $1 ? $0 : $1) and ($0 > $1 ? $0 : $1) on the C and JS lanes, beside nat_add, nat_sub, nat_cmp and nat_is_lt. A test, tests/base/nat_min_max.bend.

base.bend is untouched. Nat.min/Nat.max stay structural, as #860 made them, so the checker still unfolds them in step with Nat.cmp. Nat.max_ge_l, Nat.max_ge_r and any user proof that relies on that unfolding are unchanged. This is the split Base already uses for Nat.add or Nat.cmp: the structural definition is what the checker sees, and the machine operation is what the compiled lanes run.

Why

On the JS and C lanes a Nat is a machine word, but Nat.min/Nat.max ran the structural recursion there: one step per unit of the smaller argument. With a clock reading as an argument, that recursion is the whole cost. demos/io_rollback_netcode takes Nat.max of millisecond timestamps (Nat.max(ctime, born) in the relay, Nat.max(ctime, Nat.sub(stime, tol)) in the client).

I measured 10,000 calls of Nat.min(t, 10000n) in a tail-recursive loop, with t past 10000n, timed with IO.now() around the loop. The table gives the median of 3-5 runs, on Linux x86-64:

lane before after
C (-o x) 141 ms under 1 ms
JS (-o x.js, run with bun) 10.9 s 9 ms
bend file.bend (IO main) 10.9 s 3 ms

Nat.max(10000n, t) behaves the same: 117 ms → under 1 ms in C, 10.9 s → 9 ms in JS. A single Nat.max of two IO.now() readings near 2.6·10⁸ ms took 980 ms in a binary before this change and under 1 ms after.

A pure main run by the checker's evaluator (bend file.bend on a non-IO main) still unfolds the definitions, as it does Nat.add.

Test

tests/base/nat_min_max.bend covers 0 with 0, 0 with n on either side, equal values, a < b, a > b, and the same cases on pairs near 2^40 (below the 2^48-1 immediate cap). It also calls Nat.max inside the lambda List.foldl takes as its template argument (~Nat.max itself is refused there: its + quantities don't match B -> A -> B), and Nat.min through a closure. It prints its 22 #| lines on all four lanes: --check-only, bend file.bend, -o x.js under bun, and -o x.

On ac0ddb7 the three running lanes print the 12 small cases. At the first pair near 2^40 they stop with memory fault (machine stack overflow?), so the old definition cannot pass the test by printing a wrong line.

Regression

I ran all of tests/ through a local replica of gates/test.ts: the same test reading, shard script, parser and judge, with bash instead of ssh.

pass
this branch 1440 / 1442
ac0ddb7 1439 / 1441

The same two tests fail on both: gfx_clicks and gfx_window on the C lane. They expect a host without a display. No bench in bench/ calls Nat.min or Nat.max. demos/io_rollback_netcode (server.bend, walkers_test.bend) builds to C and JS. comp.ts is 62258 ttok, under its 64000 cap.

Not tested

  • macOS, Metal and CUDA. The C template is a plain u64 comparison with the same shape as nat_is_lt and nat_sub, but I had no such host to build on.
  • The cluster gates.

…lt do

Base keeps them structural (bendlang#860): the checker unfolds them in step with
Nat.cmp, so Nat.max_ge_l and Nat.max_ge_r prove by cases. base.bend is
untouched here, and every law and proof over them stays as it was. The
JS and C lanes ran that same recursion, one step per unit of the smaller
argument, on a Nat that is a machine word there. OPERATIONS gains nat_min
and nat_max beside nat_add, nat_sub, nat_cmp and nat_is_lt: ($0 < $1 ?
$0 : $1) and ($0 > $1 ? $0 : $1) on both lanes. A pure main run by the
checker's evaluator still unfolds them, as it does Nat.add.

10,000 calls of Nat.min(t, 10000n), t past 10000n, in a tail loop (median
of the runs): C 141 ms -> under 1 ms, JS 10.9 s -> 9 ms, `bend file.bend`
(IO main) 10.9 s -> 3 ms; Nat.max(10000n, t) 117 ms -> under 1 ms in C.
One Nat.max of two IO.now() readings near 2.6e8 ms: 980 ms -> under 1 ms
in a binary.

tests/base/nat_min_max prints its 22 lines on check, interp, JS and C. On
ac0ddb7 the three running lanes print the 12 small cases, then stop with
"memory fault (machine stack overflow?)" at the first pair near 2^40. The
whole tests/ through a local replica of gates/test.ts (its shard script,
parser and judge, bash in place of ssh): 1440 / 1442 here, 1439 / 1441 on
ac0ddb7, the same two failing on both: gfx_clicks and gfx_window [c],
which expect a host without a display. comp.ts is 62258 ttok (cap 64000).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@nicolas-abril nicolas-abril self-assigned this Sep 24, 2026
@nicolas-abril nicolas-abril mentioned this pull request Sep 24, 2026
@nicolas-abril

Copy link
Copy Markdown
Collaborator

Thank you, this is a clean change, and we verified it. It keeps the structural Nat.min/Nat.max for the checker, nested calls bind temporaries so no argument is evaluated twice, your test prints its lines on all four lanes, and one Nat.max near 2.6·10⁸ goes from about 290 ms to process startup in C. We are closing it only because #974 adds the same nat_min/nat_max templates on both lanes, together with the other selector intrinsics, so the two would conflict. #974 has no tests yet, and we have asked for yours there: tests/base/nat_min_max.bend covers exactly the cases it needs. If you would like to contribute it on that PR, or as a follow-up once #974 lands, it is very welcome.

Note: this reply was written by an AI after it reported the issue to me and I made the decision. If anything here is wrong, reply and I will review it myself.

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.

2 participants