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: Improve Performance of c.Body() by 125% #3090

Merged
merged 2 commits into from
Jul 25, 2024

Conversation

gaby
Copy link
Member

@gaby gaby commented Jul 25, 2024

Description

  • Improved performance of Body() across all cases by using the Content-Encoding from the request instead of iterating through all headers.
    • I found this improvement by accident while adding tests for BodyRaw().
  • Added missing unit-tests for BodyRaw().
  • Added missing benchmarks for BodyRaw().

Summary for Body()

Before

  • Iterations: Approximately 124-126 million iterations
  • Time per Operation: Around 9.498 to 9.529 nanoseconds per operation

After

  • Iterations: Approximately 281-289 million iterations
  • Time per Operation: Around 4.149 to 4.202 nanoseconds per operation

Benchmark from Branch vs Main

image

Changes introduced

  • Benchmarks: Describe any performance benchmarks and improvements related to the changes.

Type of change

  • Enhancement (improvement to existing features and functionality)
  • Performance improvement (non-breaking change which improves efficiency)

@gaby gaby requested a review from a team as a code owner July 25, 2024 04:04
@gaby gaby requested review from sixcolors, ReneWerner87 and efectn and removed request for a team July 25, 2024 04:04
Copy link
Contributor

coderabbitai bot commented Jul 25, 2024

Walkthrough

The changes enhance the Body method in the DefaultCtx struct by simplifying the retrieval and processing of the Content-Encoding header. The new implementation directly accesses this header and optimizes how request bodies are managed based on encoding. Additionally, two new test functions were introduced to ensure comprehensive testing of the BodyRaw() method and its performance. Overall, these modifications boost both code readability and efficiency.

Changes

Files Change Summary
ctx.go, ctx_test.go Updated Body method to streamline Content-Encoding handling. Added Test_Ctx_BodyRaw and Benchmark_Ctx_BodyRaw for testing raw body functionality.

Poem

In the meadow where rabbits hop,
Changes made, we’ll never stop!
Bodies handled, clean and bright,
Code now dances, pure delight!
With tests to prove the tweaks are right,
Let’s celebrate this coding flight! 🐇✨


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.
    • @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 as 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.

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

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.

@gaby gaby changed the title v3: Improve Performance of c.Body() by 120% v3: Improve Performance of c.Body() by 125% Jul 25, 2024
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 details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between fadedcb and 57d31cc.

Files selected for processing (2)
  • ctx.go (1 hunks)
  • ctx_test.go (2 hunks)
Additional comments not posted (5)
ctx.go (3)

342-343: Improvement: Direct retrieval of Content-Encoding header.

The direct retrieval of the Content-Encoding header using c.Request().Header.ContentEncoding() improves performance by eliminating the need to iterate through all headers.


345-351: Improvement: Streamlined handling for no encoding.

The handling of cases where no encoding is provided is now more efficient. The code checks if headerEncoding is empty and returns the request body accordingly, considering the immutable configuration.


Line range hint 353-361:
Improvement: Decoding the body based on encoding.

The use of tryDecodeBodyInOrder to decode the body based on the encoding is a robust approach. It ensures that the body is correctly decoded and handles errors appropriately. The restoration of the original body if decoding is performed is also a good practice.

ctx_test.go (2)

359-367: LGTM!

The Test_Ctx_BodyRaw function correctly tests the BodyRaw() method by setting the request body to a raw byte slice and verifying the output.


387-401: LGTM!

The Benchmark_Ctx_BodyRaw function correctly benchmarks the BodyRaw() method by measuring the time taken to repeatedly call the method and reporting allocations.

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: 70b808f Previous: fadedcb Ratio
Benchmark_Cache_AppendMsgitem - MB/s 1813.26 MB/s 961.87 MB/s 1.89
Benchmark_Compress_Levels/Zstd_LevelDefault - B/op 1 B/op 0 B/op +∞

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

@gaby
Copy link
Member Author

gaby commented Jul 25, 2024

The Benchmark_Cache_AppendMsgitem benchmark keeps failing because we are using random data which is probably affected by the entropy in the GitHub runner:

middleware/cache/manager_msgp_test.go

v := item{}
bts := make([]byte, 0, v.Msgsize())

Copy link

codecov bot commented Jul 25, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 83.60%. Comparing base (fadedcb) to head (70b808f).

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #3090      +/-   ##
==========================================
- Coverage   83.67%   83.60%   -0.07%     
==========================================
  Files         115      115              
  Lines        8342     8344       +2     
==========================================
- Hits         6980     6976       -4     
- Misses       1040     1043       +3     
- Partials      322      325       +3     
Flag Coverage Δ
unittests 83.60% <100.00%> (-0.07%) ⬇️

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

@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 details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 57d31cc and 70b808f.

Files selected for processing (1)
  • ctx_test.go (2 hunks)
Files skipped from review as they are similar to previous changes (1)
  • ctx_test.go

@ReneWerner87 ReneWerner87 added this to the v3 milestone Jul 25, 2024
@ReneWerner87 ReneWerner87 merged commit 0592e01 into main Jul 25, 2024
14 of 16 checks passed
@gaby gaby deleted the improve-body-performance branch July 25, 2024 12: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