Skip to content

Commit 0a24555

Browse files
Julien Cretelneild
Julien Cretel
authored andcommitted
http/httpguts: speed up ValidHeaderFieldName
Eliminate bounds checks and eschews UTF-8 decoding in ValidHeaderFieldName, thereby doubling its speed without introducing any allocations. Also eliminate bounds checks in IsTokenRune. Add tests and benchmarks for both ValidHeaderFieldName and IsTokenRune. goos: darwin goarch: amd64 pkg: golang.org/x/net/http/httpguts cpu: Intel(R) Core(TM) i7-6700HQ CPU @ 2.60GHz │ before │ after │ │ sec/op │ sec/op vs base │ IsTokenRune-8 315.2n ± 0% 316.2n ± 1% ~ (p=0.245 n=20) ValidHeaderFieldName-8 62.77n ± 0% 29.16n ± 0% -53.55% (p=0.000 n=20) geomean 140.7n 96.02n -31.73% │ before │ after │ │ B/op │ B/op vs base │ IsTokenRune-8 0.000 ± 0% 0.000 ± 0% ~ (p=1.000 n=20) ValidHeaderFieldName-8 0.000 ± 0% 0.000 ± 0% ~ (p=1.000 n=20) geomean ² +0.00% │ before │ after │ │ allocs/op │ allocs/op vs base │ IsTokenRune-8 0.000 ± 0% 0.000 ± 0% ~ (p=1.000 n=20) ValidHeaderFieldName-8 0.000 ± 0% 0.000 ± 0% ~ (p=1.000 n=20) geomean ² +0.00% Fixes golang/go#66700 Change-Id: Ia3ea80e5f0d173e3a69eb7429023587fd7bc5933 GitHub-Last-Rev: 1f1d25d GitHub-Pull-Request: #207 Reviewed-on: https://go-review.googlesource.com/c/net/+/578075 Run-TryBot: Emmanuel Odeke <emmanuel@orijtech.com> Reviewed-by: Damien Neil <dneil@google.com> Reviewed-by: Dmitri Shuralyov <dmitshur@google.com> TryBot-Result: Gopher Robot <gobot@golang.org> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
1 parent ec05fdc commit 0a24555

File tree

2 files changed

+52
-10
lines changed

2 files changed

+52
-10
lines changed

http/httpguts/httplex.go

+4-9
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212
"golang.org/x/net/idna"
1313
)
1414

15-
var isTokenTable = [127]bool{
15+
var isTokenTable = [256]bool{
1616
'!': true,
1717
'#': true,
1818
'$': true,
@@ -93,12 +93,7 @@ var isTokenTable = [127]bool{
9393
}
9494

9595
func IsTokenRune(r rune) bool {
96-
i := int(r)
97-
return i < len(isTokenTable) && isTokenTable[i]
98-
}
99-
100-
func isNotToken(r rune) bool {
101-
return !IsTokenRune(r)
96+
return r < utf8.RuneSelf && isTokenTable[byte(r)]
10297
}
10398

10499
// HeaderValuesContainsToken reports whether any string in values
@@ -202,8 +197,8 @@ func ValidHeaderFieldName(v string) bool {
202197
if len(v) == 0 {
203198
return false
204199
}
205-
for _, r := range v {
206-
if !IsTokenRune(r) {
200+
for i := 0; i < len(v); i++ {
201+
if !isTokenTable[v[i]] {
207202
return false
208203
}
209204
}

http/httpguts/httplex_test.go

+48-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ func isSeparator(c rune) bool {
2020
return false
2121
}
2222

23-
func TestIsToken(t *testing.T) {
23+
func TestIsTokenRune(t *testing.T) {
2424
for i := 0; i <= 130; i++ {
2525
r := rune(i)
2626
expected := isChar(r) && !isCtl(r) && !isSeparator(r)
@@ -30,6 +30,15 @@ func TestIsToken(t *testing.T) {
3030
}
3131
}
3232

33+
func BenchmarkIsTokenRune(b *testing.B) {
34+
for i := 0; i < b.N; i++ {
35+
var r rune
36+
for ; r < 1024; r++ {
37+
IsTokenRune(r)
38+
}
39+
}
40+
}
41+
3342
func TestHeaderValuesContainsToken(t *testing.T) {
3443
tests := []struct {
3544
vals []string
@@ -100,6 +109,44 @@ func TestHeaderValuesContainsToken(t *testing.T) {
100109
}
101110
}
102111

112+
func TestValidHeaderFieldName(t *testing.T) {
113+
tests := []struct {
114+
in string
115+
want bool
116+
}{
117+
{"", false},
118+
{"Accept Charset", false},
119+
{"Accept-Charset", true},
120+
{"AccepT-EncodinG", true},
121+
{"CONNECTION", true},
122+
{"résumé", false},
123+
}
124+
for _, tt := range tests {
125+
got := ValidHeaderFieldName(tt.in)
126+
if tt.want != got {
127+
t.Errorf("ValidHeaderFieldName(%q) = %t; want %t", tt.in, got, tt.want)
128+
}
129+
}
130+
}
131+
132+
func BenchmarkValidHeaderFieldName(b *testing.B) {
133+
names := []string{
134+
"",
135+
"Accept Charset",
136+
"Accept-Charset",
137+
"AccepT-EncodinG",
138+
"CONNECTION",
139+
"résumé",
140+
}
141+
b.ReportAllocs()
142+
b.ResetTimer()
143+
for i := 0; i < b.N; i++ {
144+
for _, name := range names {
145+
ValidHeaderFieldName(name)
146+
}
147+
}
148+
}
149+
103150
func TestPunycodeHostPort(t *testing.T) {
104151
tests := []struct {
105152
in, want string

0 commit comments

Comments
 (0)