Skip to content

refactor: Change header keys to be canonical#3389

Merged
gmlewis merged 1 commit intogoogle:masterfrom
alexandear-org:refactor/canonical-headers
Dec 16, 2024
Merged

refactor: Change header keys to be canonical#3389
gmlewis merged 1 commit intogoogle:masterfrom
alexandear-org:refactor/canonical-headers

Conversation

@alexandear
Copy link
Copy Markdown
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
Copy Markdown
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.

@codecov
Copy link
Copy Markdown

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.

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