Skip to content

Commit

Permalink
apply review changes: call root on view, nits
Browse files Browse the repository at this point in the history
  • Loading branch information
iFrostizz committed Apr 22, 2024
1 parent 2fa9847 commit f604320
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 35 deletions.
8 changes: 2 additions & 6 deletions merkle/merkle.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,13 @@ import (

"github.com/ava-labs/avalanchego/database"
"github.com/ava-labs/avalanchego/database/memdb"
"github.com/ava-labs/avalanchego/trace"
"github.com/ava-labs/avalanchego/x/merkledb"
"github.com/ava-labs/hypersdk/utils"
"github.com/ava-labs/avalanchego/ids"
)

// Generate merkle root for a set of items
func GenerateMerkleRoot(ctx context.Context, config merkledb.Config, tracer trace.Tracer, merkleItems [][]byte, consumeBytes bool) (ids.ID, merkledb.MerkleDB, error) {
func GenerateMerkleRoot(ctx context.Context, config merkledb.Config, merkleItems [][]byte, consumeBytes bool) (ids.ID, merkledb.MerkleDB, error) {
batchOps := make([]database.BatchOp, 0, len(merkleItems))

for _, item := range merkleItems {
Expand All @@ -32,11 +31,8 @@ func GenerateMerkleRoot(ctx context.Context, config merkledb.Config, tracer trac
if err != nil {
return ids.Empty, nil, err
}
if err := view.CommitToDB(ctx); err != nil {
return ids.Empty, nil, err
}

root, err := db.GetMerkleRoot(ctx)
root, err := view.GetMerkleRoot(ctx)
if err != nil {
return ids.Empty, nil, err
}
Expand Down
57 changes: 28 additions & 29 deletions merkle/merkle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/ava-labs/avalanchego/trace"
"github.com/ava-labs/avalanchego/utils/units"
"github.com/ava-labs/avalanchego/x/merkledb"
"github.com/stretchr/testify/require"
)

var resRoot ids.ID
Expand All @@ -19,43 +20,41 @@ var resErr error

func BenchmarkMerkleTxRoot(b *testing.B) {
for _, size := range []int{10, 100, 1000, 10000} {
ctx := context.TODO()
tracer := trace.Noop
merkleItems := make([][]byte, 0, size)
for i := 0; i < size; i++ {
item := make([]byte, 32)
_, err := rand.Read(item)
if err != nil {
b.Fatal(err)
}
merkleItems = append(merkleItems, item)
}

var root ids.ID
var db merkledb.MerkleDB
var err error

defaultConfig := merkledb.Config{
BranchFactor: merkledb.BranchFactor16,
HistoryLength: 100,
IntermediateNodeCacheSize: units.MiB,
ValueNodeCacheSize: units.MiB,
Tracer: tracer,
}
ctx := context.TODO()
tracer := trace.Noop
merkleItems := make([][]byte, 0, size)
for i := 0; i < size; i++ {
item := make([]byte, 32)
_, err := rand.Read(item)
require.NoError(b, err)
merkleItems = append(merkleItems, item)
}

var root ids.ID
var db merkledb.MerkleDB
var err error

defaultConfig := merkledb.Config{
BranchFactor: merkledb.BranchFactor16,
HistoryLength: 100,
IntermediateNodeCacheSize: units.MiB,
ValueNodeCacheSize: units.MiB,
Tracer: tracer,
}

b.Run(strconv.Itoa(size), func(b *testing.B) {
for n := 0; n < b.N; n++ {
for i := 0; i < size; i++ {
root, db, err = GenerateMerkleRoot(ctx, defaultConfig, tracer, merkleItems, false)
root, db, err = GenerateMerkleRoot(ctx, defaultConfig, merkleItems, false)
}
}
})

// avoid compiler optimizations to cancel out the bench
resRoot = root
resDb = db
resErr = err
// avoid compiler optimizations to cancel out the bench
resRoot = root
resDb = db
resErr = err
}

b.ReportAllocs()
b.ReportAllocs()
}

0 comments on commit f604320

Please sign in to comment.