Skip to content

Commit 3abe5c8

Browse files
authored
chore: Cut out some useless allocations when computing payset commitments (algorand#5840)
1 parent fcc686d commit 3abe5c8

File tree

4 files changed

+9
-6
lines changed

4 files changed

+9
-6
lines changed

crypto/hashes.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ func (z *HashFactory) Validate() error {
118118
}
119119

120120
// GenericHashObj Makes it easier to sum using hash interface and Hashable interface
121-
func GenericHashObj(hsh hash.Hash, h Hashable) []byte {
121+
func GenericHashObj[H Hashable](hsh hash.Hash, h H) []byte {
122122
rep := HashRep(h)
123123
return hashBytes(hsh, rep)
124124
}

crypto/merklearray/layer.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,14 @@ type pair struct {
3737
hashDigestSize int
3838
}
3939

40-
func (p *pair) ToBeHashed() (protocol.HashID, []byte) {
40+
func (p pair) ToBeHashed() (protocol.HashID, []byte) {
4141
// hashing of internal node will always be fixed length.
4242
// If one of the children is missing we use [0...0].
4343
// The size of the slice is based on the relevant hash function output size
4444
buf := make([]byte, 2*p.hashDigestSize)
4545
copy(buf[:], p.l[:])
4646
copy(buf[len(p.l):], p.r[:])
47-
return protocol.MerkleArrayNode, buf[:]
47+
return protocol.MerkleArrayNode, buf
4848
}
4949

5050
func upWorker(ws *workerState, in Layer, out Layer, h hash.Hash) {
@@ -69,7 +69,7 @@ func upWorker(ws *workerState, in Layer, out Layer, h hash.Hash) {
6969
p.r = in[i+1]
7070
}
7171

72-
out[i/2] = crypto.GenericHashObj(h, &p)
72+
out[i/2] = crypto.GenericHashObj(h, p)
7373
}
7474

7575
batchSize += 2

crypto/merklearray/merkle_test.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1172,12 +1172,13 @@ func merkleCommitBench(b *testing.B, hashType crypto.HashType) {
11721172
msg := make(TestBuf, sz)
11731173
crypto.RandBytes(msg[:])
11741174

1175-
for cnt := 10; cnt <= 10000000; cnt *= 10 {
1175+
for cnt := 10; cnt <= 100000; cnt *= 10 {
11761176
var a TestRepeatingArray
11771177
a.item = msg
11781178
a.count = uint64(cnt)
11791179

11801180
b.Run(fmt.Sprintf("Item%d/Count%d", sz, cnt), func(b *testing.B) {
1181+
b.ReportAllocs()
11811182
for i := 0; i < b.N; i++ {
11821183
tree, err := Build(a, crypto.HashFactory{HashType: hashType})
11831184
require.NoError(b, err)
@@ -1205,6 +1206,7 @@ func benchmarkMerkleProve1M(b *testing.B, hashType crypto.HashType) {
12051206
require.NoError(b, err)
12061207

12071208
b.ResetTimer()
1209+
b.ReportAllocs()
12081210

12091211
for i := uint64(0); i < uint64(b.N); i++ {
12101212
_, err := tree.Prove([]uint64{i % a.count})
@@ -1238,6 +1240,7 @@ func benchmarkMerkleVerify1M(b *testing.B, hashType crypto.HashType) {
12381240
}
12391241

12401242
b.ResetTimer()
1243+
b.ReportAllocs()
12411244

12421245
for i := uint64(0); i < uint64(b.N); i++ {
12431246
err := Verify(root, map[uint64]crypto.Hashable{i % a.count: msg}, proofs[i])

crypto/merklearray/partial.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ func (pl partialLayer) up(s *siblings, l uint64, doHash bool, hsh hash.Hash) (pa
118118
p.l = siblingHash
119119
p.r = posHash
120120
}
121-
nextLayerHash = crypto.GenericHashObj(hsh, &p)
121+
nextLayerHash = crypto.GenericHashObj(hsh, p)
122122
}
123123

124124
res = append(res, layerItem{

0 commit comments

Comments
 (0)