Skip to content

Commit

Permalink
feat: small optimization to not reset twice the route in ServeHTTP
Browse files Browse the repository at this point in the history
  • Loading branch information
tigerwill90 committed Oct 15, 2024
1 parent 59c27fe commit dc2dadf
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 14 deletions.
26 changes: 13 additions & 13 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,17 +120,6 @@ type cTx struct {
tsr bool
}

// Reset resets the [Context] to its initial state, attaching the provided [ResponseWriter] and [http.Request].
func (c *cTx) Reset(w ResponseWriter, r *http.Request) {
c.req = r
c.w = w
c.tsr = false
c.cachedQuery = nil
c.route = nil
c.scope = RouteHandler
*c.params = (*c.params)[:0]
}

// Rehydrate updates the current [Context] to serve the provided [Route], bypassing the need for a full tree lookup.
// It succeeds only if the [http.Request]'s URL path strictly matches the given [Route]. If successful, the internal state
// of the context is updated, allowing the context to serve the route directly, regardless of whether the route
Expand Down Expand Up @@ -169,13 +158,13 @@ func (c *cTx) Rehydrate(route *Route) bool {

// reset resets the [Context] to its initial state, attaching the provided [http.ResponseWriter] and [http.Request].
// Caution: always pass the original [http.ResponseWriter] to this method, not the [ResponseWriter] itself, to
// avoid wrapping the [ResponseWriter] within itself. Use wisely!
// avoid wrapping the [ResponseWriter] within itself. Use wisely! Note that ServeHTTP is managing the reset of
// c.route and c.tsr.
func (c *cTx) reset(w http.ResponseWriter, r *http.Request) {
c.rec.reset(w)
c.req = r
c.w = &c.rec
c.cachedQuery = nil
c.route = nil
c.scope = RouteHandler
*c.params = (*c.params)[:0]
}
Expand All @@ -188,6 +177,17 @@ func (c *cTx) resetNil() {
*c.params = (*c.params)[:0]
}

// resetWithWriter resets the [Context] to its initial state, attaching the provided [ResponseWriter] and [http.Request].
func (c *cTx) resetWithWriter(w ResponseWriter, r *http.Request) {
c.req = r
c.w = w
c.tsr = false
c.cachedQuery = nil
c.route = nil
c.scope = RouteHandler
*c.params = (*c.params)[:0]
}

// Request returns the [http.Request].
func (c *cTx) Request() *http.Request {
return c.req
Expand Down
2 changes: 2 additions & 0 deletions fox.go
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,7 @@ func (fox *Router) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if n.route.redirectTrailingSlash && target == CleanPath(target) {
// Reset params as it may have recorded wildcard segment (the context may still be used in a middleware)
*c.params = (*c.params)[:0]
c.route = nil
c.tsr = false
c.scope = RedirectHandler
fox.tsrRedirect(c)
Expand All @@ -384,6 +385,7 @@ func (fox *Router) ServeHTTP(w http.ResponseWriter, r *http.Request) {
// Reset params as it may have recorded wildcard segment (the context may still be used in no route, no method and
// automatic option handler or middleware)
*c.params = (*c.params)[:0]
c.route = nil
c.tsr = false

NoMethodFallback:
Expand Down
2 changes: 1 addition & 1 deletion tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ func (t *Tree) Lookup(w ResponseWriter, r *http.Request) (route *Route, cc Conte
}

c := t.ctx.Get().(*cTx)
c.Reset(w, r)
c.resetWithWriter(w, r)

target := r.URL.Path
if len(r.URL.RawPath) > 0 {
Expand Down

0 comments on commit dc2dadf

Please sign in to comment.