Debian Codegraph @

benchmarks_test.go

raw symbols SCIP
1 package re2
2
3 import (
4 "strings"
5 "testing"
6 "unicode/utf8"
7 )
8
9 func BenchmarkFind(b *testing.B) {
10 b.StopTimer()
11 re := MustCompileBenchmark("a+b+")
12 wantSubs := "aaabb"
13 s := []byte("acbb" + wantSubs + "dd")
14 b.StartTimer()
15 b.ReportAllocs()
16 for range b.N {
17 subs := re.Find(s)
18 if string(subs) != wantSubs {
19 b.Fatalf("Find(%q) = %q; want %q", s, subs, wantSubs)
20 }
21 }
22 }
23
24 func BenchmarkFindAllNoMatches(b *testing.B) {
25 re := MustCompileBenchmark("a+b+")
26 s := []byte("acddee")
27 b.ReportAllocs()
28 b.ResetTimer()
29 for range b.N {
30 all := re.FindAll(s, -1)
31 if all != nil {
32 b.Fatalf("FindAll(%q) = %q; want nil", s, all)
33 }
34 }
35 }
36
37 func BenchmarkFindString(b *testing.B) {
38 b.StopTimer()
39 re := MustCompileBenchmark("a+b+")
40 wantSubs := "aaabb"
41 s := "acbb" + wantSubs + "dd"
42 b.StartTimer()
43 b.ReportAllocs()
44 for range b.N {
45 subs := re.FindString(s)
46 if subs != wantSubs {
47 b.Fatalf("FindString(%q) = %q; want %q", s, subs, wantSubs)
48 }
49 }
50 }
51
52 func BenchmarkFindSubmatch(b *testing.B) {
53 b.StopTimer()
54 re := MustCompileBenchmark("a(a+b+)b")
55 wantSubs := "aaabb"
56 s := []byte("acbb" + wantSubs + "dd")
57 b.StartTimer()
58 b.ReportAllocs()
59 for range b.N {
60 subs := re.FindSubmatch(s)
61 if string(subs[0]) != wantSubs {
62 b.Fatalf("FindSubmatch(%q)[0] = %q; want %q", s, subs[0], wantSubs)
63 }
64 if string(subs[1]) != "aab" {
65 b.Fatalf("FindSubmatch(%q)[1] = %q; want %q", s, subs[1], "aab")
66 }
67 }
68 }
69
70 func BenchmarkFindStringSubmatch(b *testing.B) {
71 b.StopTimer()
72 re := MustCompileBenchmark("a(a+b+)b")
73 wantSubs := "aaabb"
74 s := "acbb" + wantSubs + "dd"
75 b.StartTimer()
76 b.ReportAllocs()
77 for range b.N {
78 subs := re.FindStringSubmatch(s)
79 if subs[0] != wantSubs {
80 b.Fatalf("FindStringSubmatch(%q)[0] = %q; want %q", s, subs[0], wantSubs)
81 }
82 if subs[1] != "aab" {
83 b.Fatalf("FindStringSubmatch(%q)[1] = %q; want %q", s, subs[1], "aab")
84 }
85 }
86 }
87
88 func BenchmarkLiteral(b *testing.B) {
89 x := strings.Repeat("x", 50) + "y"
90 b.StopTimer()
91 re := MustCompileBenchmark("y")
92 b.StartTimer()
93 for range b.N {
94 if !re.MatchString(x) {
95 b.Fatalf("no match!")
96 }
97 }
98 }
99
100 func BenchmarkNotLiteral(b *testing.B) {
101 x := strings.Repeat("x", 50) + "y"
102 b.StopTimer()
103 re := MustCompileBenchmark(".y")
104 b.StartTimer()
105 for range b.N {
106 if !re.MatchString(x) {
107 b.Fatalf("no match!")
108 }
109 }
110 }
111
112 func BenchmarkMatchClass(b *testing.B) {
113 b.StopTimer()
114 x := strings.Repeat("xxxx", 20) + "w"
115 re := MustCompileBenchmark("[abcdw]")
116 b.StartTimer()
117 for range b.N {
118 if !re.MatchString(x) {
119 b.Fatalf("no match!")
120 }
121 }
122 }
123
124 func BenchmarkMatchClass_InRange(b *testing.B) {
125 b.StopTimer()
126 // 'b' is between 'a' and 'c', so the charclass
127 // range checking is no help here.
128 x := strings.Repeat("bbbb", 20) + "c"
129 re := MustCompileBenchmark("[ac]")
130 b.StartTimer()
131 for range b.N {
132 if !re.MatchString(x) {
133 b.Fatalf("no match!")
134 }
135 }
136 }
137
138 func BenchmarkReplaceAll(b *testing.B) {
139 x := "abcdefghijklmnopqrstuvwxyz"
140 b.StopTimer()
141 re := MustCompileBenchmark("[cjrw]")
142 b.StartTimer()
143 for range b.N {
144 re.ReplaceAllString(x, "")
145 }
146 }
147
148 func BenchmarkAnchoredLiteralShortNonMatch(b *testing.B) {
149 b.StopTimer()
150 x := []byte("abcdefghijklmnopqrstuvwxyz")
151 re := MustCompileBenchmark("^zbc(d|e)")
152 b.StartTimer()
153 for range b.N {
154 re.Match(x)
155 }
156 }
157
158 func BenchmarkAnchoredLiteralLongNonMatch(b *testing.B) {
159 b.StopTimer()
160 x := []byte("abcdefghijklmnopqrstuvwxyz")
161 for range 15 {
162 x = append(x, x...)
163 }
164 re := MustCompileBenchmark("^zbc(d|e)")
165 b.StartTimer()
166 for range b.N {
167 re.Match(x)
168 }
169 }
170
171 func BenchmarkAnchoredShortMatch(b *testing.B) {
172 b.StopTimer()
173 x := []byte("abcdefghijklmnopqrstuvwxyz")
174 re := MustCompileBenchmark("^.bc(d|e)")
175 b.StartTimer()
176 for range b.N {
177 re.Match(x)
178 }
179 }
180
181 func BenchmarkAnchoredLongMatch(b *testing.B) {
182 b.StopTimer()
183 x := []byte("abcdefghijklmnopqrstuvwxyz")
184 for range 15 {
185 x = append(x, x...)
186 }
187 re := MustCompileBenchmark("^.bc(d|e)")
188 b.StartTimer()
189 for range b.N {
190 re.Match(x)
191 }
192 }
193
194 func BenchmarkOnePassShortA(b *testing.B) {
195 b.StopTimer()
196 x := []byte("abcddddddeeeededd")
197 re := MustCompileBenchmark("^.bc(d|e)*$")
198 b.StartTimer()
199 for range b.N {
200 re.Match(x)
201 }
202 }
203
204 func BenchmarkNotOnePassShortA(b *testing.B) {
205 b.StopTimer()
206 x := []byte("abcddddddeeeededd")
207 re := MustCompileBenchmark(".bc(d|e)*$")
208 b.StartTimer()
209 for range b.N {
210 re.Match(x)
211 }
212 }
213
214 func BenchmarkOnePassShortB(b *testing.B) {
215 b.StopTimer()
216 x := []byte("abcddddddeeeededd")
217 re := MustCompileBenchmark("^.bc(?:d|e)*$")
218 b.StartTimer()
219 for range b.N {
220 re.Match(x)
221 }
222 }
223
224 func BenchmarkNotOnePassShortB(b *testing.B) {
225 b.StopTimer()
226 x := []byte("abcddddddeeeededd")
227 re := MustCompileBenchmark(".bc(?:d|e)*$")
228 b.StartTimer()
229 for range b.N {
230 re.Match(x)
231 }
232 }
233
234 func BenchmarkOnePassLongPrefix(b *testing.B) {
235 b.StopTimer()
236 x := []byte("abcdefghijklmnopqrstuvwxyz")
237 re := MustCompileBenchmark("^abcdefghijklmnopqrstuvwxyz.*$")
238 b.StartTimer()
239 for range b.N {
240 re.Match(x)
241 }
242 }
243
244 func BenchmarkOnePassLongNotPrefix(b *testing.B) {
245 b.StopTimer()
246 x := []byte("abcdefghijklmnopqrstuvwxyz")
247 re := MustCompileBenchmark("^.bcdefghijklmnopqrstuvwxyz.*$")
248 b.StartTimer()
249 for range b.N {
250 re.Match(x)
251 }
252 }
253
254 func BenchmarkMatchParallelShared(b *testing.B) {
255 x := []byte("this is a long line that contains foo bar baz")
256 re := MustCompileBenchmark("foo (ba+r)? baz")
257 b.ResetTimer()
258 b.RunParallel(func(pb *testing.PB) {
259 for pb.Next() {
260 re.Match(x)
261 }
262 })
263 }
264
265 func BenchmarkMatchParallelCopied(b *testing.B) {
266 x := []byte("this is a long line that contains foo bar baz")
267 re := MustCompileBenchmark("foo (ba+r)? baz")
268 b.ResetTimer()
269 b.RunParallel(func(pb *testing.PB) {
270 re := re.Copy()
271 for pb.Next() {
272 re.Match(x)
273 }
274 })
275 }
276
277 var sink string
278
279 func BenchmarkQuoteMetaAll(b *testing.B) {
280 specials := make([]byte, 0)
281 for i := byte(0); i < utf8.RuneSelf; i++ {
282 if special(i) {
283 specials = append(specials, i)
284 }
285 }
286 s := string(specials)
287 b.SetBytes(int64(len(s)))
288 b.ResetTimer()
289 for range b.N {
290 sink = QuoteMeta(s)
291 }
292 }
293
294 func BenchmarkQuoteMetaNone(b *testing.B) {
295 s := "abcdefghijklmnopqrstuvwxyz"
296 b.SetBytes(int64(len(s)))
297 b.ResetTimer()
298 for range b.N {
299 sink = QuoteMeta(s)
300 }
301 }
302
303 var compileBenchData = []struct{ name, re string }{
304 {"Onepass", `^a.[l-nA-Cg-j]?e$`},
305 {"Medium", `^((a|b|[d-z0-9])*(日){4,5}.)+$`},
306 {"Hard", strings.Repeat(`((abc)*|`, 50) + strings.Repeat(`)`, 50)},
307 }
308
309 func BenchmarkCompile(b *testing.B) {
310 for _, data := range compileBenchData {
311 b.Run(data.name, func(b *testing.B) {
312 b.ReportAllocs()
313 for range b.N {
314 if _, err := CompileBenchmark(data.re); err != nil {
315 b.Fatal(err)
316 }
317 }
318 })
319 }
320 }
321
322 func BenchmarkMatch(b *testing.B) {
323 for _, data := range benchData {
324 r := MustCompileBenchmark(data.re)
325 for _, size := range benchSizes {
326 if testing.Short() && size.n > 1<<10 {
327 continue
328 }
329 t := makeText(size.n)
330 b.Run(data.name+"/"+size.name, func(b *testing.B) {
331 b.SetBytes(int64(size.n))
332 for range b.N {
333 if r.Match(t) {
334 b.Fatal("match!")
335 }
336 }
337 })
338 }
339 }
340 }
341
342 func BenchmarkMatchParallel(b *testing.B) {
343 for _, data := range benchData {
344 r := MustCompileBenchmark(data.re)
345 for _, size := range benchSizes {
346 if testing.Short() && size.n > 1<<10 {
347 continue
348 }
349 t := makeText(size.n)
350 b.Run(data.name+"/"+size.name, func(b *testing.B) {
351 b.SetBytes(int64(size.n))
352 b.RunParallel(func(pb *testing.PB) {
353 for pb.Next() {
354 if r.Match(t) {
355 b.Fatal("match!")
356 }
357 }
358 })
359 })
360 }
361 }
362 }
363
364 func BenchmarkMatch_onepass_regex(b *testing.B) {
365 r := MustCompileBenchmark(`(?s)\A.*\z`)
366 for _, size := range benchSizes {
367 if testing.Short() && size.n > 1<<10 {
368 continue
369 }
370 t := makeText(size.n)
371 b.Run(size.name, func(b *testing.B) {
372 b.SetBytes(int64(size.n))
373 b.ReportAllocs()
374 for range b.N {
375 if !r.Match(t) {
376 b.Fatal("not match!")
377 }
378 }
379 })
380 }
381 }
382
383 var benchData = []struct{ name, re string }{
384 {"Easy0", "ABCDEFGHIJKLMNOPQRSTUVWXYZ$"},
385 {"Easy0i", "(?i)ABCDEFGHIJklmnopqrstuvwxyz$"},
386 {"Easy1", "A[AB]B[BC]C[CD]D[DE]E[EF]F[FG]G[GH]H[HI]I[IJ]J$"},
387 {"Medium", "[XYZ]ABCDEFGHIJKLMNOPQRSTUVWXYZ$"},
388 {"Hard", "[ -~]*ABCDEFGHIJKLMNOPQRSTUVWXYZ$"},
389 {"Hard1", "ABCD|CDEF|EFGH|GHIJ|IJKL|KLMN|MNOP|OPQR|QRST|STUV|UVWX|WXYZ"},
390 }
391
392 var benchSizes = []struct {
393 name string
394 n int
395 }{
396 {"16", 16},
397 {"32", 32},
398 {"1K", 1 << 10},
399 {"32K", 32 << 10},
400 {"1M", 1 << 20},
401 {"32M", 32 << 20},
402 }
403
404 // Bitmap used by func special to check whether a character needs to be escaped.
405 var specialBytes [16]byte
406
407 // special reports whether byte b needs to be escaped by QuoteMeta.
408 func special(b byte) bool {
409 return b < utf8.RuneSelf && specialBytes[b%16]&(1<<(b/16)) != 0
410 }
411
412 func init() {
413 for _, b := range []byte(`\.+*?()|[]{}^$`) {
414 specialBytes[b%16] |= 1 << (b / 16)
415 }
416 }
417
418 var text []byte
419
420 func makeText(n int) []byte {
421 if len(text) >= n {
422 return text[:n]
423 }
424 text = make([]byte, n)
425 x := ^uint32(0)
426 for i := range text {
427 x += x
428 x ^= 1
429 if int32(x) < 0 {
430 x ^= 0x88888eef
431 }
432 if x%31 == 0 {
433 text[i] = '\n'
434 } else {
435 text[i] = byte(x%(0x7E+1-0x20) + 0x20)
436 }
437 }
438 return text
439 }