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

header.go Referer() optimize #1313

Merged
merged 3 commits into from Jun 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion args.go
Expand Up @@ -343,7 +343,7 @@ func (a *Args) GetUfloatOrZero(key string) float64 {
// true is returned for "1", "t", "T", "true", "TRUE", "True", "y", "yes", "Y", "YES", "Yes",
// otherwise false is returned.
func (a *Args) GetBool(key string) bool {
switch b2s(a.Peek(key)) {
switch string(a.Peek(key)) {
// Support the same true cases as strconv.ParseBool
// See: https://github.com/golang/go/blob/4e1b11e2c9bdb0ddea1141eed487be1a626ff5be/src/strconv/atob.go#L12
// and Y and Yes versions.
Expand Down
2 changes: 1 addition & 1 deletion header.go
Expand Up @@ -586,7 +586,7 @@ func (h *RequestHeader) SetUserAgentBytes(userAgent []byte) {

// Referer returns Referer header value.
func (h *RequestHeader) Referer() []byte {
return h.PeekBytes(strReferer)
return peekArgBytes(h.h, strReferer)
}

// SetReferer sets Referer header value.
Expand Down
46 changes: 39 additions & 7 deletions header_timing_test.go
Expand Up @@ -12,6 +12,9 @@ import (

var strFoobar = []byte("foobar.com")

// it has the same length as Content-Type
var strNonSpecialHeader = []byte("Dontent-Type")

type benchReadBuf struct {
s []byte
n int
Expand Down Expand Up @@ -96,26 +99,55 @@ func BenchmarkResponseHeaderWrite(b *testing.B) {
})
}

func BenchmarkRequestHeaderPeekBytesCanonical(b *testing.B) {
// Result: 2.2 ns/op
func BenchmarkRequestHeaderPeekBytesSpecialHeader(b *testing.B) {
b.RunParallel(func(pb *testing.PB) {
var h RequestHeader
h.SetBytesV("Host", strFoobar)
h.SetContentTypeBytes(strFoobar)
for pb.Next() {
v := h.PeekBytes(strHost)
v := h.PeekBytes(strContentType)
if !bytes.Equal(v, strFoobar) {
b.Fatalf("unexpected result: %q. Expected %q", v, strFoobar)
}
}
})
}

func BenchmarkRequestHeaderPeekBytesNonCanonical(b *testing.B) {
// Result: 2.9 ns/op
func BenchmarkRequestHeaderPeekBytesNonSpecialHeader(b *testing.B) {
b.RunParallel(func(pb *testing.PB) {
var h RequestHeader
h.SetBytesV("Host", strFoobar)
hostBytes := []byte("HOST")
h.SetBytesKV(strNonSpecialHeader, strFoobar)
for pb.Next() {
v := h.PeekBytes(strNonSpecialHeader)
if !bytes.Equal(v, strFoobar) {
b.Fatalf("unexpected result: %q. Expected %q", v, strFoobar)
}
}
})
}

// Result: 2.3 ns/op
func BenchmarkResponseHeaderPeekBytesSpecialHeader(b *testing.B) {
b.RunParallel(func(pb *testing.PB) {
var h ResponseHeader
h.SetContentTypeBytes(strFoobar)
for pb.Next() {
v := h.PeekBytes(strContentType)
if !bytes.Equal(v, strFoobar) {
b.Fatalf("unexpected result: %q. Expected %q", v, strFoobar)
}
}
})
}

// Result: 2.9 ns/op
func BenchmarkResponseHeaderPeekBytesNonSpecialHeader(b *testing.B) {
b.RunParallel(func(pb *testing.PB) {
var h ResponseHeader
h.SetBytesKV(strNonSpecialHeader, strFoobar)
for pb.Next() {
v := h.PeekBytes(hostBytes)
v := h.PeekBytes(strNonSpecialHeader)
if !bytes.Equal(v, strFoobar) {
b.Fatalf("unexpected result: %q. Expected %q", v, strFoobar)
}
Expand Down