From bf9f27976892b2889c58e020fc6afa6dd97dd8ec Mon Sep 17 00:00:00 2001 From: Brad Fitzpatrick Date: Mon, 5 Jul 2021 21:28:54 -0700 Subject: [PATCH] util/deephash: optimize CPU a bit by by avoiding fmt in more places MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit name old time/op new time/op delta Hash-6 179µs ± 5% 173µs ± 4% -3.12% (p=0.004 n=10+10) HashMapAcyclic-6 115µs ± 3% 101µs ± 5% -11.51% (p=0.000 n=9+9) TailcfgNode-6 30.8µs ± 4% 29.4µs ± 2% -4.51% (p=0.000 n=10+8) name old alloc/op new alloc/op delta Hash-6 3.60kB ± 0% 3.60kB ± 0% ~ (p=0.445 n=9+10) HashMapAcyclic-6 2.53kB ± 0% 2.53kB ± 0% ~ (p=0.065 n=9+10) TailcfgNode-6 528B ± 0% 528B ± 0% ~ (all equal) name old allocs/op new allocs/op delta Hash-6 84.0 ± 0% 84.0 ± 0% ~ (all equal) HashMapAcyclic-6 202 ± 0% 202 ± 0% ~ (all equal) TailcfgNode-6 11.0 ± 0% 11.0 ± 0% ~ (all equal) Signed-off-by: Brad Fitzpatrick --- util/deephash/deephash.go | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/util/deephash/deephash.go b/util/deephash/deephash.go index 3f1b8ac94..ac1c267c7 100644 --- a/util/deephash/deephash.go +++ b/util/deephash/deephash.go @@ -15,6 +15,7 @@ import ( "encoding/hex" "fmt" "hash" + "math" "reflect" "strconv" "sync" @@ -159,14 +160,15 @@ func print(w *bufio.Writer, v reflect.Value, visited map[uintptr]bool, scratch [ case reflect.String: w.WriteString(v.String()) case reflect.Bool: - fmt.Fprintf(w, "%v", v.Bool()) + w.Write(strconv.AppendBool(scratch[:0], v.Bool())) case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: - fmt.Fprintf(w, "%v", v.Int()) + w.Write(strconv.AppendInt(scratch[:0], v.Int(), 10)) case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr: scratch = strconv.AppendUint(scratch[:0], v.Uint(), 10) w.Write(scratch) case reflect.Float32, reflect.Float64: - fmt.Fprintf(w, "%v", v.Float()) + scratch = strconv.AppendUint(scratch[:0], math.Float64bits(v.Float()), 10) + w.Write(scratch) case reflect.Complex64, reflect.Complex128: fmt.Fprintf(w, "%v", v.Complex()) }