️ Performance optimizations (#3477)

adpater / HTTPHandler
NEW
Benchmark_HTTPHandler-12    	 1762837	       640.6 ns/op	     696 B/op	      10 allocs/op
Benchmark_HTTPHandler-12    	 1924524	       616.5 ns/op	     696 B/op	      10 allocs/op
Benchmark_HTTPHandler-12    	 1838780	       650.4 ns/op	     696 B/op	      10 allocs/op
Benchmark_HTTPHandler-12    	 1876947	       644.0 ns/op	     696 B/op	      10 allocs/op
OLD
Benchmark_HTTPHandler-12    	 1864819	       667.2 ns/op	     720 B/op	      11 allocs/op
Benchmark_HTTPHandler-12    	 1892569	       677.0 ns/op	     720 B/op	      11 allocs/op
Benchmark_HTTPHandler-12    	 1811704	       639.5 ns/op	     720 B/op	      11 allocs/op
Benchmark_HTTPHandler-12    	 1879849	       644.0 ns/op	     720 B/op	      11 allocs/op

Utils / IsNoCache
NEW
Benchmark_Utils_IsNoCache-12    	44307204	        27.08 ns/op	       0 B/op	       0 allocs/op
Benchmark_Utils_IsNoCache-12    	40782919	        26.88 ns/op	       0 B/op	       0 allocs/op
Benchmark_Utils_IsNoCache-12    	44228217	        26.69 ns/op	       0 B/op	       0 allocs/op
Benchmark_Utils_IsNoCache-12    	45605700	        26.75 ns/op	       0 B/op	       0 allocs/op
OLD
Benchmark_Utils_IsNoCache-12    	30043908	        37.80 ns/op	       0 B/op	       0 allocs/op
Benchmark_Utils_IsNoCache-12    	32137476	        37.51 ns/op	       0 B/op	       0 allocs/op
Benchmark_Utils_IsNoCache-12    	31474653	        37.92 ns/op	       0 B/op	       0 allocs/op
Benchmark_Utils_IsNoCache-12    	31838683	        37.71 ns/op	       0 B/op	       0 allocs/op
This commit is contained in:
RW 2025-05-25 17:23:03 +02:00 committed by GitHub
parent 9c123ce957
commit e722d82206
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 54 additions and 21 deletions

View File

@ -603,28 +603,30 @@ const noCacheValue = "no-cache"
// isNoCache checks if the cacheControl header value is a `no-cache`.
func isNoCache(cacheControl string) bool {
i := strings.Index(cacheControl, noCacheValue)
if i == -1 {
return false
n := len(cacheControl)
ncLen := len(noCacheValue)
for i := 0; i < n; i++ {
if cacheControl[i] != 'n' {
continue
}
if i+ncLen > n {
return false
}
if cacheControl[i:i+ncLen] != noCacheValue {
continue
}
if i > 0 {
prev := cacheControl[i-1]
if prev != ' ' && prev != ',' {
continue
}
}
if i+ncLen == n || cacheControl[i+ncLen] == ',' {
return true
}
}
// Xno-cache
if i > 0 && !(cacheControl[i-1] == ' ' || cacheControl[i-1] == ',') {
return false
}
// bla bla, no-cache
if i+len(noCacheValue) == len(cacheControl) {
return true
}
// bla bla, no-cacheX
if cacheControl[i+len(noCacheValue)] != ',' {
return false
}
// OK
return true
return false
}
var errTestConnClosed = errors.New("testConn is closed")

View File

@ -32,8 +32,8 @@ func HTTPHandlerFunc(h http.HandlerFunc) fiber.Handler {
// HTTPHandler wraps net/http handler to fiber handler
func HTTPHandler(h http.Handler) fiber.Handler {
handler := fasthttpadaptor.NewFastHTTPHandler(h)
return func(c fiber.Ctx) error {
handler := fasthttpadaptor.NewFastHTTPHandler(h)
handler(c.RequestCtx())
return nil
}

View File

@ -634,3 +634,34 @@ func Benchmark_FiberHandlerFunc_Parallel(b *testing.B) {
})
}
}
func Benchmark_HTTPHandler(b *testing.B) {
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
w.Write([]byte("ok")) //nolint:errcheck // not needed
})
var err error
app := fiber.New()
ctx := app.AcquireCtx(&fasthttp.RequestCtx{})
defer func() {
app.ReleaseCtx(ctx)
}()
b.ReportAllocs()
b.ResetTimer()
fiberHandler := HTTPHandler(handler)
for i := 0; i < b.N; i++ {
ctx.Request().Reset()
ctx.Response().Reset()
ctx.Request().SetRequestURI("/test")
ctx.Request().Header.SetMethod("GET")
err = fiberHandler(ctx)
}
require.NoError(b, err)
}