Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

opt: Small memory usage optimizations #1562

Merged
merged 4 commits into from
Oct 12, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 21 additions & 7 deletions table/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -318,15 +318,15 @@ func (t *Table) initBiggestAndSmallest() error {
return y.Wrapf(err, "failed to read index.")
}

t.smallest = ko.KeyBytes()
t.smallest = y.Copy(ko.KeyBytes())

it2 := t.NewIterator(REVERSED | NOCACHE)
defer it2.Close()
it2.Rewind()
if !it2.Valid() {
return y.Wrapf(it2.err, "failed to initialize biggest for table %s", t.Filename())
}
t.biggest = it2.Key()
t.biggest = y.Copy(it2.Key())
return nil
}

Expand Down Expand Up @@ -493,9 +493,11 @@ func (t *Table) block(idx int, useCache bool) (*block, error) {

if t.shouldDecrypt() {
// Decrypt the block if it is encrypted.
if blk.data, err = t.decrypt(blk.data); err != nil {
if blk.data, err = t.decrypt(blk.data, true); err != nil {
return nil, err
}
// blk.data is allocated via Calloc. So, do free.
blk.freeMe = true
}

if err = t.decompress(blk); err != nil {
Expand Down Expand Up @@ -641,7 +643,7 @@ func (t *Table) readTableIndex() (*fb.TableIndex, error) {
var err error
// Decrypt the table index if it is encrypted.
if t.shouldDecrypt() {
if data, err = t.decrypt(data); err != nil {
if data, err = t.decrypt(data, false); err != nil {
return nil, y.Wrapf(err,
"Error while decrypting table index for the table %d in readTableIndex", t.id)
}
Expand Down Expand Up @@ -690,14 +692,18 @@ func (t *Table) KeyID() uint64 {
}

// decrypt decrypts the given data. It should be called only after checking shouldDecrypt.
func (t *Table) decrypt(data []byte) ([]byte, error) {
func (t *Table) decrypt(data []byte, viaCalloc bool) ([]byte, error) {
// Last BlockSize bytes of the data is the IV.
iv := data[len(data)-aes.BlockSize:]
// Rest all bytes are data.
data = data[:len(data)-aes.BlockSize]

// TODO: Check if this is done via Calloc. Otherwise, we'll have a memory leak.
dst := make([]byte, len(data))
var dst []byte
if viaCalloc {
dst = z.Calloc(len(data))
} else {
dst = make([]byte, len(data))
}
if err := y.XORBlock(dst, data, t.opt.DataKey.Data, iv); err != nil {
return nil, y.Wrapf(err, "while decrypt")
}
Expand Down Expand Up @@ -736,6 +742,9 @@ func (t *Table) decompress(b *block) error {
var dst []byte
var err error

// Point to the original b.data
src := b.data

switch t.opt.Compression {
case options.None:
// Nothing to be done here.
Expand Down Expand Up @@ -763,6 +772,11 @@ func (t *Table) decompress(b *block) error {
return errors.New("Unsupported compression type")
}

if b.freeMe == true {
z.Free(src)
b.freeMe = false
}

if len(b.data) > 0 && len(dst) > 0 && &dst[0] != &b.data[0] {
z.Free(dst)
} else {
Expand Down