Skip to content

Commit

Permalink
use const [N]byte array in struct instead of heap allocated make([]by…
Browse files Browse the repository at this point in the history
…te) slice

one less malloc()+GC per marshal
  • Loading branch information
brianolson committed Sep 2, 2024
1 parent 6dace5a commit 0e49e5c
Showing 1 changed file with 4 additions and 5 deletions.
9 changes: 4 additions & 5 deletions io.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ var (

type CborWriter struct {
w io.Writer
hbuf []byte
hbuf [maxHeaderSize]byte

sw io.StringWriter
}
Expand All @@ -63,8 +63,7 @@ func NewCborWriter(w io.Writer) *CborWriter {
}

cw := &CborWriter{
w: w,
hbuf: make([]byte, maxHeaderSize),
w: w,
}

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

func (cw *CborWriter) WriteMajorTypeHeader(t byte, l uint64) error {
return WriteMajorTypeHeaderBuf(cw.hbuf, cw.w, t, l)
return WriteMajorTypeHeaderBuf(cw.hbuf[:], cw.w, t, l)
}

func (cw *CborWriter) CborWriteHeader(t byte, l uint64) error {
return WriteMajorTypeHeaderBuf(cw.hbuf, cw.w, t, l)
return WriteMajorTypeHeaderBuf(cw.hbuf[:], cw.w, t, l)
}

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

0 comments on commit 0e49e5c

Please sign in to comment.