Repository navigation
Conversation
…uards (65,962 -> 65,711 ttok)
Behavior-preserving only: single-line { C, JS } rows for U32.div/mod/cmp,
F32.pow/mod/to_u32, Nat.divmod and Array.clone; RUNTIME_ADTS as a split
string; !xs.length / xs.length truthiness where only non-emptiness is
tested; and a one-line if for bodies that are a single return, break,
continue or assignment. The C and JS emitted for 12 tests across
base/io/reg/parse/compile/run are byte-identical to main's.
Base's String.cmp rebuilds a node of both strings for every equal Char
before handing them back, so Map.get/Map.set and every other caller pay a
copy per character. The C intrinsic reads both cons lists where they are
and returns the original terms: 0 LT, 1 EQ, 2 GT, numeric over the full
U32 of each Char (outside Unicode too). The JS and interpreter lanes keep
Base's def; intr_of already skips a C-only entry there.
tests/base/string_cmp_native_model.bend pins the order on empty and
prefix pairs and on chars 0, 2^31, 2^31-1 and 2^32-1, and pins that both
strings come back whole; the {==} model makes --verdict check it.
131,072 compares of two 256-char strings, checksum 131328 both ways:
329-330 ms before, 116-121 ms after (Apple M2 Pro, Apple clang 21).
comp.ts is 65,911 / 66,000; the preceding refactor commit made the room.
Map no longer reaches String.cmp (it uses Map.diff), so the reported
Map.get/Map.set wins do not apply on main; this is the direct win.
|
Thanks for this, and for the measurements in #2073. Closing it: we'd rather not hard-code We're going to fix it in the compiler instead. One option is #2148 (draft). It finds the places in a def's result that hand a parameter back unchanged and compiles a core that borrows those parameters instead of taking them apart and rebuilding them. Callers take their own arguments back. On #2073's program it reaches your intrinsic's speed (7.89G -> 0.79G instructions; the intrinsic is 0.76G). It also applies to |
Fixes #2073.
Base's
String.cmpis a recursive cons walk that rebuilds a node of both strings for every equal Char before handing them back (String.cmp.rec), so every character compare allocates. The C lane now has an intrinsic that reads both cons lists where they are and returns the original terms -- no rebuild:Order is numeric over the full U32 of each Char, including values outside Unicode, matching Base. The JS and interpreter lanes keep Base's def;
intr_ofalready skips a C-only entry there, as it does forU32.pow.Test
tests/base/string_cmp_native_model.bendfollowscmp_native_model.bend: a{==}model pins the result codes, so--verdictchecks the intrinsic against Base's def;mainprints the same calls, so the interpreter, C and JS lanes must agree.Cases: empty/empty, empty/non-empty both ways, equal prefixes, shorter prefix sorts first both ways, chars
0,2^31-1,2^31,2^32-1, and a 256-char equal pair plus a differing 257th char. It also checks that both returned strings still equal their inputs (the rebuild is gone, not the values).On
upstream/main9162bfe9(Apple M2 Pro, Metal 4, Apple clang 21):--verdict->ALL PROOFS CHECK120010222201/TrueTrueTrueTruetests/base/map_leaf_eq.bend, which usesString.cmp, builds and runs on+RTS --gpu on(the helper needsDEV u64*for the Metal address space)tests/compile/gpu_*tests -> pass with+RTS --gpu onbun gates/repo.ts->PASS: 56 / 56Benchmark
The issue's program: 131,072 compares of two 256-character strings, only the compare result consumed, checksum unchanged at
131328.Built with the same compiler, alternating patched/unpatched on the same machine; earlier after-runs were 116-118 ms. The
Map.get/Map.setclaim in the issue is stale on main -- Map now compares withMap.diff-- so this is the directString.cmpwin only.Cap
comp.tsis capped at 66,000. The intrinsic plus helper costs 199 tokens, which did not fit (it was 65,962 before). This PR is two commits: a behavior-preserving refactor first, 65,962 -> 65,711 (packed intrinsic rows, truthy lengths, one-line guards; C and JS emitted for 12 tests across base/io/reg/parse/compile/run are byte-identical to main's), then the intrinsic, 65,711 -> 65,911. No cap was changed, no code was moved out of the file.I could not run
gates/test.tslocally (it dispatches to the cluster), so the new test was exercised by hand on all four lanes.