Skip to content

Commit

Permalink
Decrease number of allocations during get or set operations (#126)
Browse files Browse the repository at this point in the history
  • Loading branch information
laura-zelenku authored Mar 22, 2023
1 parent 8480cb3 commit a3ea333
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 10 deletions.
14 changes: 14 additions & 0 deletions cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1086,3 +1086,17 @@ func TestUpdate(t *testing.T) {
found, replaced, err = cache.Update(key, updater)
assertExpectations(4, true, false, val2, val2)
}

func TestBenchmarkCacheGetWithBuf(t *testing.T) {
alloc := testing.Benchmark(BenchmarkCacheGetWithBuf).AllocsPerOp()
if alloc > 0 {
t.Errorf("current alloc count '%d' is higher than 0", alloc)
}
}

func TestBenchmarkCacheSet(t *testing.T) {
alloc := testing.Benchmark(BenchmarkCacheSet).AllocsPerOp()
if alloc > 0 {
t.Errorf("current alloc count '%d' is higher than 0", alloc)
}
}
20 changes: 10 additions & 10 deletions segment.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ func (seg *segment) set(key, value []byte, hashVal uint64, expireSeconds int) (e
hdr.expireAt = expireAt
hdr.valLen = uint32(len(value))
if hdr.valCap >= hdr.valLen {
//in place overwrite
// in place overwrite
atomic.AddInt64(&seg.totalTime, int64(hdr.accessTime)-int64(originAccessTime))
seg.rb.WriteAt(hdrBuf[:], matchedPtr.offset)
seg.rb.WriteAt(value, matchedPtr.offset+ENTRY_HDR_SIZE+int64(hdr.keyLen))
Expand Down Expand Up @@ -186,7 +186,7 @@ func (seg *segment) touch(key []byte, hashVal uint64, expireSeconds int) (err er
originAccessTime := hdr.accessTime
hdr.accessTime = now
hdr.expireAt = expireAt
//in place overwrite
// in place overwrite
atomic.AddInt64(&seg.totalTime, int64(hdr.accessTime)-int64(originAccessTime))
seg.rb.WriteAt(hdrBuf[:], matchedPtr.offset)
atomic.AddInt64(&seg.touched, 1)
Expand Down Expand Up @@ -236,7 +236,7 @@ func (seg *segment) evacuate(entryLen int64, slotId uint8, now uint32) (slotModi
}

func (seg *segment) get(key, buf []byte, hashVal uint64, peek bool) (value []byte, expireAt uint32, err error) {
hdr, ptr, err := seg.locate(key, hashVal, peek)
hdr, ptrOffset, err := seg.locate(key, hashVal, peek)
if err != nil {
return
}
Expand All @@ -247,7 +247,7 @@ func (seg *segment) get(key, buf []byte, hashVal uint64, peek bool) (value []byt
value = make([]byte, hdr.valLen)
}

seg.rb.ReadAt(value, ptr.offset+ENTRY_HDR_SIZE+int64(hdr.keyLen))
seg.rb.ReadAt(value, ptrOffset+ENTRY_HDR_SIZE+int64(hdr.keyLen))
if !peek {
atomic.AddInt64(&seg.hitCount, 1)
}
Expand All @@ -257,11 +257,11 @@ func (seg *segment) get(key, buf []byte, hashVal uint64, peek bool) (value []byt
// view provides zero-copy access to the element's value, without copying to
// an intermediate buffer.
func (seg *segment) view(key []byte, fn func([]byte) error, hashVal uint64, peek bool) (err error) {
hdr, ptr, err := seg.locate(key, hashVal, peek)
hdr, ptrOffset, err := seg.locate(key, hashVal, peek)
if err != nil {
return
}
start := ptr.offset + ENTRY_HDR_SIZE + int64(hdr.keyLen)
start := ptrOffset + ENTRY_HDR_SIZE + int64(hdr.keyLen)
val, err := seg.rb.Slice(start, int64(hdr.valLen))
if err != nil {
return err
Expand All @@ -273,7 +273,7 @@ func (seg *segment) view(key []byte, fn func([]byte) error, hashVal uint64, peek
return
}

func (seg *segment) locate(key []byte, hashVal uint64, peek bool) (hdr *entryHdr, ptr *entryPtr, err error) {
func (seg *segment) locate(key []byte, hashVal uint64, peek bool) (hdrEntry entryHdr, ptrOffset int64, err error) {
slotId := uint8(hashVal >> 8)
hash16 := uint16(hashVal >> 16)
slot := seg.getSlot(slotId)
Expand All @@ -285,11 +285,11 @@ func (seg *segment) locate(key []byte, hashVal uint64, peek bool) (hdr *entryHdr
}
return
}
ptr = &slot[idx]
ptr := &slot[idx]

var hdrBuf [ENTRY_HDR_SIZE]byte
seg.rb.ReadAt(hdrBuf[:], ptr.offset)
hdr = (*entryHdr)(unsafe.Pointer(&hdrBuf[0]))
hdr := (*entryHdr)(unsafe.Pointer(&hdrBuf[0]))
if !peek {
now := seg.timer.Now()
if isExpired(hdr.expireAt, now) {
Expand All @@ -303,7 +303,7 @@ func (seg *segment) locate(key []byte, hashVal uint64, peek bool) (hdr *entryHdr
hdr.accessTime = now
seg.rb.WriteAt(hdrBuf[:], ptr.offset)
}
return hdr, ptr, err
return *hdr, ptr.offset, nil
}

func (seg *segment) del(key []byte, hashVal uint64) (affected bool) {
Expand Down

0 comments on commit a3ea333

Please sign in to comment.