Skip to content

perf(request): reduce overhead by lazy-loading headers for json/form data#1058

Merged
0x676e67 merged 1 commit intomainfrom
perf
Jan 25, 2026
Merged

perf(request): reduce overhead by lazy-loading headers for json/form data#1058
0x676e67 merged 1 commit intomainfrom
perf

Conversation

@0x676e67
Copy link
Owner

No description provided.

@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @0x676e67, 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 introduces a performance enhancement by refactoring how certain HTTP headers are set within the RequestBuilder. By switching from or_insert to or_insert_with, the creation of HeaderValue instances is now lazy-loaded. This means the header value is only computed and allocated if the header is not already present, thereby reducing overhead and improving efficiency, particularly for requests involving multipart, form, or JSON bodies where these headers might be frequently set.

Highlights

  • Performance Optimization: Replaced or_insert with or_insert_with for header insertions, deferring the creation of HeaderValue until it's actually needed. This avoids unnecessary allocations and computations when a header already exists.
  • Targeted Headers: The optimization applies to CONTENT_LENGTH for multipart form data and CONTENT_TYPE for application/x-www-form-urlencoded and application/json bodies.
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.

Copy link
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 improves performance by using or_insert_with to lazily initialize headers for multipart, form, and JSON data. This is a good optimization, especially for multipart forms where calculating the content length can be non-trivial. For form and json data, the use of HeaderValue::from_static is already very efficient as it's a const fn, so the performance gain is likely negligible, but the change is good for consistency. I've added one minor suggestion to improve the formatting and make the code more idiomatic in the form method.

@0x676e67 0x676e67 merged commit 6992b6f into main Jan 25, 2026
23 checks passed
@0x676e67 0x676e67 deleted the perf branch January 25, 2026 14:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant