mirror of
				https://github.com/gin-gonic/gin.git
				synced 2025-11-04 17:22:12 +08:00 
			
		
		
		
	perf(tree): optimize path parsing using strings.Count (#4246)
Co-authored-by: 1911860538 <alxps1911@gmail.com>
This commit is contained in:
		
							parent
							
								
									52f70cf18a
								
							
						
					
					
						commit
						2e22e50859
					
				
							
								
								
									
										18
									
								
								tree.go
									
									
									
									
									
								
							
							
						
						
									
										18
									
								
								tree.go
									
									
									
									
									
								
							@ -5,7 +5,6 @@
 | 
				
			|||||||
package gin
 | 
					package gin
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import (
 | 
					import (
 | 
				
			||||||
	"bytes"
 | 
					 | 
				
			||||||
	"net/url"
 | 
						"net/url"
 | 
				
			||||||
	"strings"
 | 
						"strings"
 | 
				
			||||||
	"unicode"
 | 
						"unicode"
 | 
				
			||||||
@ -14,12 +13,6 @@ import (
 | 
				
			|||||||
	"github.com/gin-gonic/gin/internal/bytesconv"
 | 
						"github.com/gin-gonic/gin/internal/bytesconv"
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
var (
 | 
					 | 
				
			||||||
	strColon = []byte(":")
 | 
					 | 
				
			||||||
	strStar  = []byte("*")
 | 
					 | 
				
			||||||
	strSlash = []byte("/")
 | 
					 | 
				
			||||||
)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
// Param is a single URL parameter, consisting of a key and a value.
 | 
					// Param is a single URL parameter, consisting of a key and a value.
 | 
				
			||||||
type Param struct {
 | 
					type Param struct {
 | 
				
			||||||
	Key   string
 | 
						Key   string
 | 
				
			||||||
@ -85,16 +78,13 @@ func (n *node) addChild(child *node) {
 | 
				
			|||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func countParams(path string) uint16 {
 | 
					func countParams(path string) uint16 {
 | 
				
			||||||
	var n uint16
 | 
						colons := strings.Count(path, ":")
 | 
				
			||||||
	s := bytesconv.StringToBytes(path)
 | 
						stars := strings.Count(path, "*")
 | 
				
			||||||
	n += uint16(bytes.Count(s, strColon))
 | 
						return uint16(colons + stars)
 | 
				
			||||||
	n += uint16(bytes.Count(s, strStar))
 | 
					 | 
				
			||||||
	return n
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func countSections(path string) uint16 {
 | 
					func countSections(path string) uint16 {
 | 
				
			||||||
	s := bytesconv.StringToBytes(path)
 | 
						return uint16(strings.Count(path, "/"))
 | 
				
			||||||
	return uint16(bytes.Count(s, strSlash))
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
type nodeType uint8
 | 
					type nodeType uint8
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user