Skip to content

Commit 0e49e5c

Browse files
committed
use const [N]byte array in struct instead of heap allocated make([]byte) slice
one less malloc()+GC per marshal
1 parent 6dace5a commit 0e49e5c

File tree

1 file changed

+4
-5
lines changed

1 file changed

+4
-5
lines changed

io.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ var (
5252

5353
type CborWriter struct {
5454
w io.Writer
55-
hbuf []byte
55+
hbuf [maxHeaderSize]byte
5656

5757
sw io.StringWriter
5858
}
@@ -63,8 +63,7 @@ func NewCborWriter(w io.Writer) *CborWriter {
6363
}
6464

6565
cw := &CborWriter{
66-
w: w,
67-
hbuf: make([]byte, maxHeaderSize),
66+
w: w,
6867
}
6968

7069
if sw, ok := w.(io.StringWriter); ok {
@@ -88,11 +87,11 @@ func (cw *CborWriter) Write(p []byte) (n int, err error) {
8887
}
8988

9089
func (cw *CborWriter) WriteMajorTypeHeader(t byte, l uint64) error {
91-
return WriteMajorTypeHeaderBuf(cw.hbuf, cw.w, t, l)
90+
return WriteMajorTypeHeaderBuf(cw.hbuf[:], cw.w, t, l)
9291
}
9392

9493
func (cw *CborWriter) CborWriteHeader(t byte, l uint64) error {
95-
return WriteMajorTypeHeaderBuf(cw.hbuf, cw.w, t, l)
94+
return WriteMajorTypeHeaderBuf(cw.hbuf[:], cw.w, t, l)
9695
}
9796

9897
func (cw *CborWriter) WriteString(s string) (int, error) {

0 commit comments

Comments
 (0)