| 1 |
package utils |
| 2 |
|
| 3 |
import ( |
| 4 |
"math/rand" |
| 5 |
"strings" |
| 6 |
"testing" |
| 7 |
|
| 8 |
"github.com/stretchr/testify/require" |
| 9 |
) |
| 10 |
|
| 11 |
// Scalar references the SWAR implementations are checked against. |
| 12 |
|
| 13 |
// Tight scalar loops mirroring what fiber's byte-at-a-time call sites do |
| 14 |
// today — the honest baseline for the SWAR versions. |
| 15 |
func refIndexAny2(s string, a, b byte) int { |
| 16 |
for i := 0; i < len(s); i++ { |
| 17 |
if s[i] == a || s[i] == b { |
| 18 |
return i |
| 19 |
} |
| 20 |
} |
| 21 |
return -1 |
| 22 |
} |
| 23 |
|
| 24 |
func refIndexAny3(s string, a, b, c byte) int { |
| 25 |
for i := 0; i < len(s); i++ { |
| 26 |
if s[i] == a || s[i] == b || s[i] == c { |
| 27 |
return i |
| 28 |
} |
| 29 |
} |
| 30 |
return -1 |
| 31 |
} |
| 32 |
|
| 33 |
func refIndexFold(s, needle string) int { |
| 34 |
if len(needle) == 0 { |
| 35 |
return 0 |
| 36 |
} |
| 37 |
fold := func(c byte) byte { |
| 38 |
if c >= 'A' && c <= 'Z' { |
| 39 |
return c + 'a' - 'A' |
| 40 |
} |
| 41 |
return c |
| 42 |
} |
| 43 |
for i := 0; i+len(needle) <= len(s); i++ { |
| 44 |
ok := true |
| 45 |
for j := 0; j < len(needle); j++ { |
| 46 |
if fold(s[i+j]) != fold(needle[j]) { |
| 47 |
ok = false |
| 48 |
break |
| 49 |
} |
| 50 |
} |
| 51 |
if ok { |
| 52 |
return i |
| 53 |
} |
| 54 |
} |
| 55 |
return -1 |
| 56 |
} |
| 57 |
|
| 58 |
func refIndexNonQuotable(s string) int { |
| 59 |
for i := 0; i < len(s); i++ { |
| 60 |
if c := s[i]; c == '\\' || c == '"' || (c < 0x20 && c != '\t') || c == 0x7f { |
| 61 |
return i |
| 62 |
} |
| 63 |
} |
| 64 |
return -1 |
| 65 |
} |
| 66 |
|
| 67 |
func Test_IndexAny_Fixed(t *testing.T) { |
| 68 |
t.Parallel() |
| 69 |
require.Equal(t, -1, IndexAny2("", ',', '"')) |
| 70 |
require.Equal(t, 0, IndexAny2(",", ',', '"')) |
| 71 |
require.Equal(t, 3, IndexAny2([]byte("abc,def"), ',', '"')) |
| 72 |
require.Equal(t, 5, IndexAny2("abcde\"fgh,ij", '"', ',')) |
| 73 |
require.Equal(t, -1, IndexAny3("abcdefgh", ',', '"', '\\')) |
| 74 |
require.Equal(t, 7, IndexAny3("abcdefg\\", ',', '"', '\\')) |
| 75 |
// Needle equal to 0x00 and >= 0x80 must work exactly. |
| 76 |
require.Equal(t, 2, IndexAny2("ab\x00cd", 0x00, 0x01)) |
| 77 |
require.Equal(t, 4, IndexAny2("abcd\xe9f", 0xE9, 0x01)) |
| 78 |
} |
| 79 |
|
| 80 |
func Test_Search_Randomized(t *testing.T) { |
| 81 |
t.Parallel() |
| 82 |
rng := rand.New(rand.NewSource(7)) //nolint:gosec // deterministic test data |
| 83 |
|
| 84 |
for range 30000 { |
| 85 |
// Lengths 0..100 cover all %8 tails, the overlap reads, and 2+ |
| 86 |
// iterations of IsASCII's 32-byte unrolled loop. |
| 87 |
n := rng.Intn(101) |
| 88 |
buf := make([]byte, n) |
| 89 |
for i := range buf { |
| 90 |
// Small alphabet so needles hit often, plus occasional raw bytes. |
| 91 |
if rng.Intn(8) == 0 { |
| 92 |
buf[i] = byte(rng.Intn(256)) |
| 93 |
} else { |
| 94 |
buf[i] = "abcDEF,\"\\\x00\x7f\xe9\t :."[rng.Intn(15)] |
| 95 |
} |
| 96 |
} |
| 97 |
s := string(buf) |
| 98 |
|
| 99 |
a, b, c := byte(rng.Intn(256)), byte(rng.Intn(256)), byte(rng.Intn(256)) |
| 100 |
require.Equal(t, refIndexAny2(s, a, b), IndexAny2(s, a, b), "IndexAny2 %q %q %q", s, a, b) |
| 101 |
require.Equal(t, refIndexAny2(s, a, b), IndexAny2(buf, a, b)) |
| 102 |
require.Equal(t, refIndexAny3(s, a, b, c), IndexAny3(s, a, b, c), "IndexAny3 %q", s) |
| 103 |
require.Equal(t, refIndexAny3(s, a, b, c), IndexAny3(buf, a, b, c)) |
| 104 |
|
| 105 |
require.Equal(t, refIndexNonQuotable(s), IndexNonQuotable(s), "IndexNonQuotable %q", s) |
| 106 |
require.Equal(t, refIndexNonQuotable(s), IndexNonQuotable(buf)) |
| 107 |
require.Equal(t, isASCIIRef(s), IsASCII(s), "IsASCII %q", s) |
| 108 |
require.Equal(t, isASCIIRef(s), IsASCII(buf)) |
| 109 |
|
| 110 |
// Needle: random slice of s (guaranteed present) or random bytes. |
| 111 |
var needle string |
| 112 |
if n > 0 && rng.Intn(2) == 0 { |
| 113 |
start := rng.Intn(n) |
| 114 |
end := start + rng.Intn(min(n-start, 12)+1) |
| 115 |
needle = s[start:end] |
| 116 |
if rng.Intn(2) == 0 { |
| 117 |
// Random-case the needle: must still be found. |
| 118 |
nb := []byte(needle) |
| 119 |
for i := range nb { |
| 120 |
if rng.Intn(2) == 0 { |
| 121 |
if nb[i] >= 'a' && nb[i] <= 'z' { |
| 122 |
nb[i] -= 'a' - 'A' |
| 123 |
} else if nb[i] >= 'A' && nb[i] <= 'Z' { |
| 124 |
nb[i] += 'a' - 'A' |
| 125 |
} |
| 126 |
} |
| 127 |
} |
| 128 |
needle = string(nb) |
| 129 |
} |
| 130 |
} else { |
| 131 |
nb := make([]byte, rng.Intn(12)) |
| 132 |
for i := range nb { |
| 133 |
nb[i] = byte(rng.Intn(256)) |
| 134 |
} |
| 135 |
needle = string(nb) |
| 136 |
} |
| 137 |
require.Equal(t, refIndexFold(s, needle), IndexFold(s, needle), "IndexFold %q %q", s, needle) |
| 138 |
require.Equal(t, refIndexFold(s, needle), IndexFold(buf, needle)) |
| 139 |
require.Equal(t, refIndexFold(s, needle) >= 0, ContainsFold(s, needle)) |
| 140 |
} |
| 141 |
} |
| 142 |
|
| 143 |
func isASCIIRef(s string) bool { |
| 144 |
for i := 0; i < len(s); i++ { |
| 145 |
if s[i] >= 0x80 { |
| 146 |
return false |
| 147 |
} |
| 148 |
} |
| 149 |
return true |
| 150 |
} |
| 151 |
|
| 152 |
func Test_IndexFold_Fixed(t *testing.T) { |
| 153 |
t.Parallel() |
| 154 |
require.Equal(t, 0, IndexFold("", "")) |
| 155 |
require.Equal(t, 0, IndexFold("abc", "")) |
| 156 |
require.Equal(t, -1, IndexFold("", "a")) |
| 157 |
require.Equal(t, 0, IndexFold("NO-CACHE", "no-cache")) |
| 158 |
require.Equal(t, 10, IndexFold("max-age=0,No-Cache,private", "no-cache")) |
| 159 |
require.Equal(t, 4, IndexFold([]byte("xyz BeArEr token"), "bearer")) |
| 160 |
// Fold must only touch A-Z/a-z lanes: CR|0x20 == '-' would falsely match |
| 161 |
// with a blanket |0x20 fold. |
| 162 |
require.Equal(t, -1, IndexFold("no\rcache", "no-cache")) |
| 163 |
require.Equal(t, -1, IndexFold("NO\rCACHE", "no-cache")) |
| 164 |
// Non-ASCII bytes must match exactly, never fold. |
| 165 |
require.Equal(t, 0, IndexFold("\xc9abc", "\xc9ABC")) |
| 166 |
require.Equal(t, -1, IndexFold("\xe9abc", "\xc9abc")) |
| 167 |
// Needles longer than 8 bytes take the scalar path. |
| 168 |
require.Equal(t, 2, IndexFold("__Proxy-Authorization: basic", "proxy-authorization")) |
| 169 |
require.Equal(t, -1, IndexFold("__Proxy-Authorizatio", "proxy-authorization!")) |
| 170 |
// Match at the very end, crossing the overlapping tail window. |
| 171 |
require.Equal(t, 12, IndexFold("aaaaaaaaaaaaNO-CACHE", "no-cache")) |
| 172 |
require.Equal(t, 5, IndexFold("aaaaaGZip", "gzip")) |
| 173 |
// First-byte candidates that overrun the last valid start must not match. |
| 174 |
require.Equal(t, -1, IndexFold("aaaaaaagz", "gzip")) |
| 175 |
// Same folded word, different case mixes. |
| 176 |
require.Equal(t, 0, IndexFold("gZiP, deflate", "GzIp")) |
| 177 |
require.True(t, ContainsFold("Cache-Control: NO-CACHE", "no-cache")) |
| 178 |
require.False(t, ContainsFold("Cache-Control: no\rcache", "no-cache")) |
| 179 |
// Long needles (> 8 bytes) use the SWAR candidate scan with a word-wise |
| 180 |
// fold-compare verify, including matches ending exactly at the tail. |
| 181 |
require.Equal(t, 21, IndexFold("x-real-ip, x-req-id, Proxy-Authorization", "proxy-authorization")) |
| 182 |
require.Equal(t, 7, IndexFold("cookie CONTENT-type: text", "content-type")) |
| 183 |
require.Equal(t, -1, IndexFold("cookie CONTENT-typ: text.", "content-type")) |
| 184 |
require.Equal(t, 12, IndexFold("abcdefghijklTRANSFER-ENCODING", "transfer-encoding")) |
| 185 |
require.Equal(t, 0, IndexFold("authorizatioN", "authorization")) |
| 186 |
require.Equal(t, -1, IndexFold("authorizatio", "authorization")) |
| 187 |
// The first candidate appears only in the scalar remainder after the |
| 188 |
// word loop, so the folded needle is built there. |
| 189 |
require.Equal(t, 10, IndexFold("aaaaaaaaaaGZ", "gz")) |
| 190 |
require.Equal(t, -1, IndexFold("aaaaaaaaaaGx", "gz")) |
| 191 |
// Needle length an exact multiple of the word size: the fold-compare |
| 192 |
// covers it with full words only, matching and mismatching. |
| 193 |
require.Equal(t, 2, IndexFold("__proxy-authorizatXYZ", "PROXY-AUTHORIZAT")) |
| 194 |
require.Equal(t, -1, IndexFold("__proxy-authorizaQXYZ", "PROXY-AUTHORIZAT")) |
| 195 |
} |
| 196 |
|
| 197 |
// refHasPrefixFold/refHasSuffixFold reduce to refIndexFold on the clipped |
| 198 |
// window: with len(window) == len(needle) a 0 return means "equal folded". |
| 199 |
func refHasPrefixFold(s, prefix string) bool { |
| 200 |
return len(s) >= len(prefix) && refIndexFold(s[:len(prefix)], prefix) == 0 |
| 201 |
} |
| 202 |
|
| 203 |
func refHasSuffixFold(s, suffix string) bool { |
| 204 |
return len(s) >= len(suffix) && refIndexFold(s[len(s)-len(suffix):], suffix) == 0 |
| 205 |
} |
| 206 |
|
| 207 |
func Test_HasPrefixFold_HasSuffixFold_Fixed(t *testing.T) { |
| 208 |
t.Parallel() |
| 209 |
// Empty needles match anything, oversized needles nothing. |
| 210 |
require.True(t, HasPrefixFold("", "")) |
| 211 |
require.True(t, HasSuffixFold("", "")) |
| 212 |
require.True(t, HasPrefixFold("x", "")) |
| 213 |
require.True(t, HasSuffixFold("x", "")) |
| 214 |
require.False(t, HasPrefixFold("", "a")) |
| 215 |
require.False(t, HasSuffixFold("", "a")) |
| 216 |
require.False(t, HasPrefixFold("bear", "bearer ")) |
| 217 |
require.False(t, HasSuffixFold("zip", "gzip")) |
| 218 |
|
| 219 |
// Scalar path (needle < 8 bytes), both hit and miss, string and []byte. |
| 220 |
require.True(t, HasPrefixFold("Bearer abc.def", "bearer ")) |
| 221 |
require.True(t, HasPrefixFold([]byte("BEARER abc.def"), "bearer ")) |
| 222 |
require.False(t, HasPrefixFold("Basic QWxhZGRpbg==", "bearer ")) |
| 223 |
require.True(t, HasSuffixFold("application/vnd.api+JSON", "+json")) |
| 224 |
require.True(t, HasSuffixFold([]byte("deflate, GZIP"), "gzip")) |
| 225 |
require.False(t, HasSuffixFold("deflate, GZIPx", "gzip")) |
| 226 |
|
| 227 |
// Word path (needle >= 8 bytes), including the overlapping tail compare. |
| 228 |
require.True(t, HasPrefixFold("MULTIPART/FORM-DATA; boundary=X", "multipart/form-data")) |
| 229 |
require.False(t, HasPrefixFold("multipart/form-datX; boundary=X", "multipart/form-data")) |
| 230 |
require.True(t, HasPrefixFold("multipart/form-data", "multipart/form-data")) |
| 231 |
require.True(t, HasSuffixFold("application/ATOM+XML", "atom+xml")) |
| 232 |
require.False(t, HasSuffixFold("application/atomxxml", "atom+xml")) |
| 233 |
require.True(t, HasSuffixFold("PROXY-AUTHORIZATION", "proxy-authorization")) |
| 234 |
|
| 235 |
// Exact-length inputs on the scalar path. |
| 236 |
require.True(t, HasPrefixFold("GZip", "gzip")) |
| 237 |
require.True(t, HasSuffixFold("GZip", "gzip")) |
| 238 |
|
| 239 |
// Only A-Z/a-z fold: CR|0x20 == '-' style false folds must not match, |
| 240 |
// and non-ASCII bytes must match exactly. |
| 241 |
require.False(t, HasPrefixFold("no\rcache", "no-cache")) |
| 242 |
require.False(t, HasSuffixFold("xno\rcache", "no-cache")) |
| 243 |
require.True(t, HasPrefixFold("\xc9abcdefgh", "\xc9ABCDEFGH")) |
| 244 |
require.False(t, HasPrefixFold("\xe9abcdefgh", "\xc9abcdefgh")) |
| 245 |
require.True(t, HasSuffixFold("x\xc9abcdefg", "\xc9ABCDEFG")) |
| 246 |
require.False(t, HasSuffixFold("x\xe9abcdefg", "\xc9abcdefg")) |
| 247 |
} |
| 248 |
|
| 249 |
func Test_PrefixSuffixFold_Randomized(t *testing.T) { |
| 250 |
t.Parallel() |
| 251 |
rng := rand.New(rand.NewSource(13)) //nolint:gosec // deterministic test data |
| 252 |
|
| 253 |
for range 30000 { |
| 254 |
n := rng.Intn(41) |
| 255 |
buf := make([]byte, n) |
| 256 |
for i := range buf { |
| 257 |
if rng.Intn(8) == 0 { |
| 258 |
buf[i] = byte(rng.Intn(256)) |
| 259 |
} else { |
| 260 |
buf[i] = "abcDEF,\"\\\x00\x7f\xe9\t :."[rng.Intn(15)] |
| 261 |
} |
| 262 |
} |
| 263 |
s := string(buf) |
| 264 |
|
| 265 |
// Needle: prefix/suffix of s (random-cased, guaranteed hit) or |
| 266 |
// random bytes (usually a miss), lengths crossing the 8-byte split. |
| 267 |
k := rng.Intn(min(n, 12) + 1) |
| 268 |
var needle []byte |
| 269 |
switch rng.Intn(3) { |
| 270 |
case 0: |
| 271 |
needle = []byte(s[:k]) |
| 272 |
case 1: |
| 273 |
needle = []byte(s[n-k:]) |
| 274 |
default: |
| 275 |
needle = make([]byte, rng.Intn(12)) |
| 276 |
for i := range needle { |
| 277 |
needle[i] = byte(rng.Intn(256)) |
| 278 |
} |
| 279 |
} |
| 280 |
for i := range needle { |
| 281 |
if rng.Intn(2) == 0 { |
| 282 |
if needle[i] >= 'a' && needle[i] <= 'z' { |
| 283 |
needle[i] -= 'a' - 'A' |
| 284 |
} else if needle[i] >= 'A' && needle[i] <= 'Z' { |
| 285 |
needle[i] += 'a' - 'A' |
| 286 |
} |
| 287 |
} |
| 288 |
} |
| 289 |
nd := string(needle) |
| 290 |
|
| 291 |
require.Equal(t, refHasPrefixFold(s, nd), HasPrefixFold(s, nd), "HasPrefixFold %q %q", s, nd) |
| 292 |
require.Equal(t, refHasPrefixFold(s, nd), HasPrefixFold(buf, nd)) |
| 293 |
require.Equal(t, refHasSuffixFold(s, nd), HasSuffixFold(s, nd), "HasSuffixFold %q %q", s, nd) |
| 294 |
require.Equal(t, refHasSuffixFold(s, nd), HasSuffixFold(buf, nd)) |
| 295 |
} |
| 296 |
} |
| 297 |
|
| 298 |
func Test_IndexNonQuotable_Fixed(t *testing.T) { |
| 299 |
t.Parallel() |
| 300 |
require.Equal(t, -1, IndexNonQuotable("")) |
| 301 |
require.Equal(t, -1, IndexNonQuotable("simple filename.txt")) |
| 302 |
require.Equal(t, 4, IndexNonQuotable(`name"quoted`)) |
| 303 |
require.Equal(t, 4, IndexNonQuotable(`name\quoted`)) |
| 304 |
require.Equal(t, 0, IndexNonQuotable("\x1ftail")) |
| 305 |
require.Equal(t, 7, IndexNonQuotable("abcdefg\x7f")) |
| 306 |
// obs-text (>= 0x80) is quotable. |
| 307 |
require.Equal(t, -1, IndexNonQuotable("caf\xc3\xa9 r\xc3\xa9sum\xc3\xa9.pdf")) |
| 308 |
// HTAB is valid qdtext (RFC 9110) and must NOT be flagged, in the word |
| 309 |
// path, the scalar path, and directly before a real control byte. |
| 310 |
require.Equal(t, -1, IndexNonQuotable([]byte("aaaaaaaaa\ttab"))) |
| 311 |
require.Equal(t, -1, IndexNonQuotable("a\tb")) |
| 312 |
require.Equal(t, 9, IndexNonQuotable("aaaaaaaa\t\x1f")) |
| 313 |
require.Equal(t, 3, IndexNonQuotable("ab\t\x08cdefgh")) |
| 314 |
// Borrow-stress shapes: obs-text lanes before a quote, and a quote |
| 315 |
// directly ahead of a control byte. |
| 316 |
require.Equal(t, 7, IndexNonQuotable("\xff\xff\xff\xff\xff\xff\xff\"")) |
| 317 |
require.Equal(t, 1, IndexNonQuotable("a\"\x00defgh")) |
| 318 |
} |
| 319 |
|
| 320 |
func Test_IsASCII_Fixed(t *testing.T) { |
| 321 |
t.Parallel() |
| 322 |
require.True(t, IsASCII("")) |
| 323 |
require.True(t, IsASCII("plain ascii filename.txt")) |
| 324 |
require.False(t, IsASCII("caf\xc3\xa9")) |
| 325 |
require.False(t, IsASCII([]byte{0x80})) |
| 326 |
require.True(t, IsASCII([]byte{0x7f, 0x00, 'a'})) |
| 327 |
require.False(t, IsASCII("aaaaaaaaaaaaaaa\xff")) // high byte only in the tail |
| 328 |
} |
| 329 |
|
| 330 |
func Test_Search_NamedTypes_SIMDDispatch(t *testing.T) { |
| 331 |
t.Parallel() |
| 332 |
// Named byteSeq instantiations at 32+ bytes take the amd64 simd dispatch, |
| 333 |
// whose zero-copy []byte view assumes the header layouts named types |
| 334 |
// share with plain string/[]byte. |
| 335 |
type headerString string |
| 336 |
type headerBytes []byte |
| 337 |
long := strings.Repeat("x", 40) + "," |
| 338 |
require.Equal(t, 40, IndexAny2(headerString(long), ',', '"')) |
| 339 |
require.Equal(t, 40, IndexAny2(headerBytes(long), ',', '"')) |
| 340 |
require.Equal(t, 40, IndexAny3(headerString(long), ',', '"', '\\')) |
| 341 |
require.Equal(t, 40, IndexAny3(headerBytes(long), ',', '"', '\\')) |
| 342 |
require.True(t, IsASCII(headerString(long))) |
| 343 |
require.True(t, IsASCII(headerBytes(long))) |
| 344 |
require.False(t, IsASCII(headerString(long+"\xe9"))) |
| 345 |
require.False(t, IsASCII(headerBytes(long+"\xe9"))) |
| 346 |
} |
| 347 |
|
| 348 |
// --- Benchmarks: SWAR vs scalar reference at HTTP-typical sizes --- |
| 349 |
|
| 350 |
var benchSizes = []int{7, 8, 16, 32, 64, 512} |
| 351 |
|
| 352 |
func benchInput(size, needleAt int, needle byte) string { |
| 353 |
b := make([]byte, size) |
| 354 |
for i := range b { |
| 355 |
b[i] = 'a' + byte(i%23) |
| 356 |
} |
| 357 |
if needleAt >= 0 && needleAt < size { |
| 358 |
b[needleAt] = needle |
| 359 |
} |
| 360 |
return string(b) |
| 361 |
} |
| 362 |
|
| 363 |
func Benchmark_IndexAny2(b *testing.B) { |
| 364 |
for _, size := range benchSizes { |
| 365 |
s := benchInput(size, size-1, ',') // worst case: match at the very end |
| 366 |
b.Run(FormatInt(int64(size))+"B/swar", func(b *testing.B) { |
| 367 |
b.SetBytes(int64(size)) |
| 368 |
var r int |
| 369 |
for b.Loop() { |
| 370 |
r = IndexAny2(s, ',', '"') |
| 371 |
} |
| 372 |
_ = r |
| 373 |
}) |
| 374 |
b.Run(FormatInt(int64(size))+"B/scalar", func(b *testing.B) { |
| 375 |
b.SetBytes(int64(size)) |
| 376 |
var r int |
| 377 |
for b.Loop() { |
| 378 |
r = refIndexAny2(s, ',', '"') |
| 379 |
} |
| 380 |
_ = r |
| 381 |
}) |
| 382 |
b.Run(FormatInt(int64(size))+"B/stdlib-indexany", func(b *testing.B) { |
| 383 |
b.SetBytes(int64(size)) |
| 384 |
var r int |
| 385 |
for b.Loop() { |
| 386 |
r = strings.IndexAny(s, ",\"") |
| 387 |
} |
| 388 |
_ = r |
| 389 |
}) |
| 390 |
} |
| 391 |
} |
| 392 |
|
| 393 |
func Benchmark_IndexAny3(b *testing.B) { |
| 394 |
for _, size := range benchSizes { |
| 395 |
s := benchInput(size, size-1, ';') |
| 396 |
b.Run(FormatInt(int64(size))+"B/swar", func(b *testing.B) { |
| 397 |
b.SetBytes(int64(size)) |
| 398 |
var r int |
| 399 |
for b.Loop() { |
| 400 |
r = IndexAny3(s, ';', '"', '\\') |
| 401 |
} |
| 402 |
_ = r |
| 403 |
}) |
| 404 |
b.Run(FormatInt(int64(size))+"B/scalar", func(b *testing.B) { |
| 405 |
b.SetBytes(int64(size)) |
| 406 |
var r int |
| 407 |
for b.Loop() { |
| 408 |
r = refIndexAny3(s, ';', '"', '\\') |
| 409 |
} |
| 410 |
_ = r |
| 411 |
}) |
| 412 |
} |
| 413 |
} |
| 414 |
|
| 415 |
func Benchmark_IndexFold(b *testing.B) { |
| 416 |
headers := map[string]string{ |
| 417 |
"miss-8B": "max-age=", |
| 418 |
"miss-32B": "max-age=31536000, must-revalidat", |
| 419 |
"hit-32B": "max-age=0, private, No-CaChE, mu", |
| 420 |
"miss-64B": strings.Repeat("max-age=31536000", 4), |
| 421 |
"hit-tail": "max-age=31536000, immutable, private, stale-if-error, NO-CACHE", |
| 422 |
"miss-512B": strings.Repeat("max-age=31536000", 32), |
| 423 |
} |
| 424 |
longNeedle := map[string]string{ |
| 425 |
"long-miss-64B": strings.Repeat("max-age=31536000", 4), |
| 426 |
"long-hit-64B": "max-age=31536000, immutable, Proxy-Authorization: basic aaaa", |
| 427 |
} |
| 428 |
for name, s := range headers { |
| 429 |
b.Run(name+"/swar", func(b *testing.B) { |
| 430 |
b.SetBytes(int64(len(s))) |
| 431 |
var r int |
| 432 |
for b.Loop() { |
| 433 |
r = IndexFold(s, "no-cache") |
| 434 |
} |
| 435 |
_ = r |
| 436 |
}) |
| 437 |
b.Run(name+"/scalar", func(b *testing.B) { |
| 438 |
b.SetBytes(int64(len(s))) |
| 439 |
var r int |
| 440 |
for b.Loop() { |
| 441 |
r = refIndexFold(s, "no-cache") |
| 442 |
} |
| 443 |
_ = r |
| 444 |
}) |
| 445 |
} |
| 446 |
for name, s := range longNeedle { |
| 447 |
b.Run(name+"/swar", func(b *testing.B) { |
| 448 |
b.SetBytes(int64(len(s))) |
| 449 |
var r int |
| 450 |
for b.Loop() { |
| 451 |
r = IndexFold(s, "proxy-authorization") |
| 452 |
} |
| 453 |
_ = r |
| 454 |
}) |
| 455 |
b.Run(name+"/scalar", func(b *testing.B) { |
| 456 |
b.SetBytes(int64(len(s))) |
| 457 |
var r int |
| 458 |
for b.Loop() { |
| 459 |
r = refIndexFold(s, "proxy-authorization") |
| 460 |
} |
| 461 |
_ = r |
| 462 |
}) |
| 463 |
} |
| 464 |
} |
| 465 |
|
| 466 |
func Benchmark_HasPrefixFold(b *testing.B) { |
| 467 |
cases := []struct { |
| 468 |
name string |
| 469 |
s string |
| 470 |
prefix string |
| 471 |
}{ |
| 472 |
{"bearer-hit", "Bearer abc.def.ghi.jkl.mno.pqr", "bearer "}, |
| 473 |
{"bearer-miss", "Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==", "bearer "}, |
| 474 |
{"text-hit", "TEXT/HTML; charset=utf-8", "text/"}, |
| 475 |
{"multipart-hit", "MULTIPART/FORM-DATA; boundary=X-BOUNDARY", "multipart/form-data"}, |
| 476 |
{"multipart-miss", "application/x-www-form-urlencoded", "multipart/form-data"}, |
| 477 |
} |
| 478 |
for _, tc := range cases { |
| 479 |
b.Run(tc.name+"/fiber", func(b *testing.B) { |
| 480 |
b.SetBytes(int64(len(tc.prefix))) |
| 481 |
var r bool |
| 482 |
for b.Loop() { |
| 483 |
r = HasPrefixFold(tc.s, tc.prefix) |
| 484 |
} |
| 485 |
_ = r |
| 486 |
}) |
| 487 |
b.Run(tc.name+"/default", func(b *testing.B) { |
| 488 |
b.SetBytes(int64(len(tc.prefix))) |
| 489 |
var r bool |
| 490 |
for b.Loop() { |
| 491 |
r = len(tc.s) >= len(tc.prefix) && strings.EqualFold(tc.s[:len(tc.prefix)], tc.prefix) |
| 492 |
} |
| 493 |
_ = r |
| 494 |
}) |
| 495 |
} |
| 496 |
} |
| 497 |
|
| 498 |
func Benchmark_HasSuffixFold(b *testing.B) { |
| 499 |
cases := []struct { |
| 500 |
name string |
| 501 |
s string |
| 502 |
suffix string |
| 503 |
}{ |
| 504 |
{"gzip-hit", "deflate, br, GZIP", "gzip"}, |
| 505 |
{"gzip-miss", "deflate, br, zstd", "gzip"}, |
| 506 |
{"json-hit", "application/vnd.api+JSON", "+json"}, |
| 507 |
{"atomxml-hit", "application/ATOM+XML", "atom+xml"}, |
| 508 |
{"charset-miss", "text/html; charset=utf-8", "charset=iso-8859-1"}, |
| 509 |
} |
| 510 |
for _, tc := range cases { |
| 511 |
b.Run(tc.name+"/fiber", func(b *testing.B) { |
| 512 |
b.SetBytes(int64(len(tc.suffix))) |
| 513 |
var r bool |
| 514 |
for b.Loop() { |
| 515 |
r = HasSuffixFold(tc.s, tc.suffix) |
| 516 |
} |
| 517 |
_ = r |
| 518 |
}) |
| 519 |
b.Run(tc.name+"/default", func(b *testing.B) { |
| 520 |
b.SetBytes(int64(len(tc.suffix))) |
| 521 |
var r bool |
| 522 |
for b.Loop() { |
| 523 |
r = len(tc.s) >= len(tc.suffix) && strings.EqualFold(tc.s[len(tc.s)-len(tc.suffix):], tc.suffix) |
| 524 |
} |
| 525 |
_ = r |
| 526 |
}) |
| 527 |
} |
| 528 |
} |
| 529 |
|
| 530 |
func Benchmark_IsASCII(b *testing.B) { |
| 531 |
for _, size := range benchSizes { |
| 532 |
s := benchInput(size, -1, 0) |
| 533 |
b.Run(FormatInt(int64(size))+"B/swar", func(b *testing.B) { |
| 534 |
b.SetBytes(int64(size)) |
| 535 |
var r bool |
| 536 |
for b.Loop() { |
| 537 |
r = IsASCII(s) |
| 538 |
} |
| 539 |
_ = r |
| 540 |
}) |
| 541 |
b.Run(FormatInt(int64(size))+"B/scalar", func(b *testing.B) { |
| 542 |
b.SetBytes(int64(size)) |
| 543 |
var r bool |
| 544 |
for b.Loop() { |
| 545 |
r = isASCIIRef(s) |
| 546 |
} |
| 547 |
_ = r |
| 548 |
}) |
| 549 |
} |
| 550 |
} |
| 551 |
|
| 552 |
func Benchmark_IndexNonQuotable(b *testing.B) { |
| 553 |
for _, size := range benchSizes { |
| 554 |
s := benchInput(size, -1, 0) // clean input: full scan |
| 555 |
b.Run(FormatInt(int64(size))+"B/swar", func(b *testing.B) { |
| 556 |
b.SetBytes(int64(size)) |
| 557 |
var r int |
| 558 |
for b.Loop() { |
| 559 |
r = IndexNonQuotable(s) |
| 560 |
} |
| 561 |
_ = r |
| 562 |
}) |
| 563 |
b.Run(FormatInt(int64(size))+"B/scalar", func(b *testing.B) { |
| 564 |
b.SetBytes(int64(size)) |
| 565 |
var r int |
| 566 |
for b.Loop() { |
| 567 |
r = refIndexNonQuotable(s) |
| 568 |
} |
| 569 |
_ = r |
| 570 |
}) |
| 571 |
} |
| 572 |
} |