mirror of
https://github.com/honojs/hono.git
synced 2024-11-24 11:07:29 +01:00
perf(trie-router): optimize and remove unnecessary processes (#3647)
* perf(trie-router): remove unnecessary processes and speed up * revert (4) * fix
This commit is contained in:
parent
61bee9f651
commit
7e17b76ce9
@ -63,10 +63,6 @@ export class Node<T> {
|
|||||||
curNode = curNode.children[p]
|
curNode = curNode.children[p]
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!curNode.methods.length) {
|
|
||||||
curNode.methods = []
|
|
||||||
}
|
|
||||||
|
|
||||||
const m: Record<string, HandlerSet<T>> = Object.create(null)
|
const m: Record<string, HandlerSet<T>> = Object.create(null)
|
||||||
|
|
||||||
const handlerSet: HandlerSet<T> = {
|
const handlerSet: HandlerSet<T> = {
|
||||||
@ -82,7 +78,7 @@ export class Node<T> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// getHandlerSets
|
// getHandlerSets
|
||||||
#gHSets(
|
#getHandlerSets(
|
||||||
node: Node<T>,
|
node: Node<T>,
|
||||||
method: string,
|
method: string,
|
||||||
nodeParams: Record<string, string>,
|
nodeParams: Record<string, string>,
|
||||||
@ -92,7 +88,7 @@ export class Node<T> {
|
|||||||
for (let i = 0, len = node.methods.length; i < len; i++) {
|
for (let i = 0, len = node.methods.length; i < len; i++) {
|
||||||
const m = node.methods[i]
|
const m = node.methods[i]
|
||||||
const handlerSet = (m[method] || m[METHOD_NAME_ALL]) as HandlerParamsSet<T>
|
const handlerSet = (m[method] || m[METHOD_NAME_ALL]) as HandlerParamsSet<T>
|
||||||
const processedSet: Record<string, boolean> = Object.create(null)
|
const processedSet: Record<number, boolean> = {}
|
||||||
if (handlerSet !== undefined) {
|
if (handlerSet !== undefined) {
|
||||||
handlerSet.params = Object.create(null)
|
handlerSet.params = Object.create(null)
|
||||||
for (let i = 0, len = handlerSet.possibleKeys.length; i < len; i++) {
|
for (let i = 0, len = handlerSet.possibleKeys.length; i < len; i++) {
|
||||||
@ -133,10 +129,17 @@ export class Node<T> {
|
|||||||
// '/hello/*' => match '/hello'
|
// '/hello/*' => match '/hello'
|
||||||
if (nextNode.children['*']) {
|
if (nextNode.children['*']) {
|
||||||
handlerSets.push(
|
handlerSets.push(
|
||||||
...this.#gHSets(nextNode.children['*'], method, node.params, Object.create(null))
|
...this.#getHandlerSets(
|
||||||
|
nextNode.children['*'],
|
||||||
|
method,
|
||||||
|
node.params,
|
||||||
|
Object.create(null)
|
||||||
|
)
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
handlerSets.push(...this.#gHSets(nextNode, method, node.params, Object.create(null)))
|
handlerSets.push(
|
||||||
|
...this.#getHandlerSets(nextNode, method, node.params, Object.create(null))
|
||||||
|
)
|
||||||
} else {
|
} else {
|
||||||
tempNodes.push(nextNode)
|
tempNodes.push(nextNode)
|
||||||
}
|
}
|
||||||
@ -152,7 +155,9 @@ export class Node<T> {
|
|||||||
if (pattern === '*') {
|
if (pattern === '*') {
|
||||||
const astNode = node.children['*']
|
const astNode = node.children['*']
|
||||||
if (astNode) {
|
if (astNode) {
|
||||||
handlerSets.push(...this.#gHSets(astNode, method, node.params, Object.create(null)))
|
handlerSets.push(
|
||||||
|
...this.#getHandlerSets(astNode, method, node.params, Object.create(null))
|
||||||
|
)
|
||||||
tempNodes.push(astNode)
|
tempNodes.push(astNode)
|
||||||
}
|
}
|
||||||
continue
|
continue
|
||||||
@ -170,18 +175,17 @@ export class Node<T> {
|
|||||||
const restPathString = parts.slice(i).join('/')
|
const restPathString = parts.slice(i).join('/')
|
||||||
if (matcher instanceof RegExp && matcher.test(restPathString)) {
|
if (matcher instanceof RegExp && matcher.test(restPathString)) {
|
||||||
params[name] = restPathString
|
params[name] = restPathString
|
||||||
handlerSets.push(...this.#gHSets(child, method, node.params, params))
|
handlerSets.push(...this.#getHandlerSets(child, method, node.params, params))
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
if (matcher === true || matcher.test(part)) {
|
if (matcher === true || matcher.test(part)) {
|
||||||
if (typeof key === 'string') {
|
|
||||||
params[name] = part
|
params[name] = part
|
||||||
if (isLast) {
|
if (isLast) {
|
||||||
handlerSets.push(...this.#gHSets(child, method, params, node.params))
|
handlerSets.push(...this.#getHandlerSets(child, method, params, node.params))
|
||||||
if (child.children['*']) {
|
if (child.children['*']) {
|
||||||
handlerSets.push(
|
handlerSets.push(
|
||||||
...this.#gHSets(child.children['*'], method, params, node.params)
|
...this.#getHandlerSets(child.children['*'], method, params, node.params)
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
@ -191,7 +195,6 @@ export class Node<T> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
curNodes = tempNodes
|
curNodes = tempNodes
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user