From 1e417c7a50f5b33db31d161c8212a8fb7a5971b2 Mon Sep 17 00:00:00 2001 From: Manu Mtz-Almeida Date: Wed, 25 Mar 2015 19:33:17 +0100 Subject: [PATCH] Refactors Context allocation --- context.go | 12 ++++++++---- gin.go | 10 +++++++--- 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/context.go b/context.go index 85caf99..745301b 100644 --- a/context.go +++ b/context.go @@ -79,14 +79,11 @@ type Context struct { func (engine *Engine) createContext(w http.ResponseWriter, req *http.Request, params httprouter.Params, handlers []HandlerFunc) *Context { c := engine.pool.Get().(*Context) + c.reset() c.writermem.reset(w) c.Request = req c.Params = params c.handlers = handlers - c.Keys = nil - c.index = -1 - c.accepted = nil - c.Errors = c.Errors[0:0] return c } @@ -94,6 +91,13 @@ func (engine *Engine) reuseContext(c *Context) { engine.pool.Put(c) } +func (c *Context) reset() { + c.Keys = nil + c.index = -1 + c.accepted = nil + c.Errors = c.Errors[0:0] +} + func (c *Context) Copy() *Context { var cp Context = *c cp.index = AbortIndex diff --git a/gin.go b/gin.go index 6a34e2e..a7eb030 100644 --- a/gin.go +++ b/gin.go @@ -56,9 +56,7 @@ func New() *Engine { engine.router.NotFound = engine.handle404 engine.router.MethodNotAllowed = engine.handle405 engine.pool.New = func() interface{} { - c := &Context{Engine: engine} - c.Writer = &c.writermem - return c + return engine.allocateContext() } return engine } @@ -70,6 +68,12 @@ func Default() *Engine { return engine } +func (engine *Engine) allocateContext() (c *Context) { + c = &Context{Engine: engine} + c.Writer = &c.writermem + return +} + func (engine *Engine) LoadHTMLGlob(pattern string) { if IsDebugging() { render.HTMLDebug.AddGlob(pattern)