Skip to content

🧹 chore: Improve RemoveEscapeChar performance #3496

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

Closed
wants to merge 2 commits into from

Conversation

gaby
Copy link
Member

@gaby gaby commented May 31, 2025

Summary

  • avoid allocations in RemoveEscapeChar.
  • return early if escape character is not found.
  • start loop at first location of escape character.
  • add missing unit-test and benchmarks for RemoveEscapeCharBytes()

Note:

  • Applying the same improvement to RemoveEscapeCharBytes() resulted in slower performance

@gaby gaby added the codex label May 31, 2025 — with ChatGPT Connector
Copy link
Contributor

coderabbitai bot commented May 31, 2025

Important

Review skipped

Draft detected.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ 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.
    • Explain this complex logic.
    • 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 explain this code block.
    • @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 explain its main purpose.
    • @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.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

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 generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this 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.

@gaby gaby changed the title Improve RemoveEscapeChar performance 🧹 chore: Improve RemoveEscapeChar performance May 31, 2025
@gaby gaby added this to v3 May 31, 2025
@gaby gaby removed the 🧹 Updates label May 31, 2025
@gaby gaby added this to the v3 milestone May 31, 2025
Copy link

codecov bot commented May 31, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 90.97%. Comparing base (e9e0367) to head (2a1d0a5).
Report is 21 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #3496   +/-   ##
=======================================
  Coverage   90.97%   90.97%           
=======================================
  Files         110      110           
  Lines       11064    11072    +8     
=======================================
+ Hits        10065    10073    +8     
  Misses        745      745           
  Partials      254      254           
Flag Coverage Δ
unittests 90.97% <100.00%> (+<0.01%) ⬆️

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.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@gaby
Copy link
Member Author

gaby commented May 31, 2025

New vs Old:

