Skip to content

Commit

Permalink
chore(perf): Optimize the Copy method of the Context struct (#3859)
Browse files Browse the repository at this point in the history
* 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'.

* Using temporary variables to save c.Keys and c.Params to prevent them from changing during the copying process.

---------

Co-authored-by: huangzw <huangzw@hsmap.com>
  • Loading branch information
1911860538 and huangzw authored Mar 5, 2024
1 parent ecdbbbe commit 739d2d9
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,20 +113,24 @@ 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{}
for k, v := range c.Keys {

cKeys := c.Keys
cp.Keys = make(map[string]any, len(cKeys))
for k, v := range cKeys {
cp.Keys[k] = v
}
paramCopy := make([]Param, len(cp.Params))
copy(paramCopy, cp.Params)
cp.Params = paramCopy

cParams := c.Params
cp.Params = make([]Param, len(cParams))
copy(cp.Params, cParams)

return &cp
}

Expand Down

0 comments on commit 739d2d9

Please sign in to comment.