Micro-optimization for hashing.Writer/PackerManager

name             old time/op    new time/op    delta
PackerManager-8     247ms ± 1%     246ms ± 1%  -0.43%  (p=0.001 n=18+18)

name             old speed      new speed      delta
PackerManager-8   213MB/s ± 1%   214MB/s ± 1%  +0.43%  (p=0.001 n=18+18)

name             old alloc/op   new alloc/op   delta
PackerManager-8    92.2kB ± 0%    91.5kB ± 0%  -0.82%  (p=0.000 n=19+20)

name             old allocs/op  new allocs/op  delta
PackerManager-8     1.43k ± 0%     1.41k ± 0%  -1.67%  (p=0.000 n=20+20)
This commit is contained in:
greatroar 2020-03-05 21:06:16 +01:00
parent b592614061
commit 41fee11f66

View file

@ -15,13 +15,14 @@ type Writer struct {
func NewWriter(w io.Writer, h hash.Hash) *Writer { func NewWriter(w io.Writer, h hash.Hash) *Writer {
return &Writer{ return &Writer{
h: h, h: h,
w: io.MultiWriter(w, h), w: w,
} }
} }
// Write wraps the write method of the underlying writer and also hashes all data. // Write wraps the write method of the underlying writer and also hashes all data.
func (h *Writer) Write(p []byte) (int, error) { func (h *Writer) Write(p []byte) (int, error) {
n, err := h.w.Write(p) n, err := h.w.Write(p)
h.h.Write(p[:n])
return n, err return n, err
} }