Skip to content

⚡ perf: Improve performance of decoder and encoder#45

Merged
ReneWerner87 merged 5 commits intomainfrom
performance_improvements
Feb 7, 2026
Merged

⚡ perf: Improve performance of decoder and encoder#45
ReneWerner87 merged 5 commits intomainfrom
performance_improvements

Conversation

@ReneWerner87
Copy link
Member

@ReneWerner87 ReneWerner87 commented Feb 6, 2026

✅ 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:
    • B/op: 2.297KiB → 2.141KiB (-6.80%, p=0.000)
    • allocs/op: 97 → 88 (-9.28%, p=0.000)
  • BenchmarkSimpleStructDecode:
    • B/op: 975B → 879B (-9.85%, p=0.000)
    • allocs/op: 47 → 42 (-10.64%, p=0.000)

No significant change

  • BenchmarkConvertBool
  • BenchmarkSimpleStructEncode
  • BenchmarkParseTag

Summary by CodeRabbit

  • Refactor
    • Improved internal path parsing and boundary handling for more reliable field resolution.
    • Faster struct field lookup via cached mappings, boosting performance.
    • Unified numeric and boolean parsing/formatting routines for consistent conversions.
    • Streamlined encoding/decoding access patterns to reduce errors and improve maintainability.
  • Tests
    • Added unit tests covering invalid path scenarios to improve robustness.

Related #2

@gemini-code-assist
Copy link

Summary of Changes

Hello @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

  • Optimized Path Parsing: The parsePath function in cache.go has been refactored to manually parse path segments, avoiding the overhead of strings.Split and reducing string allocations.
  • Faster Field Lookups: A new fieldsByName map has been introduced in structInfo to provide O(1) lookup for struct fields by alias, significantly improving performance over linear iteration.
  • Utility Package Integration: The github.com/gofiber/utils/v2 package has been integrated, replacing standard library strconv functions with optimized versions for parsing integers and unsigned integers, and using utils.CopyString for string initialization.
  • Efficient Anonymous Pointer Handling: The decoder now efficiently allocates embedded anonymous pointer fields using a dedicated ensureAnonymousPtrs method, leveraging pre-computed indices stored in structInfo.
  • Encoder Reflection Optimization: The encoder loop has been optimized to reduce redundant reflection calls by caching reflect.Value and reflect.Type for fields.

🧠 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
  • cache.go
    • Imported github.com/gofiber/utils/v2 for utility functions.
    • Initialized tag field with utils.CopyString("schema") for potential string allocation optimization.
    • Refactored parsePath to use manual string parsing instead of strings.Split to reduce allocations.
    • Added fieldsByName map to structInfo for direct field lookup by alias.
    • Introduced anonymousPtrFields slice to structInfo to store indices of anonymous pointer fields.
    • Updated structInfo.get to utilize the fieldsByName map for faster lookups.
    • Replaced strings.HasPrefix and strings.SplitN with strings.CutPrefix in getDefaultOptionValue for efficiency.
  • converter.go
    • Imported github.com/gofiber/utils/v2.
    • Replaced strconv.ParseInt and strconv.ParseUint calls with corresponding optimized functions from gofiber/utils/v2 (e.g., utils.ParseInt, utils.ParseUint8).
  • decoder.go
    • Refactored the logic for allocating embedded anonymous struct pointers into a new ensureAnonymousPtrs method.
    • The ensureAnonymousPtrs method now uses the anonymousPtrFields stored in structInfo for efficient allocation.
  • encoder.go
    • Optimized the encoding loop by caching fieldValue and fieldType to avoid repeated reflection calls for each field.
  • go.mod
    • Added github.com/gofiber/utils/v2 v2.0.0 as a new dependency.
  • go.sum
    • Updated with checksums for github.com/gofiber/utils/v2 and its indirect dependency github.com/google/uuid.
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.

@codecov
Copy link

codecov bot commented Feb 6, 2026

Codecov Report

❌ Patch coverage is 95.34884% with 4 lines in your changes missing coverage. Please review.
✅ Project coverage is 97.09%. Comparing base (7ca6d20) to head (e1ee313).
⚠️ Report is 6 commits behind head on main.

Files with missing lines Patch % Lines
cache.go 95.12% 1 Missing and 1 partial ⚠️
encoder.go 89.47% 2 Missing ⚠️
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              
Flag Coverage Δ
unittests 97.09% <95.34%> (+0.12%) ⬆️

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

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

@ReneWerner87 ReneWerner87 marked this pull request as ready for review February 6, 2026 07:41
@ReneWerner87 ReneWerner87 requested a review from a team as a code owner February 6, 2026 07:41
@ReneWerner87 ReneWerner87 requested review from Copilot, efectn, gaby and sixcolors and removed request for a team February 6, 2026 07:41
Copy link

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

@coderabbitai
Copy link

coderabbitai bot commented Feb 6, 2026

Walkthrough

Refactors 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

Cohort / File(s) Summary
Path parsing & cache
cache.go
Added nextPathSegment helper; robust dotted-path parsing with boundary checks; slice-of-struct index parsing via utils.ParseInt; switch to nil-based path handling; added fieldsByName map and anonymousPtrFields tracking; path assembly/refactor.
Converters (parsing)
converter.go
Replaced strconv parsing with github.com/gofiber/utils/v2 Parse* helpers across numeric/bool converters; return reflect.Value directly on successful parse; preserved error semantics.
Decoder initialization
decoder.go, decoder_test.go
Introduced Decoder.ensureAnonymousPtrs(v) to initialize cached anonymous pointer fields; decoder uses helper instead of inline allocation. Added tests TestCacheParsePathInvalidPathCases covering invalid path scenarios.
Encoder formatting & access patterns
encoder.go
Standardized use of local fieldValue/fieldType; switched integer formatting to utils.FormatInt/FormatUint; updated encoder calls to use fieldValue and element indexing consistently.
Tests
decoder_test.go
New parallelized subtests asserting parsePath returns errInvalidPath for various invalid path inputs (non-struct, unknown fields, bad indices, trailing dots).

Sequence Diagram(s)

(omitted)

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Possibly related PRs

Suggested reviewers

  • gaby
  • sixcolors
  • efectn

Poem

🐰 Hopping through paths with nimble feet,

Dots and indices now parse neat,
Anonymous pointers find their place,
Utils whisper numbers into place,
Cache and encoder dance in sync — hooray, neat feat! 🥕

🚥 Pre-merge checks | ✅ 2 | ❌ 1
❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 8.70% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Title check ✅ Passed The title "⚡ perf: Improve performance of decoder and encoder" is directly related to the main objectives of the pull request, which includes measurable performance improvements across decoder and encoder benchmarks with specific gains documented.
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ 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 performance_improvements

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.

@gaby gaby changed the title Performance improvements ⚡ perf: Improve performance of decoder and encoder Feb 7, 2026
@ReneWerner87 ReneWerner87 merged commit 5ce5c13 into main Feb 7, 2026
12 of 13 checks passed
@ReneWerner87 ReneWerner87 deleted the performance_improvements branch February 7, 2026 18:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants