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
  • Loading branch information
greatroar committed Mar 8, 2023
1 parent 3588812 commit 3ae448f
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 46 deletions.
14 changes: 6 additions & 8 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 @@ -76,7 +75,7 @@ type GzipResponseWriter struct {
suffixETag string // Suffix to add to ETag header if response is compressed.
dropETag bool // Drop ETag header if response is compressed (supersedes suffixETag).
sha256Jitter bool // Use sha256 for jitter.
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 @@ -216,7 +215,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 CRC or SHA-256 of current buffer.
// Before first write.
if len(w.randomJitter) > 0 {
var jitRNG uint32
Expand All @@ -232,7 +231,7 @@ func (w *GzipResponseWriter) startGzip(remain []byte) error {
}
h.Write(remain)
}
var tmp [sha256.BlockSize]byte
var tmp [sha256.Size]byte
jitRNG = binary.LittleEndian.Uint32(h.Sum(tmp[:0]))
} else {
h := crc32.New(castagnoliTable)
Expand Down Expand Up @@ -526,7 +525,7 @@ type config struct {
suffixETag string
dropETag bool
jitterBuffer int
randomJitter []byte
randomJitter string
sha256Jitter bool
}

Expand Down Expand Up @@ -721,14 +720,13 @@ func RandomJitter(n, buffer int, paranoid bool) option {
return func(c *config) {
if n > 0 {
c.sha256Jitter = paranoid
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 3ae448f

Please sign in to comment.