diff options
author | Dominik Braun <dominikbraun1997@web.de> | 2019-08-08 07:59:02 +0200 |
---|---|---|
committer | Matt Holt <mholt@users.noreply.github.com> | 2019-08-07 23:59:02 -0600 |
commit | 4950ce485f7d931890fcfd2ee287b6df1b5db435 (patch) | |
tree | 81cafb7b6f235d001c99cc5185118a8490d98458 /modules/caddyhttp/encode/encode.go | |
parent | c8b0a97b1c2524f7486aef38b2cf78a93d7b98b0 (diff) |
Part 1: Optimize using compiler's inliner (#2687)
* optimized functions for inlining
* added note regarding ResponseWriterWrapper
* optimzed browseWrite* methods for FileServer
* created benchmarks for comparison
* creating browseListing instance in each function
* created benchmarks for openResponseWriter
* removed benchmarks of old implementations
* implemented sync.Pool for byte buffers
* using global sync.Pool for writing JSON/HTML
Diffstat (limited to 'modules/caddyhttp/encode/encode.go')
-rw-r--r-- | modules/caddyhttp/encode/encode.go | 21 |
1 files changed, 15 insertions, 6 deletions
diff --git a/modules/caddyhttp/encode/encode.go b/modules/caddyhttp/encode/encode.go index c78ccb9..b2c1327 100644 --- a/modules/caddyhttp/encode/encode.go +++ b/modules/caddyhttp/encode/encode.go @@ -93,14 +93,23 @@ func (enc *Encode) ServeHTTP(w http.ResponseWriter, r *http.Request, next caddyh // encode the response with encodingName. The returned response writer MUST // be closed after the handler completes. func (enc *Encode) openResponseWriter(encodingName string, w http.ResponseWriter) *responseWriter { + var rw responseWriter + return enc.initResponseWriter(&rw, encodingName, w) +} + +// initResponseWriter initializes the responseWriter instance +// allocated in openResponseWriter, enabling mid-stack inlining. +func (enc *Encode) initResponseWriter(rw *responseWriter, encodingName string, wrappedRW http.ResponseWriter) *responseWriter { buf := bufPool.Get().(*bytes.Buffer) buf.Reset() - return &responseWriter{ - ResponseWriterWrapper: &caddyhttp.ResponseWriterWrapper{ResponseWriter: w}, - encodingName: encodingName, - buf: buf, - config: enc, - } + + // The allocation of ResponseWriterWrapper might be optimized as well. + rw.ResponseWriterWrapper = &caddyhttp.ResponseWriterWrapper{ResponseWriter: wrappedRW} + rw.encodingName = encodingName + rw.buf = buf + rw.config = enc + + return rw } // responseWriter writes to an underlying response writer |