Skip to content

🧹 chore: pool form binder maps#3966

Closed
gaby wants to merge 1 commit into
mainfrom
add-sync.pool-instances-for-maps-yrl5g8
Closed

🧹 chore: pool form binder maps#3966
gaby wants to merge 1 commit into
mainfrom
add-sync.pool-instances-for-maps-yrl5g8

Conversation

@gaby
Copy link
Copy Markdown
Member

@gaby gaby commented Dec 24, 2025

Summary

  • reuse pooled maps for form values and multipart file headers in the form binder, clearing them on acquire and release to reduce allocations
  • update binding logic to return pooled maps after parsing while keeping multipart and urlencoded handling unchanged
  • add regression tests confirming pooled form and multipart maps are cleared between requests

Testing

  • make audit (fails: govulncheck reports standard library vulnerabilities in Go 1.25; see GO-2025-4010, GO-2025-4011, GO-2025-4012, GO-2025-4013, GO-2025-4007, GO-2025-4008, GO-2025-4155, GO-2025-4175)
  • make generate
  • make betteralign
  • make modernize
  • make format
  • make lint
  • make test

Codex Task

@gaby gaby requested a review from a team as a code owner December 24, 2025 13:23
Copilot AI review requested due to automatic review settings December 24, 2025 13:23
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Dec 24, 2025

Caution

Review failed

The pull request is closed.

Walkthrough

The changes introduce object pooling for form maps and file header maps in the form binder to reduce memory allocations. Maps are acquired from pools during binding operations and explicitly cleared before being returned to pools for reuse. Tests verify that state is properly cleared between successive requests.

Changes

Cohort / File(s) Summary
Core Pooling Implementation
binder/form.go
Introduces sync.Pool instances for form value maps and file header maps with acquire/release helpers (acquireFormMap, releaseFormMap, acquireFileHeaderMap, releaseFileHeaderMap). Integrates pooling into Bind and bindMultipart methods with explicit clearing helpers (clearFormMap, clearFileHeaderMap) to ensure maps are reset before reuse. Adds sync import for pool support.
Verification Tests
binder/form_test.go
Adds Test_FormBinder_Bind_MapClearedBetweenRequests to verify form maps are cleared between separate requests, and Test_FormBinder_BindMultipart_MapsClearedBetweenRequests to ensure multipart form fields and file header maps do not leak state across successive binding operations.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Possibly related PRs

  • 🧹 chore: Expand Binder tests coverage #3714: Both PRs modify binder/form.go's FormBinding.Bind implementation — the main PR adds map pools and acquire/release helpers while the retrieved PR changes error-handling control flow.
  • bind: add support for multipart file binding #3309: Both PRs modify binder/form.go (notably bindMultipart/Bind) and binder/form_test.go — the main PR adds pooled map reuse/clear logic while the retrieved PR adds multipart file-binding support, with overlapping changes in the same functions.

Suggested labels

🧹 Updates, v3, ⚡️ Performance

Suggested reviewers

  • sixcolors
  • ReneWerner87
  • efectn

Poem

🐰 Hops of joy through memory lanes,
Pools of maps now ease the strain!
Clear and reuse, a rabbit's dream,
Allocations? Gone! Just pristine.
Efficiency hops along!

✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch add-sync.pool-instances-for-maps-yrl5g8

📜 Recent review details

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 93830f2 and 30705b5.

📒 Files selected for processing (2)
  • binder/form.go
  • binder/form_test.go

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

Comment @coderabbitai help to get the list of available commands and usage tips.

@gemini-code-assist
Copy link
Copy Markdown
Contributor

Summary of Changes

Hello @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 significantly optimizes the form binding process by implementing object pooling for the maps used to store form values and multipart file headers. By reusing these map objects instead of allocating new ones for each request, the change aims to reduce memory pressure and garbage collection overhead, leading to improved performance. Comprehensive regression tests have been added to ensure the integrity of data binding and prevent any cross-request data contamination from the pooled objects.

Highlights

  • Memory Optimization: Introduced sync.Pool for map[string][]string (form values) and map[string][]*multipart.FileHeader (multipart file headers) to enable object pooling and reduce memory allocations.
  • Binding Logic Update: Modified the FormBinding.Bind and FormBinding.bindMultipart methods to acquire maps from these pools at the start of binding and release them back to the pools after processing, ensuring reuse.
  • Map Clearing Mechanism: Implemented helper functions (acquireFormMap, releaseFormMap, acquireFileHeaderMap, releaseFileHeaderMap, clearFormMap, clearFileHeaderMap) to manage the lifecycle of pooled maps, including clearing their contents before reuse to prevent data leakage.
  • Regression Tests: Added new tests (Test_FormBinder_Bind_MapClearedBetweenRequests and Test_FormBinder_BindMultipart_MapsClearedBetweenRequests) to explicitly verify that pooled maps are correctly cleared between requests, preventing unintended data persistence.

🧠 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 Assist

