Skip to content
Myr

perf(trie-router): fine tuning, 9~10% faster (#473)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

* perf(trie-router): fine tuning, 9~10% faster! * update readme

authored by Yusuke Wada · committed by GitHub · parent 869ab52 · Browse files · 4 files+36 added−30 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

4 files+36 added−30 removed

  • other:+36 added−30 removed

4 files+36 added−30 removed

modified:deno_dist/router/trie-router/node.ts+12 added-9 removed
107 unmodified lines
108109 private getHandlerSets(node: Node<T>, method: string, wildcard?: boolean): HandlerSet<T>[] {110 return (node.handlerSetCache[`${method}:${wildcard ? '1' : '0'}`] ||= (() => {111 const handlerSets: HandlerSet<T>[] = []112 node.methods.map((m) => {112 for (let i = 0, len = node.methods.length; i < len; i++) {113 const m = node.methods[i]114 const handlerSet = m[method] || m[METHOD_NAME_ALL]115 if (handlerSet !== undefined) {115 const hs = { ...handlerSet }116 handlerSets.push(hs)116 handlerSets.push(handlerSet)117 return117 }119 })120118 }119 return handlerSets120 })())121 }122
23 unmodified lines
146 handlerSets.push(...this.getHandlerSets(nextNode.children['*'], method, true))147 }148 handlerSets.push(...this.getHandlerSets(nextNode, method))149 matched = true150 } else {151 tempNodes.push(nextNode)152 }153 tempNodes.push(nextNode)153 }154155 for (let k = 0, len3 = node.patterns.length; k < len3; k++) {156 const pattern = node.patterns[k]
17 unmodified lines
174 if (matcher === true || (matcher instanceof RegExp && matcher.test(part))) {175 if (typeof key === 'string') {176 if (isLast === true) {177 handlerSets.push(...this.getHandlerSets(node.children[key], method))178 } else {179 tempNodes.push(node.children[key])180 }180 tempNodes.push(node.children[key])181 }182183 // '/book/a' => not-slug184 // '/book/:slug' => slug
1 unmodified line
186 // GET /book/foo ~> slug, param['slug'] => foo187 if (typeof name === 'string' && !matched) {188 params[name] = part189 }190 break191 }192 }193 }194195 curNodes = tempNodes196 }197197 if (handlerSets.length <= 0) return null198 const len = handlerSets.length199 if (len === 0) return null200 if (len === 1) return { handlers: [handlerSets[0].handler], params }201202 const handlers = handlerSets203 .sort((a, b) => {204 return a.score - b.score
8 unmodified lines
modified:deno_dist/README.md+6 added-6 removed
modified:src/router/trie-router/node.ts+12 added-9 removed
107 unmodified lines
108109 private getHandlerSets(node: Node<T>, method: string, wildcard?: boolean): HandlerSet<T>[] {110 return (node.handlerSetCache[`${method}:${wildcard ? '1' : '0'}`] ||= (() => {111 const handlerSets: HandlerSet<T>[] = []112 node.methods.map((m) => {112 for (let i = 0, len = node.methods.length; i < len; i++) {113 const m = node.methods[i]114 const handlerSet = m[method] || m[METHOD_NAME_ALL]115 if (handlerSet !== undefined) {115 const hs = { ...handlerSet }116 handlerSets.push(hs)116 handlerSets.push(handlerSet)117 return117 }119 })120118 }119 return handlerSets120 })())121 }122
23 unmodified lines
146 handlerSets.push(...this.getHandlerSets(nextNode.children['*'], method, true))147 }148 handlerSets.push(...this.getHandlerSets(nextNode, method))149 matched = true150 } else {151 tempNodes.push(nextNode)152 }153 tempNodes.push(nextNode)153 }154155 for (let k = 0, len3 = node.patterns.length; k < len3; k++) {156 const pattern = node.patterns[k]
17 unmodified lines
174 if (matcher === true || (matcher instanceof RegExp && matcher.test(part))) {175 if (typeof key === 'string') {176 if (isLast === true) {177 handlerSets.push(...this.getHandlerSets(node.children[key], method))178 } else {179 tempNodes.push(node.children[key])180 }180 tempNodes.push(node.children[key])181 }182183 // '/book/a' => not-slug184 // '/book/:slug' => slug
1 unmodified line
186 // GET /book/foo ~> slug, param['slug'] => foo187 if (typeof name === 'string' && !matched) {188 params[name] = part189 }190 break191 }192 }193 }194195 curNodes = tempNodes196 }197197 if (handlerSets.length <= 0) return null198 const len = handlerSets.length199 if (len === 0) return null200 if (len === 1) return { handlers: [handlerSets[0].handler], params }201202 const handlers = handlerSets203 .sort((a, b) => {204 return a.score - b.score
8 unmodified lines
modified:README.md+6 added-6 removed