From c8ac1d34f2c3493a8eada1f373c8eac5013fa7c0 Mon Sep 17 00:00:00 2001 From: Josh Rickmar Date: Mon, 10 Jun 2024 18:38:15 +0000 Subject: [PATCH] mixing: Prealloc buffers The buffers written to during signature creation and validation have a known constant size. Preallocate these buffers to reduce the total number of allocations that are needed as fmt.Sprintf writes to the buffer. --- mixing/signatures.go | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/mixing/signatures.go b/mixing/signatures.go index 214e40afb..190a06c28 100644 --- a/mixing/signatures.go +++ b/mixing/signatures.go @@ -12,6 +12,7 @@ import ( "github.com/decred/dcrd/crypto/blake256" "github.com/decred/dcrd/dcrec/secp256k1/v4" "github.com/decred/dcrd/dcrec/secp256k1/v4/schnorr" + "github.com/decred/dcrd/wire" ) const tag = "decred-mix-signature" @@ -85,6 +86,12 @@ func sign(priv *secp256k1.PrivateKey, m Signed) ([]byte, error) { } buf := new(bytes.Buffer) + buf.Grow(len(tag) + wire.CommandSize + + 64 + // sid + 4 + // run + 64 + // sigHash + 4, // commas + ) fmt.Fprintf(buf, tag+",%s,%x,%d,%x", m.Command(), sid, run, sigHash) h.Write(buf.Bytes()) @@ -111,6 +118,12 @@ func verify(h hash.Hash, pk []byte, sig []byte, sigHash []byte, command string, h.Reset() buf := new(bytes.Buffer) + buf.Grow(len(tag) + wire.CommandSize + + 64 + // sid + 4 + // run + 64 + // sigHash + 4, // commas + ) fmt.Fprintf(buf, tag+",%s,%x,%d,%x", command, sid, run, sigHash) h.Write(buf.Bytes()) return sigParsed.Verify(h.Sum(nil), pkParsed)