perf(http3): 移除 ctxPool sync.Pool 提升性能

基准测试显示 sync.Pool 反而降低性能:
- 有池:574 ns/op ~ 704 ns/op
- 无池:263 ns/op ~ 264 ns/op

移除池化逻辑,直接创建 RequestCtx 对象。

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
xfy 2026-04-14 14:17:43 +08:00
parent 9f04b92a75
commit 0e3ce8cf52
2 changed files with 3 additions and 23 deletions

View File

@ -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{}
// 初始化 ctxfasthttp 的 RequestCtx 需要 Init 方法)
ctx.Init(&fasthttp.Request{}, nil, nil)

View File

@ -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 函数基本功能