Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

refactor: Change header keys to be canonical #3389

Merged
merged 1 commit into from
Dec 16, 2024

Conversation

alexandear
Copy link
Contributor

The PR enables the canonicalheader linter and changes header key strings to be in canonical form.

This is needed because canonical header keys don't need to be converted to a canonical form via http.CanonicalHeaderKey, leading to fewer allocations when adding, setting or getting a header.

Benchmarks
func BenchmarkCanonical(b *testing.B) {
	v := http.Header{
		"Canonical-Header": []string{"hello_world"},
	}
	b.ReportAllocs()
	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		s := v.Get("Canonical-Header")
		if s != "hello_world" {
			b.Fatal()
		}
	}
}

func BenchmarkNonCanonical(b *testing.B) {
	v := http.Header{
		"Canonical-Header": []string{"hello_world"},
	}
	b.ReportAllocs()
	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		s := v.Get("CANONICAL-HEADER")
		if s != "hello_world" {
			b.Fatal()
		}
	}
}
goos: darwin
goarch: arm64
pkg: github.com/lasiar/canonicalheader
cpu: Apple M1 Pro
BenchmarkNonCanonical
BenchmarkNonCanonical-8   	 1407914	       848.2 ns/op	      16 B/op	       1 allocs/op
BenchmarkCanonical
BenchmarkCanonical-8         11176232	       107.7 ns/op	       0 B/op	       0 allocs/op
PASS

Copy link
Collaborator

@gmlewis gmlewis left a comment

Choose a reason for hiding this comment

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

Thank you, @alexandear !
LGTM.
Merging.

Copy link

codecov bot commented Dec 16, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 92.21%. Comparing base (2b8c7fa) to head (900c040).
Report is 199 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #3389      +/-   ##
==========================================
- Coverage   97.72%   92.21%   -5.51%     
==========================================
  Files         153      173      +20     
  Lines       13390    14770    +1380     
==========================================
+ Hits        13085    13620     +535     
- Misses        215     1060     +845     
  Partials       90       90              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@gmlewis gmlewis merged commit 7beb0aa into google:master Dec 16, 2024
6 of 7 checks passed
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants