⚡ perf: Improve allocations for Request Params()#3766
⚡ perf: Improve allocations for Request Params()#3766gaby merged 5 commits intogofiber:mainfrom arturmelanchyk:prealloc
Conversation
WalkthroughAdds an early-return fast-path when there are no values in Request.Params() and Request.AllFormData(); otherwise both iterators allocate a single backing slice sized 2×vals and split it into k and v subslices to reduce temporary allocations. Tests refactored to exercise iterator-style callbacks for empty and populated cases. Changes
Sequence Diagram(s)sequenceDiagram
autonumber
participant C as Caller
participant R as Request.Params()/AllFormData()
C->>R: call iterator with callback
R->>R: compute vals
alt vals == 0
R-->>C: fast-path — do not invoke callback (return)
else vals > 0
R->>R: prealloc := make([]string, 2*vals)
R->>R: k := prealloc[:0:vals]
R->>R: v := prealloc[vals:vals:2*vals]
loop for i in 0..vals-1
R->>C: invoke callback(k[i], v[i])
end
end
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Possibly related PRs
Suggested reviewers
Poem
Pre-merge checks and finishing touches❌ Failed checks (2 warnings)
✅ Passed checks (1 passed)
✨ Finishing touches
🧪 Generate unit tests
📜 Recent review detailsConfiguration used: CodeRabbit UI Review profile: CHILL Plan: Pro 📒 Files selected for processing (1)
🧰 Additional context used📓 Path-based instructions (1)**/*.go📄 CodeRabbit inference engine (AGENTS.md)
Files:
🧬 Code graph analysis (1)client/request_test.go (1)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (3)
🔇 Additional comments (2)
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. Comment |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #3766 +/- ##
=======================================
Coverage 91.37% 91.38%
=======================================
Files 113 113
Lines 11874 11882 +8
=======================================
+ Hits 10850 10858 +8
Misses 755 755
Partials 269 269
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.
Actionable comments posted: 0
🧹 Nitpick comments (2)
client/request.go (2)
219-225: Skip work when there are no paramsMicro-optimization to avoid allocation and sort on empty input.
Apply this diff:
func (r *Request) Params() iter.Seq2[string, []string] { return func(yield func(string, []string) bool) { vals := r.params.Len() + if vals == 0 { + return + } prealloc := make([]string, 2*vals) p := pair{ k: prealloc[:0:vals], v: prealloc[vals : vals : 2*vals], }
456-461: Skip work when there is no form dataSame micro-optimization as Params.
Apply this diff:
func (r *Request) AllFormData() iter.Seq2[string, []string] { return func(yield func(string, []string) bool) { vals := r.formData.Len() + if vals == 0 { + return + } prealloc := make([]string, 2*vals) p := pair{ k: prealloc[:0:vals], v: prealloc[vals : vals : 2*vals], }
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
client/request.go(2 hunks)
🧰 Additional context used
📓 Path-based instructions (1)
**/*.go
📄 CodeRabbit inference engine (AGENTS.md)
**/*.go: Format Go code using gofumpt (enforced viamake format)
Ensure code passes golangci-lint checks (enforced viamake lint)
Optimize struct field alignment using betteralign (enforced viamake betteralign)
Modernize Go code using gopls modernize (enforced viamake modernize)
Files:
client/request.go
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (4)
- GitHub Check: unit (1.25.x, macos-13)
- GitHub Check: repeated
- GitHub Check: unit (1.25.x, windows-latest)
- GitHub Check: Compare
🔇 Additional comments (4)
client/request.go (4)
221-225: Prealloc for paired slices is correct and beneficialUsing a single backing array and three-index slicing for k and v reduces allocations while keeping capacities aligned. Nice win.
457-461: Same solid prealloc pattern for form dataMirrors the params optimization; good use of shared backing array and capacities.
221-225: No action needed:Args.Len()equals the number of pairs yielded byAll(), so the prealloc optimization remains valid
233-240: Ensure minimum Go version ≥1.21 for integer range loops
for i := range valsrelies on range-over-int added in Go 1.21; update your go.mod or CI to target Go 1.21+ to avoid build failures.
Description
Request::Params()benchstat old-params.txt new-params.txt goos: darwin goarch: arm64 pkg: github.com/gofiber/fiber/v3/client cpu: Apple M3 Max │ old-params.txt │ new-params.txt │ │ sec/op │ sec/op vs base │ _Request_Params 151.6n ± 0% 139.3n ± 11% -8.11% (p=0.000 n=20) │ old-params.txt │ new-params.txt │ │ B/op │ B/op vs base │ _Request_Params 184.0 ± 0% 184.0 ± 0% ~ (p=1.000 n=20) ¹ ¹ all samples are equal │ old-params.txt │ new-params.txt │ │ allocs/op │ allocs/op vs base │ _Request_Params 6.000 ± 0% 5.000 ± 0% -16.67% (p=0.000 n=20)Request::AllFormData()benchstat old-allformdata.txt new-allformdata.txt goos: darwin goarch: arm64 pkg: github.com/gofiber/fiber/v3/client cpu: Apple M3 Max │ old-allformdata.txt │ new-allformdata.txt │ │ sec/op │ sec/op vs base │ _Request_AllFormData 151.2n ± 1% 142.4n ± 2% -5.79% (p=0.000 n=20) │ old-allformdata.txt │ new-allformdata.txt │ │ B/op │ B/op vs base │ _Request_AllFormData 184.0 ± 0% 184.0 ± 0% ~ (p=1.000 n=20) ¹ ¹ all samples are equal │ old-allformdata.txt │ new-allformdata.txt │ │ allocs/op │ allocs/op vs base │ _Request_AllFormData 6.000 ± 0% 5.000 ± 0% -16.67% (p=0.000 n=20)Changes introduced
List the new features or adjustments introduced in this pull request. Provide details on benchmarks, documentation updates, changelog entries, and if applicable, the migration guide.
Type of change
Please delete options that are not relevant.
Checklist
Before you submit your pull request, please make sure you meet these requirements:
/docs/directory for Fiber's documentation.Commit formatting
Please use emojis in commit messages for an easy way to identify the purpose or intention of a commit. Check out the emoji cheatsheet here: CONTRIBUTING.md