Improved performance
This commit is contained in:
parent
8f507e4430
commit
7ec90a691b
10
README.md
10
README.md
@ -59,11 +59,11 @@ coverage: 100.0% of statements
|
|||||||
## Benchmarks
|
## Benchmarks
|
||||||
|
|
||||||
```
|
```
|
||||||
BenchmarkBlog/Len1-Param0-12 201446469 5.956 ns/op 0 B/op 0 allocs/op
|
BenchmarkBlog/Len1-Param0-12 211814850 5.646 ns/op 0 B/op 0 allocs/op
|
||||||
BenchmarkBlog/Len1-Param1-12 130212613 9.221 ns/op 0 B/op 0 allocs/op
|
BenchmarkBlog/Len1-Param1-12 132838722 8.978 ns/op 0 B/op 0 allocs/op
|
||||||
BenchmarkGitHub/Len7-Param0-12 70604431 16.97 ns/op 0 B/op 0 allocs/op
|
BenchmarkGitHub/Len7-Param0-12 84768382 14.14 ns/op 0 B/op 0 allocs/op
|
||||||
BenchmarkGitHub/Len7-Param1-12 50499285 22.55 ns/op 0 B/op 0 allocs/op
|
BenchmarkGitHub/Len7-Param1-12 55290044 20.74 ns/op 0 B/op 0 allocs/op
|
||||||
BenchmarkGitHub/Len7-Param2-12 25460875 46.84 ns/op 0 B/op 0 allocs/op
|
BenchmarkGitHub/Len7-Param2-12 26057244 46.08 ns/op 0 B/op 0 allocs/op
|
||||||
```
|
```
|
||||||
|
|
||||||
## License
|
## License
|
||||||
|
29
Tree.go
29
Tree.go
@ -93,8 +93,7 @@ func (tree *Tree[T]) Lookup(path string) (T, []Parameter) {
|
|||||||
func (tree *Tree[T]) LookupNoAlloc(path string, addParameter func(key string, value string)) T {
|
func (tree *Tree[T]) LookupNoAlloc(path string, addParameter func(key string, value string)) T {
|
||||||
var (
|
var (
|
||||||
i uint
|
i uint
|
||||||
offset uint
|
wildcardPath string
|
||||||
wildcardOffset uint
|
|
||||||
wildcard *treeNode[T]
|
wildcard *treeNode[T]
|
||||||
node = &tree.root
|
node = &tree.root
|
||||||
)
|
)
|
||||||
@ -110,10 +109,10 @@ begin:
|
|||||||
// The node we just checked is entirely included in our path.
|
// The node we just checked is entirely included in our path.
|
||||||
// node: /|
|
// node: /|
|
||||||
// path: /|blog
|
// path: /|blog
|
||||||
if i-offset == uint(len(node.prefix)) {
|
if i == uint(len(node.prefix)) {
|
||||||
if node.wildcard != nil {
|
if node.wildcard != nil {
|
||||||
wildcard = node.wildcard
|
wildcard = node.wildcard
|
||||||
wildcardOffset = i
|
wildcardPath = path[i:]
|
||||||
}
|
}
|
||||||
|
|
||||||
char := path[i]
|
char := path[i]
|
||||||
@ -123,8 +122,8 @@ begin:
|
|||||||
|
|
||||||
if index != 0 {
|
if index != 0 {
|
||||||
node = node.children[index]
|
node = node.children[index]
|
||||||
offset = i
|
path = path[i:]
|
||||||
i++
|
i = 1
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -133,25 +132,25 @@ begin:
|
|||||||
// path: /|blog
|
// path: /|blog
|
||||||
if node.parameter != nil {
|
if node.parameter != nil {
|
||||||
node = node.parameter
|
node = node.parameter
|
||||||
offset = i
|
path = path[i:]
|
||||||
i++
|
i = 1
|
||||||
|
|
||||||
for i < uint(len(path)) {
|
for i < uint(len(path)) {
|
||||||
// node: /:id|/posts
|
// node: /:id|/posts
|
||||||
// path: /123|/posts
|
// path: /123|/posts
|
||||||
if path[i] == separator {
|
if path[i] == separator {
|
||||||
addParameter(node.prefix, path[offset:i])
|
addParameter(node.prefix, path[:i])
|
||||||
index := node.indices[separator-node.startIndex]
|
index := node.indices[separator-node.startIndex]
|
||||||
node = node.children[index]
|
node = node.children[index]
|
||||||
offset = i
|
path = path[i:]
|
||||||
i++
|
i = 1
|
||||||
goto begin
|
goto begin
|
||||||
}
|
}
|
||||||
|
|
||||||
i++
|
i++
|
||||||
}
|
}
|
||||||
|
|
||||||
addParameter(node.prefix, path[offset:i])
|
addParameter(node.prefix, path[:i])
|
||||||
return node.data
|
return node.data
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -163,7 +162,7 @@ begin:
|
|||||||
// We got a conflict.
|
// We got a conflict.
|
||||||
// node: /b|ag
|
// node: /b|ag
|
||||||
// path: /b|riefcase
|
// path: /b|riefcase
|
||||||
if path[i] != node.prefix[i-offset] {
|
if path[i] != node.prefix[i] {
|
||||||
goto notFound
|
goto notFound
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -172,7 +171,7 @@ begin:
|
|||||||
|
|
||||||
// node: /blog|
|
// node: /blog|
|
||||||
// path: /blog|
|
// path: /blog|
|
||||||
if i-offset == uint(len(node.prefix)) {
|
if i == uint(len(node.prefix)) {
|
||||||
return node.data
|
return node.data
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -180,7 +179,7 @@ begin:
|
|||||||
// path: /|image.png
|
// path: /|image.png
|
||||||
notFound:
|
notFound:
|
||||||
if wildcard != nil {
|
if wildcard != nil {
|
||||||
addParameter(wildcard.prefix, path[wildcardOffset:])
|
addParameter(wildcard.prefix, wildcardPath)
|
||||||
return wildcard.data
|
return wildcard.data
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user