-
Notifications
You must be signed in to change notification settings - Fork 674
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Optimize key creation in hashing (#2899)
- Loading branch information
1 parent
e4b82cf
commit 6699924
Showing
2 changed files
with
165 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
// Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved. | ||
// See the file LICENSE for licensing terms. | ||
|
||
package merkledb | ||
|
||
import ( | ||
"context" | ||
"encoding/binary" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/ava-labs/avalanchego/database" | ||
"github.com/ava-labs/avalanchego/database/memdb" | ||
"github.com/ava-labs/avalanchego/utils/hashing" | ||
) | ||
|
||
var hashChangedNodesTests = []struct { | ||
name string | ||
numKeys uint64 | ||
expectedRootHash string | ||
}{ | ||
{ | ||
name: "1", | ||
numKeys: 1, | ||
expectedRootHash: "2A4DRkSWbTvSxgA1UMGp1Mpt1yzMFaeMMiDnrijVGJXPcRYiD4", | ||
}, | ||
{ | ||
name: "10", | ||
numKeys: 10, | ||
expectedRootHash: "2PGy7QvbYwVwn5QmLgj4KBgV2BisanZE8Nue2SxK9ffybb4mAn", | ||
}, | ||
{ | ||
name: "100", | ||
numKeys: 100, | ||
expectedRootHash: "LCeS4DWh6TpNKWH4ke9a2piSiwwLbmxGUj8XuaWx1XDGeCMAv", | ||
}, | ||
{ | ||
name: "1000", | ||
numKeys: 1000, | ||
expectedRootHash: "2S6f84wdRHmnx51mj35DF2owzf8wio5pzNJXfEWfFYFNxUB64T", | ||
}, | ||
{ | ||
name: "10000", | ||
numKeys: 10000, | ||
expectedRootHash: "wF6UnhaDoA9fAqiXAcx27xCYBK2aspDBEXkicmC7rs8EzLCD8", | ||
}, | ||
{ | ||
name: "100000", | ||
numKeys: 100000, | ||
expectedRootHash: "2Dy3RWZeNDUnUvzXpruB5xdp1V7xxb14M53ywdZVACDkdM66M1", | ||
}, | ||
} | ||
|
||
func makeViewForHashChangedNodes(t require.TestingT, numKeys uint64, parallelism uint) *view { | ||
config := newDefaultConfig() | ||
config.RootGenConcurrency = parallelism | ||
db, err := newDatabase( | ||
context.Background(), | ||
memdb.New(), | ||
config, | ||
&mockMetrics{}, | ||
) | ||
require.NoError(t, err) | ||
|
||
ops := make([]database.BatchOp, 0, numKeys) | ||
for i := uint64(0); i < numKeys; i++ { | ||
k := binary.AppendUvarint(nil, i) | ||
ops = append(ops, database.BatchOp{ | ||
Key: k, | ||
Value: hashing.ComputeHash256(k), | ||
}) | ||
} | ||
|
||
ctx := context.Background() | ||
viewIntf, err := db.NewView(ctx, ViewChanges{BatchOps: ops}) | ||
require.NoError(t, err) | ||
|
||
view := viewIntf.(*view) | ||
require.NoError(t, view.calculateNodeChanges(ctx)) | ||
return view | ||
} | ||
|
||
func Test_HashChangedNodes(t *testing.T) { | ||
for _, test := range hashChangedNodesTests { | ||
t.Run(test.name, func(t *testing.T) { | ||
view := makeViewForHashChangedNodes(t, test.numKeys, 16) | ||
ctx := context.Background() | ||
view.hashChangedNodes(ctx) | ||
require.Equal(t, test.expectedRootHash, view.changes.rootID.String()) | ||
}) | ||
} | ||
} | ||
|
||
func Benchmark_HashChangedNodes(b *testing.B) { | ||
for _, test := range hashChangedNodesTests { | ||
view := makeViewForHashChangedNodes(b, test.numKeys, 1) | ||
ctx := context.Background() | ||
b.Run(test.name, func(b *testing.B) { | ||
for i := 0; i < b.N; i++ { | ||
view.hashChangedNodes(ctx) | ||
} | ||
}) | ||
} | ||
} |