Improved performance
This commit is contained in:
35
Tree.go
35
Tree.go
@ -92,11 +92,10 @@ func (tree *Tree[T]) Lookup(path string) (T, []Parameter) {
|
||||
// LookupNoAlloc finds the data for the given path without using any memory allocations.
|
||||
func (tree *Tree[T]) LookupNoAlloc(path string, addParameter func(key string, value string)) T {
|
||||
var (
|
||||
i uint
|
||||
offset uint
|
||||
wildcardOffset uint
|
||||
wildcard *treeNode[T]
|
||||
node = &tree.root
|
||||
i uint
|
||||
wildcardPath string
|
||||
wildcard *treeNode[T]
|
||||
node = &tree.root
|
||||
)
|
||||
|
||||
// Skip the first loop iteration if the starting characters are equal
|
||||
@ -110,10 +109,10 @@ begin:
|
||||
// The node we just checked is entirely included in our path.
|
||||
// node: /|
|
||||
// path: /|blog
|
||||
if i-offset == uint(len(node.prefix)) {
|
||||
if i == uint(len(node.prefix)) {
|
||||
if node.wildcard != nil {
|
||||
wildcard = node.wildcard
|
||||
wildcardOffset = i
|
||||
wildcardPath = path[i:]
|
||||
}
|
||||
|
||||
char := path[i]
|
||||
@ -123,8 +122,8 @@ begin:
|
||||
|
||||
if index != 0 {
|
||||
node = node.children[index]
|
||||
offset = i
|
||||
i++
|
||||
path = path[i:]
|
||||
i = 1
|
||||
continue
|
||||
}
|
||||
}
|
||||
@ -133,25 +132,25 @@ begin:
|
||||
// path: /|blog
|
||||
if node.parameter != nil {
|
||||
node = node.parameter
|
||||
offset = i
|
||||
i++
|
||||
path = path[i:]
|
||||
i = 1
|
||||
|
||||
for i < uint(len(path)) {
|
||||
// node: /:id|/posts
|
||||
// path: /123|/posts
|
||||
if path[i] == separator {
|
||||
addParameter(node.prefix, path[offset:i])
|
||||
addParameter(node.prefix, path[:i])
|
||||
index := node.indices[separator-node.startIndex]
|
||||
node = node.children[index]
|
||||
offset = i
|
||||
i++
|
||||
path = path[i:]
|
||||
i = 1
|
||||
goto begin
|
||||
}
|
||||
|
||||
i++
|
||||
}
|
||||
|
||||
addParameter(node.prefix, path[offset:i])
|
||||
addParameter(node.prefix, path[:i])
|
||||
return node.data
|
||||
}
|
||||
|
||||
@ -163,7 +162,7 @@ begin:
|
||||
// We got a conflict.
|
||||
// node: /b|ag
|
||||
// path: /b|riefcase
|
||||
if path[i] != node.prefix[i-offset] {
|
||||
if path[i] != node.prefix[i] {
|
||||
goto notFound
|
||||
}
|
||||
|
||||
@ -172,7 +171,7 @@ begin:
|
||||
|
||||
// node: /blog|
|
||||
// path: /blog|
|
||||
if i-offset == uint(len(node.prefix)) {
|
||||
if i == uint(len(node.prefix)) {
|
||||
return node.data
|
||||
}
|
||||
|
||||
@ -180,7 +179,7 @@ begin:
|
||||
// path: /|image.png
|
||||
notFound:
|
||||
if wildcard != nil {
|
||||
addParameter(wildcard.prefix, path[wildcardOffset:])
|
||||
addParameter(wildcard.prefix, wildcardPath)
|
||||
return wildcard.data
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user