Skip to content

Commit

Permalink
perf(trie-router): optimize and remove unnecessary processes (#3647)
Browse files Browse the repository at this point in the history
* perf(trie-router): remove unnecessary processes and speed up

* revert (4)

* fix
  • Loading branch information
EdamAme-x authored Nov 12, 2024
1 parent 61bee9f commit 7e17b76
Showing 1 changed file with 25 additions and 22 deletions.
47 changes: 25 additions & 22 deletions src/router/trie-router/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,6 @@ export class Node<T> {
curNode = curNode.children[p]
}

if (!curNode.methods.length) {
curNode.methods = []
}

const m: Record<string, HandlerSet<T>> = Object.create(null)

const handlerSet: HandlerSet<T> = {
Expand All @@ -82,7 +78,7 @@ export class Node<T> {
}

// getHandlerSets
#gHSets(
#getHandlerSets(
node: Node<T>,
method: string,
nodeParams: Record<string, string>,
Expand All @@ -92,7 +88,7 @@ export class Node<T> {
for (let i = 0, len = node.methods.length; i < len; i++) {
const m = node.methods[i]
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) {
handlerSet.params = Object.create(null)
for (let i = 0, len = handlerSet.possibleKeys.length; i < len; i++) {
Expand Down Expand Up @@ -133,10 +129,17 @@ export class Node<T> {
// '/hello/*' => match '/hello'
if (nextNode.children['*']) {
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 {
tempNodes.push(nextNode)
}
Expand All @@ -152,7 +155,9 @@ export class Node<T> {
if (pattern === '*') {
const astNode = node.children['*']
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)
}
continue
Expand All @@ -170,24 +175,22 @@ export class Node<T> {
const restPathString = parts.slice(i).join('/')
if (matcher instanceof RegExp && matcher.test(restPathString)) {
params[name] = restPathString
handlerSets.push(...this.#gHSets(child, method, node.params, params))
handlerSets.push(...this.#getHandlerSets(child, method, node.params, params))
continue
}

if (matcher === true || matcher.test(part)) {
if (typeof key === 'string') {
params[name] = part
if (isLast) {
handlerSets.push(...this.#gHSets(child, method, params, node.params))
if (child.children['*']) {
handlerSets.push(
...this.#gHSets(child.children['*'], method, params, node.params)
)
}
} else {
child.params = params
tempNodes.push(child)
params[name] = part
if (isLast) {
handlerSets.push(...this.#getHandlerSets(child, method, params, node.params))
if (child.children['*']) {
handlerSets.push(
...this.#getHandlerSets(child.children['*'], method, params, node.params)
)
}
} else {
child.params = params
tempNodes.push(child)
}
}
}
Expand Down

0 comments on commit 7e17b76

Please sign in to comment.