From 48f4914165a9f2d54fcbe7f6b02bb365ad9ec71c Mon Sep 17 00:00:00 2001 From: Manu Mtz-Almeida Date: Fri, 18 Jul 2014 00:10:28 +0200 Subject: [PATCH] Performance improvement - Reduces number of allocations per context - Improves CPU cache usage --- context.go | 20 +++++++++++--------- gin.go | 4 +++- response_writer.go | 1 - 3 files changed, 14 insertions(+), 11 deletions(-) diff --git a/context.go b/context.go index 290bfffd..01a053f4 100644 --- a/context.go +++ b/context.go @@ -54,14 +54,15 @@ func (a errorMsgs) String() string { // Context is the most important part of gin. It allows us to pass variables between middleware, // manage the flow, validate the JSON of a request and render a JSON response for example. type Context struct { - Request *http.Request - Writer ResponseWriter - Keys map[string]interface{} - Errors errorMsgs - Params httprouter.Params - Engine *Engine - handlers []HandlerFunc - index int8 + writermem responseWriter + Request *http.Request + Writer ResponseWriter + Keys map[string]interface{} + Errors errorMsgs + Params httprouter.Params + Engine *Engine + handlers []HandlerFunc + index int8 } /************************************/ @@ -70,7 +71,8 @@ type Context struct { func (engine *Engine) createContext(w http.ResponseWriter, req *http.Request, params httprouter.Params, handlers []HandlerFunc) *Context { c := engine.cache.Get().(*Context) - c.Writer.reset(w) + c.writermem.reset(w) + c.Request = req c.Params = params c.handlers = handlers diff --git a/gin.go b/gin.go index d72822c8..2dd2da68 100644 --- a/gin.go +++ b/gin.go @@ -60,7 +60,9 @@ func New() *Engine { engine.router = httprouter.New() engine.router.NotFound = engine.handle404 engine.cache.New = func() interface{} { - return &Context{Engine: engine, Writer: &responseWriter{}} + c := &Context{Engine: engine} + c.Writer = &c.writermem + return c } return engine } diff --git a/response_writer.go b/response_writer.go index 88c1b20d..cf02e90c 100644 --- a/response_writer.go +++ b/response_writer.go @@ -11,7 +11,6 @@ type ( Written() bool // private - reset(http.ResponseWriter) setStatus(int) }