404 not found performance improvements

benchmark            old ns/op     new ns/op     delta
Benchmark404         737           249           -66.21%
Benchmark404Many     2330          454           -80.52%

benchmark            old allocs     new allocs     delta
Benchmark404         3              0              -100.00%
Benchmark404Many     10             0              -100.00%

benchmark            old bytes     new bytes     delta
Benchmark404         115           68            -40.87%
Benchmark404Many     235           57            -75.74%
This commit is contained in:
Manu Mtz-Almeida
2015-05-30 14:45:13 +02:00
parent deb137cdd2
commit 835f66fdc9
11 changed files with 88 additions and 22 deletions

18
gin.go
View File

@ -11,7 +11,6 @@ import (
"os"
"sync"
"github.com/gin-gonic/gin/binding"
"github.com/gin-gonic/gin/render"
)
@ -73,8 +72,8 @@ func New() *Engine {
BasePath: "/",
},
RedirectTrailingSlash: true,
RedirectFixedPath: true,
HandleMethodNotAllowed: true,
RedirectFixedPath: false,
HandleMethodNotAllowed: false,
trees: make(methodTrees, 0, 6),
}
engine.RouterGroup.engine = engine
@ -285,7 +284,7 @@ func (engine *Engine) serveAutoRedirect(c *Context, root *node, tsr bool) bool {
// Try to fix the request path
if engine.RedirectFixedPath {
fixedPath, found := root.findCaseInsensitivePath(
CleanPath(path),
cleanPath(path),
engine.RedirectTrailingSlash,
)
if found {
@ -299,14 +298,17 @@ func (engine *Engine) serveAutoRedirect(c *Context, root *node, tsr bool) bool {
return false
}
var mimePlain = []string{MIMEPlain}
func serveError(c *Context, code int, defaultMessage []byte) {
c.writermem.status = code
c.Next()
if !c.Writer.Written() {
if c.Writer.Status() == code {
c.Data(-1, binding.MIMEPlain, defaultMessage)
if !c.writermem.Written() {
if c.writermem.Status() == code {
c.writermem.Header()["Content-Type"] = mimePlain
c.Writer.Write(defaultMessage)
} else {
c.Writer.WriteHeaderNow()
c.writermem.WriteHeaderNow()
}
}
}