Debian Codegraph @

simd/bench_test.go

raw symbols SCIP
1 package simd
2
3 import (
4 "bytes"
5 "strconv"
6 "testing"
7 )
8
9 // The benchmarks pair each function with the closest idiomatic alternative
10 // (stdlib where one exists, a scalar loop otherwise) using the
11 // fiber-vs-default sub-run naming shared with the other packages. Inputs are
12 // worst-case misses so every variant scans the full haystack.
13
14 var benchSizes = []int{8, 32, 64, 512, 4096}
15
16 func benchName(n int) string {
17 return strconv.Itoa(n) + "B"
18 }
19
20 // benchData is 'a'..'w' repeating: pure ASCII, no digits, no match for the
21 // searched needles below.
22 func benchData(n int) []byte {
23 b := make([]byte, n)
24 for i := range b {
25 b[i] = 'a' + byte(i%23)
26 }
27 return b
28 }
29
30 func Benchmark_Memchr2(b *testing.B) {
31 for _, n := range benchSizes {
32 data := benchData(n)
33 b.Run(benchName(n)+"/simd", func(b *testing.B) {
34 b.ReportAllocs()
35 for range b.N {
36 if Memchr2(data, 'z', 'y') != -1 {
37 b.Fatal("unexpected match")
38 }
39 }
40 })
41 b.Run(benchName(n)+"/scalar", func(b *testing.B) {
42 b.ReportAllocs()
43 for range b.N {
44 if memchr2Generic(data, 'z', 'y') != -1 {
45 b.Fatal("unexpected match")
46 }
47 }
48 })
49 b.Run(benchName(n)+"/default", func(b *testing.B) {
50 b.ReportAllocs()
51 for range b.N {
52 if bytes.ContainsAny(data, "zy") {
53 b.Fatal("unexpected match")
54 }
55 }
56 })
57 }
58 }
59
60 func Benchmark_Memchr3(b *testing.B) {
61 for _, n := range benchSizes {
62 data := benchData(n)
63 b.Run(benchName(n)+"/simd", func(b *testing.B) {
64 b.ReportAllocs()
65 for range b.N {
66 if Memchr3(data, 'z', 'y', 'x') != -1 {
67 b.Fatal("unexpected match")
68 }
69 }
70 })
71 b.Run(benchName(n)+"/scalar", func(b *testing.B) {
72 b.ReportAllocs()
73 for range b.N {
74 if memchr3Generic(data, 'z', 'y', 'x') != -1 {
75 b.Fatal("unexpected match")
76 }
77 }
78 })
79 b.Run(benchName(n)+"/default", func(b *testing.B) {
80 b.ReportAllocs()
81 for range b.N {
82 if bytes.ContainsAny(data, "zyx") {
83 b.Fatal("unexpected match")
84 }
85 }
86 })
87 }
88 }
89
90 func Benchmark_MemchrPair(b *testing.B) {
91 for _, n := range benchSizes {
92 data := benchData(n)
93 b.Run(benchName(n)+"/simd", func(b *testing.B) {
94 b.ReportAllocs()
95 for range b.N {
96 if MemchrPair(data, 'a', 'c', 1) != -1 {
97 b.Fatal("unexpected match")
98 }
99 }
100 })
101 b.Run(benchName(n)+"/scalar", func(b *testing.B) {
102 b.ReportAllocs()
103 for range b.N {
104 if refMemchrPair(data, 'a', 'c', 1) != -1 {
105 b.Fatal("unexpected match")
106 }
107 }
108 })
109 }
110 }
111
112 func Benchmark_MemchrDigit(b *testing.B) {
113 for _, n := range benchSizes {
114 data := benchData(n)
115 b.Run(benchName(n)+"/simd", func(b *testing.B) {
116 b.ReportAllocs()
117 for range b.N {
118 if MemchrDigit(data) != -1 {
119 b.Fatal("unexpected match")
120 }
121 }
122 })
123 b.Run(benchName(n)+"/scalar", func(b *testing.B) {
124 b.ReportAllocs()
125 for range b.N {
126 if memchrDigitGeneric(data) != -1 {
127 b.Fatal("unexpected match")
128 }
129 }
130 })
131 }
132 }
133
134 // benchNonWordData is a repeating run of punctuation and spaces holding no
135 // \w character, so MemchrWord has to scan the whole haystack. benchData
136 // cannot serve here: its first byte is already a word character.
137 func benchNonWordData(n int) []byte {
138 const punct = "!\"#$%&'()*+,-./:;<=>?@[\\]^`{|}~ "
139 b := make([]byte, n)
140 for i := range b {
141 b[i] = punct[i%len(punct)]
142 }
143 return b
144 }
145
146 func Benchmark_MemchrWord(b *testing.B) {
147 for _, n := range benchSizes {
148 data := benchNonWordData(n)
149 b.Run(benchName(n)+"/simd", func(b *testing.B) {
150 b.ReportAllocs()
151 for range b.N {
152 if MemchrWord(data) != -1 {
153 b.Fatal("unexpected match")
154 }
155 }
156 })
157 b.Run(benchName(n)+"/scalar", func(b *testing.B) {
158 b.ReportAllocs()
159 for range b.N {
160 if memchrWordGeneric(data) != -1 {
161 b.Fatal("unexpected match")
162 }
163 }
164 })
165 }
166 }
167
168 func Benchmark_MemchrNotWord(b *testing.B) {
169 for _, n := range benchSizes {
170 data := benchData(n)
171 b.Run(benchName(n)+"/simd", func(b *testing.B) {
172 b.ReportAllocs()
173 for range b.N {
174 if MemchrNotWord(data) != -1 {
175 b.Fatal("unexpected match")
176 }
177 }
178 })
179 b.Run(benchName(n)+"/scalar", func(b *testing.B) {
180 b.ReportAllocs()
181 for range b.N {
182 if memchrNotWordGeneric(data) != -1 {
183 b.Fatal("unexpected match")
184 }
185 }
186 })
187 }
188 }
189
190 // memmemBenchSizes brackets the 128-byte crossover that memmemMinHaystack
191 // encodes. Benchmark_Memmem measures the exported routing end to end — its
192 // sub-128B simd rows run bytes.Index plus dispatch, so they show routing
193 // overhead only — while Benchmark_Memmem_Prefilter forces the prefilter
194 // helpers at every size, making the crossover itself reproducible from
195 // this file.
196 var memmemBenchSizes = []int{8, 32, 64, 96, 128, 192, 512, 4096}
197
198 func Benchmark_Memmem(b *testing.B) {
199 needle := []byte("q7z")
200 for _, n := range memmemBenchSizes {
201 data := benchData(n)
202 b.Run(benchName(n)+"/simd", func(b *testing.B) {
203 b.ReportAllocs()
204 for range b.N {
205 if Memmem(data, needle) != -1 {
206 b.Fatal("unexpected match")
207 }
208 }
209 })
210 b.Run(benchName(n)+"/default", func(b *testing.B) {
211 b.ReportAllocs()
212 for range b.N {
213 if bytes.Contains(data, needle) {
214 b.Fatal("unexpected match")
215 }
216 }
217 })
218 }
219 }
220
221 // Benchmark_Memmem_Prefilter drives the prefilter helpers directly,
222 // bypassing Memmem's 128-byte routing, so the memmemMinHaystack tradeoff
223 // is measurable instead of asserted. Each helper gets the needle shape
224 // Memmem actually routes to it: the paired scan (short needle, two
225 // distinct rare bytes) breaks even with bytes.Index around the 64B point
226 // and wins from 96B up, so 128 carries a conservative margin that also
227 // covers the per-call SelectRareBytes cost these direct calls exclude.
228 // The single scan (long needle) anchors on 'q', which benchData's
229 // repeating 'a'..'w' cycle yields every 23 bytes, so from a few hundred
230 // bytes up it burns its miss budget and lands on the fallback, tracking
231 // bytes.Index within a constant — the documented bounded worst case; its
232 // wins come on data where the anchor byte is genuinely rare.
233 func Benchmark_Memmem_Prefilter(b *testing.B) {
234 pairNeedle := []byte("q7z")
235 pairInfo := SelectRareBytes(pairNeedle)
236 singleNeedle := []byte("the quick brown fox")
237 singleInfo := SelectRareBytes(singleNeedle)
238 for _, n := range memmemBenchSizes {
239 data := benchData(n)
240 b.Run(benchName(n)+"/paired", func(b *testing.B) {
241 b.ReportAllocs()
242 for range b.N {
243 if memmemPaired(data, pairNeedle, pairInfo) != -1 {
244 b.Fatal("unexpected match")
245 }
246 }
247 })
248 b.Run(benchName(n)+"/paired-stdlib", func(b *testing.B) {
249 b.ReportAllocs()
250 for range b.N {
251 if bytes.Contains(data, pairNeedle) {
252 b.Fatal("unexpected match")
253 }
254 }
255 })
256 if n < len(singleNeedle) {
257 continue
258 }
259 b.Run(benchName(n)+"/single", func(b *testing.B) {
260 b.ReportAllocs()
261 for range b.N {
262 if memmemSingle(data, singleNeedle, singleInfo.Byte1, singleInfo.Index1) != -1 {
263 b.Fatal("unexpected match")
264 }
265 }
266 })
267 b.Run(benchName(n)+"/single-stdlib", func(b *testing.B) {
268 b.ReportAllocs()
269 for range b.N {
270 if bytes.Contains(data, singleNeedle) {
271 b.Fatal("unexpected match")
272 }
273 }
274 })
275 }
276 }
277
278 // Benchmark_Memmem_Adversarial pins the miss-budget fallback: every
279 // haystack position is a rare-byte anchor and the needle mismatches only at
280 // its last byte, the shape that used to degrade to O(n*m). With the budget
281 // it must track bytes.Index within a constant.
282 func Benchmark_Memmem_Adversarial(b *testing.B) {
283 // The mismatch byte must rank as more common than 'z' so the rare-byte
284 // selection anchors on 'z' and every haystack position is a candidate.
285 haystack := bytes.Repeat([]byte{'z'}, 1<<20)
286 needle := append(bytes.Repeat([]byte{'z'}, 999), 'e')
287 b.Run("simd", func(b *testing.B) {
288 b.ReportAllocs()
289 for range b.N {
290 if Memmem(haystack, needle) != -1 {
291 b.Fatal("unexpected match")
292 }
293 }
294 })
295 b.Run("default", func(b *testing.B) {
296 b.ReportAllocs()
297 for range b.N {
298 if bytes.Contains(haystack, needle) {
299 b.Fatal("unexpected match")
300 }
301 }
302 })
303 }
304
305 func Benchmark_IsASCII(b *testing.B) {
306 for _, n := range benchSizes {
307 data := benchData(n)
308 b.Run(benchName(n)+"/simd", func(b *testing.B) {
309 b.ReportAllocs()
310 for range b.N {
311 if !IsASCII(data) {
312 b.Fatal("unexpected non-ASCII")
313 }
314 }
315 })
316 b.Run(benchName(n)+"/swar", func(b *testing.B) {
317 b.ReportAllocs()
318 for range b.N {
319 if !isASCIIGeneric(data) {
320 b.Fatal("unexpected non-ASCII")
321 }
322 }
323 })
324 }
325 }
326
327 func Benchmark_FirstNonASCII(b *testing.B) {
328 for _, n := range benchSizes {
329 data := benchData(n)
330 b.Run(benchName(n)+"/simd", func(b *testing.B) {
331 b.ReportAllocs()
332 for range b.N {
333 if FirstNonASCII(data) != -1 {
334 b.Fatal("unexpected non-ASCII")
335 }
336 }
337 })
338 b.Run(benchName(n)+"/swar", func(b *testing.B) {
339 b.ReportAllocs()
340 for range b.N {
341 if firstNonASCIIGeneric(data) != -1 {
342 b.Fatal("unexpected non-ASCII")
343 }
344 }
345 })
346 }
347 }
348
349 func Benchmark_CountNonASCII(b *testing.B) {
350 for _, n := range benchSizes {
351 data := benchData(n)
352 b.Run(benchName(n)+"/simd", func(b *testing.B) {
353 b.ReportAllocs()
354 for range b.N {
355 if CountNonASCII(data) != 0 {
356 b.Fatal("unexpected non-ASCII")
357 }
358 }
359 })
360 b.Run(benchName(n)+"/swar", func(b *testing.B) {
361 b.ReportAllocs()
362 for range b.N {
363 if countNonASCIIGeneric(data) != 0 {
364 b.Fatal("unexpected non-ASCII")
365 }
366 }
367 })
368 }
369 }