Skip to content
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

Optimize header access #445

Merged
merged 3 commits into from
Jan 28, 2023

Conversation

mattrobenolt
Copy link
Contributor

There are a lot of attempts to directly set header values already, this unifies all Get/Set/Add/Del accesses to assume a canonical form already.

In my personal testings, this actually stands out in our hot paths in flame graphs doing header Get/Sets. Within this codebase, we already have constants in place already in the correct formats.

I've dug so deeply into this in the past that I created https://github.com/mattrobenolt/go-httpheaders which specifically highlights benchmarks of direct access vs using the Get/Set helpers.

Since these are called multiple times in very hot paths, it's a worthwhile optimization since we can trust the inputs.

Before submitting your PR: Please read through the contribution guide at https://github.com/bufbuild/connect-go/blob/main/.github/CONTRIBUTING.md

There are a lot of attempts to directly set header values already, this
unifies all Get/Set/Add/Del accesses to assume a canonical form already.

In my personal testings, this actually stands out in our hot paths in
flame graphs doing header Get/Sets. Within this codebase, we already
have constants in place already in the correct formats.

I've dug so deeply into this in the past that I created https://github.com/mattrobenolt/go-httpheaders
which specifically highlights benchmarks of direct access vs using the
Get/Set helpers.

Since these are called multiple times in very hot paths, it's a
worthwhile optimization since we can trust the inputs.
@@ -37,6 +37,7 @@ const (
const (
headerContentType = "Content-Type"
headerUserAgent = "User-Agent"
headerTrailer = "Trailer"
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Figured since this was referenced multiple times, might as well elevate it to a constant.

Copy link
Member

@akshayjshah akshayjshah left a comment

Choose a reason for hiding this comment

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

This is great - thank you! I pushed a commit that changes the "XXX:" comments to match Buf's style a bit better, but the remainder of the change is excellent.

@mattrobenolt
Copy link
Contributor Author

Perfect, those comments were more for yall while reviewing anyways, since I wasn't entirely sure if my assumption was true or not.

@akshayjshah akshayjshah merged commit 5999752 into connectrpc:main Jan 28, 2023
@mattrobenolt mattrobenolt deleted the optimize-header-access branch January 28, 2023 00:08
renovate bot referenced this pull request in open-feature/flagd Feb 2, 2023
[![Mend
Renovate](https://app.renovatebot.com/images/banner.svg)](https://renovatebot.com)

This PR contains the following updates:

| Package | Type | Update | Change |
|---|---|---|---|
|
[github.com/bufbuild/connect-go](https://togithub.com/bufbuild/connect-go)
| require | patch | `v1.5.0` -> `v1.5.1` |

---

### Release Notes

<details>
<summary>bufbuild/connect-go</summary>

###
[`v1.5.1`](https://togithub.com/bufbuild/connect-go/releases/tag/v1.5.1)

[Compare
Source](https://togithub.com/bufbuild/connect-go/compare/v1.5.0...v1.5.1)

<!-- Release notes generated using configuration in .github/release.yml
at main -->

#### What's Changed

Thanks to [@&#8203;mattrobenolt](https://togithub.com/mattrobenolt),
v1.5.1 exclusively contains performance improvements. There should be no
other user-visible behavior changes.

##### Bugfixes

- Minimize allocations writing User-Agent header by
[@&#8203;mattrobenolt](https://togithub.com/mattrobenolt) in
[https://github.com/bufbuild/connect-go/pull/446](https://togithub.com/bufbuild/connect-go/pull/446)
- Minimize allocations parsing Content-Type by
[@&#8203;mattrobenolt](https://togithub.com/mattrobenolt) in
[https://github.com/bufbuild/connect-go/pull/444](https://togithub.com/bufbuild/connect-go/pull/444)
- Optimize header access by
[@&#8203;mattrobenolt](https://togithub.com/mattrobenolt) in
[https://github.com/bufbuild/connect-go/pull/445](https://togithub.com/bufbuild/connect-go/pull/445)
- Optimize Peer lookups by
[@&#8203;mattrobenolt](https://togithub.com/mattrobenolt) in
[https://github.com/bufbuild/connect-go/pull/447](https://togithub.com/bufbuild/connect-go/pull/447)

**Full Changelog**:
bufbuild/connect-go@v1.5.0...v1.5.1

</details>

---

### Configuration

📅 **Schedule**: Branch creation - At any time (no schedule defined),
Automerge - At any time (no schedule defined).

🚦 **Automerge**: Enabled.

♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the
rebase/retry checkbox.

🔕 **Ignore**: Close this PR and you won't be reminded about this update
again.

---

- [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check
this box

---

This PR has been generated by [Mend
Renovate](https://www.mend.io/free-developer-tools/renovate/). View
repository job log
[here](https://app.renovatebot.com/dashboard#github/open-feature/flagd).

<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzNC4xMTkuMCIsInVwZGF0ZWRJblZlciI6IjM0LjExOS4wIn0=-->

Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
akshayjshah added a commit that referenced this pull request Jul 26, 2023
The standard library's `http.Header.{Get,Set,Add,Del}` automatically canonicalize header keys. This is nice, but expensive for us - we're using these APIs in many hot paths, and our constants are already in canonical form. This PR adds functions that bypass canonicalization and uses them wherever possible.

---------

Co-authored-by: Akshay Shah <[email protected]>
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.

2 participants