summaryrefslogtreecommitdiff
path: root/modules/caddyhttp/encode
diff options
context:
space:
mode:
Diffstat (limited to 'modules/caddyhttp/encode')
-rw-r--r--modules/caddyhttp/encode/encode.go21
-rw-r--r--modules/caddyhttp/encode/encode_test.go12
2 files changed, 27 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
diff --git a/modules/caddyhttp/encode/encode_test.go b/modules/caddyhttp/encode/encode_test.go
new file mode 100644
index 0000000..79eae3c
--- /dev/null
+++ b/modules/caddyhttp/encode/encode_test.go
@@ -0,0 +1,12 @@
+package encode
+
+import (
+ "testing"
+)
+
+func BenchmarkOpenResponseWriter(b *testing.B) {
+ enc := new(Encode)
+ for n := 0; n < b.N; n++ {
+ enc.openResponseWriter("test", nil)
+ }
+}