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

performance optimizations #2947

Merged
merged 6 commits into from Apr 5, 2024
Merged

performance optimizations #2947

merged 6 commits into from Apr 5, 2024

Conversation

ReneWerner87
Copy link
Member

@ReneWerner87 ReneWerner87 commented Mar 28, 2024

Summary by CodeRabbit

  • Refactor
    • Enhanced benchmarking capabilities with new functions for communication flow and context management.
    • Optimized context management by adjusting initialization and reset processes.
  • Chores
    • Streamlined context interface and default context implementation by removing redundant methods and properties.

Copy link
Contributor

coderabbitai bot commented Mar 28, 2024

Walkthrough

The update focuses on refining benchmarking and context management within the application. It eliminates outdated benchmarks and introduces new ones for precise performance analysis. Context handling is streamlined by adjusting when and how the context is reset, enhancing efficiency and maintainability.

Changes

File(s) Summary of Changes
app_test.go - Removed Benchmark_AcquireCtx
- Added Benchmark_Communication_Flow and Benchmark_Ctx_AcquireReleaseFlow
ctx_interface.go, DefaultCtx - Removed setReq method
- Moved field initializations to Reset method
- Adjusted release method to not reset certain fields
router.go - Removed context resetting line from requestHandler function

Possibly related issues

Poem

🐇 In the land of code and byte,
A rabbit hopped with all its might.
"Let's make our app swift and light,
Adjust, refine, and set it right."
🌟 With benchmarks new and context keen,
Our code now runs like it's on caffeine!
🚀 For efficiency, we've always been keen.

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>.
    • 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 generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @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.

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 as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger a review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration 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/coderabbit-overrides.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.

@ReneWerner87
Copy link
Member Author

ETAG bench on my machine
v2: 60ns
v3(main): 110ns

v3(current): 82ns

@ReneWerner87
Copy link
Member Author

not finished yet, I'm still looking for the missing 20ns

Copy link

codecov bot commented Mar 28, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 82.76%. Comparing base (6a7f015) to head (4a1443f).

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2947      +/-   ##
==========================================
- Coverage   82.78%   82.76%   -0.03%     
==========================================
  Files         116      116              
  Lines        8422     8411      -11     
==========================================
- Hits         6972     6961      -11     
  Misses       1110     1110              
  Partials      340      340              
Flag Coverage Δ
unittests 82.76% <100.00%> (-0.03%) ⬇️

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.

this will be used to make differences between version 2 and 3 directly visible
this will be used to show differences between version 2 and 3 directly
@ReneWerner87 ReneWerner87 marked this pull request as ready for review April 3, 2024 20:46
@ReneWerner87 ReneWerner87 requested a review from a team as a code owner April 3, 2024 20:46
@ReneWerner87 ReneWerner87 requested review from gaby, sixcolors and efectn and removed request for a team April 3, 2024 20:46
@ReneWerner87
Copy link
Member Author

ReneWerner87 commented Apr 3, 2024

this PR should fix the performance lost in the benchmarks
f.e.
image
from https://github.com/gofiber/fiber/actions/runs/8534697735/job/23379597810

| Benchmark suite | Current: 6a7f0158976cb54c2eba55ace2e1082a7d5cd60f | Previous: fd811cf84af282db8ec50adedce01a5886d5fd46 | Ratio |
|-|-|-|-|
| `Benchmark_Etag` | `200.3` ns/op	       0 B/op	       0 allocs/op | `97.79` ns/op	       0 B/op	       0 allocs/op | `2.05` |
| `Benchmark_Middleware_Favicon` | `212.9` ns/op	      12 B/op	       4 allocs/op | `89.1` ns/op	       3 B/op	       1 allocs/op | `2.39` |
| `Benchmark_Idempotency/skip` | `210.7` ns/op	       0 B/op	       0 allocs/op | `105` ns/op	       0 B/op	       0 allocs/op | `2.01` |

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 2

Review Status

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between dfe5c6f and 799df8e.
Files selected for processing (3)
  • app_test.go (2 hunks)
  • ctx_interface.go (4 hunks)
  • router.go (1 hunks)
Additional comments not posted (4)
router.go (1)

218-223: > 📝 NOTE

This review was outside the diff hunks, and no overlapping diff hunk was found. Original lines [442-455]

Consider verifying the impact of removing c.Reset(rctx) on context management and request handling to ensure that this optimization does not introduce any unintended side effects.

ctx_interface.go (3)

442-455: The removal of setReq from the Ctx interface simplifies context management. Ensure that no functionality is adversely affected by this change.

Verification successful

The search for any remaining references to setReq in the codebase did not produce any results, indicating that the removal of setReq from the Ctx interface has been thoroughly implemented with no lingering references. This supports the initial review comment about simplifying context management through the removal of setReq.

Scripts Executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Search for any remaining references to setReq in the codebase
rg --type go "setReq\("

Length of output: 25


442-455: Moving the initialization of indexRoute, indexHandler, matched, and baseURI to the Reset method could improve efficiency. Verify that context reuse is not negatively impacted by these changes.


442-455: The adjustments to the release method align with the changes to the Reset method. Confirm that the context is correctly reset upon release to prevent state leakage between requests.

