🧹 chore: Use sync.Pool for form and multipart binding#3965
Conversation
|
Note Other AI code review bot(s) detectedCodeRabbit has detected other AI code review bot(s) in this pull request and will avoid duplicating their findings in the review comments. This may lead to a less comprehensive review. WalkthroughThe PR introduces object pooling for form data maps in the binder package to reduce memory allocations during form binding operations. Maps are acquired from pools before use and released after, with proper clearing to prevent cross-request data leakage. Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Possibly related PRs
Suggested labels
Suggested reviewers
Poem
Pre-merge checks and finishing touches❌ Failed checks (1 warning, 1 inconclusive)
✅ Passed checks (1 passed)
✨ Finishing touches
🧪 Generate unit tests (beta)
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 |
Summary of ChangesHello @gaby, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request implements an optimization to the form binding mechanism by introducing object pooling for the internal maps used to store form values and multipart file headers. By reusing these maps across requests, the change aims to reduce memory allocations and garbage collector pressure, thereby improving the overall performance of the binder. The update ensures that all pooled maps are properly cleared before reuse to maintain data integrity and prevent unintended data leakage between different binding operations. Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #3965 +/- ##
==========================================
- Coverage 91.67% 91.60% -0.08%
==========================================
Files 119 119
Lines 10177 10227 +50
==========================================
+ Hits 9330 9368 +38
- Misses 536 544 +8
- Partials 311 315 +4
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.
Code Review
This pull request introduces sync.Pool for form value maps and multipart file headers to reduce memory allocations, which is a good performance optimization. The changes are accompanied by regression tests to ensure maps are cleared between requests.
My review identified a critical issue where a map is returned to the wrong pool, which would lead to memory leaks and negate some of the pooling benefits. I've also suggested a few improvements for maintainability and minor performance gains, such as using the clear() built-in function and removing redundant map clearing operations.
Overall, this is a valuable change once the critical issue is addressed.
|
|
||
| func releaseFileHeaderMap(m map[string][]*multipart.FileHeader) { | ||
| clearFileHeaderMap(m) | ||
| formFileMapPool.Put(m) |
There was a problem hiding this comment.
There appears to be a copy-paste error. The fileHeaderMap is being put into formMapPool instead of formFileMapPool. This will cause the type assertion to fail in acquireFormMap, negating the pooling benefit for form maps, and will cause a memory leak as file header maps are never returned to their correct pool.
| formFileMapPool.Put(m) | |
| formFileMapPool.Put(m) |
| if !ok { | ||
| m = make(map[string][]string) | ||
| } | ||
| clearFormMap(m) |
There was a problem hiding this comment.
The map is cleared in releaseFormMap before being returned to the pool. This additional clear upon acquisition is redundant. While it adds a layer of safety, it's generally sufficient to ensure resources are cleaned on release. Removing this would be a minor performance optimization, which aligns with the goal of this PR.
| if !ok { | ||
| m = make(map[string][]*multipart.FileHeader) | ||
| } | ||
| clearFileHeaderMap(m) |
There was a problem hiding this comment.
Pull request overview
This PR introduces memory pooling for form and multipart binding to reduce allocations during request processing. The implementation adds sync.Pool instances for form value maps and multipart file header maps, with acquire/release helper functions that ensure maps are properly cleared between uses.
Key Changes:
- Added pooled map storage for form values and multipart file headers using
sync.Pool - Modified binding methods to acquire maps from pools and release them after parsing
- Added regression tests confirming maps are properly cleared between requests
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| binder/form.go | Implements pool infrastructure with acquire/release/clear functions for form and file header maps; integrates pooling into Bind and bindMultipart methods |
| binder/form_test.go | Adds regression tests verifying pooled maps are cleared between requests for both urlencoded and multipart forms |
There was a problem hiding this comment.
Actionable comments posted: 0
🧹 Nitpick comments (1)
binder/form.go (1)
92-130: The double-clearing pattern ensures data isolation; considerclear()builtin for Go 1.21+.The belt-and-suspenders approach of clearing on both acquire and release is sound for preventing cross-request data leakage. The defensive type assertion fallback (lines 94-96, 108-110) is good practice.
If the minimum supported Go version is 1.21+, you could simplify
clearFormMapandclearFileHeaderMapusing theclear()builtin:func clearFormMap(m map[string][]string) { clear(m) }Otherwise, the current range-delete pattern is correct for broader compatibility. Note:
github.com/gofiber/utils/v2does not provide a map clearing helper, so the current approach aligns with standard Go patterns.
📜 Review details
Configuration used: Repository UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (2)
binder/form.gobinder/form_test.go
🧰 Additional context used
📓 Path-based instructions (2)
**/*_test.go
📄 CodeRabbit inference engine (AGENTS.md)
When adding Go tests, always invoke
t.Parallel()at the start of each test and subtest to maximize concurrency
Files:
binder/form_test.go
**/*.go
📄 CodeRabbit inference engine (AGENTS.md)
Prefer
github.com/gofiber/utils/v2helpers (for example,utils.Trim) when performing common operations such as string manipulation, whenever it is practical and appropriate for the surrounding code
Files:
binder/form_test.gobinder/form.go
🧠 Learnings (1)
📚 Learning: 2024-12-01T10:28:36.011Z
Learnt from: ReneWerner87
Repo: gofiber/fiber PR: 0
File: :0-0
Timestamp: 2024-12-01T10:28:36.011Z
Learning: Fiber currently supports square bracket notation in form data with 'application/x-www-form-urlencoded' content type, but this feature is not available for multipart form data. The implementation for form-urlencoded is in ctx.go (BodyParser method), while multipart form data handling needs enhancement to support this feature.
Applied to files:
binder/form.go
🧬 Code graph analysis (2)
binder/form_test.go (3)
binder/form.go (2)
FormBinding(27-29)FormBinding(32-34)client/request.go (2)
AcquireRequest(983-989)ReleaseRequest(993-996)bind.go (1)
Bind(33-37)
binder/form.go (1)
app.go (1)
New(522-635)
⏰ 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). (6)
- GitHub Check: Agent
- GitHub Check: lint
- GitHub Check: Compare
- GitHub Check: unit (1.25.x, macos-latest)
- GitHub Check: unit (1.25.x, windows-latest)
- GitHub Check: repeated
🔇 Additional comments (4)
binder/form.go (2)
43-54: Correct pool usage with defer.The
defer releaseFormMap(data)ensures the pooled map is returned to the pool on all code paths, including early error returns fromformatBindData.
64-84: Pool usage is consistent and correct for multipart handling.Both the form data map and file header map are properly acquired from pools and released via defer, ensuring cleanup on all code paths.
binder/form_test.go (2)
241-270: Well-structured regression test for URL-encoded form isolation.The test properly validates that pooled maps are cleared between requests, ensuring the second request doesn't inherit values from the first. Good use of
t.Parallel()per coding guidelines.
272-322: Comprehensive regression test for multipart form isolation.Excellent coverage of both value and file map isolation. The test ensures that file headers (Avatar) and form values (Name) from the first request don't bleed into the second request. Good use of
t.Parallel()per coding guidelines.
Summary
Related #3767