From 7d8f420f9d25c48306e8f337e44fc60eec7f46d5 Mon Sep 17 00:00:00 2001 From: Ignacio Van Droogenbroeck Date: Mon, 2 Mar 2026 19:40:31 -0600 Subject: [PATCH] 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. --- decode.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/decode.go b/decode.go index 8040370..13ee2e5 100644 --- a/decode.go +++ b/decode.go @@ -624,6 +624,11 @@ func (d *Decoder) ReadFull(buf []byte) error { } func (d *Decoder) hasNilCode() bool { + // Fast path: when decoding from a byte slice, peek directly + // to avoid two interface method calls (ReadByte + UnreadByte). + if d.s == &d.bsr { + return d.bsr.pos < len(d.bsr.data) && d.bsr.data[d.bsr.pos] == msgpcode.Nil + } code, err := d.PeekCode() return err == nil && code == msgpcode.Nil }