Skip to content

Commit c7035bb

Browse files
author
1911860538
committed
perf: optimize path parsing using strings.Count
1 parent e4c2a27 commit c7035bb

File tree

1 file changed

+4
-14
lines changed

1 file changed

+4
-14
lines changed

tree.go

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
package gin
66

77
import (
8-
"bytes"
98
"net/url"
109
"strings"
1110
"unicode"
@@ -14,12 +13,6 @@ import (
1413
"github.com/gin-gonic/gin/internal/bytesconv"
1514
)
1615

17-
var (
18-
strColon = []byte(":")
19-
strStar = []byte("*")
20-
strSlash = []byte("/")
21-
)
22-
2316
// Param is a single URL parameter, consisting of a key and a value.
2417
type Param struct {
2518
Key string
@@ -85,16 +78,13 @@ func (n *node) addChild(child *node) {
8578
}
8679

8780
func countParams(path string) uint16 {
88-
var n uint16
89-
s := bytesconv.StringToBytes(path)
90-
n += uint16(bytes.Count(s, strColon))
91-
n += uint16(bytes.Count(s, strStar))
92-
return n
81+
colons := strings.Count(path, ":")
82+
stars := strings.Count(path, "*")
83+
return uint16(colons + stars)
9384
}
9485

9586
func countSections(path string) uint16 {
96-
s := bytesconv.StringToBytes(path)
97-
return uint16(bytes.Count(s, strSlash))
87+
return uint16(strings.Count(path, "/"))
9888
}
9989

10090
type nodeType uint8

0 commit comments

Comments
 (0)