diff --git a/marshal.go b/marshal.go index 6aa16a320634e4d4508631f2ef72b0451fda2d10..f156236a0cb563026c373f9b4e8c38f6a53f6ad3 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())