Skip to content

http/httpguts: speed up ValidHeaderFieldName #207

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

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 4 additions & 9 deletions http/httpguts/httplex.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"golang.org/x/net/idna"
)

var isTokenTable = [127]bool{
var isTokenTable = [256]bool{
'!': true,
'#': true,
'$': true,
Expand Down Expand Up @@ -93,12 +93,7 @@ var isTokenTable = [127]bool{
}

func IsTokenRune(r rune) bool {
i := int(r)
return i < len(isTokenTable) && isTokenTable[i]
}

func isNotToken(r rune) bool {
return !IsTokenRune(r)
return r < utf8.RuneSelf && isTokenTable[byte(r)]
}

// HeaderValuesContainsToken reports whether any string in values
Expand Down Expand Up @@ -202,8 +197,8 @@ func ValidHeaderFieldName(v string) bool {
if len(v) == 0 {
return false
}
for _, r := range v {
if !IsTokenRune(r) {
for i := 0; i < len(v); i++ {
if !isTokenTable[v[i]] {
return false
}
}
Expand Down
49 changes: 48 additions & 1 deletion http/httpguts/httplex_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func isSeparator(c rune) bool {
return false
}

func TestIsToken(t *testing.T) {
func TestIsTokenRune(t *testing.T) {
for i := 0; i <= 130; i++ {
r := rune(i)
expected := isChar(r) && !isCtl(r) && !isSeparator(r)
Expand All @@ -30,6 +30,15 @@ func TestIsToken(t *testing.T) {
}
}

func BenchmarkIsTokenRune(b *testing.B) {
for i := 0; i < b.N; i++ {
var r rune
for ; r < 1024; r++ {
IsTokenRune(r)
}
}
}

func TestHeaderValuesContainsToken(t *testing.T) {
tests := []struct {
vals []string
Expand Down Expand Up @@ -100,6 +109,44 @@ func TestHeaderValuesContainsToken(t *testing.T) {
}
}

func TestValidHeaderFieldName(t *testing.T) {
tests := []struct {
in string
want bool
}{
{"", false},
{"Accept Charset", false},
{"Accept-Charset", true},
{"AccepT-EncodinG", true},
{"CONNECTION", true},
{"résumé", false},
}
for _, tt := range tests {
got := ValidHeaderFieldName(tt.in)
if tt.want != got {
t.Errorf("ValidHeaderFieldName(%q) = %t; want %t", tt.in, got, tt.want)
}
}
}

func BenchmarkValidHeaderFieldName(b *testing.B) {
names := []string{
"",
"Accept Charset",
"Accept-Charset",
"AccepT-EncodinG",
"CONNECTION",
"résumé",
}
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
for _, name := range names {
ValidHeaderFieldName(name)
}
}
}

func TestPunycodeHostPort(t *testing.T) {
tests := []struct {
in, want string
Expand Down