Skip to content

Commit

Permalink
Implement binary trie (#127)
Browse files Browse the repository at this point in the history
* renamed hex trie
* implemented insert for binary hex trie
* added retrieve for binary trie
* added delete for binary trie
* added hashing to binary trie
* added tests for binary trie
* improved test output
* fixed bitset/slice shenanigans
* added benchmark for hex & bin tries
* added benchmark for hashing

closes #125
  • Loading branch information
Max Wolter committed Mar 14, 2018
1 parent daca31d commit 6607743
Show file tree
Hide file tree
Showing 9 changed files with 682 additions and 59 deletions.
14 changes: 10 additions & 4 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions Gopkg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@
branch = "master"
name = "github.com/hashicorp/go-multierror"

[[constraint]]
branch = "master"
name = "github.com/jrick/bitset"

[[constraint]]
name = "github.com/pierrec/lz4"
version = "1.1.0"
Expand Down
2 changes: 1 addition & 1 deletion node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func New(log zerolog.Logger, net Network, chain Blockchain, finder Finder, codec
n.peers = peers

// initialize simple transaction pool
store := trie.New()
store := trie.NewBin()
pool := newPool(codec, store)
n.pool = pool

Expand Down
185 changes: 185 additions & 0 deletions trie/benchmark_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,185 @@
// Copyright (c) 2017 The Alvalor Authors
//
// This file is part of Alvalor.
//
// Alvalor is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Alvalor is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with Alvalor. If not, see <http://www.gnu.org/licenses/>.

package trie

import (
"crypto/rand"
"testing"
)

func BenchmarkBinInsert(b *testing.B) {
t := NewBin()
keys := make([][]byte, 0, b.N)
hashes := make([][]byte, 0, b.N)
for i := 0; i < b.N; i++ {
key := make([]byte, 32)
hash := make([]byte, 32)
_, _ = rand.Read(key)
_, _ = rand.Read(hash)
keys = append(keys, key)
hashes = append(hashes, hash)
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
t.MustPut(keys[i], hashes[i])
}
}

func BenchmarkBinRetrieve(b *testing.B) {
t := NewBin()
keys := make([][]byte, 0, b.N)
hashes := make([][]byte, 0, b.N)
for i := 0; i < b.N; i++ {
key := make([]byte, 32)
hash := make([]byte, 32)
_, _ = rand.Read(key)
_, _ = rand.Read(hash)
keys = append(keys, key)
hashes = append(hashes, hash)
}
for i := 0; i < b.N; i++ {
t.MustPut(keys[i], hashes[i])
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
t.Get(keys[i])
}
}

func BenchmarkBinDelete(b *testing.B) {
t := NewBin()
keys := make([][]byte, 0, b.N)
hashes := make([][]byte, 0, b.N)
for i := 0; i < b.N; i++ {
key := make([]byte, 32)
hash := make([]byte, 32)
_, _ = rand.Read(key)
_, _ = rand.Read(hash)
keys = append(keys, key)
hashes = append(hashes, hash)
}
for i := 0; i < b.N; i++ {
t.MustPut(keys[i], hashes[i])
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
t.Del(keys[i])
}
}

func BenchmarkBinHash(b *testing.B) {
t := NewBin()
keys := make([][]byte, 0, b.N)
hashes := make([][]byte, 0, b.N)
for i := 0; i < b.N; i++ {
key := make([]byte, 32)
hash := make([]byte, 32)
_, _ = rand.Read(key)
_, _ = rand.Read(hash)
keys = append(keys, key)
hashes = append(hashes, hash)
}
for i := 0; i < b.N; i++ {
t.MustPut(keys[i], hashes[i])
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
t.Hash()
}
}

func BenchmarkHexInsert(b *testing.B) {
t := NewHex()
keys := make([][]byte, 0, b.N)
hashes := make([][]byte, 0, b.N)
for i := 0; i < b.N; i++ {
key := make([]byte, 32)
hash := make([]byte, 32)
_, _ = rand.Read(key)
_, _ = rand.Read(hash)
keys = append(keys, key)
hashes = append(hashes, hash)
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
t.MustPut(keys[i], hashes[i])
}
}

func BenchmarkHexRetrieve(b *testing.B) {
t := NewHex()
keys := make([][]byte, 0, b.N)
hashes := make([][]byte, 0, b.N)
for i := 0; i < b.N; i++ {
key := make([]byte, 32)
hash := make([]byte, 32)
_, _ = rand.Read(key)
_, _ = rand.Read(hash)
keys = append(keys, key)
hashes = append(hashes, hash)
}
for i := 0; i < b.N; i++ {
t.MustPut(keys[i], hashes[i])
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
t.Get(keys[i])
}
}

func BenchmarkHexDelete(b *testing.B) {
t := NewHex()
keys := make([][]byte, 0, b.N)
hashes := make([][]byte, 0, b.N)
for i := 0; i < b.N; i++ {
key := make([]byte, 32)
hash := make([]byte, 32)
_, _ = rand.Read(key)
_, _ = rand.Read(hash)
keys = append(keys, key)
hashes = append(hashes, hash)
}
for i := 0; i < b.N; i++ {
t.MustPut(keys[i], hashes[i])
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
t.Del(keys[i])
}
}

func BenchmarkHexHash(b *testing.B) {
t := NewHex()
keys := make([][]byte, 0, b.N)
hashes := make([][]byte, 0, b.N)
for i := 0; i < b.N; i++ {
key := make([]byte, 32)
hash := make([]byte, 32)
_, _ = rand.Read(key)
_, _ = rand.Read(hash)
keys = append(keys, key)
hashes = append(hashes, hash)
}
for i := 0; i < b.N; i++ {
t.MustPut(keys[i], hashes[i])
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
t.Hash()
}
}
Loading

0 comments on commit 6607743

Please sign in to comment.