Skip to content

Commit

Permalink
Simplify code (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
outofforest authored Sep 10, 2024
1 parent 51bfcdb commit 209cf76
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 36 deletions.
20 changes: 13 additions & 7 deletions benchmark_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"testing"
)

// go test -benchtime=10000x -bench=BenchmarkQuantum -run=^$ -cpuprofile profile.out
// go test -benchtime=100x -bench=BenchmarkQuantum -run=^$ -cpuprofile profile.out
// go tool pprof -http="localhost:8000" pprofbin ./profile.out

type key struct {
Expand Down Expand Up @@ -34,9 +34,13 @@ func BenchmarkMaps(b *testing.B) {

for range b.N {
b.StartTimer()
for i := range 10 {
for j := i * 10; j < i*10+100; j++ {
snapshot2[keys[j]] = j
for i := range 1000 {
for j := i * 10; j < i*10+10; j++ {
k := keys[j]
v2 := snapshot2[k]
v1 := snapshot1[k]
v := db[k]
snapshot2[k] = v + v1 + v2 + j
}
for k, v := range snapshot2 {
snapshot1[k] = v
Expand Down Expand Up @@ -70,10 +74,12 @@ func BenchmarkQuantum(b *testing.B) {
for range b.N {
b.StartTimer()
snapshot1 := db.Next()
for i := range 10 {
for i := range 1000 {
snapshot2 := snapshot1.Next()
for j := i * 10; j < i*10+100; j++ {
snapshot2.Set(keys[j], j)
for j := i * 10; j < i*10+10; j++ {
k := keys[j]
v, _ := snapshot2.Get(k)
snapshot2.Set(k, v+j)
}
snapshot1 = snapshot2
}
Expand Down
44 changes: 15 additions & 29 deletions quantum.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ import (
func New[K comparable, V any]() Snapshot[K, V] {
massNodes := mass.New[node[K, V]](1000)
return Snapshot[K, V]{
root: massNodes.New(),
rootSet: new(bool),
massNodes: massNodes,
defaultValue: *new(V),
}
Expand All @@ -26,7 +24,6 @@ func New[K comparable, V any]() Snapshot[K, V] {
type Snapshot[K comparable, V any] struct {
version uint64
root *node[K, V]
rootSet *bool
massNodes *mass.Mass[node[K, V]]
defaultValue V
hasher hasher[K]
Expand All @@ -36,18 +33,11 @@ type Snapshot[K comparable, V any] struct {
// Next transitions to the next snapshot of the state.
func (s Snapshot[K, V]) Next() Snapshot[K, V] {
s.version++

r := *s.root
s.root = &r

rs := *s.rootSet
s.rootSet = &rs

return s
}

// Get gets the value of the key.
func (s Snapshot[K, V]) Get(key K) (value V, exists bool) {
func (s *Snapshot[K, V]) Get(key K) (value V, exists bool) {
h := s.hasher.Hash(key)
n := s.root
for {
Expand Down Expand Up @@ -80,25 +70,14 @@ func (s Snapshot[K, V]) Get(key K) (value V, exists bool) {
}

// Set sets the value for the key.
func (s Snapshot[K, V]) Set(key K, value V) {
func (s *Snapshot[K, V]) Set(key K, value V) {
const (
leftChild int = iota
rightChild
)

h := s.hasher.Hash(key)

if !*s.rootSet {
*s.root = node[K, V]{
Key: key,
Value: value,
Version: s.version,
Hash: h,
}
*s.rootSet = true
return
}

var parentNode *node[K, V]
var child int
n := s.root
Expand All @@ -110,11 +89,19 @@ func (s Snapshot[K, V]) Set(key K, value V) {
n.Version = s.version
n.Hash = h

if child == leftChild {
if s.root == nil {
s.root = n
return
}
switch {
case s.root == nil:
s.root = n
case child == leftChild:
parentNode.Left = n
} else {
default:
parentNode.Right = n
}

return
}
if n.Version < s.version {
Expand All @@ -126,19 +113,18 @@ func (s Snapshot[K, V]) Set(key K, value V) {
n2.Left = n.Left
n2.Right = n.Right
n2.hasher = n.hasher
n = n2

switch {
case parentNode == nil:
*s.root = *n2
n = s.root
s.root = n
case child == leftChild:
n = n2
parentNode.Left = n
default:
n = n2
parentNode.Right = n
}
}

if n.Hash == h {
if n.Key == key {
n.Value = value
Expand Down

0 comments on commit 209cf76

Please sign in to comment.