From ea2ee5b24689c7445c452fa82ad245babb7a4607 Mon Sep 17 00:00:00 2001 From: Carl Jackson Date: Wed, 18 Jun 2014 23:04:06 -0700 Subject: [PATCH] Unroll loop in web.routeMachine.route On my machine, this is worth about 10ns at 5 routes, 20ns at 50 routes, 70ns at 500 routes (a ~5% win), and 40ns at 5000. --- web/router.go | 29 ++++++++++++++++++++++------- 1 file changed, 22 insertions(+), 7 deletions(-) diff --git a/web/router.go b/web/router.go index bff96d5..8719e8c 100644 --- a/web/router.go +++ b/web/router.go @@ -178,13 +178,28 @@ func (rm routeMachine) route(c *C, w http.ResponseWriter, r *http.Request) (meth } length := int(s.mode & smLengthMask) - match := length <= len(p) - for j := 0; match && j < length; j++ { - match = match && p[j] == s.bs[j] - } - - if match { - p = p[length:] + match := false + if length <= len(p) { + switch length { + case 3: + if p[2] != s.bs[2] { + break + } + fallthrough + case 2: + if p[1] != s.bs[1] { + break + } + fallthrough + case 1: + if p[0] != s.bs[0] { + break + } + fallthrough + case 0: + p = p[length:] + match = true + } } if match && s.mode&smRoute != 0 {