[go] net/http/internal/http2: reuse responseWriterState.snapHeader

4 views
Skip to first unread message

qiu laidongfeng (Gerrit)

unread,
Aug 6, 2026, 2:02:27 AM (4 days ago) Aug 6
to goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com

qiu laidongfeng has uploaded the change for review

Commit message

net/http/internal/http2: reuse responseWriterState.snapHeader

This is part of CL 676155 and no longer reuse handlerHeader.

goos: windows
goarch: amd64
pkg: net/http
cpu: AMD Ryzen 7 7840HS w/ Radeon 780M Graphics
│ old │ new │
│ sec/op │ sec/op vs base │
SnapHeaderH2-16 138.2µ ± 4% 132.7µ ± 3% -3.99% (p=0.043 n=10)

│ old │ new │
│ B/op │ B/op vs base │
SnapHeaderH2-16 9.702Ki ± 0% 8.938Ki ± 0% -7.88% (p=0.000 n=10)

│ old │ new │
│ allocs/op │ allocs/op vs base │
SnapHeaderH2-16 105.0 ± 1% 101.0 ± 0% -3.81% (p=0.000 n=10)
Change-Id: I39447716470d07764bd7d043f391b27cc2f07320

Change diff

diff --git a/src/net/http/internal/http2/server.go b/src/net/http/internal/http2/server.go
index aed43b2..827d217 100644
--- a/src/net/http/internal/http2/server.go
+++ b/src/net/http/internal/http2/server.go
@@ -2569,6 +2569,13 @@
if !rws.wroteHeader {
rws.writeHeader(200)
}
+ defer func() {
+ if rws.snapHeader != nil {
+ clear(rws.snapHeader)
+ headerPool.Put(rws.snapHeader)
+ rws.snapHeader = nil
+ }
+ }()

if rws.handlerDone {
rws.promoteUndeclaredTrailers()
@@ -2898,8 +2905,12 @@
}
}

+var headerPool = sync.Pool{
+ New: func() any { return make(Header) },
+}
+
func cloneHeader(h Header) Header {
- h2 := make(Header, len(h))
+ h2 := headerPool.Get().(Header)
for k, vv := range h {
vv2 := make([]string, len(vv))
copy(vv2, vv)
diff --git a/src/net/http/request_test.go b/src/net/http/request_test.go
index 37b8883..eafe7da 100644
--- a/src/net/http/request_test.go
+++ b/src/net/http/request_test.go
@@ -1510,6 +1510,39 @@
}
}