The 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 /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

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 .gemini/ folder in the base of the repository. Detailed instructions can be found here.

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

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@ReneWerner87 ReneWerner87 added this to the v3 milestone Dec 24, 2025
@ReneWerner87 ReneWerner87 added this to v3 Dec 24, 2025
@gaby gaby closed this Dec 24, 2025
@github-project-automation github-project-automation Bot moved this to Done in v3 Dec 24, 2025
@codecov
Copy link
Copy Markdown

codecov Bot commented Dec 24, 2025

Codecov Report

❌ Patch coverage is 88.88889% with 4 lines in your changes missing coverage. Please review.
✅ Project coverage is 91.65%. Comparing base (032ff5f) to head (30705b5).
⚠️ Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
binder/form.go 88.88% 2 Missing and 2 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #3966      +/-   ##
==========================================
- Coverage   91.67%   91.65%   -0.02%     
==========================================
  Files         119      119              
  Lines       10177    10227      +50     
==========================================
+ Hits         9330     9374      +44     
- Misses        536      539       +3     
- Partials      311      314       +3     
Flag Coverage Δ
unittests 91.65% <88.88%> (-0.02%) ⬇️

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.

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces sync.Pool to reuse maps for form values and multipart file headers, aiming to reduce memory allocations. The implementation is sound and includes regression tests to verify that pooled maps are cleared between requests. My review includes a few suggestions to improve the implementation by using the clear() built-in function (available since Go 1.21) and removing a redundant map clearing operation to make the code more efficient and maintainable.

Comment thread binder/form.go
Comment on lines +101 to +104
func releaseFormMap(m map[string][]string) {
clearFormMap(m)
formMapPool.Put(m)
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The acquireFormMap function already clears the map before it's used. Clearing it again here in releaseFormMap before putting it back in the pool is redundant. To avoid this duplicate work, it's best to stick to one pattern. Clearing on acquisition is generally safer, so I recommend removing the clear operation from this release function.

func releaseFormMap(m map[string][]string) {
	formMapPool.Put(m)
}

Comment thread binder/form.go
Comment on lines +115 to +118
func releaseFileHeaderMap(m map[string][]*multipart.FileHeader) {
clearFileHeaderMap(m)
formFileMapPool.Put(m)
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Similar to releaseFormMap, the acquireFileHeaderMap function already handles clearing the map. To avoid redundant work, the call to clearFileHeaderMap can be removed from this release function.

func releaseFileHeaderMap(m map[string][]*multipart.FileHeader) {
	formFileMapPool.Put(m)
}

Comment thread binder/form.go
Comment on lines +120 to +124
func clearFormMap(m map[string][]string) {
for k := range m {
delete(m, k)
}
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Since the project's Go version is 1.25.0, you can use the built-in clear() function to empty the map. This is more idiomatic and can be more efficient than iterating over the map to delete each key.

func clearFormMap(m map[string][]string) {
	clear(m)
}

Comment thread binder/form.go
Comment on lines +126 to +130
func clearFileHeaderMap(m map[string][]*multipart.FileHeader) {
for k := range m {
delete(m, k)
}
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Similar to clearFormMap, you can use the built-in clear() function here for better readability and potential performance improvement.

func clearFileHeaderMap(m map[string][]*multipart.FileHeader) {
	clear(m)
}

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR introduces memory pooling for form binding maps to reduce allocations during request processing. By reusing pooled map[string][]string and map[string][]*multipart.FileHeader instances, the binder avoids repeatedly allocating new maps for each request.

  • Adds sync.Pool instances for form value maps and multipart file header maps
  • Implements acquire/release helper functions with clearing logic to ensure maps are cleaned between uses
  • Includes regression tests verifying that pooled maps don't leak data between requests

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
binder/form.go Implements sync.Pool for form and file header maps with acquire/release/clear helper functions; updates Bind and bindMultipart methods to use pooled maps with proper cleanup via defer
binder/form_test.go Adds two regression tests to verify that pooled maps are properly cleared between sequential requests for both URL-encoded and multipart forms

Comment thread binder/form.go
Comment on lines +126 to +130
func clearFileHeaderMap(m map[string][]*multipart.FileHeader) {
for k := range m {
delete(m, k)
}
}
Copy link

Copilot AI Dec 24, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider using the built-in clear function instead of manually iterating and deleting keys. This would be more idiomatic and consistent with other parts of the codebase (e.g., client/request.go:822, ctx.go:633). The built-in clear function has been available since Go 1.21, and this project uses Go 1.25.0.

Copilot uses AI. Check for mistakes.
Comment thread binder/form.go
Comment on lines +120 to +124
func clearFormMap(m map[string][]string) {
for k := range m {
delete(m, k)
}
}
Copy link

Copilot AI Dec 24, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider using the built-in clear function instead of manually iterating and deleting keys. This would be more idiomatic and consistent with other parts of the codebase (e.g., client/request.go:822, ctx.go:633). The built-in clear function has been available since Go 1.21, and this project uses Go 1.25.0.

Copilot uses AI. Check for mistakes.
@ReneWerner87 ReneWerner87 deleted the add-sync.pool-instances-for-maps-yrl5g8 branch January 27, 2026 12:21
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.

3 participants