Last active
March 12, 2017 12:52
-
-
Save haya14busa/1e6c66559ab010ac4e872faa9d5fdc0d to your computer and use it in GitHub Desktop.
[nit] reduce alloc on commonHeader initialization
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# go test -v -bench=. | prettybench | |
PASS | |
benchmark iter time/iter bytes alloc allocs | |
--------- ---- --------- ----------- ------ | |
BenchmarkBefore-4 200000 7609.00 ns/op 4427 B/op 4 allocs/op | |
BenchmarkAfter-4 500000 3633.00 ns/op 2414 B/op 1 allocs/op |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package ch | |
import ( | |
"testing" | |
) | |
func before() { | |
// commonHeader interns common header strings. | |
var commonHeader = make(map[string]string) | |
for _, v := range []string{ | |
"Accept", | |
"Accept-Charset", | |
"Accept-Encoding", | |
"Accept-Language", | |
"Accept-Ranges", | |
"Cache-Control", | |
"Cc", | |
"Connection", | |
"Content-Id", | |
"Content-Language", | |
"Content-Length", | |
"Content-Transfer-Encoding", | |
"Content-Type", | |
"Cookie", | |
"Date", | |
"Dkim-Signature", | |
"Etag", | |
"Expires", | |
"From", | |
"Host", | |
"If-Modified-Since", | |
"If-None-Match", | |
"In-Reply-To", | |
"Last-Modified", | |
"Location", | |
"Message-Id", | |
"Mime-Version", | |
"Pragma", | |
"Received", | |
"Return-Path", | |
"Server", | |
"Set-Cookie", | |
"Subject", | |
"To", | |
"User-Agent", | |
"Via", | |
"X-Forwarded-For", | |
"X-Imforwards", | |
"X-Powered-By", | |
} { | |
commonHeader[v] = v | |
} | |
_ = commonHeader | |
} | |
func after() { | |
// commonHeader interns common header strings. | |
var commonHeader map[string]string | |
vs := [...]string{ | |
"Accept", | |
"Accept-Charset", | |
"Accept-Encoding", | |
"Accept-Language", | |
"Accept-Ranges", | |
"Cache-Control", | |
"Cc", | |
"Connection", | |
"Content-Id", | |
"Content-Language", | |
"Content-Length", | |
"Content-Transfer-Encoding", | |
"Content-Type", | |
"Cookie", | |
"Date", | |
"Dkim-Signature", | |
"Etag", | |
"Expires", | |
"From", | |
"Host", | |
"If-Modified-Since", | |
"If-None-Match", | |
"In-Reply-To", | |
"Last-Modified", | |
"Location", | |
"Message-Id", | |
"Mime-Version", | |
"Pragma", | |
"Received", | |
"Return-Path", | |
"Server", | |
"Set-Cookie", | |
"Subject", | |
"To", | |
"User-Agent", | |
"Via", | |
"X-Forwarded-For", | |
"X-Imforwards", | |
"X-Powered-By", | |
} | |
commonHeader = make(map[string]string, len(vs)) | |
for _, v := range vs { | |
commonHeader[v] = v | |
} | |
_ = commonHeader | |
} | |
func BenchmarkBefore(b *testing.B) { | |
b.ReportAllocs() | |
for i := 0; i < b.N; i++ { | |
before() | |
} | |
} | |
func BenchmarkAfter(b *testing.B) { | |
b.ReportAllocs() | |
for i := 0; i < b.N; i++ { | |
after() | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/src/net/textproto/reader.go b/src/net/textproto/reader.go | |
index e07d1d6..464e027 100644 | |
--- a/src/net/textproto/reader.go | |
+++ b/src/net/textproto/reader.go | |
@@ -631,10 +631,10 @@ func canonicalMIMEHeaderKey(a []byte) string { | |
} | |
// commonHeader interns common header strings. | |
-var commonHeader = make(map[string]string) | |
+var commonHeader map[string]string | |
func init() { | |
- for _, v := range []string{ | |
+ vs := [...]string{ | |
"Accept", | |
"Accept-Charset", | |
"Accept-Encoding", | |
@@ -674,7 +674,9 @@ func init() { | |
"X-Forwarded-For", | |
"X-Imforwards", | |
"X-Powered-By", | |
- } { | |
+ } | |
+ commonHeader = make(map[string]string, len(vs)) | |
+ for _, v := range vs { | |
commonHeader[v] = v | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment