@@ -19,6 +19,7 @@ type Route struct {
1919 mws []middleware
2020 params []string
2121 matchers []Matcher
22+ methodFast string
2223 pattern pattern
2324 priority uint
2425 handleSlash TrailingSlashOption
@@ -135,12 +136,27 @@ func (r *Route) String() string {
135136// match reports whether the request satisfies this route's method constraint (if any)
136137// and all attached matchers.
137138func (r * Route ) match (method string , c RequestContext ) bool {
138- // Fast path for common cases: no methods or single method
139+ // Fast path: routes with exactly one method and no matchers cache that
140+ // method in methodFast.
141+ if r .methodFast == method {
142+ return true
143+ }
144+ return r .matchSlow (method , c )
145+ }
146+
147+ // matchSlow handles the cases match's fast path does not cover: zero or many
148+ // methods, and routes with matchers. It is kept out-of-line so match remains
149+ // inlinable.
150+ //
151+ //go:noinline
152+ func (r * Route ) matchSlow (method string , c RequestContext ) bool {
139153 methods := r .methods
140154 switch len (methods ) {
141155 case 0 :
142- // No method constraint
156+ // No method constraint.
143157 case 1 :
158+ // Avoid the slices.Contains overhead for the single-method case (which
159+ // match's fast path leaves to us when matchers are present).
144160 if methods [0 ] != method {
145161 return false
146162 }
@@ -149,7 +165,6 @@ func (r *Route) match(method string, c RequestContext) bool {
149165 return false
150166 }
151167 }
152-
153168 for _ , m := range r .matchers {
154169 if ! m .Match (c ) {
155170 return false
0 commit comments