From e321cae7321c6c4c369923ed0676bb187159a538 Mon Sep 17 00:00:00 2001 From: Nico Abril Date: Tue, 17 Nov 2026 15:32:01 +0100 Subject: [PATCH 1/3] perf(base): String.contains tests a position from the node's fields and 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 --- bend2/base.bend | 36 +++++++++- tests/base/string_contains.bend | 7 +- tests/run/string_contains_lent.bend | 102 ++++++++++++++++++++++++++++ 3 files changed, 143 insertions(+), 2 deletions(-) create mode 100644 tests/run/string_contains_lent.bend diff --git a/bend2/base.bend b/bend2/base.bend index cbbe089e64..2e09467624 100644 --- a/bend2/base.bend +++ b/bend2/base.bend @@ -2245,12 +2245,46 @@ def String.contains.if(t: String, p: String, here: Bool) -> Bool: case True{}: True{} +law String.contains.pre: + for p: String + for s: String + Bool + +def String.contains.pre.if(pt: String, t: String, same: Bool) -> Bool: + match same: + case False{}: + False{} + case True{}: + String.contains.pre(pt, t) + +# String.starts_with.go's walk under a name only String.contains reaches. +# String.contains lends it both strings at every position, so it borrows them +# in every program; String.starts_with.go owns its strings as soon as one +# caller hands them over, and a borrowed search would then count each tail. +def String.contains.pre(p, s): + match p s: + case SNil{} _: + True{} + case SCon{y, yt} SNil{}: + False{} + case SCon{y, yt} SCon{x, xt}: + String.contains.pre.if(yt, xt, Char.is_eq(x, y)) + +# Whether p is a prefix of SCon{h, t}, from the node's two fields: testing a +# position rebuilds no node, so the search neither allocates nor counts. +def String.contains.at(p: String, h: Char, t: String) -> Bool: + match p: + case SNil{}: + True{} + case SCon{y, yt}: + String.contains.pre.if(yt, t, Char.is_eq(h, y)) + def String.contains(s, p): match s: case SNil{}: String.is_empty(p) case SCon{h, t}: - String.contains.if(t, p, String.starts_with(SCon{h, t}, p)) + String.contains.if(t, p, String.contains.at(p, h, t)) def String.take(s: String, n: Nat) -> String: match s n: diff --git a/tests/base/string_contains.bend b/tests/base/string_contains.bend index dd56931917..ad0637134b 100644 --- a/tests/base/string_contains.bend +++ b/tests/base/string_contains.bend @@ -16,7 +16,12 @@ def apply(f: @+p: String -> Bool, p: String) -> Bool: def unfold(-h: Char, -t: String, -p: String) -> {String.contains(SCon{h, t}, p) == - String.contains.if(t, p, String.starts_with(SCon{h, t}, p)) : Bool}: + String.contains.if(t, p, String.contains.at(p, h, t)) : Bool}: + {==} + +# An empty pattern is found at the first position, whatever follows it. +def empty_pattern(-h: Char, -t: String) -> + {String.contains(SCon{h, t}, "") == True{} : Bool}: {==} def main() -> IO(Unit): diff --git a/tests/run/string_contains_lent.bend b/tests/run/string_contains_lent.bend new file mode 100644 index 0000000000..19a35c171e --- /dev/null +++ b/tests/run/string_contains_lent.bend @@ -0,0 +1,102 @@ +# String.contains searches without taking either string apart: a text or a +# pattern the caller keeps, shares, built for the call or handed over in a +# tail call gives the same answer, and what the caller keeps reads the same +# afterwards. The texts are built at run time, so they sit on the heap. +import Base + +def one(x: U32) -> String: + SCon{Char.from_u32(x), SNil{}} + +# Another caller hands String.starts_with a built string in a tail call. +def starts_built(a: String, b: String) -> Bool: + String.starts_with(String.append(a, b), "ab") + +# A kept text and a kept pattern, each read whole after two searches. +def kept(+s: String, +p: String) -> String: + Bool.show(String.contains(s, p)) ++ Bool.show(String.contains(s, p)) + ++ " " ++ Nat.show(String.length(s)) ++ " " ++ String.take(s, 5n) + ++ " " ++ Nat.show(String.length(p)) ++ " " ++ String.take(p, 3n) + +# A tail of a kept text shares its cells with it. +def tails(+s: String, +p: String) -> String: + +t = String.drop(s, 4n) + Bool.show(String.contains(t, p)) ++ Bool.show(String.contains(s, p)) + ++ Bool.show(String.contains(t, s)) ++ Bool.show(String.contains(s, t)) + ++ " " ++ String.take(t, 6n) ++ " " ++ Nat.show(String.length(t)) + ++ " " ++ Nat.show(String.length(s)) + +# A text built for the search, and a search that is the def's tail call. +def owned(a: String, b: String, p: String) -> Bool: + String.contains(String.append(a, b), p) + +# The words of a kept list, each searched in place. +def count(ws: List<&2, String>, +p: String, acc: Nat) -> Nat: + match ws: + case Nil{}: + acc + case +w <> t: + count(t, p, Nat.add(Nat.add(acc, String.length(w)), + Bool.pick(Nat, String.contains(w, p), 100n, 0n))) + +def words(+n: Nat) -> List<&2, String>: + [String.repeat("ab", n), String.repeat("b", n), "", String.repeat("ba", Nat.add(n, n))] + +def show(b: Bool) -> String: + Bool.pick(String, b, "1", "0") + +def main() -> IO(Unit): + do IO: + xs : List <- IO.args() + +n : Nat = List.length(&1, String, xs) + +ab : String = String.repeat("ab", Nat.add(n, 2n)) + +big : String = String.repeat("abc", Nat.add(n, 299n)) + +hi : String = String.append(one(1114209), String.append(one(128512), String.repeat("x", n))) + +ws : List<&2, String> = words(Nat.add(n, 1n)) + IO.print(kept(ab, "ba")) + IO.print(kept(ab, "bb")) + IO.print(kept(ab, String.repeat("ab", Nat.add(n, 2n)))) + IO.print(kept(ab, String.repeat("ab", Nat.add(n, 3n)))) + IO.print(kept(ab, String.repeat("b", n))) + IO.print(kept(ab, String.repeat("b", Nat.sub(n, 1n)))) + IO.print(kept(String.repeat("b", Nat.sub(n, 1n)), String.repeat("b", Nat.sub(n, 1n)))) + IO.print(kept(String.repeat("b", Nat.sub(n, 1n)), ab)) + IO.print(kept(String.append(big, "d"), "cd")) + IO.print(kept(String.append(big, "d"), "cabd")) + IO.print(kept(String.repeat("a", Nat.add(n, 5n)), "aaab")) + IO.print(kept(String.append(String.repeat("a", Nat.add(n, 5n)), "b"), "aaab")) + IO.print(kept(hi, one(128512))) + IO.print(kept(hi, String.append(one(128512), "x"))) + IO.print(kept(hi, one(55357))) + IO.print(kept(hi, String.append(one(1114209), one(128513)))) + IO.print(tails(ab, "abab")) + IO.print(tails(String.append(big, "d"), "abcd")) + IO.print(show(owned(ab, big, "babc")) ++ show(owned(ab, big, "bb")) + ++ show(owned(ab, "", "")) ++ show(owned("", "", ab)) + ++ show(starts_built(ab, big)) ++ show(starts_built("", hi)) + ++ show(String.contains(ab, ab)) ++ show(String.contains(ab, big)) + ++ show(String.contains(big, ab)) ++ show(String.contains(big, String.drop(big, 890n)))) + IO.print(Nat.show(count(ws, "ba", 0n)) ++ " " ++ Nat.show(count(ws, "", 0n)) + ++ " " ++ Nat.show(count(ws, ab, 0n)) ++ " " ++ String.join(ws, ",")) + IO.print(ab ++ " " ++ String.drop(hi, 1n) ++ " " ++ Nat.show(String.length(big))) + +#|TrueTrue 6 ababa 2 ba +#|FalseFalse 6 ababa 2 bb +#|TrueTrue 6 ababa 6 aba +#|FalseFalse 6 ababa 8 aba +#|TrueTrue 6 ababa 1 b +#|TrueTrue 6 ababa 0 +#|TrueTrue 0 0 +#|FalseFalse 0 6 aba +#|TrueTrue 901 abcab 2 cd +#|FalseFalse 901 abcab 4 cab +#|FalseFalse 6 aaaaa 4 aaa +#|TrueTrue 7 aaaaa 4 aaa +#|TrueTrue 3 �😀x 1 😀 +#|TrueTrue 3 �😀x 2 😀x +#|FalseFalse 3 �😀x 1 � +#|FalseFalse 3 �😀x 2 �😁 +#|FalseTrueFalseTrue ab 2 6 +#|TrueTrueFalseTrue bcabca 897 901 +#|1010101001 +#|214 414 114 abab,bb,,babababa +#|ababab 😀x 900 From 84a62dfe5536a9f03b1af3cc6b4ebb64e4b29594 Mon Sep 17 00:00:00 2001 From: Nico Abril Date: Tue, 17 Nov 2026 19:34:11 +0100 Subject: [PATCH 2/3] perf(base): String.contains' prefix walk is one def that carries a flag 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 --- bend2/base.bend | 33 +++++++++++---------------------- 1 file changed, 11 insertions(+), 22 deletions(-) diff --git a/bend2/base.bend b/bend2/base.bend index 2e09467624..c54d215996 100644 --- a/bend2/base.bend +++ b/bend2/base.bend @@ -2245,30 +2245,19 @@ def String.contains.if(t: String, p: String, here: Bool) -> Bool: case True{}: True{} -law String.contains.pre: - for p: String - for s: String - Bool - -def String.contains.pre.if(pt: String, t: String, same: Bool) -> Bool: - match same: - case False{}: +# Whether p is a prefix of s, while every char so far was the same. One def +# that calls itself, so it is a loop, and one only String.contains reaches, so +# it borrows both strings whatever the program's other calls do. +def String.contains.pre(p: String, s: String, same: Bool) -> Bool: + match p s same: + case _ _ False{}: False{} - case True{}: - String.contains.pre(pt, t) - -# String.starts_with.go's walk under a name only String.contains reaches. -# String.contains lends it both strings at every position, so it borrows them -# in every program; String.starts_with.go owns its strings as soon as one -# caller hands them over, and a borrowed search would then count each tail. -def String.contains.pre(p, s): - match p s: - case SNil{} _: + case SNil{} _ True{}: True{} - case SCon{y, yt} SNil{}: + case SCon{y, yt} SNil{} True{}: False{} - case SCon{y, yt} SCon{x, xt}: - String.contains.pre.if(yt, xt, Char.is_eq(x, y)) + case SCon{y, yt} SCon{x, xt} True{}: + String.contains.pre(yt, xt, Char.is_eq(x, y)) # Whether p is a prefix of SCon{h, t}, from the node's two fields: testing a # position rebuilds no node, so the search neither allocates nor counts. @@ -2277,7 +2266,7 @@ def String.contains.at(p: String, h: Char, t: String) -> Bool: case SNil{}: True{} case SCon{y, yt}: - String.contains.pre.if(yt, t, Char.is_eq(h, y)) + String.contains.pre(yt, t, Char.is_eq(h, y)) def String.contains(s, p): match s: From fc5a04e700b00fb4a440ab856326de66ec1812b1 Mon Sep 17 00:00:00 2001 From: Nico Abril Date: Wed, 18 Nov 2026 12:23:33 +0100 Subject: [PATCH 3/3] chore(base): String.contains' two helpers carry no comments Co-Authored-By: Claude Fable 5.2 --- bend2/base.bend | 5 ----- 1 file changed, 5 deletions(-) diff --git a/bend2/base.bend b/bend2/base.bend index c54d215996..7822a33677 100644 --- a/bend2/base.bend +++ b/bend2/base.bend @@ -2245,9 +2245,6 @@ def String.contains.if(t: String, p: String, here: Bool) -> Bool: case True{}: True{} -# Whether p is a prefix of s, while every char so far was the same. One def -# that calls itself, so it is a loop, and one only String.contains reaches, so -# it borrows both strings whatever the program's other calls do. def String.contains.pre(p: String, s: String, same: Bool) -> Bool: match p s same: case _ _ False{}: @@ -2259,8 +2256,6 @@ def String.contains.pre(p: String, s: String, same: Bool) -> Bool: case SCon{y, yt} SCon{x, xt} True{}: String.contains.pre(yt, xt, Char.is_eq(x, y)) -# Whether p is a prefix of SCon{h, t}, from the node's two fields: testing a -# position rebuilds no node, so the search neither allocates nor counts. def String.contains.at(p: String, h: Char, t: String) -> Bool: match p: case SNil{}: