Skip to content

Commit

Permalink
reduce lock contention in record cache (#23)
Browse files Browse the repository at this point in the history
  • Loading branch information
Preetam authored Jan 22, 2017
1 parent 4ee9e5a commit 346ee5e
Showing 1 changed file with 20 additions and 4 deletions.
24 changes: 20 additions & 4 deletions lm2.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,19 +179,35 @@ func (rc *recordCache) findLastLessThan(key string) int64 {
}

func (rc *recordCache) push(rec *record) {
rc.lock.Lock()
defer rc.lock.Unlock()
rc.lock.RLock()

if rc.maxKeyRecord == nil || rc.maxKeyRecord.Key < rec.Key {
rc.maxKeyRecord = rec
} else if len(rc.cache) == rc.size && rand.Float32() >= 0.01 {
rc.lock.RUnlock()

rc.lock.Lock()
if rc.maxKeyRecord == nil || rc.maxKeyRecord.Key < rec.Key {
rc.maxKeyRecord = rec
}
rc.lock.Unlock()

return
}

if len(rc.cache) == rc.size && rand.Float32() >= 0.01 {
rc.lock.RUnlock()
return
}

rc.lock.RUnlock()
rc.lock.Lock()

rc.cache[rec.Offset] = rec
rc.updatesSinceSave++
if !rc.preventPurge {
rc.purge()
}

rc.lock.Unlock()
}

func (rc *recordCache) save() {
Expand Down

0 comments on commit 346ee5e

Please sign in to comment.