Repository navigation
Description
Bend 2.0.40 and main 266b02ee40a2, Darwin arm64, Homebrew clang 22.1.8.
On the native backend String.cmp (String.cmp.rec in Base) rebuilds a node of both strings for every equal character before handing them back. This is the native counterpart of the JS-only #1701, and the same shape as #1724. Map.get and Map.set reach it at every leaf, so each hit on a String key pays it.
Seven alternating runs, two equal 256-character strings, 131,072 compares, time inside the program:
| 2.0.40 | main | C, the same cons list of 32-bit chars | |
|---|---|---|---|
| Median | 363 ms | 358 ms | 46 ms |
| Checksum | 131328 | 131328 | 131328 |
The C loop only reads, so it is the floor for this data shape, not a proposed implementation. The machine was busy (load 13-20), so read the ratio, about 8x, and not the absolute times.
A native path that walks the two original strings and returns them, as #1725 did for Map.bit, should remove the rebuild. It must keep numeric order over the full U32 of each Char, including values outside Unicode, as #1701 does.
Save as str_cmp.bend:
import Base
def fold(n: Nat, r: (String & String) & Cmp, acc: U32) -> U32:
match n:
case 0n:
((a, b), c) = r
U32.add(acc, U32.from_nat(String.length(a)))
case 1n+p:
((a, b), c) = r
fold(p, String.cmp(a, b), U32.add(acc, Bool.to_u32(Cmp.is_eq(c))))
def run(+key: String) -> IO(Unit):
do IO<Unit>:
start : Nat <- IO.now()
sum : U32 <- IO.pure(U32, fold(131072n, String.cmp(key, String.repeat("a", 256n)), 0))
end : Nat <- IO.now()
IO.print(U32.show(sum))
IO.print(Nat.show(Nat.sub(end, start)))
def main() -> IO(Unit):
run(String.repeat("a", 256n))
bend str_cmp.bend -o str_cmp && ./str_cmpThe C baseline:
// The same data shape as Bend's String: a cons list of 32-bit chars. Compare two 256-node lists 131072 times.
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <time.h>
typedef struct cell { uint32_t c; struct cell *t; } cell;
static cell *mk(int n){ cell *s=0; for(int i=0;i<n;i++){ cell *x=malloc(sizeof *x); x->c=97; x->t=s; s=x; } return s; }
static int cmp(const cell *a, const cell *b){ for(;;){ if(!a) return b?-1:0; if(!b) return 1; if(a->c!=b->c) return a->c<b->c?-1:1; a=a->t; b=b->t; } }
int main(void){ cell *a=mk(256), *b=mk(256); struct timespec t0,t1; clock_gettime(CLOCK_MONOTONIC,&t0); uint32_t acc=0;
for(int i=0;i<131072;i++){ acc += cmp(a,b)==0; __asm__ volatile("" ::: "memory"); }
clock_gettime(CLOCK_MONOTONIC,&t1); printf("%u\n%ld\n", acc+256, (long)((t1.tv_sec-t0.tv_sec)*1000+(t1.tv_nsec-t0.tv_nsec)/1000000)); }
Activity
YidaWeng commentedon Nov 9, 2026
I reproduced the rebuild on current
main(f0ddfe7b, 2.0.40 + #2097) and checked both shapes a fix could take. No Base-only version wins, but a C-only intrinsic does.A
+let in Base does not help. Keeping both strings copyable and callingString.order:typechecks, answers correctly, and returns both strings. On the fold in the issue it is slower: shipped
String.cmp322–334 ms,+let 698–713 ms (131,072 compares of two 256-character strings, checksum131328on both). With both strings threaded instead of rebuilding the right one, the two are within noise (326–327 vs 323–326 ms): sharing both binders puts every later read behind a redirect cell, which costs about what the rebuild costs.A C-only intrinsic is the fix, exactly as #1725 did
map_bit:It walks the original lists with
term_peek, compares heads as(u32)so the order is numeric over the full U32 (outside Unicode too), and returns the sameLT/EQ/GTencoding asU32.cmp.base.bendneeds no edit, and the interpreter and JS keep Base's definition (intr_ofskips an op with noJStext), so this changes the C lane only.On the issue's fold, warmed alternating process pairs on Apple M2 Pro / macOS 27.2 / clang 21.0.0: 322–344 ms before, 118–124 ms after, checksum
131328on both. The C lane and the interpreter agree on 12 cases: empty strings, prefixes, a late first difference,U+FFFFFFFFvsU+7FFFFFFF,U+80000000vsU+7FFFFFFF, andU+0vs empty, each returning both strings intact.The blocker is the source cap, not the patch.
bend2/comp.tsis 65,974 ttok against the 66,000 cap. The two additions above measure +188 ttok (66,162; +212 with the comment), so this needs 162 tokens removed elsewhere. Since the cap is Taelin's call, which do you prefer:refactor(comp)reduction first (as refactor(comp): a round of simplifications, 65,994 -> 65,940 ttok #2088 did, 65,994 → 65,940), then the intrinsic in its own PR;comp.tsyourselves?I can also push the prototype as a draft PR against the cap if seeing the exact diff and emitted C is more useful than the numbers above.
YidaWeng commentedon Nov 13, 2026
Sent #2144. The C lane now has a
String.cmpintrinsic that walks both cons lists where they are and returns the original terms, so no node is rebuilt per equal Char. Order is numeric over the full U32 of each Char, including values outside Unicode; the JS and interpreter lanes keep Base's def, asintr_ofalready skips a C-only entry there.tests/base/string_cmp_native_model.bendfollowscmp_native_model.bend: a{==}model pins the codes so--verdictchecks the intrinsic against Base's def, andmainprints the same calls. It covers empty strings, prefixes both ways, chars0,2^31-1,2^31,2^32-1, and a 256-char equal pair with a differing 257th char, and checks both returned strings still equal their inputs.--verdictsaysALL PROOFS CHECK; interpreter, C and JS all print120010222201/TrueTrueTrueTrue.Your benchmark program, 131,072 compares of two 256-char strings, checksum
131328both ways: 329-330 ms before, 116-121 ms after (same machine, Apple M2 Pro, Apple clang 21).Two notes:
Map.get/Map.setmotivation is stale on main -- Map now compares withMap.diff, notString.cmp-- so I only claim the directString.cmpwin.comp.tswas at 65,962 / 66,000, so perf(comp): String.cmp walks both strings in place in C (#2073) #2144 starts with a behavior-preserving refactor commit (65,962 -> 65,711, byte-identical C and JS on 12 tests) and then adds the intrinsic (-> 65,911). No cap changed and no code moved to another file.tests/base/map_leaf_eq.bend, which usesString.cmp, also builds and runs on Metal (+RTS --gpu on), and all 19tests/compile/gpu_*tests still pass.bun gates/repo.tsisPASS: 56 / 56. I could not rungates/test.tslocally (it dispatches to the cluster), so the new test has only been exercised by hand on all four lanes.