Skip to content

Commit

Permalink
http2: reduce init-time work & allocations
Browse files Browse the repository at this point in the history
Updates golang/go#26775

Change-Id: Iea95ea07bb0fed42410efb4e8420d8e9a17704fe
Reviewed-on: https://go-review.googlesource.com/127664
Reviewed-by: Ian Lance Taylor <[email protected]>
  • Loading branch information
bradfitz committed Aug 21, 2018
1 parent aaf6012 commit 922f481
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 17 deletions.
20 changes: 15 additions & 5 deletions http2/headermap.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,21 @@ package http2
import (
"net/http"
"strings"
"sync"
)

var (
commonLowerHeader = map[string]string{} // Go-Canonical-Case -> lower-case
commonCanonHeader = map[string]string{} // lower-case -> Go-Canonical-Case
commonBuildOnce sync.Once
commonLowerHeader map[string]string // Go-Canonical-Case -> lower-case
commonCanonHeader map[string]string // lower-case -> Go-Canonical-Case
)

func init() {
for _, v := range []string{
func buildCommonHeaderMapsOnce() {
commonBuildOnce.Do(buildCommonHeaderMaps)
}

func buildCommonHeaderMaps() {
common := []string{
"accept",
"accept-charset",
"accept-encoding",
Expand Down Expand Up @@ -63,14 +69,18 @@ func init() {
"vary",
"via",
"www-authenticate",
} {
}
commonLowerHeader = make(map[string]string, len(common))
commonCanonHeader = make(map[string]string, len(common))
for _, v := range common {
chk := http.CanonicalHeaderKey(v)
commonLowerHeader[chk] = v
commonCanonHeader[v] = chk
}
}

func lowerHeader(v string) string {
buildCommonHeaderMapsOnce()
if s, ok := commonLowerHeader[v]; ok {
return s
}
Expand Down
17 changes: 5 additions & 12 deletions http2/http2.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,19 +201,12 @@ func validWireHeaderFieldName(v string) bool {
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 {
if s, ok := httpCodeStringCommon[code]; ok {
return s
switch code {
case 200:
return "200"
case 404:
return "404"
}
return strconv.Itoa(code)
}
Expand Down
1 change: 1 addition & 0 deletions http2/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -663,6 +663,7 @@ func (sc *serverConn) condlogf(err error, format string, args ...interface{}) {

func (sc *serverConn) canonicalHeader(v string) string {
sc.serveG.check()
buildCommonHeaderMapsOnce()
cv, ok := commonCanonHeader[v]
if ok {
return cv
Expand Down

0 comments on commit 922f481

Please sign in to comment.