Skip to content

Commit 7d8f420

Browse files
committed
perf(decode): inline hasNilCode for byte-slice reader path
When decoding from a byte slice (Unmarshal path), peek directly at the underlying data instead of going through two interface method calls (ReadByte + UnreadByte). This saves ~3-7 ns per struct pointer field decode.
1 parent 82f46cf commit 7d8f420

1 file changed

Lines changed: 5 additions & 0 deletions

File tree

decode.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -624,6 +624,11 @@ func (d *Decoder) ReadFull(buf []byte) error {
624624
}
625625

626626
func (d *Decoder) hasNilCode() bool {
627+
// Fast path: when decoding from a byte slice, peek directly
628+
// to avoid two interface method calls (ReadByte + UnreadByte).
629+
if d.s == &d.bsr {
630+
return d.bsr.pos < len(d.bsr.data) && d.bsr.data[d.bsr.pos] == msgpcode.Nil
631+
}
627632
code, err := d.PeekCode()
628633
return err == nil && code == msgpcode.Nil
629634
}

0 commit comments

Comments
 (0)