optimize(nets): use netip.Addr as map key in CompareIpByte#1697
optimize(nets): use netip.Addr as map key in CompareIpByte#1697itvi-1234 wants to merge 1 commit into
Conversation
|
[APPROVALNOTIFIER] This PR is NOT APPROVED This pull-request has been approved by: The full list of commands accepted by this bot can be found here. DetailsNeeds approval from an approver in each of these files:Approvers can indicate their approval by writing |
Summary of ChangesHello, 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 optimizes the CompareIpByte function within the pkg/nets package by switching from string-based map keys to native netip.Addr types. This change improves performance by eliminating unnecessary string allocations and leveraging Go's efficient struct-based map key comparisons, while also improving error handling for invalid IP inputs. Highlights
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 the 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 counterproductive. 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. In maps where strings once held the sway, / We cast the heavy types away. / With netip.Addr, lean and fast, / The allocations do not last. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request refactors the CompareIpByte function in pkg/nets/nets.go to use netip.Addr as the map key for improved performance and robustness, while also adding error handling for invalid IP slices. The reviewer suggested further optimizing the map by using struct{} as the value type and pre-allocating the map capacity to enhance memory efficiency.
8a082f5 to
e8734ce
Compare
There was a problem hiding this comment.
Pull request overview
This PR optimizes pkg/nets.CompareIpByte by switching the internal set representation from string keys (via ip.String()) to netip.Addr keys, reducing allocations and hashing overhead during IP comparisons.
Changes:
- Replaced
map[string]...withmap[netip.Addr]...for membership checks inCompareIpByte. - Added
continuehandling whennetip.AddrFromSlicefails, avoiding use of invalid addresses as keys. - Removed a resolved TODO comment near
CompareIpByte.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Signed-off-by: itvi-1234 <rjsumit71@gmail.com>
e8734ce to
bec710c
Compare
|
Hi @LiZhenCheng9527 @yashisrani , Could you please review this PR whenever have some time |
Description
This PR optimizes the
CompareIpBytefunction in thepkg/netspackage.SUmmary
The previous implementation converted
[][]byteIP addresses into strings to use them as map keys. This approach was suboptimal due to:ip.String()call creates a new string allocation.netip.Addrvalue type.By switching to
map[netip.Addr][]byte, we leverage Go's efficient handling of thenetip.Addrstruct as a map key, which is designed for performance and zero-allocation comparisons.Changes
CompareIpByteinpkg/nets/nets.goto usenetip.Addras the map key.continuelogic for cases where IP parsing fails to ensure robustness.TODOcomment:// TODO: Optimising functions to be able to handle different data types.Verification Results
Verified the fix by running existing unit tests:
go test -v pkg/nets/nets_test.go pkg/nets/nets.goOutput: