From b8dd284d3d886b6e5a1e0bd838d632b21cb1756e Mon Sep 17 00:00:00 2001 From: Roman Khimov Date: Fri, 20 Aug 2021 10:58:51 +0300 Subject: [PATCH] io: don't allocate new error on every call to Bytes() It makes no sense and we're using Bytes() pretty often. --- pkg/io/binaryBufWriter.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/pkg/io/binaryBufWriter.go b/pkg/io/binaryBufWriter.go index 7015082e1..f90584fba 100644 --- a/pkg/io/binaryBufWriter.go +++ b/pkg/io/binaryBufWriter.go @@ -5,6 +5,9 @@ import ( "errors" ) +// ErrDrained is returned on an attempt to use already drained write buffer. +var ErrDrained = errors.New("buffer already drained") + // BufBinWriter is an additional layer on top of BinWriter that // automatically creates buffer to write into that you can get after all // writes via Bytes(). @@ -29,7 +32,7 @@ func (bw *BufBinWriter) Bytes() []byte { if bw.Err != nil { return nil } - bw.Err = errors.New("buffer already drained") + bw.Err = ErrDrained return bw.buf.Bytes() }