Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

v3: Optimize IsFromLocal() performance #3140

Merged
merged 1 commit into from
Sep 23, 2024
Merged

Conversation

gaby
Copy link
Member

@gaby gaby commented Sep 23, 2024

Description

  • Add Benchmarks for ctx.IsFromLocal()
  • Optimize performance of ctx.IsFromLocal() by using net.IP functions to detect if IP is from loopback address.
  • Reduce memory allocations by using net.IP directly instead of strings.

Benchmark Results:

Benchmark Before ns/op After ns/op Speedup Before Allocations After Allocations Improvement
Non_Localhost 26.45 7.51 ~3.52× faster 1 allocs/op 0 allocs/op Allocations Eliminated
Non_Localhost_Parallel 6.824 1.30 ~5.25× faster 1 allocs/op 0 allocs/op Allocations Eliminated
Localhost 26.48 7.52 ~3.52× faster 1 allocs/op 0 allocs/op Allocations Eliminated
Localhost_Parallel 6.974 1.31 ~5.33× faster 1 allocs/op 0 allocs/op Allocations Eliminated

Changes introduced

  • Benchmarks: Describe any performance benchmarks and improvements related to the changes.
  • API Longevity: Discuss the steps taken to ensure that the new or updated APIs are consistent and not prone to breaking changes.

Type of change

  • Performance improvement (non-breaking change which improves efficiency)

@gaby gaby added this to the v3 milestone Sep 23, 2024
@gaby gaby requested a review from a team as a code owner September 23, 2024 12:24
@gaby gaby requested review from sixcolors, ReneWerner87 and efectn and removed request for a team September 23, 2024 12:24
Copy link
Contributor

coderabbitai bot commented Sep 23, 2024

Walkthrough

The changes in this pull request involve the removal of the isLocalHost method from the DefaultCtx struct and the Ctx interface, simplifying the logic for determining local requests. The IsFromLocal method has been updated to directly use the IsLoopback method from fasthttp.RemoteIP(). Additionally, a new benchmarking function for IsFromLocal has been introduced in the tests, evaluating its performance under different scenarios.

Changes

File Change Summary
ctx.go Removed isLocalHost method; updated IsFromLocal method logic to use IsLoopback.
ctx_interface_gen.go Removed isLocalHost(address string) bool from Ctx interface.
ctx_test.go Added Benchmark_Ctx_IsFromLocalhost for performance testing of IsFromLocal method.

Possibly related PRs

Suggested reviewers

  • sixcolors
  • efectn
  • ReneWerner87

Poem

In the code where rabbits hop,
A method gone, but we won't stop.
With IsFromLocal now so bright,
Local checks are pure delight!
Performance blooms like springtime flowers,
Simplified logic, oh, what powers! 🐇✨


Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between fbc24e8 and 7b13bfe.

Files selected for processing (3)
  • ctx.go (1 hunks)
  • ctx_interface_gen.go (0 hunks)
  • ctx_test.go (1 hunks)
Files not reviewed due to no reviewable changes (1)
  • ctx_interface_gen.go
Additional comments not posted (3)
ctx.go (2)

1846-1848: LGTM!

The change to use fasthttp.RemoteIP().IsLoopback() directly simplifies the logic and improves efficiency while maintaining the same functionality.


Line range hint 1850-1859: Skipped review.

This function is unchanged in the diff, so no review is necessary.

ctx_test.go (1)

6355-6412: Well-structured and efficient benchmarking code.

This benchmark function for IsFromLocal() is well-implemented. It covers both localhost and non-localhost scenarios, with parallel and non-parallel versions. The code demonstrates good benchmarking practices such as:

  1. Using b.ReportAllocs() and b.ResetTimer() for accurate measurements.
  2. Properly implementing parallel benchmarks with b.RunParallel().
  3. Efficiently reusing app and context objects.
  4. Clear separation of different test scenarios.

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share
Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    -- I pushed a fix in commit <commit_id>, please review it.
    -- Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    -- @coderabbitai generate unit testing code for this file.
    -- @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    -- @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    -- @coderabbitai read src/utils.ts and generate unit testing code.
    -- @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    -- @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

codecov bot commented Sep 23, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 80.11%. Comparing base (fbc24e8) to head (7b13bfe).
Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #3140      +/-   ##
==========================================
- Coverage   80.19%   80.11%   -0.08%     
==========================================
  Files         117      117              
  Lines        9048     9043       -5     
==========================================
- Hits         7256     7245      -11     
- Misses       1360     1364       +4     
- Partials      432      434       +2     
Flag Coverage Δ
unittests 80.11% <100.00%> (-0.08%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Contributor

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

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

⚠️ Performance Alert ⚠️

Possible performance regression was detected for benchmark.
Benchmark result of this commit is worse than the previous benchmark result exceeding threshold 1.50.

Benchmark suite Current: 7b13bfe Previous: fbc24e8 Ratio
BenchmarkAppendMsgitem - MB/s 3209.82 MB/s 1847.42 MB/s 1.74
BenchmarkAppendMsgitem - MB/s 2878.85 MB/s 1847.42 MB/s 1.56

This comment was automatically generated by workflow using github-action-benchmark.

@gaby
Copy link
Member Author

gaby commented Sep 23, 2024

⚠️ Performance Alert ⚠️

Possible performance regression was detected for benchmark. Benchmark result of this commit is worse than the previous benchmark result exceeding threshold 1.50.

Benchmark suite Current: 7b13bfe Previous: fbc24e8 Ratio
BenchmarkAppendMsgitem - MB/s 3209.82 MB/s 1847.42 MB/s 1.74
BenchmarkAppendMsgitem - MB/s 2878.85 MB/s 1847.42 MB/s 1.56
This comment was automatically generated by workflow using github-action-benchmark.

This is related to this benchmark-action/github-action-benchmark#264

Copy link
Member

@ReneWerner87 ReneWerner87 left a comment

Choose a reason for hiding this comment

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

LGTM

@ReneWerner87 ReneWerner87 merged commit f8c514c into main Sep 23, 2024
16 of 18 checks passed
@gaby gaby deleted the optimize-isfromlocal branch September 23, 2024 14:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

2 participants