Skip to content

Commit 95831c2

Browse files
committed
impove description
1 parent 519807a commit 95831c2

File tree

2 files changed

+80
-2
lines changed

2 files changed

+80
-2
lines changed

README.md

+37-1
Original file line numberDiff line numberDiff line change
@@ -105,4 +105,40 @@ if ok := trie.Delete(key, newVal); !ok {
105105
// data not deleted
106106
}
107107
108-
```
108+
```
109+
110+
6. Get hash of the trie:
111+
112+
```go
113+
// initialize data
114+
key := []byte("key")
115+
val := []byte("val")
116+
117+
// create new trie and try add key/value pair
118+
trie := mpt.NewEmptyTrie()
119+
if ok := trie.Put(key, val); !ok {
120+
// key/pair not added
121+
}
122+
123+
// some logic...
124+
125+
// try to get hash of the trie
126+
h, err := trie.Hash()
127+
if err != nil {
128+
// error while hash calculation
129+
}
130+
131+
fmt.Println("hash of the trie:", h)
132+
133+
```
134+
135+
## Benchmarks
136+
137+
|Name|Iterations|Time|Description|
138+
|----|----------|----|-----------|
139+
|BenchmarkTrie_Get/FromFullTrie100000-4|1000000|3463 ns/op|Get data from the tree which have 100000 key/value pairs|
140+
|BenchmarkTrie_Get/FromEmptyTrie-4|1000000|1262 ns/op|Get data from the empty tree|
141+
|BenchmarkTrie_Put/ToFullTrie100000-4|3000000|513 ns/op|Put data to the tree which have 100000 key/value pairs|
142+
|BenchmarkTrie_Put/ToEmptyTrie-4|3000000|524 ns/op|Put data to empty tree|
143+
|BenchmarkTrie_Update/FullTrie100000-4|1000000|3083 ns/op|Update data in the tree which have 100000 key/value pairs|
144+
|BenchmarkTrie_Update/EmptyTrie-4|2000000|674 ns/op|Update data in empty tree|

trie_test.go

+43-1
Original file line numberDiff line numberDiff line change
@@ -339,9 +339,51 @@ func BenchmarkTrie_Put(b *testing.B) {
339339

340340
preparedExtTrie := mpt.NewTrie(node.NewExtensionNode(nil, nil))
341341
preparedExtTrie.Put(key, val)
342-
b.Run("ToPreparedBranchNodeEmptyTrie", func(b *testing.B) {
342+
b.Run("ToPreparedExtensionNodeEmptyTrie", func(b *testing.B) {
343343
for i := 0; i < b.N; i++ {
344344
preparedExtTrie.Put(key, val)
345345
}
346346
})
347347
}
348+
349+
func BenchmarkTrie_Update(b *testing.B) {
350+
key, val := []byte("tolongwseb;kjnwkjlevnoknbkomnrwobnwotrh34y6onyo"), []byte("lwebniopwjgnipwuhgpv wrhtpovm rhiopwerhgpowuihnopwrntgmopkrtmvport uiwthgipurthg puti")
351+
352+
fullTrie := mpt.NewTrie(nil)
353+
for j := 0; j < 100000; j++ {
354+
tokenKey, tokenValue := make([]byte, 56), make([]byte, 556)
355+
rand.Read(tokenKey)
356+
rand.Read(tokenValue)
357+
fullTrie.Put(tokenKey, tokenValue)
358+
}
359+
fullTrie.Put(key, val)
360+
b.Run("FullTrie100000", func(b *testing.B) {
361+
for i := 0; i < b.N; i++ {
362+
fullTrie.Update(key, val)
363+
}
364+
})
365+
366+
emptyTrie := mpt.NewTrie(nil)
367+
emptyTrie.Put(key, val)
368+
b.Run("EmptyTrie", func(b *testing.B) {
369+
for i := 0; i < b.N; i++ {
370+
emptyTrie.Update(key, val)
371+
}
372+
})
373+
374+
preparedBranchTrie := mpt.NewTrie(node.NewBranchNode())
375+
preparedBranchTrie.Put(key, val)
376+
b.Run("PreparedBranchNodeEmptyTrie", func(b *testing.B) {
377+
for i := 0; i < b.N; i++ {
378+
preparedBranchTrie.Update(key, val)
379+
}
380+
})
381+
382+
preparedExtTrie := mpt.NewTrie(node.NewExtensionNode(nil, nil))
383+
preparedExtTrie.Put(key, val)
384+
b.Run("PreparedExtensionNodeEmptyTrie", func(b *testing.B) {
385+
for i := 0; i < b.N; i++ {
386+
preparedExtTrie.Update(key, val)
387+
}
388+
})
389+
}

0 commit comments

Comments
 (0)