Signed-off-by: Bo-Yi Wu <appleboy.tw@gmail.com>
This commit is contained in:
Bo-Yi Wu 2020-05-08 02:13:52 +08:00
parent 9e8404975e
commit 71f841a1f5
2 changed files with 35 additions and 32 deletions

56
gin.go
View File

@ -113,7 +113,7 @@ type Engine struct {
noMethod HandlersChain
pool sync.Pool
trees methodTrees
// paramsPool sync.Pool
paramsPool sync.Pool
maxParams uint16
}
@ -258,7 +258,7 @@ func (engine *Engine) addRoute(method, path string, handlers HandlersChain) {
assert1(len(handlers) > 0, "there must be at least one handler")
debugPrintRoute(method, path, handlers)
// varsCount := uint16(0)
varsCount := uint16(0)
root := engine.trees.get(method)
if root == nil {
@ -269,17 +269,17 @@ func (engine *Engine) addRoute(method, path string, handlers HandlersChain) {
root.addRoute(path, handlers)
// Update maxParams
// if paramsCount := countParams(path); paramsCount+varsCount > root.maxParams {
// engine.maxParams = paramsCount + varsCount
// }
if paramsCount := countParams(path); paramsCount+varsCount > root.maxParams {
engine.maxParams = paramsCount + varsCount
}
// Lazy-init paramsPool alloc func
// if engine.paramsPool.New == nil && engine.maxParams > 0 {
// engine.paramsPool.New = func() interface{} {
// ps := make(Params, 0, engine.maxParams)
// return &ps
// }
// }
if engine.paramsPool.New == nil && engine.maxParams > 0 {
engine.paramsPool.New = func() interface{} {
ps := make(Params, 0, engine.maxParams)
return &ps
}
}
}
// Routes returns a slice of registered routes, including some useful information, such as:
@ -398,22 +398,22 @@ func (engine *Engine) HandleContext(c *Context) {
c.index = oldIndexValue
}
// func (engine *Engine) getParams() *Params {
// // c := engine.pool.Get().(*Context)
// // c.Params = c.Params[0:0]
// ps := engine.paramsPool.Get().(*Params)
// *ps = (*ps)[0:0] // reset slice
// return ps
// }
func (engine *Engine) getParams() *Params {
// c := engine.pool.Get().(*Context)
// c.Params = c.Params[0:0]
ps := engine.paramsPool.Get().(*Params)
*ps = (*ps)[0:0] // reset slice
return ps
}
// func (engine *Engine) putParams(ps *Params) {
// // c := engine.pool.Get().(*Context)
// // c.Params = *ps
// // engine.pool.Put(c)
// if ps != nil {
// engine.paramsPool.Put(ps)
// }
// }
func (engine *Engine) putParams(ps *Params) {
// c := engine.pool.Get().(*Context)
// c.Params = *ps
// engine.pool.Put(c)
if ps != nil {
engine.paramsPool.Put(ps)
}
}
func (engine *Engine) handleHTTPRequest(c *Context) {
httpMethod := c.Request.Method
@ -436,9 +436,9 @@ func (engine *Engine) handleHTTPRequest(c *Context) {
}
root := t[i].root
// Find route in tree
value := root.getValue(rPath, &c.Params, unescape)
value := root.getValue(rPath, engine.getParams, unescape)
if value.params != nil {
// engine.putParams(value.params)
engine.putParams(value.params)
c.Params = *value.params
}
if value.handlers != nil {

View File

@ -5,6 +5,7 @@
package gin
import (
"log"
"strings"
"unicode"
"unicode/utf8"
@ -418,7 +419,7 @@ type nodeValue struct {
// If no handle can be found, a TSR (trailing slash redirect) recommendation is
// made if a handle exists with an extra (without the) trailing slash for the
// given path.
func (n *node) getValue(path string, params *Params, unescape bool) (value nodeValue) {
func (n *node) getValue(path string, params func() *Params, unescape bool) (value nodeValue) {
value = nodeValue{}
walk: // Outer loop for walking the tree
for {
@ -473,10 +474,12 @@ walk: // Outer loop for walking the tree
// }
if params != nil {
if value.params == nil {
value.params = params
value.params = params()
}
// Expand slice within preallocated capacity
i := len(*value.params)
log.Println(i)
log.Println(*value.params)
*value.params = (*value.params)[:i+1]
// val := path[:end]
// if unescape {
@ -535,7 +538,7 @@ walk: // Outer loop for walking the tree
// Save param value
if params != nil {
if value.params == nil {
value.params = params
value.params = params()
}
// Expand slice within preallocated capacity
i := len(*value.params)