Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .jules/bolt.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## 2025-01-31 - Zero-alloc IP parsing
**Learning:** `netaddr.FromStdIP(net.IP(...))` causes heap allocations because `net.IP` is a slice. Using `netaddr.IPv4` or `netaddr.IPFrom16` with stack-allocated arrays avoids these allocations completely.
**Action:** Always prefer `netaddr.IPv4/IPFrom16` with `[4]byte` or `[16]byte` buffers over `net.IP` slice wrappers when performance matters.
42 changes: 25 additions & 17 deletions proc/net.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"bytes"
"encoding/binary"
"encoding/hex"
"net"
"os"

"inet.af/netaddr"
Expand Down Expand Up @@ -89,28 +88,37 @@ func nextField(s []byte) ([]byte, []byte) {
}

func decodeAddr(src []byte) netaddr.IPPort {
// Optimization: Use stack-allocated buffers to avoid heap allocations
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 netaddr.IP
var buf [16]byte
switch col {
case 8:
if _, err := hex.Decode(buf[:4], src[:col]); err != nil {
return netaddr.IPPort{}
}
v := binary.BigEndian.Uint32(buf[:4])
binary.LittleEndian.PutUint32(buf[:4], v)
Comment on lines +103 to +104

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

To make the byte-swapping logic more explicit and potentially more efficient, you can reverse the bytes directly instead of converting to and from a uint32.

Suggested change
v := binary.BigEndian.Uint32(buf[:4])
binary.LittleEndian.PutUint32(buf[:4], v)
buf[0], buf[1], buf[2], buf[3] = buf[3], buf[2], buf[1], buf[0]

ip = netaddr.IPv4(buf[0], buf[1], buf[2], buf[3])
case 32:
if _, err := hex.Decode(buf[:], src[:col]); err != nil {
return netaddr.IPPort{}
}
for i := 0; i < 16; i += 4 {
v := binary.BigEndian.Uint32(buf[i : i+4])
binary.LittleEndian.PutUint32(buf[i:i+4], v)
}
Comment on lines +110 to +113

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Similar to the IPv4 case, you can simplify the byte-swapping logic here by reversing the bytes in-place for each 4-byte chunk. This is more direct and avoids the intermediate uint32 conversion.

		for i := 0; i < 16; i += 4 {
			buf[i], buf[i+1], buf[i+2], buf[i+3] = buf[i+3], buf[i+2], buf[i+1], buf[i]
		}

ip = netaddr.IPFrom16(buf)
default:
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)
}

ipp, ok := netaddr.FromStdIP(net.IP(ip))
if !ok {
var portBuf [2]byte
if _, err := hex.Decode(portBuf[:], src[col+1:]); err != nil {
return netaddr.IPPort{}
}
return netaddr.IPPortFrom(ipp, binary.BigEndian.Uint16(port))
return netaddr.IPPortFrom(ip, binary.BigEndian.Uint16(portBuf[:]))
}
Loading