ubuntu@ubuntu:~/Desktop/git/v3-fiber/fiber$ go test -benchmem -run=^$ -bench ^Benchmark_Utils_RemoveEscapeChar_Extended$ github.com/gofiber/fiber/v3 -count=4
goos: linux
goarch: amd64
pkg: github.com/gofiber/fiber/v3
cpu: AMD Ryzen 7 7800X3D 8-Core Processor           
Benchmark_Utils_RemoveEscapeChar_Extended/simple-4         	56217482	        20.90 ns/op	      16 B/op	       1 allocs/op
Benchmark_Utils_RemoveEscapeChar_Extended/simple-4         	53135635	        20.92 ns/op	      16 B/op	       1 allocs/op
Benchmark_Utils_RemoveEscapeChar_Extended/simple-4         	57235556	        20.98 ns/op	      16 B/op	       1 allocs/op
Benchmark_Utils_RemoveEscapeChar_Extended/simple-4         	61021663	        20.90 ns/op	      16 B/op	       1 allocs/op
Benchmark_Utils_RemoveEscapeChar_Extended/no_escape-4      	349460665	         3.415 ns/op	       0 B/op	       0 allocs/op
Benchmark_Utils_RemoveEscapeChar_Extended/no_escape-4      	357582379	         3.343 ns/op	       0 B/op	       0 allocs/op
Benchmark_Utils_RemoveEscapeChar_Extended/no_escape-4      	359267046	         3.331 ns/op	       0 B/op	       0 allocs/op
Benchmark_Utils_RemoveEscapeChar_Extended/no_escape-4      	358141676	         3.348 ns/op	       0 B/op	       0 allocs/op
Benchmark_Utils_RemoveEscapeChar_Extended/multiple_escapes-4         	45769770	        23.66 ns/op	      16 B/op	       1 allocs/op
Benchmark_Utils_RemoveEscapeChar_Extended/multiple_escapes-4         	49362645	        23.47 ns/op	      16 B/op	       1 allocs/op
Benchmark_Utils_RemoveEscapeChar_Extended/multiple_escapes-4         	51237378	        23.55 ns/op	      16 B/op	       1 allocs/op
Benchmark_Utils_RemoveEscapeChar_Extended/multiple_escapes-4         	50928654	        23.22 ns/op	      16 B/op	       1 allocs/op
Benchmark_Utils_RemoveEscapeChar_Extended/leading_escape-4           	62444232	        18.88 ns/op	       8 B/op	       1 allocs/op
Benchmark_Utils_RemoveEscapeChar_Extended/leading_escape-4           	57102145	        18.98 ns/op	       8 B/op	       1 allocs/op
Benchmark_Utils_RemoveEscapeChar_Extended/leading_escape-4           	57521332	        19.12 ns/op	       8 B/op	       1 allocs/op
Benchmark_Utils_RemoveEscapeChar_Extended/leading_escape-4           	63135105	        19.02 ns/op	       8 B/op	       1 allocs/op
Benchmark_Utils_RemoveEscapeChar_Extended/trailing_escape-4          	59377788	        19.66 ns/op	      16 B/op	       1 allocs/op
Benchmark_Utils_RemoveEscapeChar_Extended/trailing_escape-4          	58263765	        19.67 ns/op	      16 B/op	       1 allocs/op
Benchmark_Utils_RemoveEscapeChar_Extended/trailing_escape-4          	61408718	        19.86 ns/op	      16 B/op	       1 allocs/op
Benchmark_Utils_RemoveEscapeChar_Extended/trailing_escape-4          	61223427	        19.84 ns/op	      16 B/op	       1 allocs/op
Benchmark_Utils_RemoveEscapeChar_Extended/only_escape_char-4         	168337048	         7.092 ns/op	       0 B/op	       0 allocs/op
Benchmark_Utils_RemoveEscapeChar_Extended/only_escape_char-4         	172812284	         6.985 ns/op	       0 B/op	       0 allocs/op
Benchmark_Utils_RemoveEscapeChar_Extended/only_escape_char-4         	170908411	         6.982 ns/op	       0 B/op	       0 allocs/op
Benchmark_Utils_RemoveEscapeChar_Extended/only_escape_char-4         	171351502	         6.976 ns/op	       0 B/op	       0 allocs/op
PASS
ok  	github.com/gofiber/fiber/v3	28.308s
ubuntu@ubuntu:~/Desktop/git/v3-fiber/fiber$ go test -benchmem -run=^$ -bench ^Benchmark_Utils_RemoveEscapeChar_Extended$ github.com/gofiber/fiber/v3 -count=4
goos: linux
goarch: amd64
pkg: github.com/gofiber/fiber/v3
cpu: AMD Ryzen 7 7800X3D 8-Core Processor           
Benchmark_Utils_RemoveEscapeChar_Extended/simple-4         	51089068	        22.03 ns/op	      16 B/op	       1 allocs/op
Benchmark_Utils_RemoveEscapeChar_Extended/simple-4         	52090060	        22.09 ns/op	      16 B/op	       1 allocs/op
Benchmark_Utils_RemoveEscapeChar_Extended/simple-4         	57271838	        22.04 ns/op	      16 B/op	       1 allocs/op
Benchmark_Utils_RemoveEscapeChar_Extended/simple-4         	56910784	        22.08 ns/op	      16 B/op	       1 allocs/op
Benchmark_Utils_RemoveEscapeChar_Extended/no_escape-4      	51142645	        22.20 ns/op	      16 B/op	       1 allocs/op
Benchmark_Utils_RemoveEscapeChar_Extended/no_escape-4      	50447330	        22.15 ns/op	      16 B/op	       1 allocs/op
Benchmark_Utils_RemoveEscapeChar_Extended/no_escape-4      	54730243	        21.97 ns/op	      16 B/op	       1 allocs/op
Benchmark_Utils_RemoveEscapeChar_Extended/no_escape-4      	56707700	        22.13 ns/op	      16 B/op	       1 allocs/op
Benchmark_Utils_RemoveEscapeChar_Extended/multiple_escapes-4         	49996170	        22.90 ns/op	      16 B/op	       1 allocs/op
Benchmark_Utils_RemoveEscapeChar_Extended/multiple_escapes-4         	49015768	        23.15 ns/op	      16 B/op	       1 allocs/op
Benchmark_Utils_RemoveEscapeChar_Extended/multiple_escapes-4         	55445076	        23.18 ns/op	      16 B/op	       1 allocs/op
Benchmark_Utils_RemoveEscapeChar_Extended/multiple_escapes-4         	50457178	        22.93 ns/op	      16 B/op	       1 allocs/op
Benchmark_Utils_RemoveEscapeChar_Extended/leading_escape-4           	70829822	        17.15 ns/op	       8 B/op	       1 allocs/op
Benchmark_Utils_RemoveEscapeChar_Extended/leading_escape-4           	68588857	        16.97 ns/op	       8 B/op	       1 allocs/op
Benchmark_Utils_RemoveEscapeChar_Extended/leading_escape-4           	71230917	        16.98 ns/op	       8 B/op	       1 allocs/op
Benchmark_Utils_RemoveEscapeChar_Extended/leading_escape-4           	73361605	        17.04 ns/op	       8 B/op	       1 allocs/op
Benchmark_Utils_RemoveEscapeChar_Extended/trailing_escape-4          	55574439	        22.00 ns/op	      16 B/op	       1 allocs/op
Benchmark_Utils_RemoveEscapeChar_Extended/trailing_escape-4          	51498166	        21.95 ns/op	      16 B/op	       1 allocs/op
Benchmark_Utils_RemoveEscapeChar_Extended/trailing_escape-4          	56275039	        21.89 ns/op	      16 B/op	       1 allocs/op
Benchmark_Utils_RemoveEscapeChar_Extended/trailing_escape-4          	53727992	        22.05 ns/op	      16 B/op	       1 allocs/op
Benchmark_Utils_RemoveEscapeChar_Extended/only_escape_char-4         	210210759	         5.713 ns/op	       0 B/op	       0 allocs/op
Benchmark_Utils_RemoveEscapeChar_Extended/only_escape_char-4         	215446750	         5.567 ns/op	       0 B/op	       0 allocs/op
Benchmark_Utils_RemoveEscapeChar_Extended/only_escape_char-4         	215944836	         5.555 ns/op	       0 B/op	       0 allocs/op
Benchmark_Utils_RemoveEscapeChar_Extended/only_escape_char-4         	216903548	         5.549 ns/op	       0 B/op	       0 allocs/op
PASS
ok  	github.com/gofiber/fiber/v3	28.664s

