From dbc4ac6945492da8aa9e84cab015d59cdf6b8fff Mon Sep 17 00:00:00 2001 From: astaxie Date: Wed, 16 Dec 2015 23:43:32 +0800 Subject: [PATCH] reduce the slicegrow --- context/input.go | 8 +++++--- tree.go | 3 ++- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/context/input.go b/context/input.go index 3f73787d..7e9f50b6 100644 --- a/context/input.go +++ b/context/input.go @@ -50,7 +50,8 @@ type BeegoInput struct { // NewInput return BeegoInput generated by Context. func NewInput() *BeegoInput { return &BeegoInput{ - pvalues: make([]string, maxParam), + pnames: make([]string, 0, maxParam), + pvalues: make([]string, 0, maxParam), data: make(map[interface{}]interface{}), } } @@ -60,6 +61,7 @@ func (input *BeegoInput) Reset(ctx *Context) { input.Context = ctx input.CruSession = nil input.pnames = input.pnames[:0] + input.pvalues = input.pvalues[:0] input.data = nil input.RequestBody = []byte{} } @@ -264,7 +266,7 @@ func (input *BeegoInput) ParamsLen() int { // Param returns router param by a given key. func (input *BeegoInput) Param(key string) string { for i, v := range input.pnames { - if v == key { + if v == key && i <= len(input.pvalues) { return input.pvalues[i] } } @@ -273,7 +275,7 @@ func (input *BeegoInput) Param(key string) string { // SetParam will set the param with key and value func (input *BeegoInput) SetParam(key, val string) { - input.pvalues[len(input.pnames)] = val + input.pvalues = append(input.pvalues, val) input.pnames = append(input.pnames, key) } diff --git a/tree.go b/tree.go index dbcc63c9..2092016f 100644 --- a/tree.go +++ b/tree.go @@ -288,7 +288,8 @@ func (t *Tree) Match(pattern string, ctx *context.Context) (runObject interface{ if len(pattern) == 0 || pattern[0] != '/' { return nil } - return t.match(pattern, nil, ctx) + w := make([]string, 0, 20) + return t.match(pattern, w, ctx) } func (t *Tree) match(pattern string, wildcardValues []string, ctx *context.Context) (runObject interface{}) {