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

performance for get shards index #150

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 7 additions & 5 deletions store.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ const numShards uint64 = 256
type shardedMap struct {
shards []*lockedMap
expiryMap *expirationMap
mask uint64
}

func newShardedMap() *shardedMap {
Expand All @@ -74,15 +75,16 @@ func newShardedMap() *shardedMap {
for i := range sm.shards {
sm.shards[i] = newLockedMap(sm.expiryMap)
}
sm.mask = numShards - 1
return sm
}

func (sm *shardedMap) Get(key, conflict uint64) (interface{}, bool) {
return sm.shards[key%numShards].get(key, conflict)
return sm.shards[key&sm.mask].get(key, conflict)
}

func (sm *shardedMap) Expiration(key uint64) time.Time {
return sm.shards[key%numShards].Expiration(key)
return sm.shards[key&sm.mask].Expiration(key)
}

func (sm *shardedMap) Set(i *item) {
Expand All @@ -91,15 +93,15 @@ func (sm *shardedMap) Set(i *item) {
return
}

sm.shards[i.key%numShards].Set(i)
sm.shards[i.key&sm.mask].Set(i)
}

func (sm *shardedMap) Del(key, conflict uint64) (uint64, interface{}) {
return sm.shards[key%numShards].Del(key, conflict)
return sm.shards[key&sm.mask].Del(key, conflict)
}

func (sm *shardedMap) Update(newItem *item) bool {
return sm.shards[newItem.key%numShards].Update(newItem)
return sm.shards[newItem.key&sm.mask].Update(newItem)
}

func (sm *shardedMap) Cleanup(policy policy, onEvict onEvictFunc) {
Expand Down