Debian Codegraph @

simd/bench_test.go

raw
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 // The SWAR fallback directly: through the exported function it is
102 // reached only below the AVX2 dispatch length on amd64 hosts.
103 b.Run(benchName(n)+"/swar", func(b *testing.B) {
104 b.ReportAllocs()
105 for range b.N {
106 if memchrPairGeneric(data, 'a', 'c', 1) != -1 {
107 b.Fatal("unexpected match")
108 }
109 }
110 })
111 b.Run(benchName(n)+"/scalar", func(b *testing.B) {
112 b.ReportAllocs()
113 for range b.N {
114 if refMemchrPair(data, 'a', 'c', 1) != -1 {
115 b.Fatal("unexpected match")
116 }
117 }
118 })
119 }
120 }
121
122 func Benchmark_MemchrDigit(b *testing.B) {
123 for _, n := range benchSizes {
124 data := benchData(n)
125 b.Run(benchName(n)+"/simd", func(b *testing.B) {
126 b.ReportAllocs()
127 for range b.N {
128 if MemchrDigit(data) != -1 {
129 b.Fatal("unexpected match")
130 }
131 }
132 })
133 b.Run(benchName(n)+"/scalar", func(b *testing.B) {
134 b.ReportAllocs()
135 for range b.N {
136 if memchrDigitGeneric(data) != -1 {
137 b.Fatal("unexpected match")
138 }
139 }
140 })
141 }
142 }
143
144 // benchNonWordData is a repeating run of punctuation and spaces holding no
145 // \w character, so MemchrWord has to scan the whole haystack. benchData
146 // cannot serve here: its first byte is already a word character.
147 func benchNonWordData(n int) []byte {
148 const punct = "!\"#$%&'()*+,-./:;<=>?@[\\]^`{|}~ "
149 b := make([]byte, n)
150 for i := range b {
151 b[i] = punct[i%len(punct)]
152 }
153 return b
154 }
155
156 func Benchmark_MemchrWord(b *testing.B) {
157 for _, n := range benchSizes {
158 data := benchNonWordData(n)
159 b.Run(benchName(n)+"/simd", func(b *testing.B) {
160 b.ReportAllocs()
161 for range b.N {
162 if MemchrWord(data) != -1 {
163 b.Fatal("unexpected match")
164 }
165 }
166 })
167 b.Run(benchName(n)+"/scalar", func(b *testing.B) {
168 b.ReportAllocs()
169 for range b.N {
170 if memchrWordGeneric(data) != -1 {
171 b.Fatal("unexpected match")
172 }
173 }
174 })
175 }
176 }
177
178 func Benchmark_MemchrNotWord(b *testing.B) {
179 for _, n := range benchSizes {
180 data := benchData(n)
181 b.Run(benchName(n)+"/simd", func(b *testing.B) {
182 b.ReportAllocs()
183 for range b.N {
184 if MemchrNotWord(data) != -1 {
185 b.Fatal("unexpected match")
186 }
187 }
188 })
189 b.Run(benchName(n)+"/scalar", func(b *testing.B) {
190 b.ReportAllocs()
191 for range b.N {
192 if memchrNotWordGeneric(data) != -1 {
193 b.Fatal("unexpected match")
194 }
195 }
196 })
197 }
198 }
199
200 // memmemBenchSizes brackets the routing points memmemMinPairedHaystack
201 // (65) and memmemMinSingleHaystack (128) encode. Benchmark_Memmem
202 // measures the exported routing end to end — its simd rows up to 64B run
203 // bytes.Index plus dispatch, so they show routing overhead only — while
204 // Benchmark_Memmem_Prefilter forces the prefilter helpers at every size,
205 // making the crossovers themselves reproducible from this file.
206 var memmemBenchSizes = []int{8, 32, 64, 96, 128, 192, 512, 4096}
207
208 func Benchmark_Memmem(b *testing.B) {
209 needle := []byte("q7z")
210 for _, n := range memmemBenchSizes {
211 data := benchData(n)
212 b.Run(benchName(n)+"/simd", func(b *testing.B) {
213 b.ReportAllocs()
214 for range b.N {
215 if Memmem(data, needle) != -1 {
216 b.Fatal("unexpected match")
217 }
218 }
219 })
220 b.Run(benchName(n)+"/default", func(b *testing.B) {
221 b.ReportAllocs()
222 for range b.N {
223 if bytes.Contains(data, needle) {
224 b.Fatal("unexpected match")
225 }
226 }
227 })
228 }
229 }
230
231 // Benchmark_Memmem_Prefilter drives the prefilter helpers directly,
232 // bypassing Memmem's routing, so the memmemMinPairedHaystack and
233 // memmemMinSingleHaystack tradeoffs are measurable instead of asserted.
234 // Each helper gets the needle shape Memmem actually routes to it: the
235 // paired scan (short needle, two distinct rare bytes) is level with
236 // bytes.Index's brute-force kernel at 64B and wins from 65B up, where
237 // bytes.Index switches to a first-byte-anchored loop.
238 // The single scan (long needle) anchors on 'q', which benchData's
239 // repeating 'a'..'w' cycle yields every 23 bytes, so from a few hundred
240 // bytes up it burns its miss budget and lands on the fallback, tracking
241 // bytes.Index within a constant — the documented bounded worst case; its
242 // wins come on data where the anchor byte is genuinely rare.
243 func Benchmark_Memmem_Prefilter(b *testing.B) {
244 pairNeedle := []byte("q7z")
245 pairInfo := SelectRareBytes(pairNeedle)
246 singleNeedle := []byte("the quick brown fox")
247 singleInfo := SelectRareBytes(singleNeedle)
248 for _, n := range memmemBenchSizes {
249 data := benchData(n)
250 b.Run(benchName(n)+"/paired", func(b *testing.B) {
251 b.ReportAllocs()
252 for range b.N {
253 if memmemPaired(data, pairNeedle, pairInfo) != -1 {
254 b.Fatal("unexpected match")
255 }
256 }
257 })
258 b.Run(benchName(n)+"/paired-stdlib", func(b *testing.B) {
259 b.ReportAllocs()
260 for range b.N {
261 if bytes.Contains(data, pairNeedle) {
262 b.Fatal("unexpected match")
263 }
264 }
265 })
266 if n < len(singleNeedle) {
267 continue
268 }
269 b.Run(benchName(n)+"/single", func(b *testing.B) {
270 b.ReportAllocs()
271 for range b.N {
272 if memmemSingle(data, singleNeedle, singleInfo.Byte1, singleInfo.Index1) != -1 {
273 b.Fatal("unexpected match")
274 }
275 }
276 })
277 b.Run(benchName(n)+"/single-stdlib", func(b *testing.B) {
278 b.ReportAllocs()
279 for range b.N {
280 if bytes.Contains(data, singleNeedle) {
281 b.Fatal("unexpected match")
282 }
283 }
284 })
285 }
286 }
287
288 // Benchmark_Memmem_Adversarial pins the miss-budget fallback: every
289 // haystack position is a rare-byte anchor and the needle mismatches only at
290 // its last byte, the shape that used to degrade to O(n*m). With the budget
291 // it must track bytes.Index within a constant.
292 func Benchmark_Memmem_Adversarial(b *testing.B) {
293 // The mismatch byte must rank as more common than 'z' so the rare-byte
294 // selection anchors on 'z' and every haystack position is a candidate.
295 haystack := bytes.Repeat([]byte{'z'}, 1<<20)
296 needle := append(bytes.Repeat([]byte{'z'}, 999), 'e')
297 b.Run("simd", func(b *testing.B) {
298 b.ReportAllocs()
299 for range b.N {
300 if Memmem(haystack, needle) != -1 {
301 b.Fatal("unexpected match")
302 }
303 }
304 })
305 b.Run("default", func(b *testing.B) {
306 b.ReportAllocs()
307 for range b.N {
308 if bytes.Contains(haystack, needle) {
309 b.Fatal("unexpected match")
310 }
311 }
312 })
313 }
314
315 func Benchmark_IsASCII(b *testing.B) {
316 for _, n := range benchSizes {
317 data := benchData(n)
318 b.Run(benchName(n)+"/simd", func(b *testing.B) {
319 b.ReportAllocs()
320 for range b.N {
321 if !IsASCII(data) {
322 b.Fatal("unexpected non-ASCII")
323 }
324 }
325 })
326 b.Run(benchName(n)+"/swar", func(b *testing.B) {
327 b.ReportAllocs()
328 for range b.N {
329 if !isASCIIGeneric(data) {
330 b.Fatal("unexpected non-ASCII")
331 }
332 }
333 })
334 }
335 }
336
337 func Benchmark_FirstNonASCII(b *testing.B) {
338 for _, n := range benchSizes {
339 data := benchData(n)
340 b.Run(benchName(n)+"/simd", func(b *testing.B) {
341 b.ReportAllocs()
342 for range b.N {
343 if FirstNonASCII(data) != -1 {
344 b.Fatal("unexpected non-ASCII")
345 }
346 }
347 })
348 b.Run(benchName(n)+"/swar", func(b *testing.B) {
349 b.ReportAllocs()
350 for range b.N {
351 if firstNonASCIIGeneric(data) != -1 {
352 b.Fatal("unexpected non-ASCII")
353 }
354 }
355 })
356 }
357 }
358
359 func Benchmark_CountNonASCII(b *testing.B) {
360 for _, n := range benchSizes {
361 data := benchData(n)
362 b.Run(benchName(n)+"/simd", func(b *testing.B) {
363 b.ReportAllocs()
364 for range b.N {
365 if CountNonASCII(data) != 0 {
366 b.Fatal("unexpected non-ASCII")
367 }
368 }
369 })
370 b.Run(benchName(n)+"/swar", func(b *testing.B) {
371 b.ReportAllocs()
372 for range b.N {
373 if countNonASCIIGeneric(data) != 0 {
374 b.Fatal("unexpected non-ASCII")
375 }
376 }
377 })
378 }
379 }