Skip to content

optimize(nets): use netip.Addr as map key in CompareIpByte#1697

Open
itvi-1234 wants to merge 1 commit into
kmesh-net:mainfrom
itvi-1234:optimize-compare-ip-byte
Open

optimize(nets): use netip.Addr as map key in CompareIpByte#1697
itvi-1234 wants to merge 1 commit into
kmesh-net:mainfrom
itvi-1234:optimize-compare-ip-byte

Conversation

@itvi-1234
Copy link
Copy Markdown

Description

This PR optimizes the CompareIpByte function in the pkg/nets package.

SUmmary

The previous implementation converted [][]byte IP addresses into strings to use them as map keys. This approach was suboptimal due to:

  1. Unnecessary Allocations: Every ip.String() call creates a new string allocation.
  2. Computational Overhead: String formatting and hashing are more expensive than using the native netip.Addr value type.

By switching to map[netip.Addr][]byte, we leverage Go's efficient handling of the netip.Addr struct as a map key, which is designed for performance and zero-allocation comparisons.

Changes

  • Modified CompareIpByte in pkg/nets/nets.go to use netip.Addr as the map key.
  • Added continue logic for cases where IP parsing fails to ensure robustness.
  • Removed the resolved TODO comment: // 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.go

Output:

image

Copilot AI review requested due to automatic review settings May 13, 2026 14:30
@kmesh-bot
Copy link
Copy Markdown
Collaborator

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by:
Once this PR has been reviewed and has the lgtm label, please assign lizhencheng9527 for approval. For more information see the Kubernetes Code Review Process.

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@gemini-code-assist
Copy link
Copy Markdown

Summary of Changes

Hello, 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

  • Performance Optimization: Refactored the CompareIpByte function to use netip.Addr as a map key instead of string conversion, reducing memory allocations and computational overhead.
  • Robustness Improvements: Added continue statements to handle invalid IP parsing cases gracefully, preventing potential errors during comparison.
  • Code Cleanup: Removed an outdated TODO comment regarding data type handling.
Using Gemini Code Assist

The 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 /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

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 .gemini/ folder in the base of the repository. Detailed instructions can be found here.

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

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

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

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.

Comment thread pkg/nets/nets.go Outdated
Comment thread pkg/nets/nets.go Outdated
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

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]... with map[netip.Addr]... for membership checks in CompareIpByte.
  • Added continue handling when netip.AddrFromSlice fails, 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.

Comment thread pkg/nets/nets.go
Comment thread pkg/nets/nets.go Outdated
Comment thread pkg/nets/nets.go Outdated
Comment thread pkg/nets/nets.go Outdated
Signed-off-by: itvi-1234 <rjsumit71@gmail.com>
@itvi-1234 itvi-1234 force-pushed the optimize-compare-ip-byte branch from e8734ce to bec710c Compare May 13, 2026 14:50
@itvi-1234
Copy link
Copy Markdown
Author

Hi @LiZhenCheng9527 @yashisrani , Could you please review this PR whenever have some time

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants