mirror of
https://github.com/golang/go
synced 2024-11-25 12:27:57 -07:00
net/http: avoid fmt.Fprintf in Header.WriteSubset
R=golang-dev, dsymonds, r CC=golang-dev https://golang.org/cl/6242062
This commit is contained in:
parent
1e814df79b
commit
0605c0c656
@ -5,7 +5,6 @@
|
||||
package http
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"net/textproto"
|
||||
"sort"
|
||||
@ -61,7 +60,7 @@ var headerNewlineToSpace = strings.NewReplacer("\n", " ", "\r", " ")
|
||||
func (h Header) WriteSubset(w io.Writer, exclude map[string]bool) error {
|
||||
keys := make([]string, 0, len(h))
|
||||
for k := range h {
|
||||
if exclude == nil || !exclude[k] {
|
||||
if !exclude[k] {
|
||||
keys = append(keys, k)
|
||||
}
|
||||
}
|
||||
@ -70,8 +69,10 @@ func (h Header) WriteSubset(w io.Writer, exclude map[string]bool) error {
|
||||
for _, v := range h[k] {
|
||||
v = headerNewlineToSpace.Replace(v)
|
||||
v = strings.TrimSpace(v)
|
||||
if _, err := fmt.Fprintf(w, "%s: %s\r\n", k, v); err != nil {
|
||||
return err
|
||||
for _, s := range []string{k, ": ", v, "\r\n"} {
|
||||
if _, err := io.WriteString(w, s); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -122,3 +122,17 @@ func TestHasToken(t *testing.T) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkHeaderWriteSubset(b *testing.B) {
|
||||
h := Header(map[string][]string{
|
||||
"Content-Length": {"123"},
|
||||
"Content-Type": {"text/plain"},
|
||||
"Date": {"some date at some time Z"},
|
||||
"Server": {"Go http package"},
|
||||
})
|
||||
var buf bytes.Buffer
|
||||
for i := 0; i < b.N; i++ {
|
||||
buf.Reset()
|
||||
h.WriteSubset(&buf, nil)
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user