Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

⚡ middleware/pprof: improve performance #2709

Merged
merged 2 commits into from Nov 9, 2023

Conversation

database64128
Copy link
Contributor

Description

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

Here's a simple benchmark:

package pprof

import (
	"crypto/rand"
	"errors"
	"strings"
	"testing"
	"unsafe"
)

var errBadPath = errors.New("bad path")

func doSlow(prefix, path string) error {
	if len(path) < 12 || !strings.HasPrefix(path, prefix+"/debug/pprof") {
		return errBadPath
	}
	// Switch to original path without stripped slashes
	switch path {
	case prefix + "/debug/pprof/":
		return errBadPath
	case prefix + "/debug/pprof/cmdline":
		return errBadPath
	case prefix + "/debug/pprof/profile":
		return errBadPath
	case prefix + "/debug/pprof/symbol":
		return errBadPath
	case prefix + "/debug/pprof/trace":
		return errBadPath
	case prefix + "/debug/pprof/allocs":
		return errBadPath
	case prefix + "/debug/pprof/block":
		return errBadPath
	case prefix + "/debug/pprof/goroutine":
		return errBadPath
	case prefix + "/debug/pprof/heap":
		return errBadPath
	case prefix + "/debug/pprof/mutex":
		return errBadPath
	case prefix + "/debug/pprof/threadcreate":
		return errBadPath
	default:
		return nil
	}
}

func newSlowBenchFunc(prefix, path string) func(b *testing.B) {
	return func(b *testing.B) {
		for i := 0; i < b.N; i++ {
			if err := doSlow(prefix, path); err != nil {
				b.Error(err)
			}
		}
	}
}

func doFast(prefix, path string) error {
	path, found := strings.CutPrefix(path, prefix)
	if !found {
		return errBadPath
	}
	// Switch to original path without stripped slashes
	switch path {
	case "", "/":
		return errBadPath
	case "/cmdline":
		return errBadPath
	case "/profile":
		return errBadPath
	case "/symbol":
		return errBadPath
	case "/trace":
		return errBadPath
	case "/allocs":
		return errBadPath
	case "/block":
		return errBadPath
	case "/goroutine":
		return errBadPath
	case "/heap":
		return errBadPath
	case "/mutex":
		return errBadPath
	case "/threadcreate":
		return errBadPath
	default:
		return nil
	}

}

func newFastBenchFunc(prefix, path string) func(b *testing.B) {
	prefix += "/debug/pprof"
	return func(b *testing.B) {
		for i := 0; i < b.N; i++ {
			if err := doFast(prefix, path); err != nil {
				b.Error(err)
			}
		}
	}
}

func BenchmarkPprof(b *testing.B) {
	buf := make([]byte, 16)
	if _, err := rand.Read(buf); err != nil {
		b.Fatal(err)
	}
	prefix := unsafe.String(unsafe.SliceData(buf), len(buf))
	path := prefix + "/debug/pprof/nonexistent"

	b.Run("Slow", newSlowBenchFunc(prefix, path))
	b.Run("Fast", newFastBenchFunc(prefix, path))
}
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

To the maintainers: These slow patterns introduced by #2194 were quite obvious, and it would've been nice if they were called out when the PR was being reviewed. Thanks!

Type of change

Please delete options that are not relevant.

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

Checklist:

  • For new functionalities I follow the inspiration of the express js framework and built them similar in usage
  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation - /docs/ directory for https://docs.gofiber.io/
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes
  • If new dependencies exist, I have checked that they are really necessary and agreed with the maintainers/community (we want to have as few dependencies as possible)
  • I tried to make my code as fast as possible with as few allocations as possible
  • For new code I have written benchmarks so that they can be analyzed and improved

Commit formatting:

Use emojis on commit messages so it provides an easy way of identifying the purpose or intention of a commit. Check out the emoji cheatsheet here: https://gitmoji.carloscuesta.me/

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
Copy link

welcome bot commented Nov 6, 2023

Thanks for opening this pull request! 🎉 Please check out our contributing guidelines. If you need help or want to chat with us, join us on Discord https://gofiber.io/discord

Copy link
Member

@efectn efectn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice catch

@ReneWerner87 ReneWerner87 added this to the v2 Next Release milestone Nov 7, 2023
Copy link
Member

@gaby gaby left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@ReneWerner87 ReneWerner87 merged commit b99712f into gofiber:master Nov 9, 2023
20 checks passed
Copy link

welcome bot commented Nov 9, 2023

Congrats on merging your first pull request! 🎉 We here at Fiber are proud of you! If you need help or want to chat with us, join us on Discord https://gofiber.io/discord

@database64128 database64128 deleted the pprof-perf branch November 10, 2023 17:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants