Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve performance of FromObject and parseUUID #129

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/hashicorp/go-memdb

go 1.13
go 1.18

require (
github.com/hashicorp/go-immutable-radix v1.3.0
Expand Down
97 changes: 70 additions & 27 deletions index.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,13 @@ func (s *StringFieldIndex) FromObject(obj interface{}) (bool, []byte, error) {
val = strings.ToLower(val)
}

// Add the null character as a terminator
val += "\x00"
return true, []byte(val), nil
return true, nulTerminatedByteSlice(val), nil
}

func nulTerminatedByteSlice(val string) []byte {
rv := make([]byte, len(val)+1)
copy(rv, val)
return rv
}

func (s *StringFieldIndex) FromArgs(args ...interface{}) ([]byte, error) {
Expand All @@ -102,9 +106,9 @@ func (s *StringFieldIndex) FromArgs(args ...interface{}) ([]byte, error) {
if s.Lowercase {
arg = strings.ToLower(arg)
}
// Add the null character as a terminator
arg += "\x00"
return []byte(arg), nil

// Convert to a []byte with a trailing nul byte
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo:

Suggested change
// Convert to a []byte with a trailing nul byte
// Convert to a []byte with a trailing null byte

return nulTerminatedByteSlice(arg), nil
}

func (s *StringFieldIndex) PrefixFromArgs(args ...interface{}) ([]byte, error) {
Expand Down Expand Up @@ -145,7 +149,7 @@ func (s *StringSliceFieldIndex) FromObject(obj interface{}) (bool, [][]byte, err

length := fv.Len()
vals := make([][]byte, 0, length)
for i := 0; i < fv.Len(); i++ {
for i := 0; i < length; i++ {
val := fv.Index(i).String()
if val == "" {
continue
Expand All @@ -156,8 +160,7 @@ func (s *StringSliceFieldIndex) FromObject(obj interface{}) (bool, [][]byte, err
}

// Add the null character as a terminator
val += "\x00"
vals = append(vals, []byte(val))
vals = append(vals, nulTerminatedByteSlice(val))
}
if len(vals) == 0 {
return false, nil, nil
Expand All @@ -177,8 +180,7 @@ func (s *StringSliceFieldIndex) FromArgs(args ...interface{}) ([]byte, error) {
arg = strings.ToLower(arg)
}
// Add the null character as a terminator
arg += "\x00"
return []byte(arg), nil
return nulTerminatedByteSlice(arg), nil
}

func (s *StringSliceFieldIndex) PrefixFromArgs(args ...interface{}) ([]byte, error) {
Expand Down Expand Up @@ -576,24 +578,64 @@ func (u *UUIDFieldIndex) parseString(s string, enforceLength bool) ([]byte, erro
return nil, fmt.Errorf("Invalid UUID length. UUID have 36 characters; got %d", l)
}

hyphens := strings.Count(s, "-")
if hyphens > 4 {
return nil, fmt.Errorf(`UUID should have maximum of 4 "-"; got %d`, hyphens)
}
rv := make([]byte, 0, 16)
loop:
for i := 0; i < 16; i++ {
switch l {
case 0:
break loop
case 1:
return nil, errors.New("odd length uuid")
}

// The sanitized length is the length of the original string without the "-".
sanitized := strings.Replace(s, "-", "", -1)
sanitizedLength := len(sanitized)
if sanitizedLength%2 != 0 {
return nil, fmt.Errorf("Input (without hyphens) must be even length")
b, err := hexToByte(s[:2])
if err != nil {
return nil, err
}
rv = append(rv, b)
if len(s) > 2 {
if s[2] == '-' {
s = s[3:]
l = l - 3
} else {
s = s[2:]
l = l - 2
}
} else {
l = l - 2
}
}
return rv, nil
}

dec, err := hex.DecodeString(sanitized)
if err != nil {
return nil, fmt.Errorf("Invalid UUID: %v", err)
}
const reverseHexTable = "" +
"\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff" +
"\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff" +
"\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff" +
"\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\xff\xff\xff\xff\xff\xff" +
"\xff\x0a\x0b\x0c\x0d\x0e\x0f\xff\xff\xff\xff\xff\xff\xff\xff\xff" +
"\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff" +
"\xff\x0a\x0b\x0c\x0d\x0e\x0f\xff\xff\xff\xff\xff\xff\xff\xff\xff" +
"\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff" +
"\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff" +
"\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff" +
"\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff" +
"\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff" +
"\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff" +
"\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff" +
"\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff" +
"\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff"

return dec, nil
func hexToByte(v string) (byte, error) {
a := reverseHexTable[v[0]]
b := reverseHexTable[v[1]]
if a > 0x0f {
return 0, hex.InvalidByteError(v[0])
}
if b > 0x0f {
return 0, hex.InvalidByteError(v[1])
}
return (a << 4) | b, nil
}

// FieldSetIndex is used to extract a field from an object using reflection and
Expand Down Expand Up @@ -834,7 +876,7 @@ forloop:
}

// Start with something higher to avoid resizing if possible
out := make([][]byte, 0, len(c.Indexes)^3)
out := make([][]byte, 0, len(c.Indexes)<<3)

// We are walking through the builder slice essentially in a depth-first fashion,
// building the prefix and leaves as we go. If AllowMissing is false, we only insert
Expand Down Expand Up @@ -903,7 +945,8 @@ func (c *CompoundMultiIndex) FromArgs(args ...interface{}) ([]byte, error) {
}
}

var out []byte
// To avoid growing the slice with each call, start with an optimistically preallocated slice
out := make([]byte, 0, 32)
var val []byte
var err error
for i, idx := range c.Indexes {
Expand Down