From be01529d44b3dd2eb8fdf0162563cdc0a196c0ca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20Mur=C3=A9?= Date: Tue, 22 Jul 2025 12:27:57 +0200 Subject: [PATCH] perf: pre-allocate the buffer when encoding a varsig --- varsig.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/varsig.go b/varsig.go index 3cdfe6a..513d10d 100644 --- a/varsig.go +++ b/varsig.go @@ -89,7 +89,10 @@ func (v varsig) Signature() []byte { } func (v varsig) encode() []byte { - var buf []byte + // Pre-allocate to the maximum size to avoid re-allocating. + // I think the maximum is 10 bytes, but it's all the same for go to allocate 16 (due to the small + // size allocation class), so we might as well get some headroom for bigger varints. + buf := make([]byte, 0, 16) buf = binary.AppendUvarint(buf, Prefix)