Skip to content

Commit e67d773

Browse files
committed
net/http/internal/http2: prevent alloc when writing status code for responses
Previously, writing responses with non-200 and non-404 status code requires an allocation. Now that CL 762040 prevents header names and values from escaping, we can modify writeFrame to not allocate status codes on the heap. Change-Id: I230bed1b83627c1fb389c0507106d8e16a6a6964 Reviewed-on: https://go-review.googlesource.com/c/go/+/762140 Reviewed-by: Nicholas Husin <husin@google.com> Reviewed-by: Damien Neil <dneil@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
1 parent c0598ed commit e67d773

2 files changed

Lines changed: 5 additions & 13 deletions

File tree

src/net/http/internal/http2/http2.go

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import (
2121
"net"
2222
"os"
2323
"slices"
24-
"strconv"
2524
"strings"
2625
"sync"
2726
"time"
@@ -215,16 +214,6 @@ func validWireHeaderFieldName(v string) bool {
215214
return true
216215
}
217216

218-
func httpCodeString(code int) string {
219-
switch code {
220-
case 200:
221-
return "200"
222-
case 404:
223-
return "404"
224-
}
225-
return strconv.Itoa(code)
226-
}
227-
228217
// A closeWaiter is like a sync.WaitGroup but only goes 1 to 0 (open to closed).
229218
type closeWaiter chan struct{}
230219

src/net/http/internal/http2/write.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import (
1010
"log"
1111
"net/http/internal/httpcommon"
1212
"net/url"
13+
"strconv"
14+
"strings"
1315

1416
"golang.org/x/net/http/httpguts"
1517
"golang.org/x/net/http2/hpack"
@@ -200,7 +202,7 @@ type writeResHeaders struct {
200202

201203
func encKV(enc *hpack.Encoder, k, v string) {
202204
if VerboseLogs {
203-
log.Printf("http2: server encoding header %q = %q", k, v)
205+
log.Printf("http2: server encoding header %q = %q", strings.Clone(k), strings.Clone(v))
204206
}
205207
enc.WriteField(hpack.HeaderField{Name: k, Value: v})
206208
}
@@ -221,7 +223,8 @@ func (w *writeResHeaders) writeFrame(ctx writeContext) error {
221223
buf.Reset()
222224

223225
if w.httpResCode != 0 {
224-
encKV(enc, ":status", httpCodeString(w.httpResCode))
226+
codeBuf := strconv.AppendInt(make([]byte, 0, 3), int64(w.httpResCode), 10)
227+
encKV(enc, ":status", string(codeBuf))
225228
}
226229

227230
encodeHeaders(enc, w.h, w.trailers)

0 commit comments

Comments
 (0)