-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replace simple cache with free cache to avoid GC runtimes
- Loading branch information
Showing
9 changed files
with
216 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
/* | ||
Copyright 2018 Banco Bilbao Vizcaya Argentaria, S.A. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package cache | ||
|
||
import ( | ||
"bytes" | ||
"runtime/debug" | ||
|
||
"github.com/bbva/qed/balloon/navigator" | ||
"github.com/bbva/qed/hashing" | ||
"github.com/bbva/qed/storage" | ||
"github.com/coocood/freecache" | ||
) | ||
|
||
type FreeCache struct { | ||
cached *freecache.Cache | ||
} | ||
|
||
func NewFreeCache(initialSize int) *FreeCache { | ||
cache := freecache.NewCache(initialSize) | ||
debug.SetGCPercent(20) | ||
return &FreeCache{cached: cache} | ||
} | ||
|
||
func (c FreeCache) Get(pos navigator.Position) (hashing.Digest, bool) { | ||
value, err := c.cached.Get(pos.Bytes()) | ||
if err != nil { | ||
return nil, false | ||
} | ||
return value, true | ||
} | ||
|
||
func (c *FreeCache) Put(pos navigator.Position, value hashing.Digest) { | ||
c.cached.Set(pos.Bytes(), value, 0) | ||
} | ||
|
||
func (c *FreeCache) Fill(r storage.KVPairReader) (err error) { | ||
defer r.Close() | ||
for { | ||
entries := make([]*storage.KVPair, 100) | ||
n, err := r.Read(entries) | ||
if err != nil || n == 0 { | ||
break | ||
} | ||
for _, entry := range entries { | ||
if entry != nil { | ||
c.cached.Set(entry.Key, entry.Value, 0) | ||
} | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func (c FreeCache) Size() int { | ||
return int(c.cached.EntryCount()) | ||
} | ||
|
||
func (c FreeCache) Equal(o *FreeCache) bool { | ||
it := c.cached.NewIterator() | ||
entry := it.Next() | ||
for entry != nil { | ||
v2, err := o.cached.Get(entry.Key) | ||
if err != nil { | ||
return false | ||
} | ||
if !bytes.Equal(entry.Value, v2) { | ||
return false | ||
} | ||
entry = it.Next() | ||
} | ||
return true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
/* | ||
Copyright 2018 Banco Bilbao Vizcaya Argentaria, S.A. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
package cache | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/bbva/qed/balloon/navigator" | ||
"github.com/bbva/qed/hashing" | ||
"github.com/bbva/qed/util" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestFreeCache(t *testing.T) { | ||
|
||
testCases := []struct { | ||
pos navigator.Position | ||
value hashing.Digest | ||
cached bool | ||
}{ | ||
{&navigator.FakePosition{[]byte{0x0}, 0}, hashing.Digest{0x1}, true}, | ||
{&navigator.FakePosition{[]byte{0x1}, 0}, hashing.Digest{0x2}, true}, | ||
{&navigator.FakePosition{[]byte{0x2}, 0}, hashing.Digest{0x3}, false}, | ||
} | ||
|
||
cache := NewFreeCache(100 * 1024) | ||
|
||
for i, c := range testCases { | ||
if c.cached { | ||
cache.Put(c.pos, c.value) | ||
} | ||
|
||
cachedValue, ok := cache.Get(c.pos) | ||
|
||
if c.cached { | ||
require.Truef(t, ok, "The key should exists in cache in test case %d", i) | ||
require.Equalf(t, c.value, cachedValue, "The cached value should be equal to stored value in test case %d", i) | ||
} else { | ||
require.Falsef(t, ok, "The key should not exist in cache in test case %d", i) | ||
} | ||
} | ||
} | ||
|
||
func TestFillFreeCache(t *testing.T) { | ||
|
||
numElems := uint64(10000) | ||
cache := NewFreeCache(10000 * 1024) | ||
reader := NewFakeKVPairReader(numElems) | ||
|
||
err := cache.Fill(reader) | ||
|
||
require.NoError(t, err) | ||
require.Truef(t, reader.Remaining == 0, "All elements should be cached. Remaining: %d", reader.Remaining) | ||
|
||
for i := uint64(0); i < numElems; i++ { | ||
pos := &navigator.FakePosition{util.Uint64AsBytes(i), 0} | ||
_, ok := cache.Get(pos) | ||
require.Truef(t, ok, "The element in position %v should be in cache", pos) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters