Skip to content

Commit e816743

Browse files
committed
Btree config update, refactoring
1 parent eeebe4e commit e816743

6 files changed

Lines changed: 64 additions & 55 deletions

File tree

.travis.yml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
language: go
22
go:
3-
- 1.12.4
4-
- 1.13.8
5-
- 1.14
3+
- 1.16.8
4+
- 1.17.1
65

76
script:
87
- go test ./...

README.md

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,28 @@ if _, err := cache.Get([]byte("key")); err != nil {
2727
For this benchmark was created memory with following specs: `2048 bytes per record`, `2048 records per shard`, `128 shards (max)`. The 2048 bytes was set.
2828

2929
```
30-
BenchmarkCacheNewMedium-4 1000 1012982 ns/op 4416184 B/op 4139 allocs/op
31-
BenchmarkCacheSetMedium-4 5000000 327 ns/op 16 B/op 0 allocs/op
32-
BenchmarkCacheGetMedium-4 30000000 48.5 ns/op 0 B/op 0 allocs/op
30+
BenchmarkCacheNewMedium-12 314 3804726 ns/op 22686418 B/op 12402 allocs/op
31+
BenchmarkCacheSetMedium-12 1845970 664.2 ns/op 129 B/op 5 allocs/op
32+
BenchmarkCacheGetMedium-12 5701435 209.1 ns/op 16 B/op 1 allocs/op
3333
```
3434

3535
*If you want do some special bencharking, go ahead.*
36+
37+
### AtomicCache vs. BigCache vs. FreeCache vs. HashicorpCache
38+
39+
**SET**
40+
```
41+
BenchmarkAtomicCacheSet-12 2402160 519.6 ns/op 153 B/op 6 allocs/op
42+
BenchmarkBigCacheSet-12 4021484 376.7 ns/op 66 B/op 1 allocs/op
43+
BenchmarkFreeCacheSet-12 10443246 100.8 ns/op 0 B/op 0 allocs/op
44+
BenchmarkHashicorpCacheSet-12 2490759 498.4 ns/op 150 B/op 5 allocs/op
45+
```
46+
47+
**GET**
48+
```
49+
BenchmarkAtomicCacheGet-12 6300744 187.6 ns/op 16 B/op 1 allocs/op
50+
BenchmarkBigCacheGet-12 3798052 322.5 ns/op 37 B/op 2 allocs/op
51+
BenchmarkFreeCacheGet-12 9000188 127.0 ns/op 24 B/op 1 allocs/op
52+
BenchmarkHashicorpCacheGet-12 3039728 405.8 ns/op 7 B/op 0 allocs/op
53+
54+
```

cache.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ import (
1111

1212
// Internal cache errors
1313
var (
14-
ErrNotFound = errors.New("Record not found")
15-
ErrDataLimit = errors.New("Can't create new record, it violates data limit")
16-
ErrFullMemory = errors.New("Can't create new rocord, memory is full")
14+
ErrNotFound = errors.New("record not found")
15+
ErrDataLimit = errors.New("cannot create new record: it violates data limit")
16+
ErrFullMemory = errors.New("cannot create new rocord: memory is full")
1717
)
1818

1919
// Constans below are used for shard section identification.
@@ -116,7 +116,7 @@ func New(opts ...Option) *AtomicCache {
116116
cache := &AtomicCache{}
117117

118118
// Init lookup table
119-
cache.lookup = btree.NewWithStringComparator(3)
119+
cache.lookup = btree.NewWithStringComparator(16)
120120

121121
// Init small shards section
122122
initShardsSection(&cache.smallShards, options.MaxShardsSmall, options.MaxRecords, options.RecordSizeSmall)
@@ -141,7 +141,7 @@ func New(opts ...Option) *AtomicCache {
141141
func initShardsSection(shardsSection *ShardsLookup, maxShards, maxRecords, recordSize uint32) {
142142
var shardIndex uint32
143143

144-
shardsSection.shards = make([]*Shard, maxShards, maxShards)
144+
shardsSection.shards = make([]*Shard, maxShards)
145145
for i := uint32(0); i < maxShards; i++ {
146146
shardsSection.shardsAvail = append(shardsSection.shardsAvail, i)
147147
}
@@ -246,7 +246,7 @@ func (a *AtomicCache) releaseShard(shardSectionID uint8, shard uint32) bool {
246246
return false
247247
}
248248

249-
if shardSection.shards[shard].IsEmpty() == true {
249+
if shardSection.shards[shard].IsEmpty() {
250250
shardSection.shards[shard] = nil
251251

252252
shardSection.shardsAvail = append(shardSection.shardsAvail, shard)

cache_test.go

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -191,23 +191,23 @@ func benchmarkCacheGet(recordCount, dataSize uint32, b *testing.B) {
191191
}
192192
}
193193

194-
func benchmarkAdvanced(recordCount, dataSize uint32, b *testing.B) {
195-
var data []byte
196-
cache := New(OptionMaxRecords(recordCount))
194+
// func benchmarkAdvanced(recordCount, dataSize uint32, b *testing.B) {
195+
// var data []byte
196+
// cache := New(OptionMaxRecords(recordCount))
197197

198-
for i := uint32(0); i < dataSize; i++ {
199-
data = append(data, 1)
200-
}
198+
// for i := uint32(0); i < dataSize; i++ {
199+
// data = append(data, 1)
200+
// }
201201

202-
cache.Set([]byte{byte(1)}, data, 0)
202+
// cache.Set([]byte{byte(1)}, data, 0)
203203

204-
b.ReportAllocs()
205-
b.ResetTimer()
204+
// b.ReportAllocs()
205+
// b.ResetTimer()
206206

207-
for n := 0; n < b.N; n++ {
208-
cache.Get([]byte{byte(1)})
209-
}
210-
}
207+
// for n := 0; n < b.N; n++ {
208+
// cache.Get([]byte{byte(1)})
209+
// }
210+
// }
211211

212212
func BenchmarkCacheNewSmall(b *testing.B) {
213213
benchmarkCacheNew(512, b)

record.go

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ func NewRecord(size uint32) *Record {
2828
// record data property and size is set.
2929
func (r *Record) Set(data []byte) {
3030
dataLength := uint32(len(data))
31-
3231
r.Lock() // Lock for writing and reading
3332
if dataLength > r.size {
3433
r.alloc = r.size
@@ -42,12 +41,11 @@ func (r *Record) Set(data []byte) {
4241
// Get returns bytes based on size of virtual allocation. It means that it
4342
// returns only specific count of bytes, based on alloc property. If array on
4443
// output is empty, then record is not exists.
45-
func (r *Record) Get() []byte {
44+
func (r *Record) Get() (data []byte) {
4645
r.RLock() // Lock for reading
47-
data := r.data[:r.alloc]
46+
data = r.data[:r.alloc]
4847
r.RUnlock() // Unlock for reading
49-
50-
return data
48+
return
5149
}
5250

5351
// Free set alloc property to 0. Through this action, we empty memory of record
@@ -59,17 +57,17 @@ func (r *Record) Free() {
5957
}
6058

6159
// GetAllocated returns size of allocated bytes.
62-
func (r *Record) GetAllocated() uint32 {
60+
func (r *Record) GetAllocated() (size uint32) {
6361
r.RLock() // Lock for reading
64-
data := r.alloc
62+
size = r.alloc
6563
r.RUnlock() // Unlock for reading
66-
return data
64+
return
6765
}
6866

6967
// GetDataLength returns real size of allocated bytes in memory.
70-
func (r *Record) GetDataLength() uint32 {
68+
func (r *Record) GetDataLength() (size uint32) {
7169
r.RLock() // Lock for reading
72-
data := uint32(len(r.data))
70+
size = uint32(len(r.data))
7371
r.RUnlock() // Unlock for reading
74-
return data
72+
return
7573
}

shard.go

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -33,27 +33,23 @@ func NewShard(slotCount, slotSize uint32) *Shard {
3333

3434
// Set store data as a record and decrease slotAvail count. On output it return
3535
// index of used slot.
36-
func (s *Shard) Set(data []byte) uint32 {
37-
var index uint32
38-
36+
func (s *Shard) Set(data []byte) (i uint32) {
3937
s.Lock() // Lock for writing and reading
40-
index, s.slotAvail = s.slotAvail[0], s.slotAvail[1:]
38+
i, s.slotAvail = s.slotAvail[0], s.slotAvail[1:]
4139
s.Unlock() // Unlock for writing and reading
42-
4340
s.RLock()
44-
s.slots[index].Set(data)
41+
s.slots[i].Set(data)
4542
s.RUnlock()
46-
47-
return index
43+
return
4844
}
4945

5046
// Get returns bytes from shard memory based on index. If array on output is
5147
// empty, then record is not exists.
52-
func (s *Shard) Get(index uint32) []byte {
48+
func (s *Shard) Get(index uint32) (v []byte) {
5349
s.RLock()
54-
value := s.slots[index].Get()
50+
v = s.slots[index].Get()
5551
s.RUnlock()
56-
return value
52+
return
5753
}
5854

5955
// Free empty memory specified by index on input and increase slot counter.
@@ -65,23 +61,20 @@ func (s *Shard) Free(index uint32) {
6561
}
6662

6763
// GetSlotsAvail returns number of available memory slots of shard.
68-
func (s *Shard) GetSlotsAvail() uint32 {
64+
func (s *Shard) GetSlotsAvail() (cnt uint32) {
6965
s.RLock()
70-
slotAvailCnt := uint32(len(s.slotAvail))
66+
cnt = uint32(len(s.slotAvail))
7167
s.RUnlock()
72-
return slotAvailCnt
68+
return
7369
}
7470

7571
// IsEmpty return true if shard has no record registered. Otherwise return
7672
// false.
77-
func (s *Shard) IsEmpty() bool {
78-
result := false
79-
73+
func (s *Shard) IsEmpty() (result bool) {
8074
s.RLock()
8175
if len(s.slotAvail) == len(s.slots) {
8276
result = true
8377
}
8478
s.RUnlock()
85-
86-
return result
79+
return
8780
}

0 commit comments

Comments
 (0)