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

Allocate map for results to hold expected number of keys #17

Merged
merged 1 commit into from
Feb 29, 2024
Merged
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 memcache/memcache.go
Original file line number Diff line number Diff line change
Expand Up @@ -619,7 +619,7 @@ func (c *Client) GetMulti(keys []string, opts ...Option) (map[string]*Item, erro
options := newOptions(opts...)

var lk sync.Mutex
m := make(map[string]*Item)
m := make(map[string]*Item, len(keys))
addItemToMap := func(it *Item) {
lk.Lock()
defer lk.Unlock()
Expand Down
27 changes: 27 additions & 0 deletions memcache/memcache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -539,6 +539,33 @@ func TestClient_Close_ShouldBeIdempotent(t *testing.T) {
c.Close()
}

func BenchmarkGetMulti(b *testing.B) {
c := New(testServer)
defer c.Close()

var keys []string
for i := 0; i < 100; i++ {
key := fmt.Sprintf("bench-%d", i)
keys = append(keys, key)

err := c.Set(&Item{
Key: key,
Value: []byte(strings.Repeat("bench payload", 10)),
Flags: 0,
Expiration: 30,
casid: 0,
})
if err != nil {
b.Fatal("Could not set item for test server: ", err)
}
}

b.ResetTimer()
for i := 0; i < b.N; i++ {
_, _ = c.GetMulti(keys)
}
}

func BenchmarkOnItem(b *testing.B) {
fakeServer, err := net.Listen("tcp", "localhost:0")
if err != nil {
Expand Down
Loading