diff --git a/internal/deephash/deephash.go b/internal/deephash/deephash.go index a15e0744e..fcbd59601 100644 --- a/internal/deephash/deephash.go +++ b/internal/deephash/deephash.go @@ -51,6 +51,13 @@ func Print(w *bufio.Writer, v ...interface{}) { tailcfgDiscoKeyType = reflect.TypeOf(tailcfg.DiscoKey{}) ) +// bufPool contains *[]byte, used when printing netaddr types. +var bufPool = sync.Pool{ + New: func() interface{} { + return new([]byte) + }, +} + // print hashes v into w. // It reports whether it was able to do so without hitting a cycle. func print(w *bufio.Writer, v reflect.Value, visited map[uintptr]bool) (acyclic bool) { @@ -62,47 +69,44 @@ func print(w *bufio.Writer, v reflect.Value, visited map[uintptr]bool) (acyclic if v.CanInterface() { switch v.Type() { case netaddrIPType: - var b []byte - var err error + b := bufPool.Get().(*[]byte) + defer bufPool.Put(b) + *b = (*b)[:0] if v.CanAddr() { x := v.Addr().Interface().(*netaddr.IP) - b, err = x.MarshalText() + *b = x.AppendTo(*b) } else { x := v.Interface().(netaddr.IP) - b, err = x.MarshalText() - } - if err == nil { - w.Write(b) - return true + *b = x.AppendTo(*b) } + w.Write(*b) + return true case netaddrIPPrefix: - var b []byte - var err error + b := bufPool.Get().(*[]byte) + defer bufPool.Put(b) + *b = (*b)[:0] if v.CanAddr() { x := v.Addr().Interface().(*netaddr.IPPrefix) - b, err = x.MarshalText() + *b = x.AppendTo(*b) } else { x := v.Interface().(netaddr.IPPrefix) - b, err = x.MarshalText() - } - if err == nil { - w.Write(b) - return true + *b = x.AppendTo(*b) } + w.Write(*b) + return true case netaddrIPPort: - var b []byte - var err error + b := bufPool.Get().(*[]byte) + defer bufPool.Put(b) + *b = (*b)[:0] if v.CanAddr() { x := v.Addr().Interface().(*netaddr.IPPort) - b, err = x.MarshalText() + *b = x.AppendTo(*b) } else { x := v.Interface().(netaddr.IPPort) - b, err = x.MarshalText() - } - if err == nil { - w.Write(b) - return true + *b = x.AppendTo(*b) } + w.Write(*b) + return true case wgkeyKeyType: if v.CanAddr() { x := v.Addr().Interface().(*wgkey.Key)