From 48395f93c2e4049c573e4b373bce104772f5a232 Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Wed, 28 Jan 2026 06:06:41 +0000 Subject: [PATCH] =?UTF-8?q?=E2=9A=A1=20Bolt:=20Optimize=20decodeAddr=20in?= =?UTF-8?q?=20proc/net=20to=20eliminate=20allocations?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Replaced `make` with stack arrays - Used `netaddr` constructors to avoid `net.IP` - Zero allocations per call Co-authored-by: blue4209211 <3078106+blue4209211@users.noreply.github.com> --- proc/net.go | 43 +++++++++++++++++++++++++++---------------- 1 file changed, 27 insertions(+), 16 deletions(-) diff --git a/proc/net.go b/proc/net.go index 70d6039d..cf9bde09 100644 --- a/proc/net.go +++ b/proc/net.go @@ -5,7 +5,6 @@ import ( "bytes" "encoding/binary" "encoding/hex" - "net" "os" "inet.af/netaddr" @@ -90,27 +89,39 @@ func nextField(s []byte) ([]byte, []byte) { func decodeAddr(src []byte) netaddr.IPPort { col := bytes.IndexByte(src, ':') - if col == -1 || (col != 8 && col != 32) { + if col == -1 { return netaddr.IPPort{} } - ip := make([]byte, col/2) - if _, err := hex.Decode(ip, src[:col]); err != nil { - return netaddr.IPPort{} - } - port := make([]byte, 2) - if _, err := hex.Decode(port, src[col+1:]); err != nil { + + var ip [16]byte + var ipp netaddr.IP + if col == 8 { + if _, err := hex.Decode(ip[:4], src[:8]); err != nil { + return netaddr.IPPort{} + } + v := binary.BigEndian.Uint32(ip[:4]) + binary.LittleEndian.PutUint32(ip[:4], v) + ipp = netaddr.IPv4(ip[0], ip[1], ip[2], ip[3]) + } else if col == 32 { + if _, err := hex.Decode(ip[:], src[:32]); err != nil { + return netaddr.IPPort{} + } + for i := 0; i < 16; i += 4 { + v := binary.BigEndian.Uint32(ip[i : i+4]) + binary.LittleEndian.PutUint32(ip[i:i+4], v) + } + ipp = netaddr.IPFrom16(ip) + } else { return netaddr.IPPort{} } - var v uint32 - for i := 0; i < len(ip); i += 4 { - v = binary.BigEndian.Uint32(ip[i : i+4]) - binary.LittleEndian.PutUint32(ip[i:i+4], v) + if len(src) < col+1+4 { + return netaddr.IPPort{} } - - ipp, ok := netaddr.FromStdIP(net.IP(ip)) - if !ok { + var port [2]byte + if _, err := hex.Decode(port[:], src[col+1:col+1+4]); err != nil { return netaddr.IPPort{} } - return netaddr.IPPortFrom(ipp, binary.BigEndian.Uint16(port)) + + return netaddr.IPPortFrom(ipp, binary.BigEndian.Uint16(port[:])) }