internal/deepprint: replace Fprintf(w, const) with w.WriteString

name    old time/op    new time/op    delta
Hash-8    7.77µs ± 0%    6.29µs ± 1%  -19.11%  (p=0.000 n=9+10)

name    old alloc/op   new alloc/op   delta
Hash-8    1.67kB ± 0%    1.67kB ± 0%     ~     (all equal)

name    old allocs/op  new allocs/op  delta
Hash-8      53.0 ± 0%      53.0 ± 0%     ~     (all equal)

Signed-off-by: Josh Bleecher Snyder <josh@tailscale.com>
pull/1908/head
Josh Bleecher Snyder 4 years ago committed by Josh Bleecher Snyder
parent d4f805339e
commit dfa0c90955

@ -132,14 +132,14 @@ func print(w *bufio.Writer, v reflect.Value, visited map[uintptr]bool) {
print(w, v.Elem(), visited) print(w, v.Elem(), visited)
return return
case reflect.Struct: case reflect.Struct:
fmt.Fprintf(w, "struct{\n") w.WriteString("struct{\n")
t := v.Type() t := v.Type()
for i, n := 0, v.NumField(); i < n; i++ { for i, n := 0, v.NumField(); i < n; i++ {
sf := t.Field(i) sf := t.Field(i)
w.WriteString(sf.Name) w.WriteString(sf.Name)
w.WriteString(": ") w.WriteString(": ")
print(w, v.Field(i), visited) print(w, v.Field(i), visited)
fmt.Fprintf(w, "\n") w.WriteString("\n")
} }
case reflect.Slice, reflect.Array: case reflect.Slice, reflect.Array:
if v.Type().Elem().Kind() == reflect.Uint8 && v.CanInterface() { if v.Type().Elem().Kind() == reflect.Uint8 && v.CanInterface() {
@ -150,9 +150,9 @@ func print(w *bufio.Writer, v reflect.Value, visited map[uintptr]bool) {
for i, ln := 0, v.Len(); i < ln; i++ { for i, ln := 0, v.Len(); i < ln; i++ {
fmt.Fprintf(w, " [%d]: ", i) fmt.Fprintf(w, " [%d]: ", i)
print(w, v.Index(i), visited) print(w, v.Index(i), visited)
fmt.Fprintf(w, "\n") w.WriteString("\n")
} }
fmt.Fprintf(w, "}\n") w.WriteString("}\n")
case reflect.Interface: case reflect.Interface:
print(w, v.Elem(), visited) print(w, v.Elem(), visited)
case reflect.Map: case reflect.Map:
@ -160,12 +160,11 @@ func print(w *bufio.Writer, v reflect.Value, visited map[uintptr]bool) {
fmt.Fprintf(w, "map[%d]{\n", len(sm.Key)) fmt.Fprintf(w, "map[%d]{\n", len(sm.Key))
for i, k := range sm.Key { for i, k := range sm.Key {
print(w, k, visited) print(w, k, visited)
fmt.Fprintf(w, ": ") w.WriteString(": ")
print(w, sm.Value[i], visited) print(w, sm.Value[i], visited)
fmt.Fprintf(w, "\n") w.WriteString("\n")
} }
fmt.Fprintf(w, "}\n") w.WriteString("}\n")
case reflect.String: case reflect.String:
w.WriteString(v.String()) w.WriteString(v.String())
case reflect.Bool: case reflect.Bool:

Loading…
Cancel
Save