Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
use const [N]byte array in struct instead of heap allocated make([]by…
…te) slice

one less malloc()+GC per marshal
  • Loading branch information
brianolson committed Sep 2, 2024
commit 0e49e5c4855af73128e5a8c8f5a885a3af2c22a2
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