From 18a7de93d4a6d3d6a082901897f4a46c643b48c7 Mon Sep 17 00:00:00 2001 From: Lcos-000 <3349888061@qq.com> Date: Tue, 21 Jul 2026 16:16:32 +0800 Subject: [PATCH 1/2] fix(pkg/route): resolve panic in BenchmarkRoute* by resetting ctx.index --- pkg/route/routes_timing_test.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pkg/route/routes_timing_test.go b/pkg/route/routes_timing_test.go index d0f18a466..04074e964 100644 --- a/pkg/route/routes_timing_test.go +++ b/pkg/route/routes_timing_test.go @@ -627,7 +627,7 @@ func BenchmarkRouteStatic(b *testing.B) { b.ResetTimer() for i := 0; i < b.N; i++ { r.ServeHTTP(context.Background(), ctx) - // ctx.index = -1 + ctx.SetIndex(-1) } } @@ -640,7 +640,7 @@ func BenchmarkRouteParam(b *testing.B) { b.ResetTimer() for i := 0; i < b.N; i++ { r.ServeHTTP(context.Background(), ctx) - // ctx.index = -1 + ctx.SetIndex(-1) } } @@ -653,6 +653,6 @@ func BenchmarkRouteAny(b *testing.B) { b.ResetTimer() for i := 0; i < b.N; i++ { r.ServeHTTP(context.Background(), ctx) - // ctx.index = -1 + ctx.SetIndex(-1) } } From 3c6e23dba50a7e65e87b70af33420976ffdbadb3 Mon Sep 17 00:00:00 2001 From: Lcos-000 <3349888061@qq.com> Date: Tue, 21 Jul 2026 16:19:57 +0800 Subject: [PATCH 2/2] perf(pkg/route): eliminate per-request path string allocation in ServeHTTP --- pkg/route/engine.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pkg/route/engine.go b/pkg/route/engine.go index 5f2ed4bf2..c5a023bee 100644 --- a/pkg/route/engine.go +++ b/pkg/route/engine.go @@ -755,7 +755,7 @@ func (engine *Engine) ServeHTTP(c context.Context, ctx *app.RequestContext) { defer engine.recv(ctx) } - rPath := string(ctx.Request.URI().Path()) + rPath := bytesconv.B2s(ctx.Request.URI().Path()) // align with https://datatracker.ietf.org/doc/html/rfc2616#section-5.2 if len(ctx.Request.Host()) == 0 && ctx.Request.Header.IsHTTP11() && bytesconv.B2s(ctx.Request.Method()) != consts.MethodConnect { @@ -767,7 +767,7 @@ func (engine *Engine) ServeHTTP(c context.Context, ctx *app.RequestContext) { httpMethod := bytesconv.B2s(ctx.Request.Header.Method()) unescape := false if engine.options.UseRawPath { - rPath = string(ctx.Request.URI().PathOriginal()) + rPath = bytesconv.B2s(ctx.Request.URI().PathOriginal()) unescape = engine.options.UnescapePathValues }