Skip to content

Commit b9bb956

Browse files
committed
Merge pull request #91 from labstack/router
Improved router performance
2 parents 80a4b41 + 33886c8 commit b9bb956

File tree

5 files changed

+59
-48
lines changed

5 files changed

+59
-48
lines changed

context.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ func (c *Context) Socket() *websocket.Conn {
4949
}
5050

5151
// P returns path parameter by index.
52-
func (c *Context) P(i uint8) (value string) {
53-
l := uint8(len(c.pnames))
52+
func (c *Context) P(i int) (value string) {
53+
l := len(c.pnames)
5454
if i <= l {
5555
value = c.pvalues[i]
5656
}

examples/website/public/favicon.ico

100755100644
File mode changed.

router.go

Lines changed: 50 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ import "net/http"
44

55
type (
66
Router struct {
7-
trees map[string]*node
8-
routes []Route
9-
echo *Echo
7+
trees [21]*node
8+
routes []Route
9+
echo *Echo
1010
}
1111
node struct {
1212
typ ntype
@@ -30,12 +30,12 @@ const (
3030

3131
func NewRouter(e *Echo) (r *Router) {
3232
r = &Router{
33-
trees: make(map[string]*node),
33+
// trees: make(map[string]*node),
3434
routes: []Route{},
3535
echo: e,
3636
}
3737
for _, m := range methods {
38-
r.trees[m] = &node{
38+
r.trees[r.treeIndex(m)] = &node{
3939
prefix: "",
4040
children: children{},
4141
}
@@ -81,13 +81,21 @@ func (r *Router) insert(method, path string, h HandlerFunc, t ntype, pnames []st
8181
*e.maxParam = l
8282
}
8383

84-
cn := r.trees[method] // Current node as root
84+
cn := r.trees[r.treeIndex(method)] // Current node as root
8585
search := path
8686

8787
for {
8888
sl := len(search)
8989
pl := len(cn.prefix)
90-
l := lcp(search, cn.prefix)
90+
l := 0
91+
92+
// LCP
93+
max := pl
94+
if sl < max {
95+
max = sl
96+
}
97+
for ; l < max && search[l] == cn.prefix[l]; l++ {
98+
}
9199

92100
if l == 0 {
93101
// At root node
@@ -102,16 +110,18 @@ func (r *Router) insert(method, path string, h HandlerFunc, t ntype, pnames []st
102110
} else if l < pl {
103111
// Split node
104112
n := newNode(cn.typ, cn.prefix[l:], cn, cn.children, cn.handler, cn.pnames, cn.echo)
105-
cn.children = children{n} // Add to parent
106113

107114
// Reset parent node
108115
cn.typ = stype
109116
cn.label = cn.prefix[0]
110117
cn.prefix = cn.prefix[:l]
118+
cn.children = nil
111119
cn.handler = nil
112120
cn.pnames = nil
113121
cn.echo = nil
114122

123+
cn.addChild(n)
124+
115125
if l == sl {
116126
// At parent node
117127
cn.typ = t
@@ -121,19 +131,19 @@ func (r *Router) insert(method, path string, h HandlerFunc, t ntype, pnames []st
121131
} else {
122132
// Create child node
123133
n = newNode(t, search[l:], cn, nil, h, pnames, e)
124-
cn.children = append(cn.children, n)
134+
cn.addChild(n)
125135
}
126136
} else if l < sl {
127137
search = search[l:]
128-
c := cn.findChild(search[0])
138+
c := cn.findChildWithLabel(search[0])
129139
if c != nil {
130140
// Go deeper
131141
cn = c
132142
continue
133143
}
134144
// Create child node
135145
n := newNode(t, search, cn, nil, h, pnames, e)
136-
cn.children = append(cn.children, n)
146+
cn.addChild(n)
137147
} else {
138148
// Node already exists
139149
if h != nil {
@@ -159,56 +169,47 @@ func newNode(t ntype, pre string, p *node, c children, h HandlerFunc, pnames []s
159169
}
160170
}
161171

162-
func (n *node) findChild(l byte) *node {
163-
for _, c := range n.children {
164-
if c.label == l {
165-
return c
166-
}
167-
}
168-
return nil
172+
func (n *node) addChild(c *node) {
173+
n.children = append(n.children, c)
169174
}
170175

171-
func (n *node) findSchild(l byte) *node {
176+
func (n *node) findChild(l byte, t ntype) *node {
172177
for _, c := range n.children {
173-
if c.label == l && c.typ == stype {
178+
if c.label == l && c.typ == t {
174179
return c
175180
}
176181
}
177182
return nil
178183
}
179184

180-
func (n *node) findPchild() *node {
185+
func (n *node) findChildWithLabel(l byte) *node {
181186
for _, c := range n.children {
182-
if c.typ == ptype {
187+
if c.label == l {
183188
return c
184189
}
185190
}
186191
return nil
187192
}
188193

189-
func (n *node) findMchild() *node {
194+
func (n *node) findChildWithType(t ntype) *node {
190195
for _, c := range n.children {
191-
if c.typ == mtype {
196+
if c.typ == t {
192197
return c
193198
}
194199
}
195200
return nil
196201
}
197202

198-
// Length of longest common prefix
199-
func lcp(a, b string) (i int) {
200-
max := len(a)
201-
l := len(b)
202-
if l < max {
203-
max = l
204-
}
205-
for ; i < max && a[i] == b[i]; i++ {
203+
func (r *Router) treeIndex(method string) uint8 {
204+
if method[0] == 'P' {
205+
return method[0]%10 + method[1] - 65
206+
} else {
207+
return method[0] % 10
206208
}
207-
return
208209
}
209210

210211
func (r *Router) Find(method, path string, ctx *Context) (h HandlerFunc, e *Echo) {
211-
cn := r.trees[method] // Current node as root
212+
cn := r.trees[r.treeIndex(method)] // Current node as root
212213
search := path
213214

214215
var (
@@ -235,8 +236,16 @@ func (r *Router) Find(method, path string, ctx *Context) (h HandlerFunc, e *Echo
235236
l := 0 // LCP length
236237

237238
if cn.label != ':' {
239+
sl := len(search)
238240
pl = len(cn.prefix)
239-
l = lcp(search, cn.prefix)
241+
242+
// LCP
243+
max := pl
244+
if sl < max {
245+
max = sl
246+
}
247+
for ; l < max && search[l] == cn.prefix[l]; l++ {
248+
}
240249
}
241250

242251
if l == pl {
@@ -257,15 +266,15 @@ func (r *Router) Find(method, path string, ctx *Context) (h HandlerFunc, e *Echo
257266

258267
if search == "" {
259268
// TODO: Needs improvement
260-
if cn.findMchild() == nil {
269+
if cn.findChildWithType(mtype) == nil {
261270
continue
262271
}
263272
// Empty value
264273
goto MatchAny
265274
}
266275

267276
// Static node
268-
c = cn.findSchild(search[0])
277+
c = cn.findChild(search[0], stype)
269278
if c != nil {
270279
// Save next
271280
if cn.label == '/' {
@@ -279,7 +288,7 @@ func (r *Router) Find(method, path string, ctx *Context) (h HandlerFunc, e *Echo
279288

280289
// Param node
281290
Param:
282-
c = cn.findPchild()
291+
c = cn.findChildWithType(ptype)
283292
if c != nil {
284293
// Save next
285294
if cn.label == '/' {
@@ -299,13 +308,15 @@ func (r *Router) Find(method, path string, ctx *Context) (h HandlerFunc, e *Echo
299308

300309
// Match-any node
301310
MatchAny:
302-
c = cn.findMchild()
311+
// c = cn.getChild()
312+
c = cn.findChildWithType(mtype)
303313
if c != nil {
304314
cn = c
305315
ctx.pvalues[0] = search
306316
search = "" // End search
307317
continue
308318
}
319+
309320
// Not found
310321
return
311322
}

router_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
)
1111

1212
var (
13-
api = []Route{
13+
api = []Route{
1414
// OAuth Authorizations
1515
{"GET", "/authorizations", nil},
1616
{"GET", "/authorizations/:id", nil},
@@ -520,18 +520,18 @@ func TestRouterAPI(t *testing.T) {
520520

521521
for _, route := range api {
522522
r.Add(route.Method, route.Path, func(c *Context) error {
523-
for i, n := range c.pnames {
524-
if assert.NotEmpty(t, n) {
525-
assert.Equal(t, ":"+n, c.P(uint8(i)))
526-
}
527-
}
528523
return nil
529524
}, e)
530525
}
531526
c := NewContext(nil, nil, e)
532527
for _, route := range api {
533528
h, _ := r.Find(route.Method, route.Path, c)
534529
if assert.NotNil(t, h) {
530+
for i, n := range c.pnames {
531+
if assert.NotEmpty(t, n) {
532+
assert.Equal(t, ":"+n, c.P(i))
533+
}
534+
}
535535
h(c)
536536
}
537537
}

website/docs/guide.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ types of handlers.
7777
### Path parameter
7878

7979
Request path parameters can be extracted either by name `Echo.Context.Param(name string) string`
80-
or by index `Echo.Context.P(i uint8) string`. Getting parameter by index gives a
80+
or by index `Echo.Context.P(i int) string`. Getting parameter by index gives a
8181
slightly better performance.
8282

8383
```go

0 commit comments

Comments
 (0)