Skip to content

Commit

Permalink
gzhttp: Use crc32.Update instead of crc32.New
Browse files Browse the repository at this point in the history
Skips another allocation:

name        old time/op    new time/op    delta
2kJitter-8    29.4µs ± 4%    29.0µs ± 3%  -1.23%  (p=0.017 n=19+24)

name        old speed      new speed      delta
2kJitter-8  69.8MB/s ± 4%  70.6MB/s ± 3%  +1.23%  (p=0.016 n=19+24)

name        old alloc/op   new alloc/op   delta
2kJitter-8    3.41kB ± 4%    3.34kB ± 4%  -2.12%  (p=0.001 n=20+25)

name        old allocs/op  new allocs/op  delta
2kJitter-8      16.0 ± 0%      15.0 ± 0%  -6.25%  (p=0.000 n=20+25)
  • Loading branch information
greatroar committed Mar 8, 2023
1 parent 3ae448f commit 45fa3f4
Showing 1 changed file with 3 additions and 4 deletions.
7 changes: 3 additions & 4 deletions gzhttp/compress.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,17 +234,16 @@ func (w *GzipResponseWriter) startGzip(remain []byte) error {
var tmp [sha256.Size]byte
jitRNG = binary.LittleEndian.Uint32(h.Sum(tmp[:0]))
} else {
h := crc32.New(castagnoliTable)
h.Write(w.buf)
h := crc32.Update(0, castagnoliTable, w.buf)
// Use only up to "w.jitterBuffer", otherwise the output depends on write sizes.
if len(remain) > 0 && len(w.buf) < w.jitterBuffer {
remain := remain
if len(remain)+len(w.buf) > w.jitterBuffer {
remain = remain[:w.jitterBuffer-len(w.buf)]
}
h.Write(remain)
h = crc32.Update(h, castagnoliTable, remain)
}
jitRNG = bits.RotateLeft32(h.Sum32(), 19) ^ 0xab0755de
jitRNG = bits.RotateLeft32(h, 19) ^ 0xab0755de
}
} else {
// Get from rand.Reader
Expand Down

0 comments on commit 45fa3f4

Please sign in to comment.