Skip to content

Commit 739d2d9

Browse files
1911860538huangzw
andauthored
chore(perf): Optimize the Copy method of the Context struct (#3859)
* 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 <[email protected]>
1 parent ecdbbbe commit 739d2d9

File tree

1 file changed

+10
-6
lines changed

1 file changed

+10
-6
lines changed

context.go

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -113,20 +113,24 @@ func (c *Context) Copy() *Context {
113113
cp := Context{
114114
writermem: c.writermem,
115115
Request: c.Request,
116-
Params: c.Params,
117116
engine: c.engine,
118117
}
118+
119119
cp.writermem.ResponseWriter = nil
120120
cp.Writer = &cp.writermem
121121
cp.index = abortIndex
122122
cp.handlers = nil
123-
cp.Keys = map[string]any{}
124-
for k, v := range c.Keys {
123+
124+
cKeys := c.Keys
125+
cp.Keys = make(map[string]any, len(cKeys))
126+
for k, v := range cKeys {
125127
cp.Keys[k] = v
126128
}
127-
paramCopy := make([]Param, len(cp.Params))
128-
copy(paramCopy, cp.Params)
129-
cp.Params = paramCopy
129+
130+
cParams := c.Params
131+
cp.Params = make([]Param, len(cParams))
132+
copy(cp.Params, cParams)
133+
130134
return &cp
131135
}
132136

0 commit comments

Comments
 (0)