Skip to content

Commit 3bb219f

Browse files
committed
Optimize StringView substring search
1 parent f0a7e32 commit 3bb219f

3 files changed

Lines changed: 362 additions & 23 deletions

File tree

builtin/string_find_bench_test.mbt

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
// Copyright 2026 International Digital Economy Academy
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
///|
16+
let string_find_bench_len : Int = 4096
17+
18+
///|
19+
fn make_string_find_single_miss() -> String {
20+
String::make(string_find_bench_len, 'a')
21+
}
22+
23+
///|
24+
fn make_string_find_single_hit_end() -> String {
25+
String::make(string_find_bench_len - 1, 'a') + "Z"
26+
}
27+
28+
///|
29+
fn make_string_find_single_hit_start() -> String {
30+
"Z" + String::make(string_find_bench_len - 1, 'a')
31+
}
32+
33+
///|
34+
fn make_string_find_substring_hit_end() -> String {
35+
String::make(string_find_bench_len - 4, 'a') + "Zabc"
36+
}
37+
38+
///|
39+
fn make_string_find_substring_hit_start() -> String {
40+
"Zabc" + String::make(string_find_bench_len - 4, 'a')
41+
}
42+
43+
///|
44+
fn make_string_find_view_substring_hit_end() -> String {
45+
"prefix-" + String::make(string_find_bench_len - 4, 'a') + "Zabc" + "-suffix"
46+
}
47+
48+
///|
49+
fn make_string_find_view_substring_hit_start() -> String {
50+
"prefix-" + "Zabc" + String::make(string_find_bench_len - 4, 'a') + "-suffix"
51+
}
52+
53+
///|
54+
test "bench StringView::find single code unit hit end n=4096" (it : @bench.T) {
55+
let data = make_string_find_single_hit_end()
56+
let view = data[:]
57+
it.bench(fn() { it.keep(view.find("Z")) })
58+
}
59+
60+
///|
61+
test "bench StringView::find single code unit miss n=4096" (it : @bench.T) {
62+
let data = make_string_find_single_miss()
63+
let view = data[:]
64+
it.bench(fn() { it.keep(view.find("Z")) })
65+
}
66+
67+
///|
68+
test "bench StringView::find substring rare hit end n=4096" (it : @bench.T) {
69+
let data = make_string_find_substring_hit_end()
70+
let view = data[:]
71+
it.bench(fn() { it.keep(view.find("Zabc")) })
72+
}
73+
74+
///|
75+
test "bench StringView::find substring dense miss n=4096" (it : @bench.T) {
76+
let data = make_string_find_single_miss()
77+
let view = data[:]
78+
it.bench(fn() { it.keep(view.find("aaaaZ")) })
79+
}
80+
81+
///|
82+
test "bench StringView::find offset view rare hit end n=4096" (it : @bench.T) {
83+
let data = make_string_find_view_substring_hit_end()
84+
let view = data[7:string_find_bench_len + 7]
85+
it.bench(fn() { it.keep(view.find("Zabc")) })
86+
}
87+
88+
///|
89+
test "bench StringView::rev_find single code unit hit start n=4096" (
90+
it : @bench.T,
91+
) {
92+
let data = make_string_find_single_hit_start()
93+
let view = data[:]
94+
it.bench(fn() { it.keep(view.rev_find("Z")) })
95+
}
96+
97+
///|
98+
test "bench StringView::rev_find single code unit miss n=4096" (it : @bench.T) {
99+
let data = make_string_find_single_miss()
100+
let view = data[:]
101+
it.bench(fn() { it.keep(view.rev_find("Z")) })
102+
}
103+
104+
///|
105+
test "bench StringView::rev_find substring rare hit start n=4096" (
106+
it : @bench.T,
107+
) {
108+
let data = make_string_find_substring_hit_start()
109+
let view = data[:]
110+
it.bench(fn() { it.keep(view.rev_find("Zabc")) })
111+
}
112+
113+
///|
114+
test "bench StringView::rev_find substring dense miss n=4096" (it : @bench.T) {
115+
let data = make_string_find_single_miss()
116+
let view = data[:]
117+
it.bench(fn() { it.keep(view.rev_find("aaaaZ")) })
118+
}
119+
120+
///|
121+
test "bench StringView::rev_find offset view rare hit start n=4096" (
122+
it : @bench.T,
123+
) {
124+
let data = make_string_find_view_substring_hit_start()
125+
let view = data[7:string_find_bench_len + 7]
126+
it.bench(fn() { it.keep(view.rev_find("Zabc")) })
127+
}

0 commit comments

Comments
 (0)