Debian Codegraph @

keyset/keyset.go

raw
1 package keyset
2
3 import (
4 "bytes"
5
6 "github.com/segmentio/asm/cpu"
7 "github.com/segmentio/asm/cpu/arm64"
8 "github.com/segmentio/asm/cpu/x86"
9 )
10
11 // New prepares a set of keys for use with Lookup.
12 //
13 // An optimized routine is used if the processor supports AVX instructions and
14 // the maximum length of any of the keys is less than or equal to 16. If New
15 // returns nil, this indicates that an optimized routine is not available, and
16 // the caller should use a fallback.
17 func New(keys [][]byte) []byte {
18 maxWidth, hasNullByte := checkKeys(keys)
19 if hasNullByte || maxWidth > 16 || !(cpu.X86.Has(x86.AVX) || cpu.ARM64.Has(arm64.ASIMD)) {
20 return nil
21 }
22
23 set := make([]byte, len(keys)*16)
24 for i, k := range keys {
25 copy(set[i*16:], k)
26 }
27 return set
28 }
29
30 func checkKeys(keys [][]byte) (maxWidth int, hasNullByte bool) {
31 for _, k := range keys {
32 if len(k) > maxWidth {
33 maxWidth = len(k)
34 }
35 if bytes.IndexByte(k, 0) >= 0 {
36 hasNullByte = true
37 }
38 }
39 return
40 }