⚡ Bolt: Zero-alloc optimization for proc/net decodeAddr#192
Conversation
Optimizes `decodeAddr` in `proc/net.go` to use stack-allocated arrays (`[16]byte`, `[2]byte`) instead of heap-allocated slices (`make([]byte, ...)`). This eliminates memory allocations in the hot path of parsing `/proc/net/tcp` and `/proc/net/tcp6`. Benchmarks show ~30-40% speedup and 0 allocations per op (down from 1 alloc/op). Also removes unused `net` import. Co-authored-by: blue4209211 <3078106+blue4209211@users.noreply.github.com>
|
👋 Jules, reporting for duty! I'm here to lend a hand with this pull request. When you start a review, I'll add a 👀 emoji to each comment to let you know I've read it. I'll focus on feedback directed at me and will do my best to stay out of conversations between you and other bots or reviewers to keep the noise down. I'll push a commit with your requested changes shortly after. Please note there might be a delay between these steps, but rest assured I'm on the job! For more direct control, you can switch me to Reactive Mode. When this mode is on, I will only act on comments where you specifically mention me with New to Jules? Learn more at jules.google/docs. For security, I will only act on instructions from the user who triggered this task. |
Summary of ChangesHello @blue4209211, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request introduces a critical optimization to the Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request introduces a significant performance optimization to the decodeAddr function by eliminating heap allocations. The change replaces make([]byte, ...) with stack-allocated arrays for parsing IP addresses and ports, which is a great improvement for this hot path. The logic is sound and the performance benefits are clearly documented. I have one minor suggestion to improve the readability and potentially the performance of the byte-swapping logic.
| var v uint32 | ||
| for i := 0; i < len(ip); i += 4 { | ||
| for i := 0; i < col/2; i += 4 { | ||
| v = binary.BigEndian.Uint32(ip[i : i+4]) | ||
| binary.LittleEndian.PutUint32(ip[i:i+4], v) | ||
| } |
There was a problem hiding this comment.
For clarity and potentially a minor performance improvement, you can perform the byte-swapping directly. This makes the 4-byte reversal explicit and removes the need for the temporary variable v.
| var v uint32 | |
| for i := 0; i < len(ip); i += 4 { | |
| for i := 0; i < col/2; i += 4 { | |
| v = binary.BigEndian.Uint32(ip[i : i+4]) | |
| binary.LittleEndian.PutUint32(ip[i:i+4], v) | |
| } | |
| // The IP address in /proc/net/tcp* is in little-endian format for each 32-bit word. | |
| // We need to swap the byte order of each word to get the standard big-endian representation. | |
| for i := 0; i < col/2; i += 4 { | |
| ip[i], ip[i+1], ip[i+2], ip[i+3] = ip[i+3], ip[i+2], ip[i+1], ip[i] | |
| } |
⚡ Bolt: Zero-alloc optimization for proc/net decodeAddr
💡 What:
Replaced heap allocations (
make([]byte, ...)) with stack-allocated arrays ([16]byte) indecodeAddrfunction.🎯 Why:
The
decodeAddrfunction is called for every socket in/proc/net/tcp*files. Allocating slices for IP and port on every call creates unnecessary GC pressure.📊 Impact:
🔬 Measurement:
Ran benchmarks in
proc/bench_test.go(temporary file).Before:
BenchmarkDecodeAddr/IPv4-4 22240496 51.88 ns/op 4 B/op 1 allocs/op
BenchmarkDecodeAddr/IPv6-4 12897699 91.40 ns/op 16 B/op 1 allocs/op
After:
BenchmarkDecodeAddr/IPv4-4 43319718 29.62 ns/op 0 B/op 0 allocs/op
BenchmarkDecodeAddr/IPv6-4 19834668 61.09 ns/op 0 B/op 0 allocs/op
PR created automatically by Jules for task 8121588879501371504 started by @blue4209211