From a4adba8b2e23913fa0e414d7d81ed1f56f0cc4ac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Mart=C3=AD?= Date: Sun, 28 Mar 2021 15:34:07 +0100 Subject: [PATCH] preallocate 1KiB on the stack for marshals MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit name old time/op new time/op delta Roundtrip-8 4.35µs ± 1% 4.27µs ± 0% -1.92% (p=0.004 n=6+5) name old alloc/op new alloc/op delta Roundtrip-8 6.86kB ± 0% 6.86kB ± 0% +0.01% (p=0.004 n=5+6) name old allocs/op new allocs/op delta Roundtrip-8 112 ± 0% 106 ± 0% -5.36% (p=0.002 n=6+6) --- marshal.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/marshal.go b/marshal.go index 6aa16a3..f156236 100644 --- a/marshal.go +++ b/marshal.go @@ -36,7 +36,11 @@ func Marshal(inNode ipld.Node, out io.Writer) error { if err != nil { return err } - var enc []byte + + // 1KiB can be allocated on the stack, and covers most small nodes + // without having to grow the buffer and cause allocations. + enc := make([]byte, 0, 1024) + if links.Length() > 0 { // collect links into a slice so we can properly sort for encoding pbLinks := make([]pbLink, links.Length()) -- GitLab