perf: remove `async` from dispatch; will be 16% faster (#550)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Yusuke Wada · committed by GitHub · parent c3c5f8a · Browse files · 3 files·+95 added−67 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+95 added−67 removed
- other:+95 added−67 removed
Provenance
- Commit
e4c8a6f- Tree
313d459- Author
Yusuke Wada <yusuke@kamawada.com>- Committer
GitHub <noreply@github.com>- Parent
c3c5f8a
3 files changed+95 added−67 removed
modified:deno_dist/utils/encode.ts+1 added-1 removed
11234572 unmodified linesimport { Buffer } from "https://deno.land/std@0.156.0/node/buffer.ts";import { Buffer } from "https://deno.land/std@0.157.0/node/buffer.ts";export const encodeBase64 = (str: string): string => { if (str === null) { throw new TypeError('1st argument of "encodeBase64" should not be null.') }72 unmodified linesmodified:deno_dist/hono.ts+47 added-33 removed
211 unmodified lines212 private matchRoute(method: string, path: string) {213 return this.router.match(method, path)214 }215 216+ private handleError(err: unknown, c: Context) {217+ if (err instanceof Error) {218+ return this.errorHandler(err, c)219+ }220+ throw err221+ }222+216- private async dispatch(223+ private dispatch(224 request: Request,225 eventOrExecutionCtx?: ExecutionContext | FetchEvent,226 env?: E['Bindings']220- ): Promise<Response> {227+ ) {228 const path = getPathFromURL(request.url, this.strict)229 const method = request.method230 231 const result = this.matchRoute(method, path)3 unmodified lines235 236 // Do not `compose` if it has only one handler237 if (result && result.handlers.length === 1) {238 const handler = result.handlers[0]239+ let res: ReturnType<Handler>240+241 try {233- const res = handler(c, async () => {})234- if (res) {235- const awaited = res instanceof Promise ? await res : res236- if (awaited) return awaited237- }238- return this.notFoundHandler(c)242+ res = handler(c, async () => {})243+ if (!res) return this.notFoundHandler(c)244 } catch (err) {240- if (err instanceof Error) {241- return this.errorHandler(err, c)245+ return this.handleError(err, c)242- }243- throw err246 }247+248+ if (res instanceof Response) return res249+250+ return (async () => {251+ let awaited: Response | undefined | void252+ try {253+ awaited = await res254+ } catch (err) {255+ return this.handleError(err, c)256+ }257+ if (!awaited) {258+ return this.notFoundHandler(c)259+ }260+ return awaited261+ })()262 }263 264 const handlers = result ? result.handlers : [this.notFoundHandler]248-265 const composed = compose<HonoContext<string, E>, E>(handlers, this.notFoundHandler)250- let context: HonoContext<string, E>251- try {252- const tmp = composed(c)253- context = tmp instanceof Promise ? await tmp : tmp254- if (!context.finalized) {255- throw new Error(256- 'Context is not finalized. You may forget returning Response object or `await next()`'257- )258- }259- } catch (err) {260- if (err instanceof Error) {261- return this.errorHandler(err, c)262- }263- throw err264- }266 267+ return (async () => {268+ try {269+ const tmp = composed(c)270+ const context = tmp instanceof Promise ? await tmp : tmp271+ if (!context.finalized) {272+ throw new Error(273+ 'Context is not finalized. You may forget returning Response object or `await next()`'274+ )275+ }266- return context.res276+ return context.res277+ } catch (err) {278+ return this.handleError(err, c)279+ }280+ })()281 }282 269- handleEvent(event: FetchEvent): Promise<Response> {270- return this.dispatch(event.request, event)283+ handleEvent = (event: FetchEvent) => {284+ return this.fetch(event.request, event)285 }286 287 fetch = (request: Request, Environment?: E['Bindings'], executionCtx?: ExecutionContext) => {288 return this.dispatch(request, executionCtx, Environment)289 }290 277- request(input: RequestInfo, requestInit?: RequestInit): Promise<Response> {291+ request = async (input: RequestInfo, requestInit?: RequestInit) => {292 const req = input instanceof Request ? input : new Request(input, requestInit)279- return this.dispatch(req)293+ return await this.fetch(req)294 }295 }modified:src/hono.ts+47 added-33 removed
211 unmodified lines212 private matchRoute(method: string, path: string) {213 return this.router.match(method, path)214 }215 216+ private handleError(err: unknown, c: Context) {217+ if (err instanceof Error) {218+ return this.errorHandler(err, c)219+ }220+ throw err221+ }222+216- private async dispatch(223+ private dispatch(224 request: Request,225 eventOrExecutionCtx?: ExecutionContext | FetchEvent,226 env?: E['Bindings']220- ): Promise<Response> {227+ ) {228 const path = getPathFromURL(request.url, this.strict)229 const method = request.method230 231 const result = this.matchRoute(method, path)3 unmodified lines235 236 // Do not `compose` if it has only one handler237 if (result && result.handlers.length === 1) {238 const handler = result.handlers[0]239+ let res: ReturnType<Handler>240+241 try {233- const res = handler(c, async () => {})234- if (res) {235- const awaited = res instanceof Promise ? await res : res236- if (awaited) return awaited237- }238- return this.notFoundHandler(c)242+ res = handler(c, async () => {})243+ if (!res) return this.notFoundHandler(c)244 } catch (err) {240- if (err instanceof Error) {241- return this.errorHandler(err, c)245+ return this.handleError(err, c)242- }243- throw err246 }247+248+ if (res instanceof Response) return res249+250+ return (async () => {251+ let awaited: Response | undefined | void252+ try {253+ awaited = await res254+ } catch (err) {255+ return this.handleError(err, c)256+ }257+ if (!awaited) {258+ return this.notFoundHandler(c)259+ }260+ return awaited261+ })()262 }263 264 const handlers = result ? result.handlers : [this.notFoundHandler]248-265 const composed = compose<HonoContext<string, E>, E>(handlers, this.notFoundHandler)250- let context: HonoContext<string, E>251- try {252- const tmp = composed(c)253- context = tmp instanceof Promise ? await tmp : tmp254- if (!context.finalized) {255- throw new Error(256- 'Context is not finalized. You may forget returning Response object or `await next()`'257- )258- }259- } catch (err) {260- if (err instanceof Error) {261- return this.errorHandler(err, c)262- }263- throw err264- }266 267+ return (async () => {268+ try {269+ const tmp = composed(c)270+ const context = tmp instanceof Promise ? await tmp : tmp271+ if (!context.finalized) {272+ throw new Error(273+ 'Context is not finalized. You may forget returning Response object or `await next()`'274+ )275+ }266- return context.res276+ return context.res277+ } catch (err) {278+ return this.handleError(err, c)279+ }280+ })()281 }282 269- handleEvent(event: FetchEvent): Promise<Response> {270- return this.dispatch(event.request, event)283+ handleEvent = (event: FetchEvent) => {284+ return this.fetch(event.request, event)285 }286 287 fetch = (request: Request, Environment?: E['Bindings'], executionCtx?: ExecutionContext) => {288 return this.dispatch(request, executionCtx, Environment)289 }290 277- request(input: RequestInfo, requestInit?: RequestInit): Promise<Response> {291+ request = async (input: RequestInfo, requestInit?: RequestInit) => {292 const req = input instanceof Request ? input : new Request(input, requestInit)279- return this.dispatch(req)293+ return await this.fetch(req)294 }295 }