From 48e6fd723d3c2fcbe0f3f60ed004426979eb8724 Mon Sep 17 00:00:00 2001 From: huangzw Date: Thu, 29 Feb 2024 10:46:58 +0800 Subject: [PATCH] Optimize the Copy method of the Context struct: using 'make' to initialize the map('cp.Keys') with a length of 'c.Keys'; avoiding repeatedly assiging the 'params' to 'context'. --- context.go | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/context.go b/context.go index 420ff167..9a72bbc6 100644 --- a/context.go +++ b/context.go @@ -113,20 +113,19 @@ func (c *Context) Copy() *Context { cp := Context{ writermem: c.writermem, Request: c.Request, - Params: c.Params, engine: c.engine, } cp.writermem.ResponseWriter = nil cp.Writer = &cp.writermem cp.index = abortIndex cp.handlers = nil - cp.Keys = map[string]any{} + cp.Keys = make(map[string]any, len(c.Keys)) for k, v := range c.Keys { cp.Keys[k] = v } - paramCopy := make([]Param, len(cp.Params)) - copy(paramCopy, cp.Params) - cp.Params = paramCopy + paramsCopy := make([]Param, len(c.Params)) + copy(paramsCopy, c.Params) + cp.Params = paramsCopy return &cp }