@gaby gaby changed the title 🧹 chore: Improve RemoveEscapeChar performance 🧹 chore: Improve RemoveEscapeChar/RemoveEscapeCharBytes performance May 31, 2025
@gaby gaby changed the title 🧹 chore: Improve RemoveEscapeChar/RemoveEscapeCharBytes performance 🧹 chore: Improve RemoveEscapeChar performance May 31, 2025
@efectn efectn moved this to In Progress in v3 Jun 25, 2025
@ReneWerner87
Copy link
Member

@gaby what happend to "Benchmark_Utils_RemoveEscapeChar_Extended/no_escape" ? its from 3 to 22ns

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: 2a1d0a5 Previous: e9e0367 Ratio
BenchmarkAppendMsgitem-4_middleware_csrf 0.623 ns/op 1605.26 MB/s 0 B/op 0 allocs/op 0.3121 ns/op 3203.98 MB/s 0 B/op 0 allocs/op 2.00
BenchmarkAppendMsgitem-4_middleware_csrf - ns/op 0.623 ns/op 0.3121 ns/op 2.00
BenchmarkAppendMsgstorageManager - MB/s 3203 MB/s 1588.2 MB/s 2.02
BenchmarkAppendMsgdata - MB/s 3205.31 MB/s 1603.21 MB/s 2.00

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

@gaby gaby closed this Jul 17, 2025
@github-project-automation github-project-automation bot moved this from In Progress to Done in v3 Jul 17, 2025
@gaby gaby deleted the 1q3h0e-codex/2025-05-31 branch July 17, 2025 12:22
@gaby
Copy link
Member Author

gaby commented Jul 17, 2025

Will address this later. Branch changed are out of sync

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