+func BenchmarkSnapHeaderH2(b *testing.B) {
+ handler := HandlerFunc(func(rw ResponseWriter, req *Request) {
+ defer req.Body.Close()
+ rw.Header().Add("Cache-Control", "no-cache")
+ rw.Header().Add("Access-Control-Allow-Origin", "example.com")
+ rw.Header().Add("Access-Control-Allow-Methods", "GET,POST,OPTIONS")
+ rw.Header().Add("Access-Control-Allow-Headers", "X-Token")
+ rw.Header().Add("X-Request-ID", "")
+ rw.Header().Set("Content-Security-Policy", "default-src 'self'; script-src 'self' 'unsafe-inline'")
+ rw.Header().Set("Strict-Transport-Security", "max-age=31536000; includeSubDomains; preload")
+ rw.Header().Set("X-Content-Type-Options", "nosniff")
+ rw.Header().Set("X-Frame-Options", "SAMEORIGIN")
+ rw.Header().Set("Referrer-Policy", "strict-origin-when-cross-origin")
+
+ io.WriteString(rw, "response")
+ })
+
+ cst := newClientServerTest(b, http2Mode, handler).ts
+
+ b.ResetTimer()
+ for i := 0; i < b.N; i++ {
+ req, err := NewRequest("GET", cst.URL, nil)
+ if err != nil {
+ b.Fatal(err)
+ }
+ res, err := cst.Client().Do(req)
+ if err != nil {
+ b.Fatalf("Failed to make request to backend: %v", err)
+ }
+ res.Body.Close()
+ }
+}
+
func TestErrNotSupported(t *testing.T) {
if !errors.Is(ErrNotSupported, errors.ErrUnsupported) {
t.Error("errors.Is(ErrNotSupported, errors.ErrUnsupported) failed")

Change information

Files:
  • M src/net/http/internal/http2/server.go
  • M src/net/http/request_test.go
Change size: S
Delta: 2 files changed, 45 insertions(+), 1 deletion(-)
Open in Gerrit

Related details

Attention set is empty
Submit Requirements:
  • requirement is not satisfiedCode-Review
  • requirement satisfiedNo-Unresolved-Comments
  • requirement is not satisfiedReview-Enforcement
  • requirement is not satisfiedTryBots-Pass
Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
Gerrit-MessageType: newchange
Gerrit-Project: go
Gerrit-Branch: master
Gerrit-Change-Id: I39447716470d07764bd7d043f391b27cc2f07320
Gerrit-Change-Number: 811240
Gerrit-PatchSet: 1
Gerrit-Owner: qiu laidongfeng <26454...@qq.com>
Gerrit-Reviewer: qiu laidongfeng <26454...@qq.com>
unsatisfied_requirement
satisfied_requirement
open
diffy

qiu laidongfeng (Gerrit)

unread,
Aug 8, 2026, 1:00:03 AM (2 days ago) Aug 8
to goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com
Attention needed from Damien Neil, Nicholas Husin and qiu laidongfeng

qiu laidongfeng uploaded new patchset

qiu laidongfeng uploaded patch set #2 to this change.
Following approvals got outdated and were removed:
Open in Gerrit

Related details

Attention is currently required from:
  • Damien Neil
  • Nicholas Husin
  • qiu laidongfeng
Submit Requirements:
  • requirement is not satisfiedCode-Review
  • requirement satisfiedNo-Unresolved-Comments
  • requirement is not satisfiedReview-Enforcement
  • requirement is not satisfiedTryBots-Pass
Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
Gerrit-MessageType: newpatchset
Gerrit-Project: go
Gerrit-Branch: master
Gerrit-Change-Id: I39447716470d07764bd7d043f391b27cc2f07320
Gerrit-Change-Number: 811240
Gerrit-PatchSet: 2
Gerrit-Owner: qiu laidongfeng <26454...@qq.com>
Gerrit-Reviewer: Damien Neil <dn...@google.com>
Gerrit-Reviewer: Nicholas Husin <n...@golang.org>
Gerrit-Reviewer: qiu laidongfeng <26454...@qq.com>
Gerrit-CC: Gopher Robot <go...@golang.org>
Gerrit-CC: Russ Cox <r...@golang.org>
Gerrit-Attention: qiu laidongfeng <26454...@qq.com>
Gerrit-Attention: Damien Neil <dn...@google.com>
Gerrit-Attention: Nicholas Husin <n...@golang.org>
unsatisfied_requirement
satisfied_requirement
open
diffy

qiu laidongfeng (Gerrit)

unread,
3:23 AM (3 hours ago) 3:23 AM
to goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com
Attention needed from Damien Neil, Nicholas Husin and qiu laidongfeng

qiu laidongfeng uploaded new patchset

qiu laidongfeng uploaded patch set #3 to this change.
Following approvals got outdated and were removed:
Open in Gerrit

Related details

Attention is currently required from:
  • Damien Neil
  • Nicholas Husin
  • qiu laidongfeng
Submit Requirements:
  • requirement is not satisfiedCode-Review
  • requirement satisfiedNo-Unresolved-Comments
  • requirement is not satisfiedReview-Enforcement
  • requirement is not satisfiedTryBots-Pass
Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
Gerrit-MessageType: newpatchset
Gerrit-Project: go
Gerrit-Branch: master
Gerrit-Change-Id: I39447716470d07764bd7d043f391b27cc2f07320
Gerrit-Change-Number: 811240
Gerrit-PatchSet: 3
unsatisfied_requirement
satisfied_requirement
open
diffy

qiu laidongfeng (Gerrit)

unread,
3:23 AM (3 hours ago) 3:23 AM
to goph...@pubsubhelper.golang.org, golang...@luci-project-accounts.iam.gserviceaccount.com, Damien Neil, Nicholas Husin, Russ Cox, Gopher Robot, golang-co...@googlegroups.com
Attention needed from Damien Neil and Nicholas Husin

qiu laidongfeng voted Commit-Queue+1

Commit-Queue+1
Open in Gerrit

Related details

Attention is currently required from:
  • Damien Neil
  • Nicholas Husin
Submit Requirements:
  • requirement is not satisfiedCode-Review
  • requirement satisfiedNo-Unresolved-Comments
  • requirement is not satisfiedReview-Enforcement
  • requirement is not satisfiedTryBots-Pass
Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
Gerrit-MessageType: comment
Gerrit-Project: go
Gerrit-Branch: master
Gerrit-Change-Id: I39447716470d07764bd7d043f391b27cc2f07320
Gerrit-Change-Number: 811240
Gerrit-PatchSet: 3
Gerrit-Owner: qiu laidongfeng <26454...@qq.com>
Gerrit-Reviewer: Damien Neil <dn...@google.com>
Gerrit-Reviewer: Nicholas Husin <n...@golang.org>
Gerrit-Reviewer: qiu laidongfeng <26454...@qq.com>
Gerrit-CC: Gopher Robot <go...@golang.org>
Gerrit-CC: Russ Cox <r...@golang.org>
Gerrit-Attention: Damien Neil <dn...@google.com>
Gerrit-Attention: Nicholas Husin <n...@golang.org>
Gerrit-Comment-Date: Mon, 10 Aug 2026 07:23:44 +0000
Gerrit-HasComments: No
Gerrit-Has-Labels: Yes
unsatisfied_requirement
satisfied_requirement
open
diffy
Reply all
Reply to author
Forward
0 new messages