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-23 - Stack Allocation for Temporary Buffers
**Learning:** Using `make([]byte, n)` for small, temporary buffers (like IP parsing) in hot loops causes significant allocation overhead. Replacing them with stack-allocated arrays (e.g., `var buf [16]byte; slice := buf[:]`) eliminates these allocations entirely.
**Action:** Look for small `make` calls in hot paths (especially in `proc` parsing) and replace them with stack buffers where the size is bounded and small.
11 changes: 6 additions & 5 deletions proc/net.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,7 @@ func readSockets(src string) ([]Sock, error) {
local, b := nextField(b)
remote, b := nextField(b)
st, b := nextField(b)
state := string(st)
if state != stateEstablished && state != stateListen {
if string(st) != stateEstablished && string(st) != stateListen {
continue
}
_, b = nextField(b)
Expand All @@ -68,7 +67,7 @@ func readSockets(src string) ([]Sock, error) {
_, b = nextField(b)
_, b = nextField(b)
inode, _ := nextField(b)
res = append(res, Sock{SAddr: decodeAddr(local), DAddr: decodeAddr(remote), Listen: state == stateListen, Inode: string(inode)})
res = append(res, Sock{SAddr: decodeAddr(local), DAddr: decodeAddr(remote), Listen: string(st) == stateListen, Inode: string(inode)})
}
return res, nil
}
Expand All @@ -93,11 +92,13 @@ func decodeAddr(src []byte) netaddr.IPPort {
if col == -1 || (col != 8 && col != 32) {
return netaddr.IPPort{}
}
ip := make([]byte, col/2)
var ipBuf [16]byte
ip := ipBuf[:col/2]
if _, err := hex.Decode(ip, src[:col]); err != nil {
return netaddr.IPPort{}
}
port := make([]byte, 2)
var portBuf [2]byte
port := portBuf[:]
if _, err := hex.Decode(port, src[col+1:]); err != nil {
return netaddr.IPPort{}
}
Expand Down
Loading