From fc8b25518a006c4103c7d0cba56626ce725d3462 Mon Sep 17 00:00:00 2001 From: Paul Buetow Date: Tue, 24 Feb 2026 21:34:07 +0200 Subject: flamegraph: pool FNV hasher for frame colors --- internal/flamegraph/svgwriter.go | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/internal/flamegraph/svgwriter.go b/internal/flamegraph/svgwriter.go index 5e3a3fc..4457fba 100644 --- a/internal/flamegraph/svgwriter.go +++ b/internal/flamegraph/svgwriter.go @@ -3,9 +3,11 @@ package flamegraph import ( "bufio" "fmt" + "hash" "hash/fnv" "io" "strings" + "sync" ) var svgEscaper = strings.NewReplacer( @@ -16,6 +18,12 @@ var svgEscaper = strings.NewReplacer( "'", "'", ) +var fnv32aPool = sync.Pool{ + New: func() any { + return fnv.New32a() + }, +} + type SVGConfig struct { Title string Width int @@ -135,9 +143,11 @@ func writeFrame(bw *bufio.Writer, name, title, fill string, x, y, w, h float64, } func frameColor(name string) string { - hasher := fnv.New32a() - _, _ = hasher.Write([]byte(name)) + hasher := fnv32aPool.Get().(hash.Hash32) + hasher.Reset() + _, _ = io.WriteString(hasher, name) h := hasher.Sum32() + fnv32aPool.Put(hasher) r := 200 + int(h%35) g := 80 + int((h>>8)%120) b := 40 + int((h>>16)%90) -- cgit v1.2.3