⚡ perf: Improve performance of decoder and encoder#45
Conversation
…d improve struct handling
…d improve struct handling
Summary of ChangesHello @ReneWerner87, 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 focuses on enhancing the performance of the schema package through a series of targeted optimizations. It aims to minimize memory allocations, particularly for string operations and reflection-based field access, and to improve the overall efficiency of parsing and encoding processes. These changes contribute to a faster and more resource-friendly operation of the package. 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. Changelog
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 #45 +/- ##
==========================================
+ Coverage 96.97% 97.09% +0.12%
==========================================
Files 4 4
Lines 760 792 +32
==========================================
+ Hits 737 769 +32
Misses 12 12
Partials 11 11
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 several significant performance improvements. Key changes include replacing strings.Split with manual parsing to reduce allocations, using optimized functions from gofiber/utils, and enhancing struct metadata caching for faster field lookups. The changes are well-aligned with the goal of improving performance. I've found a correctness issue in the new caching logic related to case-sensitivity and suggested a fix. I also have a minor suggestion to improve maintainability by reducing code duplication in the new path parsing logic. Overall, this is a great step forward for performance.
… cache, converter, and encoder
There was a problem hiding this comment.
Pull request overview
This pull request implements performance optimizations across the schema encoding/decoding library, achieving significant measured improvements in benchmark performance (up to 35.5% reduction in ns/op) and reduced memory allocations (up to 10.64% fewer allocations). The optimizations focus on reducing reflection overhead, minimizing string operations, and implementing smart caching strategies.
Changes:
- Replaced standard library string/number conversion functions with optimized utilities from gofiber/utils/v2
- Cached field values and types during struct iteration to eliminate repeated reflection calls
- Optimized path parsing by avoiding upfront string splits and using on-demand segment extraction
- Implemented map-based field lookup caching with pre-computed lowercase aliases
- Extracted anonymous pointer allocation logic into a cached, optimized method
Reviewed changes
Copilot reviewed 5 out of 6 changed files in this pull request and generated 4 comments.
Show a summary per file
| File | Description |
|---|---|
| go.mod | Adds dependencies for gofiber/utils/v2 and google/uuid, updates Go version to 1.25 |
| go.sum | Checksums for new dependencies |
| encoder.go | Caches field values/types to reduce reflection calls; uses utils for int/uint formatting |
| decoder.go | Extracts anonymous pointer allocation into cached ensureAnonymousPtrs method |
| converter.go | Replaces strconv parsing with utils library functions for all numeric types |
| cache.go | Optimizes path parsing to avoid splits; adds map-based field lookup; uses utils for string operations |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
WalkthroughRefactors internal schema parsing and encoding: introduces nextPathSegment and improved path parsing (including slice-of-struct indices), adds structInfo caches (fieldsByName, anonymousPtrFields), centralizes anonymous-pointer initialization in Decoder, and replaces strconv usage with github.com/gofiber/utils/v2 Parse/Format helpers across converters and encoders. Changes
Sequence Diagram(s)(omitted) Estimated code review effort🎯 3 (Moderate) | ⏱️ ~25 minutes Possibly related PRs
Suggested reviewers
Poem
🚥 Pre-merge checks | ✅ 2 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ 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 |
✅ Measured gains (statistically significant)
BenchmarkConvertInt: -35.50% ns/op (9.530ns → 6.147ns, p=0.000)BenchmarkConvertPointer: -16.07% ns/op (18.79ns → 15.77ns, p=0.000)BenchmarkLargeStructDecode: -9.16% ns/op (5.224µs → 4.745µs, p=0.000)BenchmarkSimpleStructDecode: -6.40% ns/op (2.272µs → 2.127µs, p=0.000)BenchmarkLargeStructEncode: -2.65% ns/op (1.998µs → 1.945µs, p=0.028)✅ Allocation improvements
BenchmarkLargeStructDecode:BenchmarkSimpleStructDecode:No significant change
BenchmarkConvertBoolBenchmarkSimpleStructEncodeBenchmarkParseTagSummary by CodeRabbit
Related #2