mirror of
https://github.com/arnaucube/arbo.git
synced 2026-01-08 15:01:29 +01:00
AddBatch implement using VirtualTree.AddBatch
Reduces the num of hashes, dbGet and dbPut on cases D & E. Current benchmarks: ``` CASE A, AddBatch was 9.101855 times faster than without AddBatch nCPU: 4, nLeafs: 1024, hash: Poseidon, db: memory dbgStats(hash: 2.047k, dbGet: 1, dbPut: 2.049k) CASE B, AddBatch was 8.244078 times faster than without AddBatch nCPU: 4, nLeafs: 1024, hash: Poseidon, db: memory dbgStats(hash: 2.047k, dbGet: 198, dbPut: 2.049k) CASE C, AddBatch was 8.511131 times faster than without AddBatch nCPU: 4, nLeafs: 1024, hash: Poseidon, db: memory dbgStats(hash: 2.047k, dbGet: 202, dbPut: 2.049k) CASE D, AddBatch was 8.565696 times faster than without AddBatch nCPU: 4, nLeafs: 4096, hash: Poseidon, db: memory dbgStats(hash: 8.191k, dbGet: 1.800k, dbPut: 8.193k) CASE E, AddBatch was 8.970056 times faster than without AddBatch nCPU: 4, nLeafs: 4096, hash: Poseidon, db: memory dbgStats(hash: 10.409k, dbGet: 2.668k, dbPut: 10.861k) TestAddBatchBench: nCPU: 4, nLeafs: 50000, hash: Blake2b, db: leveldb Add loop: 10.616145533s dbgStats(hash: 824.884k, dbGet: 788.975k, dbPut: 924.884k) AddBatch: 942.34815ms dbgStats(hash: 122.051k, dbGet: 1, dbPut: 122.053k) TestDbgStats add in loop dbgStats(hash: 141.911k, dbGet: 134.650k, dbPut: 161.911k) addbatch caseA dbgStats(hash: 24.522k, dbGet: 1, dbPut: 24.524k) addbatch caseD dbgStats(hash: 26.985k, dbGet: 2.465k, dbPut: 26.989k) ```
This commit is contained in:
@@ -14,7 +14,7 @@ import (
|
||||
"github.com/iden3/go-merkletree/db/memory"
|
||||
)
|
||||
|
||||
var debug = false
|
||||
var debug = true
|
||||
|
||||
func printTestContext(prefix string, nLeafs int, hashName, dbName string) {
|
||||
if debug {
|
||||
@@ -230,6 +230,7 @@ func TestAddBatchCaseATestVector(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
// tree1.PrintGraphviz(nil)
|
||||
|
||||
indexes, err := tree2.AddBatch(keys, values)
|
||||
c.Assert(err, qt.IsNil)
|
||||
@@ -238,9 +239,60 @@ func TestAddBatchCaseATestVector(t *testing.T) {
|
||||
|
||||
c.Check(len(indexes), qt.Equals, 0)
|
||||
|
||||
// tree1.PrintGraphviz(nil)
|
||||
// tree2.PrintGraphviz(nil)
|
||||
|
||||
// check that both trees roots are equal
|
||||
// fmt.Println(hex.EncodeToString(tree1.Root()))
|
||||
c.Check(tree2.Root(), qt.DeepEquals, tree1.Root())
|
||||
// c.Assert(tree2.Root(), qt.DeepEquals, tree1.Root())
|
||||
|
||||
// fmt.Println("\n---2nd test vector---")
|
||||
//
|
||||
// // 2nd test vectors
|
||||
// tree1, err = NewTree(memory.NewMemoryStorage(), 100, HashFunctionBlake2b)
|
||||
// c.Assert(err, qt.IsNil)
|
||||
// defer tree1.db.Close()
|
||||
//
|
||||
// tree2, err = NewTree(memory.NewMemoryStorage(), 100, HashFunctionBlake2b)
|
||||
// c.Assert(err, qt.IsNil)
|
||||
// defer tree2.db.Close()
|
||||
//
|
||||
// testvectorKeys = []string{
|
||||
// "1c7c2265e368314ca58ed2e1f33a326f1220e234a566d55c3605439dbe411642",
|
||||
// "2c9f0a578afff5bfa4e0992a43066460faaab9e8e500db0b16647c701cdb16bf",
|
||||
// "9cb87ec67e875c61390edcd1ab517f443591047709a4d4e45b0f9ed980857b8e",
|
||||
// "9b4e9e92e974a589f426ceeb4cb291dc24893513fecf8e8460992dcf52621d4d",
|
||||
// "1c45cb31f2fa39ec7b9ebf0fad40e0b8296016b5ce8844ae06ff77226379d9a5",
|
||||
// "d8af98bbbb585129798ae54d5eabbc9d0561d583faf1663b3a3724d15bda4ec7",
|
||||
// "3cd55dbfb8f975f20a0925dfbdabe79fa2d51dd0268afbb8ba6b01de9dfcdd3c",
|
||||
// "5d0a9d6d9f197c091bf054fac9cb60e11ec723d6610ed8578e617b4d46cb43d5",
|
||||
// }
|
||||
// keys = [][]byte{}
|
||||
// values = [][]byte{}
|
||||
// for i := 0; i < len(testvectorKeys); i++ {
|
||||
// key, err := hex.DecodeString(testvectorKeys[i])
|
||||
// c.Assert(err, qt.IsNil)
|
||||
// keys = append(keys, key)
|
||||
// values = append(values, []byte{0})
|
||||
// }
|
||||
//
|
||||
// for i := 0; i < len(keys); i++ {
|
||||
// if err := tree1.Add(keys[i], values[i]); err != nil {
|
||||
// t.Fatal(err)
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// indexes, err = tree2.AddBatch(keys, values)
|
||||
// c.Assert(err, qt.IsNil)
|
||||
// // tree1.PrintGraphviz(nil)
|
||||
// // tree2.PrintGraphviz(nil)
|
||||
//
|
||||
// c.Check(len(indexes), qt.Equals, 0)
|
||||
//
|
||||
// // check that both trees roots are equal
|
||||
// // fmt.Println(hex.EncodeToString(tree1.Root()))
|
||||
// c.Check(tree2.Root(), qt.DeepEquals, tree1.Root())
|
||||
}
|
||||
|
||||
func TestAddBatchCaseARandomKeys(t *testing.T) {
|
||||
@@ -264,7 +316,7 @@ func TestAddBatchCaseARandomKeys(t *testing.T) {
|
||||
// fmt.Println("K", hex.EncodeToString(keys[i]))
|
||||
}
|
||||
|
||||
// TMP:
|
||||
// TODO delete
|
||||
keys[0], _ = hex.DecodeString("1c7c2265e368314ca58ed2e1f33a326f1220e234a566d55c3605439dbe411642")
|
||||
keys[1], _ = hex.DecodeString("2c9f0a578afff5bfa4e0992a43066460faaab9e8e500db0b16647c701cdb16bf")
|
||||
keys[2], _ = hex.DecodeString("9cb87ec67e875c61390edcd1ab517f443591047709a4d4e45b0f9ed980857b8e")
|
||||
@@ -797,6 +849,35 @@ func TestDbgStats(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoadVT(t *testing.T) {
|
||||
c := qt.New(t)
|
||||
|
||||
nLeafs := 1024
|
||||
|
||||
tree, err := NewTree(memory.NewMemoryStorage(), 100, HashFunctionPoseidon)
|
||||
c.Assert(err, qt.IsNil)
|
||||
defer tree.db.Close()
|
||||
|
||||
var keys, values [][]byte
|
||||
for i := 0; i < nLeafs; i++ {
|
||||
k := randomBytes(31)
|
||||
v := randomBytes(31)
|
||||
keys = append(keys, k)
|
||||
values = append(values, v)
|
||||
}
|
||||
indexes, err := tree.AddBatch(keys, values)
|
||||
c.Assert(err, qt.IsNil)
|
||||
c.Check(len(indexes), qt.Equals, 0)
|
||||
|
||||
vt, err := tree.loadVT()
|
||||
c.Assert(err, qt.IsNil)
|
||||
_, err = vt.computeHashes()
|
||||
c.Assert(err, qt.IsNil)
|
||||
|
||||
// check that tree & vt roots are equal
|
||||
c.Check(tree.Root(), qt.DeepEquals, vt.root.h)
|
||||
}
|
||||
|
||||
// func printLeafs(name string, t *Tree) {
|
||||
// w := bytes.NewBufferString("")
|
||||
//
|
||||
|
||||
Reference in New Issue
Block a user