From f4c0b2ffc6b83477eb5d9d94121c6cca3c85158d Mon Sep 17 00:00:00 2001 From: Ame_x Edam Date: Fri, 8 Nov 2024 14:13:50 +0000 Subject: [PATCH 1/3] perf(trie-router): remove unnecessary processes and speed up --- src/router/trie-router/node.ts | 44 ++++++++++++++++------------------ 1 file changed, 21 insertions(+), 23 deletions(-) diff --git a/src/router/trie-router/node.ts b/src/router/trie-router/node.ts index 3130433bc..1b86620a3 100644 --- a/src/router/trie-router/node.ts +++ b/src/router/trie-router/node.ts @@ -63,10 +63,6 @@ export class Node { curNode = curNode.children[p] } - if (!curNode.methods.length) { - curNode.methods = [] - } - const m: Record> = Object.create(null) const handlerSet: HandlerSet = { @@ -82,7 +78,7 @@ export class Node { } // getHandlerSets - #gHSets( + #getHandlerSets( node: Node, method: string, nodeParams: Record, @@ -92,7 +88,7 @@ export class Node { 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 - const processedSet: Record = Object.create(null) + const processedSet: Record = {} if (handlerSet !== undefined) { handlerSet.params = Object.create(null) for (let i = 0, len = handlerSet.possibleKeys.length; i < len; i++) { @@ -131,12 +127,15 @@ export class Node { nextNode.params = node.params if (isLast) { // '/hello/*' => match '/hello' - if (nextNode.children['*']) { + const astNode = nextNode.children['*'] + if (astNode) { handlerSets.push( - ...this.#gHSets(nextNode.children['*'], method, node.params, Object.create(null)) + ...this.#getHandlerSets(astNode, 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) } @@ -152,7 +151,9 @@ export class Node { 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 @@ -170,24 +171,21 @@ export class Node { 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)) + const astNode = child.children['*'] + if (astNode) { + handlerSets.push(...this.#getHandlerSets(astNode, method, params, node.params)) } + } else { + child.params = params + tempNodes.push(child) } } } From 90af290ec89aec1aced8595b9ea3fe7134896930 Mon Sep 17 00:00:00 2001 From: Ame_x Edam Date: Sat, 9 Nov 2024 08:02:01 +0000 Subject: [PATCH 2/3] revert (4) --- src/router/trie-router/node.ts | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/src/router/trie-router/node.ts b/src/router/trie-router/node.ts index 1b86620a3..370a1f421 100644 --- a/src/router/trie-router/node.ts +++ b/src/router/trie-router/node.ts @@ -127,10 +127,14 @@ export class Node { nextNode.params = node.params if (isLast) { // '/hello/*' => match '/hello' - const astNode = nextNode.children['*'] - if (astNode) { + if (nextNode.children['*']) { handlerSets.push( - ...this.#getHandlerSets(astNode, method, node.params, Object.create(null)) + ...this.#getHandlerSets( + nextNode.children['*'], + method, + node.params, + Object.create(null) + ) ) } handlerSets.push( @@ -150,7 +154,7 @@ export class Node { // '/hello/*/foo' => match /hello/bar/foo if (pattern === '*') { const astNode = node.children['*'] - if (astNode) { + if (node.children['*']) { handlerSets.push( ...this.#getHandlerSets(astNode, method, node.params, Object.create(null)) ) @@ -179,9 +183,10 @@ export class Node { params[name] = part if (isLast) { handlerSets.push(...this.#getHandlerSets(child, method, params, node.params)) - const astNode = child.children['*'] - if (astNode) { - handlerSets.push(...this.#getHandlerSets(astNode, method, params, node.params)) + if (child.children['*']) { + handlerSets.push( + ...this.#getHandlerSets(child.children['*'], method, params, node.params) + ) } } else { child.params = params From 9028f2f3c076bf41620eccd54d10e76ac06ae5c7 Mon Sep 17 00:00:00 2001 From: EdamAmex <121654029+EdamAme-x@users.noreply.github.com> Date: Tue, 12 Nov 2024 11:20:20 +0900 Subject: [PATCH 3/3] fix --- src/router/trie-router/node.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/router/trie-router/node.ts b/src/router/trie-router/node.ts index 370a1f421..2d0ae6c31 100644 --- a/src/router/trie-router/node.ts +++ b/src/router/trie-router/node.ts @@ -154,7 +154,7 @@ export class Node { // '/hello/*/foo' => match /hello/bar/foo if (pattern === '*') { const astNode = node.children['*'] - if (node.children['*']) { + if (astNode) { handlerSets.push( ...this.#getHandlerSets(astNode, method, node.params, Object.create(null)) )