Skip to content

Improve performance for "equalFieldType" function #3479

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

Merged
merged 3 commits into from
May 27, 2025

Conversation

ReneWerner87
Copy link
Member

@ReneWerner87 ReneWerner87 commented May 26, 2025

OLD:

Benchmark_equalFieldType-12    	 3320424	       361.0 ns/op	      80 B/op	       9 allocs/op

NEW:

Benchmark_equalFieldType-12    	12170480	        99.85 ns/op	      16 B/op	       3 allocs/op

+ solve the problem with passing on the tag name

OLD:
```
Benchmark_equalFieldType-12    	 3320424	       361.0 ns/op	      80 B/op	       9 allocs/op
```

NEW:
```
Benchmark_equalFieldType-12    	12170480	        99.85 ns/op	      16 B/op	       3 allocs/op
```
OLD:
```
Benchmark_equalFieldType-12    	 3320424	       361.0 ns/op	      80 B/op	       9 allocs/op
```

NEW:
```
Benchmark_equalFieldType-12    	11102847	       102.2 ns/op	      16 B/op	       3 allocs/op
```
+
solve the problem with passing on the tag name
@ReneWerner87 ReneWerner87 added this to the v3 milestone May 26, 2025
@ReneWerner87 ReneWerner87 requested a review from a team as a code owner May 26, 2025 19:04
@ReneWerner87 ReneWerner87 requested review from gaby and sixcolors May 26, 2025 19:04
@ReneWerner87 ReneWerner87 added this to v3 May 26, 2025
@ReneWerner87 ReneWerner87 requested a review from efectn May 26, 2025 19:04
Copy link
Contributor

coderabbitai bot commented May 26, 2025

Walkthrough

This change refactors the data binding logic by updating the function signatures and internal calls to formatBindData, assignBindData, and equalFieldType to include an alias tag or binding name. It introduces caching and alias tag awareness for field matching, and updates related test cases and benchmarks accordingly.

Changes

File(s) Change Summary
binder/mapping.go Refactored field matching logic: added alias tag support, caching, new helpers, and updated function signatures for formatBindData, assignBindData, and equalFieldType.
binder/form.go, binder/cookie.go, binder/header.go, binder/query.go, binder/resp_header.go Updated calls to formatBindData in all binder types to pass the binding name as the first argument.
binder/mapping_test.go Updated tests to use new function signatures with alias tag/binding name; added Benchmark_equalFieldType.
state_test.go Reordered fields in test struct and updated test case literals to use explicit field names.

Sequence Diagram(s)

sequenceDiagram
    participant Binder as Binder (e.g., FormBinding)
    participant formatBindData
    participant equalFieldType
    participant FieldCache

    Binder->>formatBindData: formatBindData(bindingName, out, data, key, value, ...)
    formatBindData->>equalFieldType: equalFieldType(out, kind, key, bindingName)
    equalFieldType->>FieldCache: Check or build fieldInfo for type+aliasTag
    FieldCache-->>equalFieldType: Return field match result
    equalFieldType-->>formatBindData: true/false
    formatBindData-->>Binder: Assign or skip value
Loading

Possibly related PRs

  • gofiber/fiber#3478: Both PRs refactor equalFieldType, but this PR adds caching and alias tag support.
  • gofiber/fiber#3231: Both PRs modify binder methods and internal binding logic, updating function signatures for splitting and pooling.
  • gofiber/fiber#3235: This PR introduces the original formatBindData and assignBindData functions; the current PR builds upon and modifies these functions.

Suggested reviewers

  • gaby
  • sixcolors

Poem

In the warren of code, a cache was spun,
Tagging fields for every binding run.
With mapping neat and tests anew,
The rabbit hops—refactors through!
Now binding’s swift, with tags in tow,
To burrow faster, off we go! 🐇


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between a600d97 and 01dea71.

