Skip to content

Commit 3304e73

Browse files
committed
Revert "net/http/internal/http2: prevent alloc when writing status code for responses"
This reverts commit e67d773. Reason for revert: CL 769100 reverts CL 762040. Therefore, CL 762140 needs to be reverted too. For #78833 Change-Id: I5f836e0c0950a596c88a5d53d6d705656a6a6964 Reviewed-on: https://go-review.googlesource.com/c/go/+/769201 LUCI-TryBot-Result: golang-scoped@luci-project-accounts.iam.gserviceaccount.com <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Nicholas Husin <husin@google.com> Reviewed-by: Damien Neil <dneil@google.com>
1 parent 2c65d87 commit 3304e73

2 files changed

Lines changed: 14 additions & 5 deletions

File tree

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"net"
2222
"os"
2323
"slices"
24+
"strconv"
2425
"strings"
2526
"sync"
2627
"time"
@@ -214,6 +215,17 @@ func validWireHeaderFieldName(v string) bool {
214215
return true
215216
}
216217

218+
// TODO: avoid alloc when code is neither 200 nor 404.
219+
func httpCodeString(code int) string {
220+
switch code {
221+
case 200:
222+
return "200"
223+
case 404:
224+
return "404"
225+
}
226+
return strconv.Itoa(code)
227+
}
228+
217229
// A closeWaiter is like a sync.WaitGroup but only goes 1 to 0 (open to closed).
218230
type closeWaiter chan struct{}
219231

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

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

1614
"golang.org/x/net/http/httpguts"
1715
"golang.org/x/net/http2/hpack"
@@ -202,7 +200,7 @@ type writeResHeaders struct {
202200

203201
func encKV(enc *hpack.Encoder, k, v string) {
204202
if VerboseLogs {
205-
log.Printf("http2: server encoding header %q = %q", strings.Clone(k), strings.Clone(v))
203+
log.Printf("http2: server encoding header %q = %q", k, v)
206204
}
207205
enc.WriteField(hpack.HeaderField{Name: k, Value: v})
208206
}
@@ -223,8 +221,7 @@ func (w *writeResHeaders) writeFrame(ctx writeContext) error {
223221
buf.Reset()
224222

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

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

0 commit comments

Comments
 (0)