Skip to content

Commit d38bef3

Browse files
author
Ibrahim Jarif
authored
fix(ristretto): Update ristretto to 623d8ef1614b (#1472)
Update ristretto to hypermodeinc/ristretto@623d8ef1614b. This PR contains the following changes - Use `calloc` from `z` package and remove calloc from `y` package. - Use `onExit` handler instead of the `onReject` and `onEvict` callbacks in ristretto.
1 parent f23f935 commit d38bef3

File tree

13 files changed

+34
-218
lines changed

13 files changed

+34
-218
lines changed

db.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -324,12 +324,7 @@ func Open(opt Options) (db *DB, err error) {
324324
MaxCost: int64(float64(opt.MaxCacheSize) * 0.95),
325325
BufferItems: 64,
326326
Metrics: true,
327-
OnEvict: func(i *ristretto.Item) {
328-
table.BlockEvictHandler(i.Value)
329-
},
330-
OnReject: func(i *ristretto.Item) {
331-
table.BlockEvictHandler(i.Value)
332-
},
327+
OnExit: table.BlockEvictHandler,
333328
}
334329
db.blockCache, err = ristretto.NewCache(&config)
335330
if err != nil {

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ go 1.12
55
require (
66
github.com/DataDog/zstd v1.4.1
77
github.com/cespare/xxhash v1.1.0
8-
github.com/dgraph-io/ristretto v0.0.4-0.20200817124926-18e279725890
8+
github.com/dgraph-io/ristretto v0.0.4-0.20200820164438-623d8ef1614b
99
github.com/dgryski/go-farm v0.0.0-20190423205320-6a90982ecee2
1010
github.com/dustin/go-humanize v1.0.0
1111
github.com/golang/protobuf v1.3.1

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ github.com/cpuguy83/go-md2man v1.0.10/go.mod h1:SmD6nW6nTyfqj6ABTjUi3V3JVMnlJmwc
1313
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
1414
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
1515
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
16-
github.com/dgraph-io/ristretto v0.0.4-0.20200817124926-18e279725890 h1:/6pLcQq2GNdLPOotXztuLDXYRPraTIzZMPiJW8HzAwg=
17-
github.com/dgraph-io/ristretto v0.0.4-0.20200817124926-18e279725890/go.mod h1:KPxhHT9ZxKefz+PCeOGsrHpl1qZ7i70dGTu2u+Ahh6E=
16+
github.com/dgraph-io/ristretto v0.0.4-0.20200820164438-623d8ef1614b h1:/g8jOqvD1UzHTOwENtkqcLmMLzTcN18P3ut8aSUZ45g=
17+
github.com/dgraph-io/ristretto v0.0.4-0.20200820164438-623d8ef1614b/go.mod h1:KPxhHT9ZxKefz+PCeOGsrHpl1qZ7i70dGTu2u+Ahh6E=
1818
github.com/dgryski/go-farm v0.0.0-20190423205320-6a90982ecee2 h1:tdlZCpZ/P9DhczCTSixgIKmwPv6+wP5DGjqLYw5SUiA=
1919
github.com/dgryski/go-farm v0.0.0-20190423205320-6a90982ecee2/go.mod h1:SqUrOPUnsFjfmXRMNPybcSiG0BgUW2AuFH8PAnS2iTw=
2020
github.com/dustin/go-humanize v1.0.0 h1:VSnTsYCnlFHaM2/igO1h6X3HA71jcobQuxemgkq4zYo=

levels.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import (
3232
"github.com/dgraph-io/badger/v2/pb"
3333
"github.com/dgraph-io/badger/v2/table"
3434
"github.com/dgraph-io/badger/v2/y"
35+
"github.com/dgraph-io/ristretto/z"
3536
"github.com/pkg/errors"
3637
)
3738

@@ -703,7 +704,7 @@ nextTable:
703704
num := atomic.LoadInt32(&table.NumBlocks)
704705
mu.Unlock()
705706

706-
s.kv.opt.Debugf("Num Blocks: %d. Num Allocs (MB): %.2f\n", num, y.NumAllocs.Value())
707+
s.kv.opt.Debugf("Num Blocks: %d. Num Allocs (MB): %.2f\n", num, z.NumAllocsMB())
707708
}(builder)
708709
}
709710

table/builder.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ func NewTableBuilder(opts Options) *Builder {
9797
b := &Builder{
9898
// Additional 16 MB to store index (approximate).
9999
// We trim the additional space in table.Finish().
100-
buf: y.Calloc(int(opts.TableSize + 16*MB)),
100+
buf: z.Calloc(int(opts.TableSize + 16*MB)),
101101
tableIndex: &pb.TableIndex{},
102102
keyHashes: make([]uint64, 0, 1024), // Avoid some malloc calls.
103103
opt: &opts,
@@ -157,14 +157,14 @@ func (b *Builder) handleBlock() {
157157
item.end = item.start + uint32(len(blockBuf))
158158

159159
if doCompress {
160-
y.Free(blockBuf)
160+
z.Free(blockBuf)
161161
}
162162
}
163163
}
164164

165165
// Close closes the TableBuilder.
166166
func (b *Builder) Close() {
167-
y.Free(b.buf)
167+
z.Free(b.buf)
168168
}
169169

170170
// Empty returns whether it's empty.
@@ -228,12 +228,12 @@ func (b *Builder) grow(n uint32) {
228228
if n < l/2 {
229229
n = l / 2
230230
}
231-
newBuf := y.Calloc(int(l + n))
231+
newBuf := z.Calloc(int(l + n))
232232
y.AssertTrue(uint32(len(newBuf)) == l+n)
233233

234234
b.bufLock.Lock()
235235
copy(newBuf, b.buf)
236-
y.Free(b.buf)
236+
z.Free(b.buf)
237237
b.buf = newBuf
238238
b.bufLock.Unlock()
239239
}
@@ -471,20 +471,20 @@ func (b *Builder) encrypt(data []byte, viaC bool) ([]byte, error) {
471471
needSz := len(data) + len(iv)
472472
var dst []byte
473473
if viaC {
474-
dst = y.Calloc(needSz)
474+
dst = z.Calloc(needSz)
475475
} else {
476476
dst = make([]byte, needSz)
477477
}
478478
dst = dst[:len(data)]
479479

480480
if err = y.XORBlock(dst, data, b.DataKey().Data, iv); err != nil {
481481
if viaC {
482-
y.Free(dst)
482+
z.Free(dst)
483483
}
484484
return data, y.Wrapf(err, "Error while encrypting in Builder.encrypt")
485485
}
486486
if viaC {
487-
y.Free(data)
487+
z.Free(data)
488488
}
489489

490490
y.AssertTrue(cap(dst)-len(dst) >= len(iv))
@@ -504,11 +504,11 @@ func (b *Builder) compressData(data []byte) ([]byte, error) {
504504
return data, nil
505505
case options.Snappy:
506506
sz := snappy.MaxEncodedLen(len(data))
507-
dst := y.Calloc(sz)
507+
dst := z.Calloc(sz)
508508
return snappy.Encode(dst, data), nil
509509
case options.ZSTD:
510510
sz := zstd.CompressBound(len(data))
511-
dst := y.Calloc(sz)
511+
dst := z.Calloc(sz)
512512
return y.ZSTDCompress(dst, data, b.opt.ZSTDCompressionLevel)
513513
}
514514
return nil, errors.New("Unsupported compression type")

table/table.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ func (b *block) decrRef() {
243243
// will lead to SEGFAULT.
244244
if atomic.AddInt32(&b.ref, -1) == 0 {
245245
if b.freeMe {
246-
y.Free(b.data)
246+
z.Free(b.data)
247247
}
248248
atomic.AddInt32(&NumBlocks, -1)
249249
// blockPool.Put(&b.data)
@@ -813,29 +813,29 @@ func (t *Table) decompress(b *block) error {
813813
return nil
814814
case options.Snappy:
815815
if sz, err := snappy.DecodedLen(b.data); err == nil {
816-
dst = y.Calloc(sz)
816+
dst = z.Calloc(sz)
817817
} else {
818-
dst = y.Calloc(len(b.data) * 4) // Take a guess.
818+
dst = z.Calloc(len(b.data) * 4) // Take a guess.
819819
}
820820
b.data, err = snappy.Decode(dst, b.data)
821821
if err != nil {
822-
y.Free(dst)
822+
z.Free(dst)
823823
return errors.Wrap(err, "failed to decompress")
824824
}
825825
case options.ZSTD:
826826
sz := int(float64(t.opt.BlockSize) * 1.2)
827-
dst = y.Calloc(sz)
827+
dst = z.Calloc(sz)
828828
b.data, err = y.ZSTDDecompress(dst, b.data)
829829
if err != nil {
830-
y.Free(dst)
830+
z.Free(dst)
831831
return errors.Wrap(err, "failed to decompress")
832832
}
833833
default:
834834
return errors.New("Unsupported compression type")
835835
}
836836

837837
if len(b.data) > 0 && len(dst) > 0 && &dst[0] != &b.data[0] {
838-
y.Free(dst)
838+
z.Free(dst)
839839
} else {
840840
b.freeMe = true
841841
}

y/buffer.go

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,11 @@
1616

1717
package y
1818

19-
import "encoding/binary"
19+
import (
20+
"encoding/binary"
21+
22+
"github.com/dgraph-io/ristretto/z"
23+
)
2024

2125
type Buffer struct {
2226
buf []byte
@@ -25,7 +29,7 @@ type Buffer struct {
2529

2630
func NewBuffer(sz int) *Buffer {
2731
return &Buffer{
28-
buf: Calloc(sz),
32+
buf: z.Calloc(sz),
2933
offset: 0,
3034
}
3135
}
@@ -44,20 +48,20 @@ const smallBufferSize = 64
4448
func (b *Buffer) Grow(n int) {
4549
// In this case, len and cap are the same.
4650
if len(b.buf) == 0 && n <= smallBufferSize {
47-
b.buf = Calloc(smallBufferSize)
51+
b.buf = z.Calloc(smallBufferSize)
4852
return
4953
} else if b.buf == nil {
50-
b.buf = Calloc(n)
54+
b.buf = z.Calloc(n)
5155
return
5256
}
5357
if b.offset+n < len(b.buf) {
5458
return
5559
}
5660

5761
sz := 2*len(b.buf) + n
58-
newBuf := Calloc(sz)
62+
newBuf := z.Calloc(sz)
5963
copy(newBuf, b.buf[:b.offset])
60-
Free(b.buf)
64+
z.Free(b.buf)
6165
b.buf = newBuf
6266
}
6367

@@ -108,5 +112,5 @@ func (b *Buffer) Reset() {
108112
}
109113

110114
func (b *Buffer) Release() {
111-
Free(b.buf)
115+
z.Free(b.buf)
112116
}

y/calloc.go

Lines changed: 0 additions & 64 deletions
This file was deleted.

y/calloc_32bit.go

Lines changed: 0 additions & 12 deletions
This file was deleted.

y/calloc_64bit.go

Lines changed: 0 additions & 12 deletions
This file was deleted.

0 commit comments

Comments
 (0)