Reduce heap allocs 2 (#46)

* reduce heap allocations in tcp logging; omit use of AppendFloat which allocates a metric sh*tton

* debugheaplog: better heap statistic logging

* heap: use string for pcap.Frame.Protocol

* add potential to eliminate Flags.String heap alloc, remove incorrect HEAP comments

* add StackAsync.DebugErr and httpraw.SetBytes

* many heap alloc reductions and replacement of bytes.Equal with internal.BytesEqual
This commit is contained in:
Pat Whittingslow
2026-02-28 19:20:46 +01:00
committed by GitHub
parent eab43c4653
commit 989bb6a0b9
22 changed files with 511 additions and 163 deletions
+27 -16
View File
@@ -183,6 +183,8 @@ func (flags Flags) HasAny(mask Flags) bool { return flags&mask != 0 }
// Mask returns the flags with non-flag bits unset.
func (flags Flags) Mask() Flags { return flags & flagMask }
func (flags Flags) Invalid() bool { return flags&flagMask != flags }
// StringFlags returns human readable flag string. i.e:
//
// "[SYN,ACK]"
@@ -201,6 +203,8 @@ func (flags Flags) String() string {
return "[FIN,ACK]"
case pshack:
return "[PSH,ACK]"
case FlagFIN | FlagPSH | FlagACK:
return "[FIN,PSH,ACK]"
case FlagACK:
return "[ACK]"
case FlagSYN:
@@ -210,41 +214,48 @@ func (flags Flags) String() string {
case FlagRST:
return "[RST]"
}
if flags&flagMask != flags {
if flags.Invalid() {
return strInvalidTCPFlags
}
buf := make([]byte, 0, 2+3*bits.OnesCount16(uint16(flags)))
buf = append(buf, '[')
buf = flags.AppendFormat(buf)
buf = append(buf, ']')
return string(buf)
// Since Go 1.26 this should not allocate if returned string does not escape and is smaller than 32 bytes.
// https://go.dev/blog/allocation-optimizations
var buf [2 + 4*9]byte
buf[0] = '['
n := flags.format((*[36]byte)(buf[1:]))
buf[1+n] = ']'
return string(buf[:2+n])
}
// AppendFormat appends a human readable flag string to b returning the extended buffer.
func (flags Flags) AppendFormat(b []byte) []byte {
var buf [36]byte
n := flags.format(&buf)
return append(b, buf[:n]...)
}
const strInvalidTCPFlags = "<invalid TCP flags>"
// AppendFormat appends a human readable flag string to b returning the extended buffer.
func (flags Flags) AppendFormat(b []byte) []byte {
func (flags Flags) format(buf *[4 * 9]byte) (n int) {
if flags == 0 {
return b
} else if flags&flagMask != flags {
return append(b, strInvalidTCPFlags...)
return 0
} else if flags.Invalid() {
return copy(buf[:], strInvalidTCPFlags)
}
// String Flag const
const flaglen = 3
const strflags = "FINSYNRSTPSHACKURGECECWRNS "
var addcommas bool
for flags != 0 { // written by Github Copilot- looks OK.
i := bits.TrailingZeros16(uint16(flags))
if addcommas {
b = append(b, ',')
buf[n] = ','
n++
} else {
addcommas = true
}
b = append(b, strflags[i*flaglen:i*flaglen+flaglen]...)
n += copy(buf[n:], strflags[i*flaglen:i*flaglen+flaglen])
flags &= ^(1 << i)
}
return b
return n
}
// State enumerates states a TCP connection progresses through during its lifetime as per RFC9293.