app_test.go Show resolved Hide resolved
app_test.go Outdated Show resolved Hide resolved
@ReneWerner87
Copy link
Member Author

pkg: github.com/gofiber/fiber/v3/middleware/etag
Benchmark_Etag
Benchmark_Etag-12    	17534946	        69.83 ns/op	       0 B/op	       0 allocs/op
Benchmark_Etag-12    	17935286	        70.72 ns/op	       0 B/op	       0 allocs/op
Benchmark_Etag-12    	16889533	        69.57 ns/op	       0 B/op	       0 allocs/op
Benchmark_Etag-12    	16152487	        68.82 ns/op	       0 B/op	       0 allocs/op
Benchmark_Etag-12    	16907638	        68.04 ns/op	       0 B/op	       0 allocs/op
pkg: github.com/gofiber/fiber/v2/middleware/etag
Benchmark_Etag
Benchmark_Etag-12    	16412640	        61.38 ns/op	       0 B/op	       0 allocs/op
Benchmark_Etag-12    	20339643	        59.30 ns/op	       0 B/op	       0 allocs/op
Benchmark_Etag-12    	20296197	        61.29 ns/op	       0 B/op	       0 allocs/op
Benchmark_Etag-12    	18900700	        63.48 ns/op	       0 B/op	       0 allocs/op
Benchmark_Etag-12    	20087421	        61.59 ns/op	       0 B/op	       0 allocs/op
pkg: github.com/gofiber/fiber/v3/middleware/favicon
Benchmark_Middleware_Favicon
Benchmark_Middleware_Favicon-12    	20613506	        59.47 ns/op	       3 B/op	       1 allocs/op
Benchmark_Middleware_Favicon-12    	18785577	        57.69 ns/op	       3 B/op	       1 allocs/op
Benchmark_Middleware_Favicon-12    	19518568	        60.20 ns/op	       3 B/op	       1 allocs/op
Benchmark_Middleware_Favicon-12    	19779933	        60.08 ns/op	       3 B/op	       1 allocs/op
Benchmark_Middleware_Favicon-12    	19941021	        59.03 ns/op	       3 B/op	       1 allocs/op
pkg: github.com/gofiber/fiber/v2/middleware/favicon
Benchmark_Middleware_Favicon
Benchmark_Middleware_Favicon-12    	20951350	        48.58 ns/op	       3 B/op	       1 allocs/op
Benchmark_Middleware_Favicon-12    	25154551	        49.07 ns/op	       3 B/op	       1 allocs/op
Benchmark_Middleware_Favicon-12    	24965953	        49.23 ns/op	       3 B/op	       1 allocs/op
Benchmark_Middleware_Favicon-12    	24890929	        48.66 ns/op	       3 B/op	       1 allocs/op
Benchmark_Middleware_Favicon-12    	24523046	        49.49 ns/op	       3 B/op	       1 allocs/op
pkg: github.com/gofiber/fiber/v3/middleware/idempotency
Benchmark_Idempotency
Benchmark_Idempotency/skip
Benchmark_Idempotency/skip-12         	13999358	        77.70 ns/op	       0 B/op	       0 allocs/op
Benchmark_Idempotency/skip-12         	15908833	        77.75 ns/op	       0 B/op	       0 allocs/op
Benchmark_Idempotency/skip-12         	15722823	        77.30 ns/op	       0 B/op	       0 allocs/op
Benchmark_Idempotency/skip-12         	15650931	        80.33 ns/op	       0 B/op	       0 allocs/op
Benchmark_Idempotency/skip-12         	14185095	        82.41 ns/op	       0 B/op	       0 allocs/op
pkg: github.com/gofiber/fiber/v2/middleware/idempotency
Benchmark_Idempotency
Benchmark_Idempotency/skip
Benchmark_Idempotency/skip-12         	16240456	        61.87 ns/op	       0 B/op	       0 allocs/op
Benchmark_Idempotency/skip-12         	19814664	        61.26 ns/op	       0 B/op	       0 allocs/op
Benchmark_Idempotency/skip-12         	18448364	        62.77 ns/op	       0 B/op	       0 allocs/op
Benchmark_Idempotency/skip-12         	18232786	        60.44 ns/op	       0 B/op	       0 allocs/op
Benchmark_Idempotency/skip-12         	18487230	        61.32 ns/op	       0 B/op	       0 allocs/op

this will be used to show differences between version 2 and 3 directly
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

Review Status

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 799df8e and 73caafe.
Files selected for processing (1)
  • app_test.go (2 hunks)
Files skipped from review as they are similar to previous changes (1)
  • app_test.go

@ReneWerner87
Copy link
Member Author

somehow strange, the benchmarks still differ slightly in the middlewares

etag v3 70ns vs v2 61ns
favicon v3 60ns vs v2 50ns
idempotency v3 80ns vs v2 61ns

think because the flow works multiple times and the 4ns from the extra feature is added up in each case
maybe additionally some other functions utils functions which we have exchanged

@ReneWerner87 ReneWerner87 merged commit c8c51ee into main Apr 5, 2024
14 of 15 checks passed
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.

None yet

1 participant