From c49b93b39edb3bb00e99b054bb22b6eb93cd1842 Mon Sep 17 00:00:00 2001 From: Darien Raymond Date: Tue, 4 Dec 2018 11:07:33 +0100 Subject: [PATCH] simplify buffer allocation --- common/buf/writer.go | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/common/buf/writer.go b/common/buf/writer.go index 5e513fef7..275ccf694 100644 --- a/common/buf/writer.go +++ b/common/buf/writer.go @@ -29,17 +29,19 @@ func (w *BufferToBytesWriter) WriteMultiBuffer(mb MultiBuffer) error { return WriteAllBytes(w.Writer, mb[0].Bytes()) } + if cap(w.cache) < len(mb) { + w.cache = make([][]byte, 0, len(mb)) + } + bs := w.cache for _, b := range mb { bs = append(bs, b.Bytes()) } - w.cache = bs defer func() { - for idx := range w.cache { - w.cache[idx] = nil + for idx := range bs { + bs[idx] = nil } - w.cache = w.cache[:0] }() nb := net.Buffers(bs)