Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf(trie-router): optimize and remove unnecessary processes #3647

Merged
merged 3 commits into from
Nov 12, 2024
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 26 additions & 23 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 @@ -151,8 +154,10 @@ export class Node<T> {
// '/hello/*/foo' => match /hello/bar/foo
if (pattern === '*') {
const astNode = node.children['*']
if (astNode) {
handlerSets.push(...this.#gHSets(astNode, method, node.params, Object.create(null)))
if (node.children['*']) {
EdamAme-x marked this conversation as resolved.
Show resolved Hide resolved
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
Loading