Skip to content

Commit

Permalink
Merge pull request #104 from brianolson/fewer-alloc
Browse files Browse the repository at this point in the history
fixed array in struct instead of heap slice
  • Loading branch information
whyrusleeping authored Sep 4, 2024
2 parents 6dace5a + 7af3489 commit 8dc02b3
Showing 1 changed file with 7 additions and 9 deletions.
16 changes: 7 additions & 9 deletions io.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ var (

type CborReader struct {
r BytePeeker
hbuf []byte
hbuf [maxHeaderSize]byte
}

func NewCborReader(r io.Reader) *CborReader {
Expand All @@ -20,8 +20,7 @@ func NewCborReader(r io.Reader) *CborReader {
}

return &CborReader{
r: GetPeeker(r),
hbuf: make([]byte, maxHeaderSize),
r: GetPeeker(r),
}
}

Expand All @@ -38,7 +37,7 @@ func (cr *CborReader) UnreadByte() error {
}

func (cr *CborReader) ReadHeader() (byte, uint64, error) {
return CborReadHeaderBuf(cr.r, cr.hbuf)
return CborReadHeaderBuf(cr.r, cr.hbuf[:])
}

func (cr *CborReader) SetReader(r io.Reader) {
Expand All @@ -52,7 +51,7 @@ var (

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

sw io.StringWriter
}
Expand All @@ -63,8 +62,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 +86,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 8dc02b3

Please sign in to comment.