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

Faster scanning of memcache responses #154

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
50 changes: 41 additions & 9 deletions memcache/memcache.go
Original file line number Diff line number Diff line change
Expand Up @@ -519,17 +519,49 @@ func parseGetResponse(r *bufio.Reader, cb func(*Item)) error {
// scanGetResponseLine populates it and returns the declared size of the item.
// It does not read the bytes of the item.
func scanGetResponseLine(line []byte, it *Item) (size int, err error) {
bboreham marked this conversation as resolved.
Show resolved Hide resolved
pattern := "VALUE %s %d %d %d\r\n"
dest := []interface{}{&it.Key, &it.Flags, &size, &it.casid}
if bytes.Count(line, space) == 3 {
pattern = "VALUE %s %d %d\r\n"
dest = dest[:3]
}
n, err := fmt.Sscanf(string(line), pattern, dest...)
if err != nil || n != len(dest) {
errf := func(line []byte) (int, error) {
return -1, fmt.Errorf("memcache: unexpected line in get response: %q", line)
}
return size, nil
if !bytes.HasPrefix(line, []byte("VALUE ")) || !bytes.HasSuffix(line, []byte("\r\n")) {
return errf(line)
}
s := string(line[6 : len(line)-2])
var rest string
var found bool
it.Key, rest, found = cut(s, ' ')
if !found {
return errf(line)
}
val, rest, found := cut(rest, ' ')
if !found {
return errf(line)
}
flags64, err := strconv.ParseUint(val, 10, 32)
if err != nil {
return errf(line)
}
it.Flags = uint32(flags64)
val, rest, found = cut(rest, ' ')
size64, err := strconv.ParseUint(val, 10, 32)
if err != nil {
return errf(line)
}
if !found { // final CAS ID is optional.
return int(size64), nil
bboreham marked this conversation as resolved.
Show resolved Hide resolved
}
it.casid, err = strconv.ParseUint(rest, 10, 64)
if err != nil {
return errf(line)
}
return int(size64), nil
}

// Similar to strings.Cut in Go 1.18, but sep can only be 1 byte.
func cut(s string, sep byte) (before, after string, found bool) {
if i := strings.IndexByte(s, sep); i >= 0 {
return s[:i], s[i+1:], true
}
return s, "", false
}

// Set writes the given item, unconditionally.
Expand Down
11 changes: 11 additions & 0 deletions memcache/memcache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -290,3 +290,14 @@ func BenchmarkOnItem(b *testing.B) {
c.onItem(&item, dummyFn)
}
}

func BenchmarkScanGetResponseLine(b *testing.B) {
line := []byte("VALUE foobar1234 0 4096 1234\r\n")
var it Item
for i := 0; i < b.N; i++ {
_, err := scanGetResponseLine(line, &it)
if err != nil {
b.Fatal(err)
}
}
}