Commit 922f4815 authored by Brad Fitzpatrick's avatar Brad Fitzpatrick

http2: reduce init-time work & allocations

Updates golang/go#26775

Change-Id: Iea95ea07bb0fed42410efb4e8420d8e9a17704fe
Reviewed-on: https://go-review.googlesource.com/127664Reviewed-by: 's avatarIan Lance Taylor <iant@golang.org>
parent aaf60122
...@@ -7,15 +7,21 @@ package http2 ...@@ -7,15 +7,21 @@ package http2
import ( import (
"net/http" "net/http"
"strings" "strings"
"sync"
) )
var ( var (
commonLowerHeader = map[string]string{} // Go-Canonical-Case -> lower-case commonBuildOnce sync.Once
commonCanonHeader = map[string]string{} // lower-case -> Go-Canonical-Case commonLowerHeader map[string]string // Go-Canonical-Case -> lower-case
commonCanonHeader map[string]string // lower-case -> Go-Canonical-Case
) )
func init() { func buildCommonHeaderMapsOnce() {
for _, v := range []string{ commonBuildOnce.Do(buildCommonHeaderMaps)
}
func buildCommonHeaderMaps() {
common := []string{
"accept", "accept",
"accept-charset", "accept-charset",
"accept-encoding", "accept-encoding",
...@@ -63,7 +69,10 @@ func init() { ...@@ -63,7 +69,10 @@ func init() {
"vary", "vary",
"via", "via",
"www-authenticate", "www-authenticate",
} { }
commonLowerHeader = make(map[string]string, len(common))
commonCanonHeader = make(map[string]string, len(common))
for _, v := range common {
chk := http.CanonicalHeaderKey(v) chk := http.CanonicalHeaderKey(v)
commonLowerHeader[chk] = v commonLowerHeader[chk] = v
commonCanonHeader[v] = chk commonCanonHeader[v] = chk
...@@ -71,6 +80,7 @@ func init() { ...@@ -71,6 +80,7 @@ func init() {
} }
func lowerHeader(v string) string { func lowerHeader(v string) string {
buildCommonHeaderMapsOnce()
if s, ok := commonLowerHeader[v]; ok { if s, ok := commonLowerHeader[v]; ok {
return s return s
} }
......
...@@ -201,19 +201,12 @@ func validWireHeaderFieldName(v string) bool { ...@@ -201,19 +201,12 @@ func validWireHeaderFieldName(v string) bool {
return true return true
} }
var httpCodeStringCommon = map[int]string{} // n -> strconv.Itoa(n)
func init() {
for i := 100; i <= 999; i++ {
if v := http.StatusText(i); v != "" {
httpCodeStringCommon[i] = strconv.Itoa(i)
}
}
}
func httpCodeString(code int) string { func httpCodeString(code int) string {
if s, ok := httpCodeStringCommon[code]; ok { switch code {
return s case 200:
return "200"
case 404:
return "404"
} }
return strconv.Itoa(code) return strconv.Itoa(code)
} }
......
...@@ -663,6 +663,7 @@ func (sc *serverConn) condlogf(err error, format string, args ...interface{}) { ...@@ -663,6 +663,7 @@ func (sc *serverConn) condlogf(err error, format string, args ...interface{}) {
func (sc *serverConn) canonicalHeader(v string) string { func (sc *serverConn) canonicalHeader(v string) string {
sc.serveG.check() sc.serveG.check()
buildCommonHeaderMapsOnce()
cv, ok := commonCanonHeader[v] cv, ok := commonCanonHeader[v]
if ok { if ok {
return cv return cv
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment