1
0
mirror of https://github.com/labstack/echo.git synced 2025-03-21 21:27:04 +02:00

Improved router performance

Signed-off-by: Vishal Rana <vr@labstack.com>
This commit is contained in:
Vishal Rana 2015-06-05 15:08:32 -07:00
parent 80a4b41e16
commit 33886c8758
5 changed files with 67 additions and 56 deletions

View File

@ -49,8 +49,8 @@ func (c *Context) Socket() *websocket.Conn {
} }
// P returns path parameter by index. // P returns path parameter by index.
func (c *Context) P(i uint8) (value string) { func (c *Context) P(i int) (value string) {
l := uint8(len(c.pnames)) l := len(c.pnames)
if i <= l { if i <= l {
value = c.pvalues[i] value = c.pvalues[i]
} }

0
examples/website/public/favicon.ico Executable file → Normal file
View File

Before

Width:  |  Height:  |  Size: 1.1 KiB

After

Width:  |  Height:  |  Size: 1.1 KiB

105
router.go
View File

@ -4,9 +4,9 @@ import "net/http"
type ( type (
Router struct { Router struct {
trees map[string]*node trees [21]*node
routes []Route routes []Route
echo *Echo echo *Echo
} }
node struct { node struct {
typ ntype typ ntype
@ -30,12 +30,12 @@ const (
func NewRouter(e *Echo) (r *Router) { func NewRouter(e *Echo) (r *Router) {
r = &Router{ r = &Router{
trees: make(map[string]*node), // trees: make(map[string]*node),
routes: []Route{}, routes: []Route{},
echo: e, echo: e,
} }
for _, m := range methods { for _, m := range methods {
r.trees[m] = &node{ r.trees[r.treeIndex(m)] = &node{
prefix: "", prefix: "",
children: children{}, children: children{},
} }
@ -81,13 +81,21 @@ func (r *Router) insert(method, path string, h HandlerFunc, t ntype, pnames []st
*e.maxParam = l *e.maxParam = l
} }
cn := r.trees[method] // Current node as root cn := r.trees[r.treeIndex(method)] // Current node as root
search := path search := path
for { for {
sl := len(search) sl := len(search)
pl := len(cn.prefix) pl := len(cn.prefix)
l := lcp(search, cn.prefix) l := 0
// LCP
max := pl
if sl < max {
max = sl
}
for ; l < max && search[l] == cn.prefix[l]; l++ {
}
if l == 0 { if l == 0 {
// At root node // At root node
@ -102,16 +110,18 @@ func (r *Router) insert(method, path string, h HandlerFunc, t ntype, pnames []st
} else if l < pl { } else if l < pl {
// Split node // Split node
n := newNode(cn.typ, cn.prefix[l:], cn, cn.children, cn.handler, cn.pnames, cn.echo) n := newNode(cn.typ, cn.prefix[l:], cn, cn.children, cn.handler, cn.pnames, cn.echo)
cn.children = children{n} // Add to parent
// Reset parent node // Reset parent node
cn.typ = stype cn.typ = stype
cn.label = cn.prefix[0] cn.label = cn.prefix[0]
cn.prefix = cn.prefix[:l] cn.prefix = cn.prefix[:l]
cn.children = nil
cn.handler = nil cn.handler = nil
cn.pnames = nil cn.pnames = nil
cn.echo = nil cn.echo = nil
cn.addChild(n)
if l == sl { if l == sl {
// At parent node // At parent node
cn.typ = t cn.typ = t
@ -121,11 +131,11 @@ func (r *Router) insert(method, path string, h HandlerFunc, t ntype, pnames []st
} else { } else {
// Create child node // Create child node
n = newNode(t, search[l:], cn, nil, h, pnames, e) n = newNode(t, search[l:], cn, nil, h, pnames, e)
cn.children = append(cn.children, n) cn.addChild(n)
} }
} else if l < sl { } else if l < sl {
search = search[l:] search = search[l:]
c := cn.findChild(search[0]) c := cn.findChildWithLabel(search[0])
if c != nil { if c != nil {
// Go deeper // Go deeper
cn = c cn = c
@ -133,7 +143,7 @@ func (r *Router) insert(method, path string, h HandlerFunc, t ntype, pnames []st
} }
// Create child node // Create child node
n := newNode(t, search, cn, nil, h, pnames, e) n := newNode(t, search, cn, nil, h, pnames, e)
cn.children = append(cn.children, n) cn.addChild(n)
} else { } else {
// Node already exists // Node already exists
if h != nil { if h != nil {
@ -159,7 +169,20 @@ func newNode(t ntype, pre string, p *node, c children, h HandlerFunc, pnames []s
} }
} }
func (n *node) findChild(l byte) *node { func (n *node) addChild(c *node) {
n.children = append(n.children, c)
}
func (n *node) findChild(l byte, t ntype) *node {
for _, c := range n.children {
if c.label == l && c.typ == t {
return c
}
}
return nil
}
func (n *node) findChildWithLabel(l byte) *node {
for _, c := range n.children { for _, c := range n.children {
if c.label == l { if c.label == l {
return c return c
@ -168,47 +191,25 @@ func (n *node) findChild(l byte) *node {
return nil return nil
} }
func (n *node) findSchild(l byte) *node { func (n *node) findChildWithType(t ntype) *node {
for _, c := range n.children { for _, c := range n.children {
if c.label == l && c.typ == stype { if c.typ == t {
return c return c
} }
} }
return nil return nil
} }
func (n *node) findPchild() *node { func (r *Router) treeIndex(method string) uint8 {
for _, c := range n.children { if method[0] == 'P' {
if c.typ == ptype { return method[0]%10 + method[1] - 65
return c } else {
} return method[0] % 10
} }
return nil
}
func (n *node) findMchild() *node {
for _, c := range n.children {
if c.typ == mtype {
return c
}
}
return nil
}
// Length of longest common prefix
func lcp(a, b string) (i int) {
max := len(a)
l := len(b)
if l < max {
max = l
}
for ; i < max && a[i] == b[i]; i++ {
}
return
} }
func (r *Router) Find(method, path string, ctx *Context) (h HandlerFunc, e *Echo) { func (r *Router) Find(method, path string, ctx *Context) (h HandlerFunc, e *Echo) {
cn := r.trees[method] // Current node as root cn := r.trees[r.treeIndex(method)] // Current node as root
search := path search := path
var ( var (
@ -235,8 +236,16 @@ func (r *Router) Find(method, path string, ctx *Context) (h HandlerFunc, e *Echo
l := 0 // LCP length l := 0 // LCP length
if cn.label != ':' { if cn.label != ':' {
sl := len(search)
pl = len(cn.prefix) pl = len(cn.prefix)
l = lcp(search, cn.prefix)
// LCP
max := pl
if sl < max {
max = sl
}
for ; l < max && search[l] == cn.prefix[l]; l++ {
}
} }
if l == pl { if l == pl {
@ -257,7 +266,7 @@ func (r *Router) Find(method, path string, ctx *Context) (h HandlerFunc, e *Echo
if search == "" { if search == "" {
// TODO: Needs improvement // TODO: Needs improvement
if cn.findMchild() == nil { if cn.findChildWithType(mtype) == nil {
continue continue
} }
// Empty value // Empty value
@ -265,7 +274,7 @@ func (r *Router) Find(method, path string, ctx *Context) (h HandlerFunc, e *Echo
} }
// Static node // Static node
c = cn.findSchild(search[0]) c = cn.findChild(search[0], stype)
if c != nil { if c != nil {
// Save next // Save next
if cn.label == '/' { if cn.label == '/' {
@ -279,7 +288,7 @@ func (r *Router) Find(method, path string, ctx *Context) (h HandlerFunc, e *Echo
// Param node // Param node
Param: Param:
c = cn.findPchild() c = cn.findChildWithType(ptype)
if c != nil { if c != nil {
// Save next // Save next
if cn.label == '/' { if cn.label == '/' {
@ -299,13 +308,15 @@ func (r *Router) Find(method, path string, ctx *Context) (h HandlerFunc, e *Echo
// Match-any node // Match-any node
MatchAny: MatchAny:
c = cn.findMchild() // c = cn.getChild()
c = cn.findChildWithType(mtype)
if c != nil { if c != nil {
cn = c cn = c
ctx.pvalues[0] = search ctx.pvalues[0] = search
search = "" // End search search = "" // End search
continue continue
} }
// Not found // Not found
return return
} }

View File

@ -10,7 +10,7 @@ import (
) )
var ( var (
api = []Route{ api = []Route{
// OAuth Authorizations // OAuth Authorizations
{"GET", "/authorizations", nil}, {"GET", "/authorizations", nil},
{"GET", "/authorizations/:id", nil}, {"GET", "/authorizations/:id", nil},
@ -520,11 +520,6 @@ func TestRouterAPI(t *testing.T) {
for _, route := range api { for _, route := range api {
r.Add(route.Method, route.Path, func(c *Context) error { r.Add(route.Method, route.Path, func(c *Context) error {
for i, n := range c.pnames {
if assert.NotEmpty(t, n) {
assert.Equal(t, ":"+n, c.P(uint8(i)))
}
}
return nil return nil
}, e) }, e)
} }
@ -532,6 +527,11 @@ func TestRouterAPI(t *testing.T) {
for _, route := range api { for _, route := range api {
h, _ := r.Find(route.Method, route.Path, c) h, _ := r.Find(route.Method, route.Path, c)
if assert.NotNil(t, h) { if assert.NotNil(t, h) {
for i, n := range c.pnames {
if assert.NotEmpty(t, n) {
assert.Equal(t, ":"+n, c.P(i))
}
}
h(c) h(c)
} }
} }

View File

@ -77,7 +77,7 @@ types of handlers.
### Path parameter ### Path parameter
Request path parameters can be extracted either by name `Echo.Context.Param(name string) string` Request path parameters can be extracted either by name `Echo.Context.Param(name string) string`
or by index `Echo.Context.P(i uint8) string`. Getting parameter by index gives a or by index `Echo.Context.P(i int) string`. Getting parameter by index gives a
slightly better performance. slightly better performance.
```go ```go