Skip to content

Commit 6520e26

Browse files
committed
Add cache of common HTTP headers mapped between lower and canonical case.
For garbage reduction.
1 parent 9e0eccc commit 6520e26

File tree

2 files changed

+95
-11
lines changed

2 files changed

+95
-11
lines changed

headermap.go

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
// Copyright 2014 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
// See https://code.google.com/p/go/source/browse/CONTRIBUTORS
5+
// Licensed under the same terms as Go itself:
6+
// https://code.google.com/p/go/source/browse/LICENSE
7+
8+
package http2
9+
10+
import (
11+
"net/http"
12+
"strings"
13+
)
14+
15+
var (
16+
commonLowerHeader = map[string]string{} // Go-Canonical-Case -> lower-case
17+
commonCanonHeader = map[string]string{} // lower-case -> Go-Canonical-Case
18+
)
19+
20+
func init() {
21+
for _, v := range []string{
22+
"accept",
23+
"accept-charset",
24+
"accept-encoding",
25+
"accept-language",
26+
"accept-ranges",
27+
"age",
28+
"access-control-allow-origin",
29+
"allow",
30+
"authorization",
31+
"cache-control",
32+
"content-disposition",
33+
"content-encoding",
34+
"content-language",
35+
"content-length",
36+
"content-location",
37+
"content-range",
38+
"content-type",
39+
"cookie",
40+
"date",
41+
"etag",
42+
"expect",
43+
"expires",
44+
"from",
45+
"host",
46+
"if-match",
47+
"if-modified-since",
48+
"if-none-match",
49+
"if-unmodified-since",
50+
"last-modified",
51+
"link",
52+
"location",
53+
"max-forwards",
54+
"proxy-authenticate",
55+
"proxy-authorization",
56+
"range",
57+
"referer",
58+
"refresh",
59+
"retry-after",
60+
"server",
61+
"set-cookie",
62+
"strict-transport-security",
63+
"transfer-encoding",
64+
"user-agent",
65+
"vary",
66+
"via",
67+
"www-authenticate",
68+
} {
69+
chk := http.CanonicalHeaderKey(v)
70+
commonLowerHeader[chk] = v
71+
commonCanonHeader[v] = chk
72+
}
73+
}
74+
75+
func lowerHeader(v string) string {
76+
if s, ok := commonLowerHeader[v]; ok {
77+
return s
78+
}
79+
return strings.ToLower(v)
80+
}

server.go

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,6 @@ func (srv *Server) handleConn(hs *http.Server, c net.Conn, h http.Handler) {
7777
handler: h,
7878
framer: NewFramer(c, c), // TODO: write to a (custom?) buffered writer that can alternate when it's in buffered mode.
7979
streams: make(map[uint32]*stream),
80-
canonHeader: make(map[string]string),
8180
readFrameCh: make(chan frameAndProcessed),
8281
readFrameErrCh: make(chan error, 1), // must be buffered for 1
8382
wantWriteFrameCh: make(chan frameWriteMsg, 8),
@@ -258,12 +257,19 @@ func (sc *serverConn) onNewHeaderField(f hpack.HeaderField) {
258257

259258
func (sc *serverConn) canonicalHeader(v string) string {
260259
sc.serveG.check()
261-
// TODO: use a sync.Pool instead of putting the cache on *serverConn?
262-
cv, ok := sc.canonHeader[v]
263-
if !ok {
264-
cv = http.CanonicalHeaderKey(v)
265-
sc.canonHeader[v] = cv
260+
cv, ok := commonCanonHeader[v]
261+
if ok {
262+
return cv
263+
}
264+
cv, ok = sc.canonHeader[v]
265+
if ok {
266+
return cv
267+
}
268+
if sc.canonHeader == nil {
269+
sc.canonHeader = make(map[string]string)
266270
}
271+
cv = http.CanonicalHeaderKey(v)
272+
sc.canonHeader[v] = cv
267273
return cv
268274
}
269275

@@ -845,15 +851,13 @@ func (sc *serverConn) writeHeadersFrame(v interface{}) error {
845851
sc.headerWriteBuf.Reset()
846852
sc.hpackEncoder.WriteField(hpack.HeaderField{Name: ":status", Value: httpCodeString(req.httpResCode)})
847853
for k, vv := range req.h {
854+
k = lowerHeader(k)
848855
for _, v := range vv {
849856
// TODO: more of "8.1.2.2 Connection-Specific Header Fields"
850-
if k == "Transfer-Encoding" && v != "trailers" {
857+
if k == "transfer-encoding" && v != "trailers" {
851858
continue
852859
}
853-
// TODO: for gargage, cache lowercase copies of headers at
854-
// least for common ones and/or popular recent ones for
855-
// this serverConn. LRU?
856-
sc.hpackEncoder.WriteField(hpack.HeaderField{Name: strings.ToLower(k), Value: v})
860+
sc.hpackEncoder.WriteField(hpack.HeaderField{Name: k, Value: v})
857861
}
858862
}
859863
if req.contentType != "" {

0 commit comments

Comments
 (0)