@@ -11,6 +11,7 @@ import (
11
11
"regexp"
12
12
"strconv"
13
13
"strings"
14
+ "sync"
14
15
"time"
15
16
"unicode"
16
17
@@ -26,6 +27,12 @@ type routeParser struct {
26
27
plusCount int // number of plus parameters, used internally to give the plus parameter its number
27
28
}
28
29
30
+ var routerParserPool = & sync.Pool {
31
+ New : func () any {
32
+ return & routeParser {}
33
+ },
34
+ }
35
+
29
36
// routeSegment holds the segment metadata
30
37
type routeSegment struct {
31
38
// const information
@@ -163,7 +170,10 @@ func RoutePatternMatch(path, pattern string, cfg ...Config) bool {
163
170
patternPretty = utils .TrimRight (patternPretty , '/' )
164
171
}
165
172
166
- parser := parseRoute (string (patternPretty ))
173
+ parser , _ := routerParserPool .Get ().(* routeParser ) //nolint:errcheck // only contains routeParser
174
+ parser .reset ()
175
+ parser .parseRoute (string (patternPretty ))
176
+ defer routerParserPool .Put (parser )
167
177
168
178
if string (patternPretty ) == "/" && path == "/" {
169
179
return true
@@ -184,10 +194,16 @@ func RoutePatternMatch(path, pattern string, cfg ...Config) bool {
184
194
return string (patternPretty ) == path
185
195
}
186
196
197
+ func (parser * routeParser ) reset () {
198
+ parser .segs = parser .segs [:0 ]
199
+ parser .params = parser .params [:0 ]
200
+ parser .wildCardCount = 0
201
+ parser .plusCount = 0
202
+ }
203
+
187
204
// parseRoute analyzes the route and divides it into segments for constant areas and parameters,
188
205
// this information is needed later when assigning the requests to the declared routes
189
- func parseRoute (pattern string , customConstraints ... CustomConstraint ) routeParser {
190
- parser := routeParser {}
206
+ func (parser * routeParser ) parseRoute (pattern string , customConstraints ... CustomConstraint ) {
191
207
var n int
192
208
var seg * routeSegment
193
209
for len (pattern ) > 0 {
@@ -207,7 +223,13 @@ func parseRoute(pattern string, customConstraints ...CustomConstraint) routePars
207
223
parser .segs [len (parser .segs )- 1 ].IsLast = true
208
224
}
209
225
parser .segs = addParameterMetaInfo (parser .segs )
226
+ }
210
227
228
+ // parseRoute analyzes the route and divides it into segments for constant areas and parameters,
229
+ // this information is needed later when assigning the requests to the declared routes
230
+ func parseRoute (pattern string , customConstraints ... CustomConstraint ) routeParser {
231
+ parser := routeParser {}
232
+ parser .parseRoute (pattern , customConstraints ... )
211
233
return parser
212
234
}
213
235
@@ -290,7 +312,7 @@ func (*routeParser) analyseConstantPart(pattern string, nextParamPosition int) (
290
312
}
291
313
292
314
// analyseParameterPart find the parameter end and create the route segment
293
- func (routeParser * routeParser ) analyseParameterPart (pattern string , customConstraints ... CustomConstraint ) (int , * routeSegment ) {
315
+ func (parser * routeParser ) analyseParameterPart (pattern string , customConstraints ... CustomConstraint ) (int , * routeSegment ) {
294
316
isWildCard := pattern [0 ] == wildcardParam
295
317
isPlusParam := pattern [0 ] == plusParam
296
318
@@ -377,11 +399,11 @@ func (routeParser *routeParser) analyseParameterPart(pattern string, customConst
377
399
378
400
// add access iterator to wildcard and plus
379
401
if isWildCard {
380
- routeParser .wildCardCount ++
381
- paramName += strconv .Itoa (routeParser .wildCardCount )
402
+ parser .wildCardCount ++
403
+ paramName += strconv .Itoa (parser .wildCardCount )
382
404
} else if isPlusParam {
383
- routeParser .plusCount ++
384
- paramName += strconv .Itoa (routeParser .plusCount )
405
+ parser .plusCount ++
406
+ paramName += strconv .Itoa (parser .plusCount )
385
407
}
386
408
387
409
segment := & routeSegment {
@@ -465,9 +487,9 @@ func splitNonEscaped(s, sep string) []string {
465
487
}
466
488
467
489
// getMatch parses the passed url and tries to match it against the route segments and determine the parameter positions
468
- func (routeParser * routeParser ) getMatch (detectionPath , path string , params * [maxParams ]string , partialCheck bool ) bool { //nolint: revive // Accepting a bool param is fine here
490
+ func (parser * routeParser ) getMatch (detectionPath , path string , params * [maxParams ]string , partialCheck bool ) bool { //nolint: revive // Accepting a bool param is fine here
469
491
var i , paramsIterator , partLen int
470
- for _ , segment := range routeParser .segs {
492
+ for _ , segment := range parser .segs {
471
493
partLen = len (detectionPath )
472
494
// check const segment
473
495
if ! segment .IsParam {
0 commit comments