PoC: perf(context): defer method binding in Context to reduce init cost#4755
Draft
usualoma wants to merge 1 commit intohonojs:mainfrom
Draft
PoC: perf(context): defer method binding in Context to reduce init cost#4755usualoma wants to merge 1 commit intohonojs:mainfrom
usualoma wants to merge 1 commit intohonojs:mainfrom
Conversation
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #4755 +/- ##
==========================================
- Coverage 91.48% 90.91% -0.58%
==========================================
Files 177 177
Lines 11556 11662 +106
Branches 3357 3373 +16
==========================================
+ Hits 10572 10602 +30
- Misses 983 1059 +76
Partials 1 1 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
Member
Author
|
Hi @yusukebe (Slightly) larger in size, but (though only slightly) it represents a clear performance improvement, so I think this PR approach is preferable. |
Contributor
|
I applied this and ran the benchmark script on WSL and Apple silicon, and both results were great. WSL Result
Node (1.1x faster)runtime: node 24.13.1 (x64-linux)
benchmark avg (min … max) p75 / p99 (min … top 1%)
------------------------------------------- -------------------------------
latest app.fetch(req) 1.01 µs/iter 1.02 µs █▆
(648.00 ns … 457.66 µs) 1.91 µs ████▅
( 1.48 kb … 2.99 mb) 5.82 kb ▁▅██████▅▃▂▂▂▂▂▂▂▁▁▁▁
refactor app.fetch(req) 931.02 ns/iter 954.00 ns ▃█▄
(687.00 ns … 254.58 µs) 1.62 µs ███▇▃
(512.00 b … 418.49 kb) 4.93 kb ▂███████▆▄▃▂▂▁▁▁▁▁▁▁▁
summary
refactor app.fetch(req)
1.09x faster than latest app.fetch(req)Bun (2.7x faster)runtime: bun 1.3.9 (x64-linux)
benchmark avg (min … max) p75 / p99 (min … top 1%)
------------------------------------------- -------------------------------
latest app.fetch(req) 1.29 µs/iter 1.06 µs █
(677.00 ns … 668.63 µs) 6.01 µs █▃
( 0.00 b … 192.00 kb) 434.56 b ██▃▂▃▃▂▂▁▁▁▁▁▁▁▁▁▁▁▁▁
refactor app.fetch(req) 481.63 ns/iter 352.00 ns ██
(217.00 ns … 643.98 µs) 2.60 µs ██
( 0.00 b … 192.00 kb) 111.24 b ██▅▃▂▂▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁
summary
refactor app.fetch(req)
2.68x faster than latest app.fetch(req)(Note: These are micro-benchmark results; when tested with Bombardier, i see an improvement of about 30%) Deno (1.2x faster)runtime: deno 2.6.10 (x86_64-unknown-linux-gnu)
benchmark avg (min … max) p75 / p99 (min … top 1%)
------------------------------------------- -------------------------------
latest app.fetch(req) 333.68 ns/iter 349.00 ns █▆
(201.00 ns … 182.87 µs) 615.00 ns ███▇█▇
( 2.59 kb … 386.27 kb) 2.66 kb ▁▃███████▅▄▄▃▃▃▂▂▂▁▁▁
refactor app.fetch(req) 273.12 ns/iter 294.00 ns █
(172.00 ns … 168.74 µs) 561.00 ns █▄
( 1.84 kb … 270.59 kb) 1.89 kb ▁▃██▆▄▅▆▄▂▂▂▂▂▁▁▁▁▁▁▁
summary
refactor app.fetch(req)
1.22x faster than latest app.fetch(req) |
Member
Author
|
@EdamAme-x Thank you! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What is this?
This PR reduces Context initialization overhead by deferring method-function creation from instance field initializers to getters.
There is no intended behavioral change; this is a performance-focused refactor targeting lower
new Context(req)cost.A more radical approach
In Hono, we can now call methods by destructuring them.
https://github.com/orgs/honojs/discussions/812
If we remove support for calls via destructuring, we could optimize it a bit more.
main...usualoma:refactor-context-2
Benchmark
Benchmarks were taken in the following three patterns
These are the execution results for
new Context(req)andnew Context(req).text('hello').(Since
.text()or.json()calls typically occur at most once per request, this benchmark reflects real-world conditions)While this isn't the application's bottleneck, the localized benchmark shows
new Context(req)is over 20 times faster.Benchmark script
Command
$ git clone 'git@github.com:honojs/hono.git' /tmp/hono-main $ git clone -b refactor-context 'git@github.com:usualoma/hono.git' /tmp/usualoma-refactor-context $ git clone -b refactor-context-2 'git@github.com:usualoma/hono.git' /tmp/usualoma-refactor-context-2 $ deno run -A --sloppy-imports --node-modules-dir=auto context-new.ts --main-root=/tmp/hono-main --refactor-root=/tmp/usualoma-refactor-context --refactor-more-root=/tmp/usualoma-refactor-context-2Size
It will be about 185 bytes larger.
The author should do the following, if applicable
Add testsbun run format:fix && bun run lint:fixto format the code