Skip to content
Myr

Router optimizations

Before adoption: this commit came in with the history the project adopted, so no landing records it.

benchmark old ns/op new ns/op delta Benchmark404 249 237 -4.82% Benchmark404Many 454 390 -14.10%

authored by Manu Mtz-Almeida · committed by Manu Mtz-Almeida · parent 02a5e09 · Browse files · 1 file+46 added−38 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

1 file+46 added−38 removed

  • other:+46 added−38 removed

1 file+46 added−38 removed

modified:gin.go+46 added-38 removed
226 unmodified lines
227 // Find root of the tree for the given HTTP method228 t := engine.trees229 for i, tl := 0, len(t); i < tl; i++ {230 if t[i].method == httpMethod {231 root := t[i].root232 // Find route in tree232 handlers, params, tsr := t[i].root.getValue(path, context.Params)233 handlers, params, tsr := root.getValue(path, context.Params)234 if handlers != nil {235 context.handlers = handlers236 context.Params = params237 context.Next()238 context.writermem.WriteHeaderNow()239 return240241 } else if httpMethod != "CONNECT" && path != "/" {241 if engine.serveAutoRedirect(context, t[i].root, tsr) {242 if tsr && engine.RedirectFixedPath {243 redirectTrailingSlash(context)244 return245 }246 if engine.RedirectFixedPath && redirectFixedPath(context, root, engine.RedirectFixedPath) {247 return248 }249 }250 }251 }252
12 unmodified lines
265 context.handlers = engine.allNoRoute266 serveError(context, 404, default404Body)267}268264func (engine *Engine) serveAutoRedirect(c *Context, root *node, tsr bool) bool {269var mimePlain = []string{MIMEPlain}270271func serveError(c *Context, code int, defaultMessage []byte) {272 c.writermem.status = code273 c.Next()274 if !c.writermem.Written() {275 if c.writermem.Status() == code {276 c.writermem.Header()["Content-Type"] = mimePlain277 c.Writer.Write(defaultMessage)278 } else {279 c.writermem.WriteHeaderNow()280 }281 }282}283284func redirectTrailingSlash(c *Context) {285 req := c.Request286 path := req.URL.Path287 code := 301 // Permanent redirect, request with GET method288 if req.Method != "GET" {289 code = 307290 }291272 if tsr && engine.RedirectTrailingSlash {273 if len(path) > 1 && path[len(path)-1] == '/' {274 req.URL.Path = path[:len(path)-1]275 } else {276 req.URL.Path = path + "/"292 if len(path) > 1 && path[len(path)-1] == '/' {293 req.URL.Path = path[:len(path)-1]294 } else {295 req.URL.Path = path + "/"296 }297 debugPrint("redirecting request %d: %s --> %s", code, path, req.URL.String())298 http.Redirect(c.Writer, req, req.URL.String(), code)299 c.writermem.WriteHeaderNow()300}301302func redirectFixedPath(c *Context, root *node, trailingSlash bool) bool {303 req := c.Request304 path := req.URL.Path305306 fixedPath, found := root.findCaseInsensitivePath(307 cleanPath(path),308 trailingSlash,309 )310 if found {311 code := 301 // Permanent redirect, request with GET method312 if req.Method != "GET" {313 code = 307314 }315 req.URL.Path = string(fixedPath)316 debugPrint("redirecting request %d: %s --> %s", code, path, req.URL.String())317 http.Redirect(c.Writer, req, req.URL.String(), code)318 c.writermem.WriteHeaderNow()319 return true320 }283284 // Try to fix the request path285 if engine.RedirectFixedPath {286 fixedPath, found := root.findCaseInsensitivePath(287 cleanPath(path),288 engine.RedirectTrailingSlash,289 )290 if found {291 req.URL.Path = string(fixedPath)292 debugPrint("redirecting request %d: %s --> %s", code, path, req.URL.String())293 http.Redirect(c.Writer, req, req.URL.String(), code)294 c.writermem.WriteHeaderNow()295 return true296 }297 }321 return false322}300301var mimePlain = []string{MIMEPlain}302303func serveError(c *Context, code int, defaultMessage []byte) {304 c.writermem.status = code305 c.Next()306 if !c.writermem.Written() {307 if c.writermem.Status() == code {308 c.writermem.Header()["Content-Type"] = mimePlain309 c.Writer.Write(defaultMessage)310 } else {311 c.writermem.WriteHeaderNow()312 }313 }314}