Skip to content

Commit a3ea333

Browse files
Decrease number of allocations during get or set operations (#126)
1 parent 8480cb3 commit a3ea333

File tree

2 files changed

+24
-10
lines changed

2 files changed

+24
-10
lines changed

cache_test.go

+14
Original file line numberDiff line numberDiff line change
@@ -1086,3 +1086,17 @@ func TestUpdate(t *testing.T) {
10861086
found, replaced, err = cache.Update(key, updater)
10871087
assertExpectations(4, true, false, val2, val2)
10881088
}
1089+
1090+
func TestBenchmarkCacheGetWithBuf(t *testing.T) {
1091+
alloc := testing.Benchmark(BenchmarkCacheGetWithBuf).AllocsPerOp()
1092+
if alloc > 0 {
1093+
t.Errorf("current alloc count '%d' is higher than 0", alloc)
1094+
}
1095+
}
1096+
1097+
func TestBenchmarkCacheSet(t *testing.T) {
1098+
alloc := testing.Benchmark(BenchmarkCacheSet).AllocsPerOp()
1099+
if alloc > 0 {
1100+
t.Errorf("current alloc count '%d' is higher than 0", alloc)
1101+
}
1102+
}

segment.go

+10-10
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ func (seg *segment) set(key, value []byte, hashVal uint64, expireSeconds int) (e
9999
hdr.expireAt = expireAt
100100
hdr.valLen = uint32(len(value))
101101
if hdr.valCap >= hdr.valLen {
102-
//in place overwrite
102+
// in place overwrite
103103
atomic.AddInt64(&seg.totalTime, int64(hdr.accessTime)-int64(originAccessTime))
104104
seg.rb.WriteAt(hdrBuf[:], matchedPtr.offset)
105105
seg.rb.WriteAt(value, matchedPtr.offset+ENTRY_HDR_SIZE+int64(hdr.keyLen))
@@ -186,7 +186,7 @@ func (seg *segment) touch(key []byte, hashVal uint64, expireSeconds int) (err er
186186
originAccessTime := hdr.accessTime
187187
hdr.accessTime = now
188188
hdr.expireAt = expireAt
189-
//in place overwrite
189+
// in place overwrite
190190
atomic.AddInt64(&seg.totalTime, int64(hdr.accessTime)-int64(originAccessTime))
191191
seg.rb.WriteAt(hdrBuf[:], matchedPtr.offset)
192192
atomic.AddInt64(&seg.touched, 1)
@@ -236,7 +236,7 @@ func (seg *segment) evacuate(entryLen int64, slotId uint8, now uint32) (slotModi
236236
}
237237

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

250-
seg.rb.ReadAt(value, ptr.offset+ENTRY_HDR_SIZE+int64(hdr.keyLen))
250+
seg.rb.ReadAt(value, ptrOffset+ENTRY_HDR_SIZE+int64(hdr.keyLen))
251251
if !peek {
252252
atomic.AddInt64(&seg.hitCount, 1)
253253
}
@@ -257,11 +257,11 @@ func (seg *segment) get(key, buf []byte, hashVal uint64, peek bool) (value []byt
257257
// view provides zero-copy access to the element's value, without copying to
258258
// an intermediate buffer.
259259
func (seg *segment) view(key []byte, fn func([]byte) error, hashVal uint64, peek bool) (err error) {
260-
hdr, ptr, err := seg.locate(key, hashVal, peek)
260+
hdr, ptrOffset, err := seg.locate(key, hashVal, peek)
261261
if err != nil {
262262
return
263263
}
264-
start := ptr.offset + ENTRY_HDR_SIZE + int64(hdr.keyLen)
264+
start := ptrOffset + ENTRY_HDR_SIZE + int64(hdr.keyLen)
265265
val, err := seg.rb.Slice(start, int64(hdr.valLen))
266266
if err != nil {
267267
return err
@@ -273,7 +273,7 @@ func (seg *segment) view(key []byte, fn func([]byte) error, hashVal uint64, peek
273273
return
274274
}
275275

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

290290
var hdrBuf [ENTRY_HDR_SIZE]byte
291291
seg.rb.ReadAt(hdrBuf[:], ptr.offset)
292-
hdr = (*entryHdr)(unsafe.Pointer(&hdrBuf[0]))
292+
hdr := (*entryHdr)(unsafe.Pointer(&hdrBuf[0]))
293293
if !peek {
294294
now := seg.timer.Now()
295295
if isExpired(hdr.expireAt, now) {
@@ -303,7 +303,7 @@ func (seg *segment) locate(key []byte, hashVal uint64, peek bool) (hdr *entryHdr
303303
hdr.accessTime = now
304304
seg.rb.WriteAt(hdrBuf[:], ptr.offset)
305305
}
306-
return hdr, ptr, err
306+
return *hdr, ptr.offset, nil
307307
}
308308

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

0 commit comments

Comments
 (0)