middleware/pprof: improve performance (#2709)

*  middleware/pprof: improve performance

Concatenate the custom and fixed prefixes beforehand, so the trimmed path can be switched on against constant strings.

goos: linux
goarch: amd64
pkg: github.com/gofiber/fiber/v2/middleware/pprof
cpu: 13th Gen Intel(R) Core(TM) i9-13900K
BenchmarkPprof/Slow-32         	 4912642	       246.3 ns/op	     480 B/op	      10 allocs/op
BenchmarkPprof/Fast-32         	411908472	         2.913 ns/op	       0 B/op	       0 allocs/op
PASS

* 🌂 middleware/pprof: disable nonamedreturns linter on cutPrefix
pull/2716/head
database64128 2023-11-09 15:05:54 +08:00 committed by GitHub
parent 862ea7dbf6
commit b99712f13e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 29 additions and 14 deletions

View File

@ -29,6 +29,9 @@ func New(config ...Config) fiber.Handler {
pprofThreadcreate = fasthttpadaptor.NewFastHTTPHandlerFunc(pprof.Handler("threadcreate").ServeHTTP) pprofThreadcreate = fasthttpadaptor.NewFastHTTPHandlerFunc(pprof.Handler("threadcreate").ServeHTTP)
) )
// Construct actual prefix
prefix := cfg.Prefix + "/debug/pprof"
// Return new handler // Return new handler
return func(c *fiber.Ctx) error { return func(c *fiber.Ctx) error {
// Don't execute middleware if Next returns true // Don't execute middleware if Next returns true
@ -38,39 +41,40 @@ func New(config ...Config) fiber.Handler {
path := c.Path() path := c.Path()
// We are only interested in /debug/pprof routes // We are only interested in /debug/pprof routes
if len(path) < 12 || !strings.HasPrefix(path, cfg.Prefix+"/debug/pprof") { path, found := cutPrefix(path, prefix)
if !found {
return c.Next() return c.Next()
} }
// Switch to original path without stripped slashes // Switch on trimmed path against constant strings
switch path { switch path {
case cfg.Prefix + "/debug/pprof/": case "/":
pprofIndex(c.Context()) pprofIndex(c.Context())
case cfg.Prefix + "/debug/pprof/cmdline": case "/cmdline":
pprofCmdline(c.Context()) pprofCmdline(c.Context())
case cfg.Prefix + "/debug/pprof/profile": case "/profile":
pprofProfile(c.Context()) pprofProfile(c.Context())
case cfg.Prefix + "/debug/pprof/symbol": case "/symbol":
pprofSymbol(c.Context()) pprofSymbol(c.Context())
case cfg.Prefix + "/debug/pprof/trace": case "/trace":
pprofTrace(c.Context()) pprofTrace(c.Context())
case cfg.Prefix + "/debug/pprof/allocs": case "/allocs":
pprofAllocs(c.Context()) pprofAllocs(c.Context())
case cfg.Prefix + "/debug/pprof/block": case "/block":
pprofBlock(c.Context()) pprofBlock(c.Context())
case cfg.Prefix + "/debug/pprof/goroutine": case "/goroutine":
pprofGoroutine(c.Context()) pprofGoroutine(c.Context())
case cfg.Prefix + "/debug/pprof/heap": case "/heap":
pprofHeap(c.Context()) pprofHeap(c.Context())
case cfg.Prefix + "/debug/pprof/mutex": case "/mutex":
pprofMutex(c.Context()) pprofMutex(c.Context())
case cfg.Prefix + "/debug/pprof/threadcreate": case "/threadcreate":
pprofThreadcreate(c.Context()) pprofThreadcreate(c.Context())
default: default:
// pprof index only works with trailing slash // pprof index only works with trailing slash
if strings.HasSuffix(path, "/") { if strings.HasSuffix(path, "/") {
path = strings.TrimRight(path, "/") path = strings.TrimRight(path, "/")
} else { } else {
path = cfg.Prefix + "/debug/pprof/" path = prefix + "/"
} }
return c.Redirect(path, fiber.StatusFound) return c.Redirect(path, fiber.StatusFound)
@ -78,3 +82,14 @@ func New(config ...Config) fiber.Handler {
return nil return nil
} }
} }
// cutPrefix is a copy of [strings.CutPrefix] added in Go 1.20.
// Remove this function when we drop support for Go 1.19.
//
//nolint:nonamedreturns // Align with its original form in std.
func cutPrefix(s, prefix string) (after string, found bool) {
if !strings.HasPrefix(s, prefix) {
return s, false
}
return s[len(prefix):], true
}