Skip to content

Commit 66cd502

Browse files
authored
header.go Referer() optimize (valyala#1313)
* args.go GetBool(): use switch with string casting This should be optimized by Go compiler itself so the b2s() call is not needed. It was previously done by this but changed in 1e7885e * header.go Referer() optimize Use direct peekArgBytes() instead of PeekBytes() that will check for special headers * header_timing_test.go BenchmarkRequestHeaderPeekBytesSpecialHeader The old BenchmarkRequestHeaderPeekBytesCanonical and BenchmarkRequestHeaderPeekBytesNonCanonical are in fact just measured the header normalization. But it's anyway is benchmarked separately. Results was almost the same: 1.5 ns/op. Instead, let's reuse the benches to find a difference between peeking of special (Host, CT) and custom headers.
1 parent c9f43ea commit 66cd502

File tree

3 files changed

+41
-9
lines changed

3 files changed

+41
-9
lines changed

Diff for: args.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,7 @@ func (a *Args) GetUfloatOrZero(key string) float64 {
343343
// true is returned for "1", "t", "T", "true", "TRUE", "True", "y", "yes", "Y", "YES", "Yes",
344344
// otherwise false is returned.
345345
func (a *Args) GetBool(key string) bool {
346-
switch b2s(a.Peek(key)) {
346+
switch string(a.Peek(key)) {
347347
// Support the same true cases as strconv.ParseBool
348348
// See: https://github.com/golang/go/blob/4e1b11e2c9bdb0ddea1141eed487be1a626ff5be/src/strconv/atob.go#L12
349349
// and Y and Yes versions.

Diff for: header.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -586,7 +586,7 @@ func (h *RequestHeader) SetUserAgentBytes(userAgent []byte) {
586586

587587
// Referer returns Referer header value.
588588
func (h *RequestHeader) Referer() []byte {
589-
return h.PeekBytes(strReferer)
589+
return peekArgBytes(h.h, strReferer)
590590
}
591591

592592
// SetReferer sets Referer header value.

Diff for: header_timing_test.go

+39-7
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ import (
1212

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

15+
// it has the same length as Content-Type
16+
var strNonSpecialHeader = []byte("Dontent-Type")
17+
1518
type benchReadBuf struct {
1619
s []byte
1720
n int
@@ -96,26 +99,55 @@ func BenchmarkResponseHeaderWrite(b *testing.B) {
9699
})
97100
}
98101

99-
func BenchmarkRequestHeaderPeekBytesCanonical(b *testing.B) {
102+
// Result: 2.2 ns/op
103+
func BenchmarkRequestHeaderPeekBytesSpecialHeader(b *testing.B) {
100104
b.RunParallel(func(pb *testing.PB) {
101105
var h RequestHeader
102-
h.SetBytesV("Host", strFoobar)
106+
h.SetContentTypeBytes(strFoobar)
103107
for pb.Next() {
104-
v := h.PeekBytes(strHost)
108+
v := h.PeekBytes(strContentType)
105109
if !bytes.Equal(v, strFoobar) {
106110
b.Fatalf("unexpected result: %q. Expected %q", v, strFoobar)
107111
}
108112
}
109113
})
110114
}
111115

112-
func BenchmarkRequestHeaderPeekBytesNonCanonical(b *testing.B) {
116+
// Result: 2.9 ns/op
117+
func BenchmarkRequestHeaderPeekBytesNonSpecialHeader(b *testing.B) {
113118
b.RunParallel(func(pb *testing.PB) {
114119
var h RequestHeader
115-
h.SetBytesV("Host", strFoobar)
116-
hostBytes := []byte("HOST")
120+
h.SetBytesKV(strNonSpecialHeader, strFoobar)
121+
for pb.Next() {
122+
v := h.PeekBytes(strNonSpecialHeader)
123+
if !bytes.Equal(v, strFoobar) {
124+
b.Fatalf("unexpected result: %q. Expected %q", v, strFoobar)
125+
}
126+
}
127+
})
128+
}
129+
130+
// Result: 2.3 ns/op
131+
func BenchmarkResponseHeaderPeekBytesSpecialHeader(b *testing.B) {
132+
b.RunParallel(func(pb *testing.PB) {
133+
var h ResponseHeader
134+
h.SetContentTypeBytes(strFoobar)
135+
for pb.Next() {
136+
v := h.PeekBytes(strContentType)
137+
if !bytes.Equal(v, strFoobar) {
138+
b.Fatalf("unexpected result: %q. Expected %q", v, strFoobar)
139+
}
140+
}
141+
})
142+
}
143+
144+
// Result: 2.9 ns/op
145+
func BenchmarkResponseHeaderPeekBytesNonSpecialHeader(b *testing.B) {
146+
b.RunParallel(func(pb *testing.PB) {
147+
var h ResponseHeader
148+
h.SetBytesKV(strNonSpecialHeader, strFoobar)
117149
for pb.Next() {
118-
v := h.PeekBytes(hostBytes)
150+
v := h.PeekBytes(strNonSpecialHeader)
119151
if !bytes.Equal(v, strFoobar) {
120152
b.Fatalf("unexpected result: %q. Expected %q", v, strFoobar)
121153
}

0 commit comments

Comments
 (0)