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

misc code improvements #91

Closed
wants to merge 26 commits into from
Closed
Show file tree
Hide file tree
Changes from 25 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
0fed73a
unnecessary type conversion
Oct 17, 2019
ae50d1b
TestMain should call os.Exit to set exit code otherwise its always ze…
Oct 17, 2019
86d2fa7
error strings should not end with punctation since they are usually p…
Oct 17, 2019
1bf42a6
should omit type time.Duration from declaration of var wait; it will …
Oct 17, 2019
f875879
acronyms should be capitalized
Oct 17, 2019
74d61c8
increment with ++ is the way to go
Oct 17, 2019
91b4060
trailing whitespace removed
Oct 17, 2019
8d117f0
single case switch should be if statement
Oct 17, 2019
9181856
replace else {if cond {}} with else if cond {}
Oct 17, 2019
dec484b
gofmt -s
Oct 17, 2019
2310823
TestMain should call os.Exit to set exit code otherwise its always ze…
tegk Oct 17, 2019
df38d06
error strings should not end with punctation since they are usually p…
Oct 17, 2019
954cef3
should omit type time.Duration from declaration of var wait; it will …
tegk Oct 17, 2019
fd29622
acronyms should be capitalized
tegk Oct 17, 2019
54cacf5
increment with ++ is the way to go
tegk Oct 17, 2019
8baf1cb
trailing whitespace removed
tegk Oct 17, 2019
dfefa25
single case switch should be if statement
tegk Oct 17, 2019
098e498
replace else {if cond {}} with else if cond {}
tegk Oct 17, 2019
64e5834
gofmt -s
tegk Oct 17, 2019
40b84bd
Merge branch 'master' of https://github.com/tegk/ristretto
tegk Oct 17, 2019
e9795b2
single case switch should be if statement
tegk Oct 17, 2019
1be4690
replace else {if cond {}} with else if cond {}
tegk Oct 17, 2019
569769d
gofmt -s
tegk Oct 17, 2019
1be40e1
Merge branch 'master' of https://github.com/tegk/ristretto
tegk Oct 17, 2019
aeaab35
Merge branch 'master' of https://github.com/tegk/ristretto
tegk Oct 17, 2019
cd78c9e
resolving maintainers requewsts
tegk Oct 21, 2019
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
6 changes: 3 additions & 3 deletions cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,11 +123,11 @@ type item struct {
func NewCache(config *Config) (*Cache, error) {
switch {
case config.NumCounters == 0:
return nil, errors.New("NumCounters can't be zero.")
return nil, errors.New("NumCounters can't be zero")
case config.MaxCost == 0:
return nil, errors.New("MaxCost can't be zero.")
return nil, errors.New("MaxCost can't be zero")
case config.BufferItems == 0:
return nil, errors.New("BufferItems can't be zero.")
return nil, errors.New("BufferItems can't be zero")
}
policy := newPolicy(config.NumCounters, config.MaxCost)
cache := &Cache{
Expand Down
8 changes: 3 additions & 5 deletions cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"time"
)

var wait time.Duration = time.Millisecond * 10
var wait = time.Millisecond * 10

func TestCache(t *testing.T) {
if _, err := NewCache(&Config{
Expand Down Expand Up @@ -135,10 +135,8 @@ func TestCacheSet(t *testing.T) {
if val, ok := c.Get(1); val == nil || val.(int) != 1 || !ok {
t.Fatal("set/get returned wrong value")
}
} else {
if val, ok := c.Get(1); val != nil || ok {
t.Fatal("set was dropped but value still added")
}
} else if val, ok := c.Get(1); val != nil || ok {
t.Fatal("set was dropped but value still added")
}
c.Set(1, 2, 2)
if val, ok := c.store.Get(1); val == nil || val.(int) != 2 || !ok {
Expand Down
10 changes: 5 additions & 5 deletions policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,11 +155,11 @@ func (p *defaultPolicy) Add(key uint64, cost int64) ([]*item, bool) {
// fill up empty slots in sample
sample = p.evict.fillSample(sample)
// find minimally used item in sample
minKey, minHits, minId, minCost := uint64(0), int64(math.MaxInt64), 0, int64(0)
minKey, minHits, minID, minCost := uint64(0), int64(math.MaxInt64), 0, int64(0)
for i, pair := range sample {
// look up hit count for sample key
if hits := p.admit.Estimate(pair.key); hits < minHits {
minKey, minHits, minId, minCost = pair.key, hits, i, pair.cost
minKey, minHits, minID, minCost = pair.key, hits, i, pair.cost
}
}
// if the incoming item isn't worth keeping in the policy, reject.
Expand All @@ -170,7 +170,7 @@ func (p *defaultPolicy) Add(key uint64, cost int64) ([]*item, bool) {
// delete the victim from metadata
p.evict.del(minKey)
// delete the victim from sample
sample[minId] = sample[len(sample)-1]
sample[minID] = sample[len(sample)-1]
sample = sample[:len(sample)-1]
// store victim in evicted victims slice
victims = append(victims, &item{
Expand All @@ -197,7 +197,7 @@ func (p *defaultPolicy) Del(key uint64) {

func (p *defaultPolicy) Cap() int64 {
p.Lock()
capacity := int64(p.evict.maxCost - p.evict.used)
capacity := p.evict.maxCost - p.evict.used
p.Unlock()
return capacity
}
Expand Down Expand Up @@ -325,7 +325,7 @@ func (p *tinyLFU) Push(keys []uint64) {
func (p *tinyLFU) Estimate(key uint64) int64 {
hits := p.freq.Estimate(key)
if p.door.Has(key) {
hits += 1
hits++
}
return hits
}
Expand Down
2 changes: 1 addition & 1 deletion sketch.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ func newCmRow(numCounters int64) cmRow {
}

func (r cmRow) get(n uint64) byte {
return byte(r[n/2]>>((n&1)*4)) & 0x0f
return r[n/2] >> ((n & 1) * 4) & 0x0f
tegk marked this conversation as resolved.
Show resolved Hide resolved
}

func (r cmRow) increment(n uint64) {
Expand Down
3 changes: 1 addition & 2 deletions z/bbloom.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,7 @@ func (bl Bloom) Has(hash uint64) bool {
h := hash >> bl.shift
l := hash << bl.shift >> bl.shift
for i := uint64(0); i < bl.setLocs; i++ {
switch bl.IsSet((h + i*l) & bl.size) {
case false:
if !bl.IsSet((h + i*l) & bl.size) {
return false
}
}
Expand Down
12 changes: 5 additions & 7 deletions z/bbloom_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package z
import (
"crypto/rand"
"fmt"
"os"
"testing"
)

Expand All @@ -22,8 +23,8 @@ func TestMain(m *testing.M) {
fmt.Println("\n###############\nbbloom_test.go")
fmt.Print("Benchmarks relate to 2**16 OP. --> output/65536 op/ns\n###############\n\n")

m.Run()

exitVal := m.Run()
os.Exit(exitVal)
}

func TestM_NumberOfWrongs(t *testing.T) {
Expand All @@ -37,7 +38,6 @@ func TestM_NumberOfWrongs(t *testing.T) {
}
}
fmt.Printf("Bloomfilter New(7* 2**16, 7) (-> size=%v bit): \n Check for 'false positives': %v wrong positive 'Has' results on 2**16 entries => %v %%\n", len(bf.bitset)<<6, cnt, float64(cnt)/float64(n))

}

func TestM_JSON(t *testing.T) {
Expand All @@ -53,10 +53,10 @@ func TestM_JSON(t *testing.T) {
}
}

Json := bf.JSONMarshal()
JSON := bf.JSONMarshal()
tegk marked this conversation as resolved.
Show resolved Hide resolved

// create new bloomfilter from bloomfilter's JSON representation
bf2 := JSONUnmarshal(Json)
bf2 := JSONUnmarshal(JSON)

cnt2 := 0
for i := range wordlist1 {
Expand All @@ -69,7 +69,6 @@ func TestM_JSON(t *testing.T) {
if cnt2 != shallBe {
t.Errorf("FAILED !AddIfNotHasBytes = %v; want %v", cnt2, shallBe)
}

}

func BenchmarkM_New(b *testing.B) {
Expand Down Expand Up @@ -99,7 +98,6 @@ func BenchmarkM_Add(b *testing.B) {
bf.Add(hash)
}
}

}

func BenchmarkM_Has(b *testing.B) {
Expand Down
2 changes: 0 additions & 2 deletions z/rtutil_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ func SipHash(p []byte) (l, h uint64) {

// Compression.
for len(p) >= 8 {

m := uint64(p[0]) | uint64(p[1])<<8 | uint64(p[2])<<16 | uint64(p[3])<<24 |
uint64(p[4])<<32 | uint64(p[5])<<40 | uint64(p[6])<<48 | uint64(p[7])<<56

Expand Down Expand Up @@ -252,7 +251,6 @@ func SipHash(p []byte) (l, h uint64) {
h = hash >> 1
l = hash << 1 >> 1
return l, h

}
func BenchmarkNanoTime(b *testing.B) {
for i := 0; i < b.N; i++ {
Expand Down