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

Use fastRand instead of locked-rand in skiplist (#1173) #1173

Merged
merged 9 commits into from
Jan 14, 2020
Merged
Changes from 3 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
9 changes: 6 additions & 3 deletions skl/skl.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import (
"math"
"math/rand"
"sync/atomic"
"time"
"unsafe"

"github.com/dgraph-io/badger/v2/y"
Expand Down Expand Up @@ -79,6 +80,7 @@ type Skiplist struct {
head *node
ref int32
arena *Arena
rand *rand.Rand
}

// IncrRef increases the refcount
Expand Down Expand Up @@ -132,6 +134,7 @@ func NewSkiplist(arenaSize int64) *Skiplist {
head: head,
arena: arena,
ref: 1,
rand: rand.New(rand.NewSource(time.Now().UnixNano())),
}
}

Expand Down Expand Up @@ -165,9 +168,9 @@ func (s *node) casNextOffset(h int, old, val uint32) bool {
// return n != nil && y.CompareKeys(key, n.key) > 0
//}

func randomHeight() int {
func (s *Skiplist) randomHeight() int {
h := 1
for h < maxHeight && rand.Uint32() <= heightIncrease {
for h < maxHeight && s.rand.Uint32() <= heightIncrease {
h++
}
return h
Expand Down Expand Up @@ -300,7 +303,7 @@ func (s *Skiplist) Put(key []byte, v y.ValueStruct) {
}

// We do need to create a new node.
height := randomHeight()
height := s.randomHeight()
x := newNode(s.arena, key, v, height)

// Try to increase s.height via CAS.
Expand Down