From 7481835cb7453f44e3dd7c7dee7e62e2d42b6ea7 Mon Sep 17 00:00:00 2001 From: Agniva De Sarker Date: Sat, 15 Aug 2020 22:22:25 +0530 Subject: [PATCH] Use klauspost/compress/gzip instead of standard library MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This gives 40-50% improvements in CPU with very minor increase in memory, just as a drop-in replacement. I think that is a very reasonable tradeoff. The library is mature and safe to be used in production. name old time/op new time/op delta GzipHandler_S2k-8 74.9µs ± 2% 34.4µs ± 2% -54.07% (p=0.000 n=10+9) GzipHandler_S20k-8 379µs ± 1% 226µs ± 3% -40.42% (p=0.000 n=9+10) GzipHandler_S100k-8 1.95ms ± 2% 1.15ms ± 1% -41.27% (p=0.000 n=9+9) GzipHandler_P2k-8 24.3µs ±25% 10.7µs ±25% -55.80% (p=0.000 n=10+10) GzipHandler_P20k-8 132µs ± 2% 75µs ± 1% -42.95% (p=0.000 n=9+10) GzipHandler_P100k-8 658µs ± 2% 371µs ± 3% -43.68% (p=0.000 n=9+10) name old alloc/op new alloc/op delta GzipHandler_S2k-8 7.71kB ± 5% 9.13kB ± 7% +18.33% (p=0.000 n=10+10) GzipHandler_S20k-8 65.1kB ± 3% 70.3kB ± 3% +8.05% (p=0.000 n=10+10) GzipHandler_S100k-8 348kB ± 4% 382kB ± 2% +9.85% (p=0.000 n=10+10) GzipHandler_P2k-8 7.60kB ± 1% 7.93kB ± 2% +4.33% (p=0.000 n=10+10) GzipHandler_P20k-8 64.4kB ± 1% 66.3kB ± 2% +2.92% (p=0.000 n=10+10) GzipHandler_P100k-8 304kB ± 1% 309kB ± 1% +1.67% (p=0.000 n=10+9) name old allocs/op new allocs/op delta GzipHandler_S2k-8 21.0 ± 0% 21.0 ± 0% ~ (all equal) GzipHandler_S20k-8 24.0 ± 0% 24.0 ± 0% ~ (all equal) GzipHandler_S100k-8 27.0 ± 0% 27.0 ± 0% ~ (all equal) GzipHandler_P2k-8 21.0 ± 0% 21.0 ± 0% ~ (all equal) GzipHandler_P20k-8 24.0 ± 0% 24.0 ± 0% ~ (all equal) GzipHandler_P100k-8 26.0 ± 0% 26.0 ± 0% ~ (all equal) --- go.mod | 5 ++++- go.sum | 2 ++ gzip.go | 3 ++- gzip_test.go | 2 +- 4 files changed, 9 insertions(+), 3 deletions(-) diff --git a/go.mod b/go.mod index 8019012..b0eb97f 100644 --- a/go.mod +++ b/go.mod @@ -2,4 +2,7 @@ module github.com/NYTimes/gziphandler go 1.11 -require github.com/stretchr/testify v1.3.0 +require ( + github.com/klauspost/compress v1.10.11 + github.com/stretchr/testify v1.3.0 +) diff --git a/go.sum b/go.sum index 4347755..da2e000 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,7 @@ github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/klauspost/compress v1.10.11 h1:K9z59aO18Aywg2b/WSgBaUX99mHy2BES18Cr5lBKZHk= +github.com/klauspost/compress v1.10.11/go.mod h1:aoV0uJVorq1K+umq18yTdKaF57EivdYsUV+/s2qKfXs= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= diff --git a/gzip.go b/gzip.go index 957fc92..0143bae 100644 --- a/gzip.go +++ b/gzip.go @@ -2,7 +2,6 @@ package gziphandler // import "github.com/NYTimes/gziphandler" import ( "bufio" - "compress/gzip" "fmt" "io" "mime" @@ -11,6 +10,8 @@ import ( "strconv" "strings" "sync" + + "github.com/klauspost/compress/gzip" ) const ( diff --git a/gzip_test.go b/gzip_test.go index bed7f52..9606ed2 100644 --- a/gzip_test.go +++ b/gzip_test.go @@ -2,7 +2,6 @@ package gziphandler import ( "bytes" - "compress/gzip" "fmt" "io" "io/ioutil" @@ -13,6 +12,7 @@ import ( "strconv" "testing" + "github.com/klauspost/compress/gzip" "github.com/stretchr/testify/assert" )