⚡ perf: optimize helpers performance#4049
Conversation
…-allocated arrays
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
There was a problem hiding this comment.
Code Review
This pull request introduces several performance optimizations aimed at reducing memory allocations, primarily by using sync.Pool for maps and stack-allocated arrays for slices. The changes are well-implemented and should provide a good performance boost in most cases. I've found one area in res.go where the optimization could lead to a performance regression under a specific configuration and have provided a suggestion to improve it. Overall, this is a solid contribution to improving the framework's performance.
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #4049 +/- ##
==========================================
+ Coverage 91.09% 91.10% +0.01%
==========================================
Files 119 119
Lines 11184 11203 +19
==========================================
+ Hits 10188 10207 +19
Misses 630 630
Partials 366 366
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
Pull request overview
This PR implements several targeted performance optimizations that collectively reduce execution time and memory allocations across critical code paths. The changes focus on eliminating unnecessary UTF-8 decoding in string operations, reusing slice capacity to reduce allocations, and optimizing case-insensitive string matching. Benchmark results show significant improvements: 26% faster string splitting, 70% faster cache directive detection, and 9% faster route tree rebuilding.
Changes:
- Optimized string iteration by replacing range loops with index-based byte access when only ASCII matching is needed
- Implemented slice capacity reuse in route tree rebuilding to reduce allocations
- Refactored cache directive parsing with inline case-folding and extracted helper functions for better performance
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| router.go | Added reuseRouteBucket function to reuse slice capacity when rebuilding route trees, reducing allocations in buildTree |
| helpers.go | Optimized getSplicedStrList with index-based loop and refactored isNoCache with inline case-folding, added tab as valid delimiter |
| binder/mapping.go | Optimized FilterFlags by replacing range loop with strings.IndexAny |
| binder/mapping_test.go | Added benchmark test for FilterFlags to track performance |
|
this pr almost have no necessary performance improvements |
Thanks for the assessment. If you have any changes that significantly improve performance, please feel free to create a pull request. |
Benchstat (base:
origin/main, head: this branch)Benchmark_Utils_GetSplicedStrList32.08ns -> 23.62ns(-26.37%,p=0.000)Benchmark_Utils_IsNoCache100.70ns -> 29.79ns(-70.42%,p=0.000)Benchmark_App_RebuildTree11.63µs -> 10.57µs(-9.14%,p=0.000)Benchmark_FilterFlags10.59ns -> 10.08ns