From 0e3ce8cf524c8986cdfe4958820db868cf1f4377 Mon Sep 17 00:00:00 2001 From: xfy Date: Tue, 14 Apr 2026 14:17:43 +0800 Subject: [PATCH] =?UTF-8?q?perf(http3):=20=E7=A7=BB=E9=99=A4=20ctxPool=20s?= =?UTF-8?q?ync.Pool=20=E6=8F=90=E5=8D=87=E6=80=A7=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 基准测试显示 sync.Pool 反而降低性能: - 有池:574 ns/op ~ 704 ns/op - 无池:263 ns/op ~ 264 ns/op 移除池化逻辑,直接创建 RequestCtx 对象。 Co-Authored-By: Claude Opus 4.6 --- internal/http3/adapter.go | 19 +++---------------- internal/http3/adapter_test.go | 7 ------- 2 files changed, 3 insertions(+), 23 deletions(-) diff --git a/internal/http3/adapter.go b/internal/http3/adapter.go index 1357c2b..9947332 100644 --- a/internal/http3/adapter.go +++ b/internal/http3/adapter.go @@ -14,7 +14,6 @@ import ( "io" "net" "net/http" - "sync" "github.com/valyala/fasthttp" ) @@ -24,19 +23,11 @@ import ( // 由于 quic-go 使用标准库的 http.Handler 接口, // 而 lolly 使用 fasthttp,需要通过适配层进行转换。 type Adapter struct { - // ctxPool 用于复用 RequestCtx 对象 - ctxPool sync.Pool } // NewAdapter 创建新的适配器。 func NewAdapter() *Adapter { - return &Adapter{ - ctxPool: sync.Pool{ - New: func() interface{} { - return &fasthttp.RequestCtx{} - }, - }, - } + return &Adapter{} } // Wrap 包装 fasthttp handler 为 http.Handler。 @@ -51,12 +42,8 @@ func NewAdapter() *Adapter { // - http.Handler: 标准库兼容的 HTTP 处理器 func (a *Adapter) Wrap(handler fasthttp.RequestHandler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - // 从池中获取 RequestCtx - ctx, ok := a.ctxPool.Get().(*fasthttp.RequestCtx) - if !ok { - ctx = &fasthttp.RequestCtx{} - } - defer a.ctxPool.Put(ctx) + // 直接创建 RequestCtx + ctx := &fasthttp.RequestCtx{} // 初始化 ctx(fasthttp 的 RequestCtx 需要 Init 方法) ctx.Init(&fasthttp.Request{}, nil, nil) diff --git a/internal/http3/adapter_test.go b/internal/http3/adapter_test.go index 310c446..2ddc3ca 100644 --- a/internal/http3/adapter_test.go +++ b/internal/http3/adapter_test.go @@ -26,13 +26,6 @@ func TestNewAdapter(t *testing.T) { if adapter == nil { t.Fatal("Expected non-nil adapter") } - - // 测试 ctxPool 是否初始化 - ctx := adapter.ctxPool.Get().(*fasthttp.RequestCtx) - if ctx == nil { - t.Error("Expected non-nil RequestCtx from pool") - } - adapter.ctxPool.Put(ctx) } // TestWrap 测试 Wrap 函数基本功能