Repository navigation
String.contains tests a position from the node's fields and walks with a prefix def of its own, so a search takes neither string apart - #2196
Merged
nicolas-abril merged 3 commits intoNov 18, 2026
Conversation
…nd walks with its own prefix def, so a search takes no node apart String.contains rebuilt the node it had just matched to hand it to String.starts_with, so the C lane owned the text: at every position it counted the tail, allocated a node and a count cell, and String.starts_with took the copy apart again. String.contains.at tests a position from the node's two fields, and the walk under it is String.contains.pre, String.starts_with.go under a name only String.contains reaches. String.contains lends it both strings at every position, so it borrows them in every program, whatever the program's other calls make String.starts_with do with its own. The JS lane keeps its intrinsic. tests/base/string_contains.bend's unfolding law names the new helper; tests/run/string_contains_lent.bend searches kept, shared, built and handed-over texts and patterns and reads what the caller kept afterwards. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
The walk String.contains keeps for itself was a second copy of String.starts_with.go and its .if helper, with a law so the two could call each other. One def that calls itself and carries "every char so far was the same" does the same job: no law, no second helper, and a def whose only self call is a tail call is a loop on the C lane, where the pair was a cycle of two segments. Instructions, one thread: textstats 97.6M to 73.6M (main 205.6M); 200 searches in a kept 24,576-char string 696M to 385M (main 3,419M); the same with String.starts_with owning elsewhere 714M to 403M (main 5,596M); 200 searches each in a string built for it 243M to 210M (main 441M). Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
nicolas-abril
marked this pull request as ready for review
November 18, 2026 14:52
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
A Base change only:
comp.tsis untouched (65,981 ttok).base.bendgoes from 33,694 to 33,847 ttok; it has no cap ingates/repo.ts, andrepo.tspasses 56 / 56. Measured on one Mac (M4), not on the cluster. Two commits: the first kept a second copy ofString.starts_with's walk (two defs and a law); the second replaces it with one def, described here.The problem
The arm rebuilds the node it has just matched to hand it to
String.starts_with. A rebuilt node is a new value, so on the C laneString.containsowns its text. At every position it takes the node apart, counts the tail (term_keep) and the pattern, allocates a node (when the text is a literal, which has no spare to reuse) and a count cell for its tail (rfc_seal), andString.starts_with.gotakes that copy apart again and drops it. A text the caller keeps is first shared, so every cell of it ends up behind a count cell.On
textstats(a koru kernel: 200,000 words scored withString.length,String.contains(w, "o")andString.starts_with(w, "t")),String.containsis 75% of the instructions.What changes
String.contains.attests a position from the node's two fields, so nothing is rebuilt.String.contains.preis the prefix walk, carrying whether every char so far was the same. It is one def whose only call to itself is a tail call, so it needs no law and the C lane runs it as a loop on main. OnlyString.containsreaches it, andString.containslends it both strings at every position (it reads them again afterwards), so it borrows them in every program.With that, the walk in
String.containsonly peeks: no allocation, no count, no free. When the caller does not keep the text,String.containsstill owns it and frees each node as it passes.The JS lane keeps its intrinsic (
string_contains), so its output is unchanged.Why not
String.starts_withitself. The compiler gives a def one ownership per parameter for the whole program.String.starts_with(s, p)is a tail call intoString.starts_with.go, so as soon as one caller handsString.starts_witha string it owns,.goowns its strings everywhere, and a search sharing that walk has to count each tail before lending it. Three simpler spellings, measured below, do not keep the gain: passingsitself toString.starts_with(both parameters are already+), callingString.starts_with.go.iffrom.at, and matching the pattern inString.containsitself.Measured
One M4, one thread, GPU off,
cc -O3,/usr/bin/time -l, against main9c586f09. Instructions retired:textstats(koru kernel, literal words)String.starts_witha built stringCycles for
textstats, the least of 15 interleaved runs at one placement, on a loaded machine (load average about 8): 35.6M on main, 20.9M for the first commit, 17.4M here. The first commit was swept over 16 placements (30.52M / 31.38M / 32.36M on main, 17.90M / 18.55M / 19.77M); this one was not.The simpler spellings that do not keep the gain (instructions;
textstats/ kept string / kept string withString.starts_withowning elsewhere / built string):String.starts_with(s, p)onsitself.atcallingString.starts_with.go.if(the walk shared)String.contains, no.at.precalled onsitself, no.atWith #2188. On a branch that merges the open optimisation drafts (
opt/integration, where #2188 runs a tail cycle of two defs as one loop), the first commit's pair of defs is a loop too, and it is level or better than the flag:textstats44.6M for the pair and 45.0M for this, the kept string 282.6M and 384.2M, the built string 187.4M and 196.9M (without either, 172.7M, 2,988.8M and 372.5M). The flag costs a step on a mismatch:.prepeeks at both strings before it reads the flag, where the pair's.ifread the flag first. Parameters are matched in order and the first that changes must shrink, so the flag cannot come first.#2148 and #2134, the compiler-side drafts on borrowing, leave these programs unchanged on their own bases.
Reach
Of the programs under
tests,demosandbench/runtime, eight mentionString.contains; six of them emit different C (base/string_contains,base/string_kit,gfx/fullscreen_input,io/args,io/args_bytes,run/string_contains_long). For the first commit all 945 programs that emit C were compared: 938 the same as main, among them all 17 benches and every demo that builds. Nothing else in Base callsString.contains.Checks
Against main, locally, on the second commit:
tests/**/*.bendwith#|lines built and run with the GPU off): 886 pass on main, 887 here; no status differs over the 1,874 tests on both, and the new test passes.-DBEND_POISONand a count of live heap words at exit: same output, exit code and live count as main on each. The new test ends with 0 live words at one thread and at eight.String.containsis identical to main's.bend file.bend) on the seven tests that mention it: same output as main.bend bend2/base.bend --check-only: ALL PROOFS CHECK.main(normalized by the checker, so Base's def runs, not the intrinsic) over 112 searches: the same string as on main.tests/base/string_contains.bend: itsunfoldlaw states the def's first step, so it namesString.contains.atnow. A second law is new: an empty pattern is found at the first position of a text whose tail is unknown (it holds on main too).tests/run/string_contains_lent.bend: texts built at run time, so they sit on the heap. A text and a pattern searched twice and read whole afterwards; a tail that shares its cells with a kept text, searched both ways; a text built for the search; a search as a tail call; the strings of a kept list; empty texts and patterns; a pattern longer than the text; a match at the very end; an overlapping near-match (aaabinaaaa…); chars past U+FFFF and past Unicode; and one call that handsString.starts_witha built string. Same output frombend file.bend, the JS lane and C.Not covered
String.containstoString.starts_withon an unknown pattern by computation alone no longer goes through: the two walks are equal, but under different names. No test or demo has one.String.starts_withitself is as on main: it owns its strings when a caller hands them over.🤖 Generated with Claude Code