📒 Files selected for processing (1)
  • state_test.go (2 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • state_test.go
⏰ Context from checks skipped due to timeout of 90000ms (4)
  • GitHub Check: unit (1.24.x, windows-latest)
  • GitHub Check: unit (1.23.x, windows-latest)
  • GitHub Check: Compare
  • GitHub Check: repeated
✨ Finishing Touches
  • 📝 Generate Docstrings

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.

@ReneWerner87 ReneWerner87 requested a review from Copilot May 26, 2025 19:04
Copy link
Contributor

@Copilot 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.

Copilot encountered an error and was unable to review this pull request. You can try again by re-requesting a review.

Copy link

codecov bot commented May 26, 2025

Codecov Report

Attention: Patch coverage is 86.41975% with 11 lines in your changes missing coverage. Please review.

Project coverage is 84.36%. Comparing base (9bd7a1c) to head (01dea71).
Report is 3 commits behind head on main.

Files with missing lines Patch % Lines
binder/mapping.go 85.13% 8 Missing and 3 partials ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##             main    #3479   +/-   ##
=======================================
  Coverage   84.35%   84.36%           
=======================================
  Files         120      120           
  Lines       12190    12215   +25     
=======================================
+ Hits        10283    10305   +22     
- Misses       1477     1480    +3     
  Partials      430      430           
Flag Coverage Δ
unittests 84.36% <86.41%> (+<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.

OLD:
```
Benchmark_equalFieldType-12    	 3320424	       361.0 ns/op	      80 B/op	       9 allocs/op
```

NEW:
```
Benchmark_equalFieldType-12    	11102847	       102.2 ns/op	      16 B/op	       3 allocs/op
```
+
solve the problem with passing on the tag name
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 2

🧹 Nitpick comments (1)
binder/mapping.go (1)

222-223: Consider adding test coverage for edge cases.

Static analysis indicates several code paths lack test coverage:

  • Lines 222-223: uri cache case
  • Line 248: Nested struct field export check
  • Lines 263-264: String key map handling
  • Lines 279-280: Type assertion failure handling

Would you like me to help generate additional test cases to improve coverage for these edge cases?

Also applies to: 248-248, 263-264, 279-280

🧰 Tools
🪛 GitHub Check: codecov/patch

[warning] 222-223: binder/mapping.go#L222-L223
Added lines #L222 - L223 were not covered by tests

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 0c6283f and a600d97.

📒 Files selected for processing (8)
  • binder/cookie.go (1 hunks)
  • binder/form.go (2 hunks)
  • binder/header.go (1 hunks)
  • binder/mapping.go (4 hunks)
  • binder/mapping_test.go (13 hunks)
  • binder/query.go (1 hunks)
  • binder/resp_header.go (1 hunks)
  • state_test.go (2 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (2)
state_test.go (1)
state.go (1)
  • GetState (100-110)
binder/mapping_test.go (4)
bind_test.go (3)
  • User (1920-1927)
  • User (2046-2050)
  • User (2076-2082)
binder/form_test.go (4)
  • User (25-30)
  • User (65-69)
  • User (101-108)
  • User (205-209)
binder/cookie_test.go (2)
  • User (22-27)
  • User (60-64)
binder/header_test.go (2)
  • User (18-23)
  • User (60-64)
🪛 GitHub Actions: golangci-lint
state_test.go

[error] 448-448: golangci-lint: directive //nolint:govet // It does not really matter for test is unused for linter "govet" (nolintlint)

🪛 GitHub Check: codecov/patch
binder/mapping.go

[warning] 222-223: binder/mapping.go#L222-L223
Added lines #L222 - L223 were not covered by tests


[warning] 228-228: binder/mapping.go#L228
Added line #L228 was not covered by tests


[warning] 248-248: binder/mapping.go#L248
Added line #L248 was not covered by tests


[warning] 263-264: binder/mapping.go#L263-L264
Added lines #L263 - L264 were not covered by tests


[warning] 279-280: binder/mapping.go#L279-L280
Added lines #L279 - L280 were not covered by tests

⏰ Context from checks skipped due to timeout of 90000ms (5)
  • GitHub Check: Compare
  • GitHub Check: unit (1.24.x, macos-latest)
  • GitHub Check: unit (1.23.x, windows-latest)
  • GitHub Check: unit (1.24.x, windows-latest)
  • GitHub Check: repeated
🔇 Additional comments (13)
state_test.go (2)

451-452: Reorder and Add name and key Fields in testCase Struct

The testCase struct now includes name and key fields, positioned after expected. This aligns with their usage in runGenericTest, improving clarity and maintainability. Ensure the struct order matches all literal initializations.


472-473: Explicit Field Naming in Generic Test Cases

All testCase literals in TestState_GetGeneric have been updated to use named fields (name, key, value, expected, ok) instead of positional initialization. This eliminates ordering fragility and makes each test’s intent clear.

Also applies to: 477-478, 482-483, 487-488

binder/cookie.go (1)

30-30: LGTM! Consistent with alias tag awareness refactor.

The addition of b.Name() as the first argument to formatBindData aligns with the performance improvement goals mentioned in the PR objectives. This change enables alias tag awareness and caching optimizations.

binder/query.go (1)

30-30: LGTM! Consistent refactor pattern across binder types.

The change follows the same pattern as other binder implementations, adding the binding name as the first parameter to enable alias tag awareness and performance optimizations.

binder/form.go (3)

39-39: LGTM! Consistent with alias tag refactor.

The addition of b.Name() as the first argument maintains consistency with the performance optimization pattern across all binder types.


58-58: LGTM! Multipart form values handled consistently.

The multipart form value handling is updated with the same pattern, ensuring alias tag awareness is applied uniformly across all form binding scenarios.


66-66: LGTM! File header handling updated consistently.

The file header processing follows the same pattern, completing the consistent refactor across all form binding use cases.

binder/resp_header.go (1)

30-30: LGTM! Completes consistent refactor across all binder types.

The response header binder follows the same pattern as all other binding implementations, ensuring uniform alias tag awareness and performance benefits across the entire binding system.

binder/header.go (1)

29-29: LGTM!

The update correctly passes the binding name to formatBindData for alias tag-aware field matching.

binder/mapping_test.go (2)

16-49: Test updates look good!

All test function calls have been correctly updated to include the "query" alias tag parameter, maintaining consistency with the refactored function signatures.

Also applies to: 202-341


347-364: Great addition of the benchmark!

The benchmark effectively tests the performance of the new caching mechanism in equalFieldType with realistic scenarios including nested struct fields.

binder/mapping.go (2)

179-196: Helper functions are well-implemented!

The isStringKeyMap, isExported, and fieldName functions provide clean abstractions for the refactored logic.


258-290: Excellent performance optimization with caching!

The refactored equalFieldType function with caching and alias tag support aligns perfectly with the PR's performance improvement goals. The benchmark results in the PR description show ~3.6x speedup.

🧰 Tools
🪛 GitHub Check: codecov/patch

[warning] 263-264: binder/mapping.go#L263-L264
Added lines #L263 - L264 were not covered by tests


[warning] 279-280: binder/mapping.go#L279-L280
Added lines #L279 - L280 were not covered by tests

@ReneWerner87 ReneWerner87 merged commit da0aec0 into main May 27, 2025
18 checks passed
@github-project-automation github-project-automation bot moved this to Done in v3 May 27, 2025
@gaby gaby deleted the v3_improveEqualFieldType branch May 28, 2025 12:07
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.

1 participant