Skip to content

Commit e722d82

Browse files
authored
⚡️ 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
1 parent 9c123ce commit e722d82

File tree

3 files changed

+54
-21
lines changed

3 files changed

+54
-21
lines changed

helpers.go

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -603,28 +603,30 @@ const noCacheValue = "no-cache"
603603

604604
// isNoCache checks if the cacheControl header value is a `no-cache`.
605605
func isNoCache(cacheControl string) bool {
606-
i := strings.Index(cacheControl, noCacheValue)
607-
if i == -1 {
608-
return false
609-
}
610-
611-
// Xno-cache
612-
if i > 0 && !(cacheControl[i-1] == ' ' || cacheControl[i-1] == ',') {
613-
return false
614-
}
615-
616-
// bla bla, no-cache
617-
if i+len(noCacheValue) == len(cacheControl) {
618-
return true
619-
}
620-
621-
// bla bla, no-cacheX
622-
if cacheControl[i+len(noCacheValue)] != ',' {
623-
return false
606+
n := len(cacheControl)
607+
ncLen := len(noCacheValue)
608+
for i := 0; i < n; i++ {
609+
if cacheControl[i] != 'n' {
610+
continue
611+
}
612+
if i+ncLen > n {
613+
return false
614+
}
615+
if cacheControl[i:i+ncLen] != noCacheValue {
616+
continue
617+
}
618+
if i > 0 {
619+
prev := cacheControl[i-1]
620+
if prev != ' ' && prev != ',' {
621+
continue
622+
}
623+
}
624+
if i+ncLen == n || cacheControl[i+ncLen] == ',' {
625+
return true
626+
}
624627
}
625628

626-
// OK
627-
return true
629+
return false
628630
}
629631

630632
var errTestConnClosed = errors.New("testConn is closed")

middleware/adaptor/adaptor.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ func HTTPHandlerFunc(h http.HandlerFunc) fiber.Handler {
3232

3333
// HTTPHandler wraps net/http handler to fiber handler
3434
func HTTPHandler(h http.Handler) fiber.Handler {
35+
handler := fasthttpadaptor.NewFastHTTPHandler(h)
3536
return func(c fiber.Ctx) error {
36-
handler := fasthttpadaptor.NewFastHTTPHandler(h)
3737
handler(c.RequestCtx())
3838
return nil
3939
}

middleware/adaptor/adaptor_test.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -634,3 +634,34 @@ func Benchmark_FiberHandlerFunc_Parallel(b *testing.B) {
634634
})
635635
}
636636
}
637+
638+
func Benchmark_HTTPHandler(b *testing.B) {
639+
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
640+
w.WriteHeader(http.StatusOK)
641+
w.Write([]byte("ok")) //nolint:errcheck // not needed
642+
})
643+
644+
var err error
645+
app := fiber.New()
646+
647+
ctx := app.AcquireCtx(&fasthttp.RequestCtx{})
648+
defer func() {
649+
app.ReleaseCtx(ctx)
650+
}()
651+
652+
b.ReportAllocs()
653+
b.ResetTimer()
654+
655+
fiberHandler := HTTPHandler(handler)
656+
657+
for i := 0; i < b.N; i++ {
658+
ctx.Request().Reset()
659+
ctx.Response().Reset()
660+
ctx.Request().SetRequestURI("/test")
661+
ctx.Request().Header.SetMethod("GET")
662+
663+
err = fiberHandler(ctx)
664+
}
665+
666+
require.NoError(b, err)
667+
}

0 commit comments

Comments
 (0)