From c96b4e9eca7084c555495e586192de3e8ae19485 Mon Sep 17 00:00:00 2001 From: sabandi Date: Sat, 26 Sep 2020 23:10:16 +0530 Subject: [PATCH] Reduce allocations while creating a slice of keys from map and fix a typo --- fgprof.go | 2 +- format.go | 7 +++++-- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/fgprof.go b/fgprof.go index 97787c2..31871c8 100644 --- a/fgprof.go +++ b/fgprof.go @@ -69,7 +69,7 @@ func (p *profiler) GoroutineProfile() []runtime.StackRecord { // We don't know how many goroutines exist, so we have to grow p.stacks // dynamically. We overshoot by 10% since it's possible that more goroutines // are launched in between two calls to GoroutineProfile. Once p.stacks - // reaches the maximum numnber of goroutines used by the program, it will get + // reaches the maximum number of goroutines used by the program, it will get // reused indefinitely, eliminating GoroutineProfile calls and allocations. // // TODO(fg) There might be workloads where it would be nice to shrink diff --git a/format.go b/format.go index 448e0a2..c9bfe9f 100644 --- a/format.go +++ b/format.go @@ -9,6 +9,7 @@ import ( "github.com/google/pprof/profile" ) +// Format decides how the ouput is rendered to the user. type Format string const ( @@ -93,9 +94,11 @@ func toPprof(s map[string]int, hz int) *profile.Profile { } func sortedKeys(s map[string]int) []string { - var keys []string + keys := make([]string, len(s)) + i := 0 for stack := range s { - keys = append(keys, stack) + keys[i] = stack + i++ } sort.Strings(keys) return keys