Skip to content

Commit

Permalink
gzhttp: Use strings for randomJitter to skip a copy
Browse files Browse the repository at this point in the history
name           old speed      new speed      delta
2kJitter-8     59.9MB/s ± 4%  60.4MB/s ± 3%  +0.77%  (p=0.035 n=24+25)
2kJitterRNG-8  79.0MB/s ± 4%  79.5MB/s ± 4%    ~     (p=0.215 n=24+25)

name           old alloc/op   new alloc/op   delta
2kJitter-8       3.18kB ± 2%    3.19kB ± 2%    ~     (p=0.626 n=23+25)
2kJitterRNG-8    3.21kB ± 2%    3.21kB ± 3%    ~     (p=0.697 n=25+25)

name           old allocs/op  new allocs/op  delta
2kJitter-8         16.0 ± 0%      15.0 ± 0%  -6.25%  (p=0.000 n=25+25)
2kJitterRNG-8      16.0 ± 0%      16.0 ± 0%    ~     (all equal)
  • Loading branch information
greatroar committed Mar 7, 2023
1 parent cd2407a commit b71b059
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 45 deletions.
12 changes: 5 additions & 7 deletions gzhttp/compress.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package gzhttp

import (
"bufio"
"bytes"
"crypto/rand"
"crypto/sha256"
"encoding/binary"
Expand Down Expand Up @@ -73,7 +72,7 @@ type GzipResponseWriter struct {
setContentType bool // Add content type, if missing and detected.
suffixETag string // Suffix to add to ETag header if response is compressed.
dropETag bool // Drop ETag header if response is compressed (supersedes suffixETag).
randomJitter []byte // Add random bytes to output as header field.
randomJitter string // Add random bytes to output as header field.
jitterBuffer int // Maximum buffer to accumulate before doing jitter.

contentTypeFilter func(ct string) bool // Only compress if the response is one of these content-types. All are accepted if empty.
Expand Down Expand Up @@ -211,7 +210,7 @@ func (w *GzipResponseWriter) startGzip(remain []byte) error {
// Initialize the GZIP response.
w.init()

// Set random jitter based on CRC of current buffer
// Set random jitter based on SHA-256 of current buffer
// Before first write.
if len(w.randomJitter) > 0 {
var jitRNG uint32
Expand Down Expand Up @@ -506,7 +505,7 @@ type config struct {
suffixETag string
dropETag bool
jitterBuffer int
randomJitter []byte
randomJitter string
}

func (c *config) validate() error {
Expand Down Expand Up @@ -697,14 +696,13 @@ func DropETag() option {
func RandomJitter(n, buffer int) option {
return func(c *config) {
if n > 0 {
c.randomJitter = bytes.Repeat([]byte("Padding-"), 1+(n/8))
c.randomJitter = c.randomJitter[:(n + 1)]
c.randomJitter = strings.Repeat("Padding-", 1+(n/8))[:n+1]
c.jitterBuffer = buffer
if c.jitterBuffer == 0 {
c.jitterBuffer = 64 << 10
}
} else {
c.randomJitter = nil
c.randomJitter = ""
c.jitterBuffer = 0
}
}
Expand Down
22 changes: 6 additions & 16 deletions gzhttp/writer/gzkp/gzkp.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,23 +61,13 @@ func NewWriter(w io.Writer, level int) writer.GzipWriter {
}
}

// SetHeader will override header with any non-nil values.
// SetHeader will override the gzip header on pw.
func (pw *pooledWriter) SetHeader(h writer.Header) {
if h.Name != nil {
pw.Name = string(h.Name)
}
if h.Extra != nil {
pw.Extra = *h.Extra
}
if h.Comment != nil {
pw.Comment = string(h.Comment)
}
if h.ModTime != nil {
pw.ModTime = *h.ModTime
}
if h.OS != nil {
pw.OS = *h.OS
}
pw.Name = h.Name
pw.Extra = h.Extra
pw.Comment = h.Comment
pw.ModTime = h.ModTime
pw.OS = h.OS
}

func Levels() (min, max int) {
Expand Down
22 changes: 6 additions & 16 deletions gzhttp/writer/gzstd/stdlib.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,23 +61,13 @@ func NewWriter(w io.Writer, level int) writer.GzipWriter {
}
}

// SetHeader will override header with any non-nil values.
// SetHeader will override the gzip header on pw.
func (pw *pooledWriter) SetHeader(h writer.Header) {
if h.Name != nil {
pw.Name = string(h.Name)
}
if h.Extra != nil {
pw.Extra = *h.Extra
}
if h.Comment != nil {
pw.Comment = string(h.Comment)
}
if h.ModTime != nil {
pw.ModTime = *h.ModTime
}
if h.OS != nil {
pw.OS = *h.OS
}
pw.Name = h.Name
pw.Extra = h.Extra
pw.Comment = h.Comment
pw.ModTime = h.ModTime
pw.OS = h.OS
}

func Levels() (min, max int) {
Expand Down
12 changes: 6 additions & 6 deletions gzhttp/writer/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@ type GzipWriterExt interface {
SetHeader(h Header)
}

// Header provides nillable header fields.
// Header is a gzip header.
type Header struct {
Comment []byte // comment, converted to string if set.
Extra *[]byte // "extra data"
ModTime *time.Time // modification time
Name []byte // file name, converted to string if set.
OS *byte // operating system type
Comment string // comment
Extra []byte // "extra data"
ModTime time.Time // modification time
Name string // file name
OS byte // operating system type
}

// GzipWriterFactory contains the information needed for custom gzip implementations.
Expand Down

0 comments on commit b71b059

Please sign in to comment.