Debian Codegraph @

swar/bench_test.go

raw
1 package swar
2
3 import (
4 "bytes"
5 "encoding/binary"
6 "strconv"
7 "strings"
8 "testing"
9
10 "github.com/stretchr/testify/require"
11 )
12
13 // The benchmarks below compare the SWAR primitives against their stdlib
14 // counterparts, using the fiber-vs-default sub-run naming shared with the
15 // strings and bytes packages.
16 // The primitives are exported as building blocks, so each benchmark measures
17 // the canonical loop composed from them (documented in the examples), not a
18 // bare primitive call: that is the unit of work a stdlib function performs.
19 // The composed loops live below and are pinned to the stdlib results by
20 // Test_BenchLoops_MatchStdlib.
21
22 // swarIndexByte is ExampleZeroLanes' canonical first-match scan: broadcast
23 // once, scan full words, and finish 8+ byte inputs with one overlapping word.
24 func swarIndexByte(s string, c byte) int {
25 n := len(s)
26 if n < WordLen {
27 for i := range n {
28 if s[i] == c {
29 return i
30 }
31 }
32 return -1
33 }
34 needle := Broadcast(c)
35 i := 0
36 for ; i+WordLen <= n; i += WordLen {
37 if m := ZeroLanes(Load8(s, i) ^ needle); m != 0 {
38 return i + FirstLane(m)
39 }
40 }
41 if i == n {
42 return -1
43 }
44 if m := ZeroLanes(Load8(s, n-WordLen) ^ needle); m != 0 {
45 return n - WordLen + FirstLane(m)
46 }
47 return -1
48 }
49
50 // swarLastIndexByte is the reverse scan: MatchByteMask's exact per-lane
51 // result feeds LastLane, and the sub-word head falls back to scalar code.
52 func swarLastIndexByte(b []byte, c byte) int {
53 i := len(b)
54 for ; i >= WordLen; i -= WordLen {
55 if m := MatchByteMask(Load8(b, i-WordLen), c); m != 0 {
56 return i - WordLen + LastLane(m)
57 }
58 }
59 for i--; i >= 0; i-- {
60 if b[i] == c {
61 return i
62 }
63 }
64 return -1
65 }
66
67 // swarIndexDigit finds the first ASCII digit via MatchRangeMask, with a
68 // scalar tail for the trailing sub-word bytes.
69 func swarIndexDigit(s string) int {
70 i := 0
71 for ; i+WordLen <= len(s); i += WordLen {
72 if m := MatchRangeMask(Load8(s, i), '0', '9'); m != 0 {
73 return i + FirstLane(m)
74 }
75 }
76 for ; i < len(s); i++ {
77 if s[i] >= '0' && s[i] <= '9' {
78 return i
79 }
80 }
81 return -1
82 }
83
84 // swarToLower lower-cases ASCII in place, one word at a time, with a scalar
85 // tail — the loop internal/caseconv builds from the same primitives.
86 func swarToLower(b []byte) {
87 i := 0
88 for ; i+WordLen <= len(b); i += WordLen {
89 Store8(b, i, ToLowerWord(Load8(b, i)))
90 }
91 for ; i < len(b); i++ {
92 if c := b[i]; c >= 'A' && c <= 'Z' {
93 b[i] = c + ('a' - 'A')
94 }
95 }
96 }
97
98 // Test_BenchLoops_MatchStdlib pins the composed benchmark loops to the
99 // stdlib functions they are measured against, across sub-word, exact-word,
100 // and multi-word lengths with the needle at every position and absent.
101 func Test_BenchLoops_MatchStdlib(t *testing.T) {
102 t.Parallel()
103 sizes := []int{0, 1, 7, 8, 9, 16, 31, 64, 512}
104 for _, n := range sizes {
105 src := make([]byte, n)
106 for i := range src {
107 src[i] = 'A' + byte(i%23) // uppercase, no digits, no needle bytes
108 }
109 for at := -1; at < n; at++ {
110 b := append([]byte(nil), src...)
111 if at >= 0 {
112 b[at] = ','
113 }
114 s := string(b)
115 require.Equal(t, strings.IndexByte(s, ','), swarIndexByte(s, ','), "IndexByte size %d at %d", n, at)
116 require.Equal(t, bytes.LastIndexByte(b, ','), swarLastIndexByte(b, ','), "LastIndexByte size %d at %d", n, at)
117
118 d := append([]byte(nil), src...)
119 if at >= 0 {
120 d[at] = '7'
121 }
122 // Pin against strings.IndexAny — the same stdlib function the
123 // benchmark's default leg measures.
124 ds := string(d)
125 require.Equal(t, strings.IndexAny(ds, "0123456789"), swarIndexDigit(ds), "IndexDigit size %d at %d", n, at)
126 }
127 lower := append([]byte(nil), src...)
128 swarToLower(lower)
129 require.Equal(t, string(bytes.ToLower(src)), string(lower), "ToLower size %d", n)
130 }
131 }
132
133 var benchSizes = []int{7, 8, 16, 32, 64, 512}
134
135 func benchInput(size, needleAt int, needle byte) []byte {
136 b := make([]byte, size)
137 for i := range b {
138 b[i] = 'a' + byte(i%23)
139 }
140 if needleAt >= 0 && needleAt < size {
141 b[needleAt] = needle
142 }
143 return b
144 }
145
146 func Benchmark_Load8(b *testing.B) {
147 buf := benchInput(4096, -1, 0)
148 b.Run("fiber", func(b *testing.B) {
149 b.SetBytes(int64(len(buf)))
150 var acc uint64
151 for b.Loop() {
152 for i := 0; i+WordLen <= len(buf); i += WordLen {
153 acc += Load8(buf, i)
154 }
155 }
156 _ = acc
157 })
158 b.Run("default", func(b *testing.B) {
159 b.SetBytes(int64(len(buf)))
160 var acc uint64
161 for b.Loop() {
162 for i := 0; i+WordLen <= len(buf); i += WordLen {
163 acc += binary.LittleEndian.Uint64(buf[i:])
164 }
165 }
166 _ = acc
167 })
168 }
169
170 func Benchmark_Store8(b *testing.B) {
171 buf := make([]byte, 4096)
172 w := Broadcast('x')
173 b.Run("fiber", func(b *testing.B) {
174 b.SetBytes(int64(len(buf)))
175 for b.Loop() {
176 for i := 0; i+WordLen <= len(buf); i += WordLen {
177 Store8(buf, i, w)
178 }
179 }
180 })
181 b.Run("default", func(b *testing.B) {
182 b.SetBytes(int64(len(buf)))
183 for b.Loop() {
184 for i := 0; i+WordLen <= len(buf); i += WordLen {
185 binary.LittleEndian.PutUint64(buf[i:], w)
186 }
187 }
188 })
189 }
190
191 func Benchmark_IndexByte(b *testing.B) {
192 for _, size := range benchSizes {
193 s := string(benchInput(size, size-1, ',')) // worst case: match at the very end
194 b.Run(strconv.Itoa(size)+"B/fiber", func(b *testing.B) {
195 b.SetBytes(int64(size))
196 var r int
197 for b.Loop() {
198 r = swarIndexByte(s, ',')
199 }
200 _ = r
201 })
202 b.Run(strconv.Itoa(size)+"B/default", func(b *testing.B) {
203 b.SetBytes(int64(size))
204 var r int
205 for b.Loop() {
206 r = strings.IndexByte(s, ',')
207 }
208 _ = r
209 })
210 }
211 }
212
213 func Benchmark_LastIndexByte(b *testing.B) {
214 for _, size := range benchSizes {
215 buf := benchInput(size, 0, ',') // worst case for a reverse scan: match at the start
216 b.Run(strconv.Itoa(size)+"B/fiber", func(b *testing.B) {
217 b.SetBytes(int64(size))
218 var r int
219 for b.Loop() {
220 r = swarLastIndexByte(buf, ',')
221 }
222 _ = r
223 })
224 b.Run(strconv.Itoa(size)+"B/default", func(b *testing.B) {
225 b.SetBytes(int64(size))
226 var r int
227 for b.Loop() {
228 r = bytes.LastIndexByte(buf, ',')
229 }
230 _ = r
231 })
232 }
233 }
234
235 func Benchmark_IndexDigit(b *testing.B) {
236 for _, size := range benchSizes {
237 s := string(benchInput(size, size-1, '7'))
238 b.Run(strconv.Itoa(size)+"B/fiber", func(b *testing.B) {
239 b.SetBytes(int64(size))
240 var r int
241 for b.Loop() {
242 r = swarIndexDigit(s)
243 }
244 _ = r
245 })
246 b.Run(strconv.Itoa(size)+"B/default", func(b *testing.B) {
247 b.SetBytes(int64(size))
248 var r int
249 for b.Loop() {
250 r = strings.IndexAny(s, "0123456789")
251 }
252 _ = r
253 })
254 }
255 }
256
257 func Benchmark_ToLowerWord(b *testing.B) {
258 for _, size := range benchSizes {
259 template := bytes.ToUpper(benchInput(size, -1, 0))
260 b.Run(strconv.Itoa(size)+"B/fiber", func(b *testing.B) {
261 b.ReportAllocs()
262 b.SetBytes(int64(size))
263 work := make([]byte, len(template))
264 // The copy is timed on purpose: it resets the in-place buffer
265 // every iteration (as the strings package's fiber/unsafe legs
266 // do) and stands in for the per-iteration allocation the
267 // default leg pays inside bytes.ToLower.
268 for b.Loop() {
269 copy(work, template)
270 swarToLower(work)
271 }
272 })
273 b.Run(strconv.Itoa(size)+"B/default", func(b *testing.B) {
274 b.ReportAllocs()
275 b.SetBytes(int64(size))
276 var r []byte
277 for b.Loop() {
278 r = bytes.ToLower(template)
279 }
280 _ = r
281 })
282 }
283 }