Skip to content

perf(comp): String.cmp walks both strings in place in C (#2073) - #2144

Closed
YidaWeng wants to merge 2 commits into
bendlang:mainfrom
YidaWeng:perf/string-cmp
Closed

YidaWeng wants to merge 2 commits into
bendlang:mainfrom
YidaWeng:perf/string-cmp

Conversation

@YidaWeng

Copy link
Copy Markdown
Contributor

Fixes #2073.

Base's String.cmp is 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:

INLINE u64 string_cmp(Env e, Term a, Term b) {
  DEV u64* H = e.mem;
  while (term_aux(a) == CID(SCon)) {
    if (term_aux(b) != CID(SCon)) return 2;
    u64 at = term_peek(H, a), bt = term_peek(H, b);
    if (H[at] != H[bt]) return (u32)H[at] < (u32)H[bt] ? 0 : 2;
    a = H[at + 1], b = H[bt + 1];
  }
  return term_aux(b) == CID(SCon) ? 0 : 1;
}

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_of already skips a C-only entry there, as it does for U32.pow.

Test

tests/base/string_cmp_native_model.bend follows cmp_native_model.bend: a {==} model pins the result codes, so --verdict checks the intrinsic against Base's def; main prints 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/main 9162bfe9 (Apple M2 Pro, Metal 4, Apple clang 21):

  • --verdict -> ALL PROOFS CHECK
  • interpreter, C and JS -> 120010222201 / TrueTrueTrueTrue
  • tests/base/map_leaf_eq.bend, which uses String.cmp, builds and runs on +RTS --gpu on (the helper needs DEV u64* for the Metal address space)
  • all 19 tests/compile/gpu_* tests -> pass with +RTS --gpu on
  • bun gates/repo.ts -> PASS: 56 / 56

Benchmark

The issue's program: 131,072 compares of two 256-character strings, only the compare result consumed, checksum unchanged at 131328.

before after
run 1 329 ms 120 ms
run 2 330 ms 121 ms
run 3 330 ms 121 ms

Built with the same compiler, alternating patched/unpatched on the same machine; earlier after-runs were 116-118 ms. The Map.get/Map.set claim in the issue is stale on main -- Map now compares with Map.diff -- so this is the direct String.cmp win only.

Cap

comp.ts is 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.ts locally (it dispatches to the cluster), so the new test was exercised by hand on all four lanes.

…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.
@nicolas-abril

Copy link
Copy Markdown
Collaborator

Thanks for this, and for the measurements in #2073. Closing it: we'd rather not hard-code String.cmp in the compiler. Its cost isn't specific to strings. It's the cost of any def that hands a parameter back rebuilt from the fields its match bound, and a hand-written intrinsic only fixes the one function.

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 Map.has and to user defs over user types.

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.

Native String.cmp rebuilds both strings on a shared prefix

2 participants