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(context): improve initializing Context #3046

Merged
merged 4 commits into from
Jun 27, 2024
Merged

Conversation

yusukebe
Copy link
Member

@yusukebe yusukebe commented Jun 26, 2024

This PR improves the performance for initialization of Context:

  • Delay initialization for variables as long as possible.
  • Pass Request object instead of HonoRequest and make it later if c.req is called.
  • Some tests have been changed. But I think it is not a problem because Context is used in the hono package internally, though a type of Context is exported.
  • It's not related to performance, but use # for some private properties.

Result

With this benchmark code:

import { bench, group, run } from 'mitata'
import { Hono as HonoNext } from '../../dist/index.js'
import { Hono } from 'hono'

const req = new Request('http://localhost/')
const app = new Hono().get('/', (c) => c.text('Hi'))
const appNext = new HonoNext().get('/', (c) => c.text('Hi'))

bench('noop', () => {})

group(() => {
  bench('4.4.8', async () => {
    await app.fetch(req)
  })
  bench('next', async () => {
    await appNext.fetch(req)
  })
})

run()

The performance is improved.

Bun:

benchmark      time (avg)             (min … max)       p75       p99      p999
------------------------------------------------- -----------------------------
4.4.8         906 ns/iter     (850 ns … 1'490 ns)    892 ns  1'303 ns  1'490 ns
next          821 ns/iter     (772 ns … 1'145 ns)    822 ns  1'094 ns  1'145 ns

summary
  next
   1.1x faster than 4.4.8

Node.js:

benchmark      time (avg)             (min … max)       p75       p99      p999
------------------------------------------------- -----------------------------
4.4.8       1'099 ns/iter   (1'012 ns … 2'116 ns)  1'101 ns  1'715 ns  2'116 ns
next        1'055 ns/iter     (987 ns … 1'317 ns)  1'075 ns  1'274 ns  1'317 ns

summary
  next
   1.04x faster than 4.4.8

The author should do the following, if applicable

  • Add tests
  • Run tests
  • bun run format:fix && bun run lint:fix to format the code
  • Add TSDoc/JSDoc to document the code

Copy link

codecov bot commented Jun 26, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 95.84%. Comparing base (d21f8eb) to head (b62a687).

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #3046   +/-   ##
=======================================
  Coverage   95.83%   95.84%           
=======================================
  Files         137      137           
  Lines       13398    13423   +25     
  Branches     2258     2292   +34     
=======================================
+ Hits        12840    12865   +25     
  Misses        558      558           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@yusukebe
Copy link
Member Author

yusukebe commented Jun 26, 2024

Hey @usualoma !

What do you think of this?

@usualoma
Copy link
Member

@yusukebe
Great! Let’s go with this.

@EdamAme-x
Copy link
Contributor

looks faster!

------------------------------------------------- -----------------------------
4.4.8       1'890 ns/iter   (1'656 ns  4'385 ns)  1'768 ns  4'319 ns  4'385 ns
next        1'563 ns/iter   (1'435 ns  2'181 ns)  1'566 ns  2'167 ns  2'181 ns

summary
  next
   1.21x faster than 4.4.8

@yusukebe
Copy link
Member Author

@usualoma @EdamAme-x Thanks! I'll merge this into the main now.

@yusukebe yusukebe merged commit ae3a67b into main Jun 27, 2024
14 checks passed
@yusukebe yusukebe deleted the perf/context-initialize branch June 27, 2024 01:44
adamnolte pushed a commit to autoblocksai/cli that referenced this pull request Jul 24, 2024
[![Mend
Renovate](https://app.renovatebot.com/images/banner.svg)](https://renovatebot.com)

This PR contains the following updates:

| Package | Change | Age | Adoption | Passing | Confidence |
|---|---|---|---|---|---|
| [hono](https://hono.dev/) ([source](https://github.com/honojs/hono))
| [`4.4.7` ->
`4.5.1`](https://renovatebot.com/diffs/npm/hono/4.4.7/4.5.1) |
[![age](https://developer.mend.io/api/mc/badges/age/npm/hono/4.5.1?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|
[![adoption](https://developer.mend.io/api/mc/badges/adoption/npm/hono/4.5.1?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|
[![passing](https://developer.mend.io/api/mc/badges/compatibility/npm/hono/4.4.7/4.5.1?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|
[![confidence](https://developer.mend.io/api/mc/badges/confidence/npm/hono/4.4.7/4.5.1?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|

---

### Release Notes

<details>
<summary>honojs/hono (hono)</summary>

### [`v4.5.1`](https://github.com/honojs/hono/releases/tag/v4.5.1)

[Compare
Source](https://github.com/honojs/hono/compare/v4.5.0...v4.5.1)

#### What's Changed

- chore: remove rimraf and use bun shell by
[@&#8203;nakasyou](https://github.com/nakasyou) in
[honojs/hono#3146
- chore: moving the setup file of vitest by
[@&#8203;EdamAme-x](https://github.com/EdamAme-x) in
[honojs/hono#3157
- fix(middleware/jwt): Changed the jwt-secret type to SignatureKey by
[@&#8203;JulesVerner](https://github.com/JulesVerner) in
[honojs/hono#3167
- feat(bearer-auth): Allow empty bearer-auth middleware prefixes by
[@&#8203;prevostc](https://github.com/prevostc) in
[honojs/hono#3161
- chore(factory): remove `@experimental` from `createApp` by
[@&#8203;yusukebe](https://github.com/yusukebe) in
[honojs/hono#3164
- fix(client): support array values for `query` in `ws` by
[@&#8203;yusukebe](https://github.com/yusukebe) in
[honojs/hono#3169
- fix(validator): ignore content-type mismatches by
[@&#8203;yusukebe](https://github.com/yusukebe) in
[honojs/hono#3165

#### New Contributors

- [@&#8203;JulesVerner](https://github.com/JulesVerner) made their
first contribution in
[honojs/hono#3167
- [@&#8203;prevostc](https://github.com/prevostc) made their first
contribution in
[honojs/hono#3161

**Full Changelog**:
honojs/hono@v4.5.0...v4.5.1

###
[`v4.5.0`](https://github.com/honojs/hono/compare/v4.4.13...v4.5.0)

[Compare
Source](https://github.com/honojs/hono/compare/v4.4.13...v4.5.0)

### [`v4.4.13`](https://github.com/honojs/hono/releases/tag/v4.4.13)

[Compare
Source](https://github.com/honojs/hono/compare/v4.4.12...v4.4.13)

##### What's Changed

- chore: update benchmark by
[@&#8203;yusukebe](https://github.com/yusukebe) in
[honojs/hono#3102
- chore: replace tsx with Bun by
[@&#8203;nakasyou](https://github.com/nakasyou) in
[honojs/hono#3103
- refactor(http-status): remove unnecessary line of types and use common
types by [@&#8203;EdamAme-x](https://github.com/EdamAme-x) in
[honojs/hono#3110
- fix(jsx): redefine scope attribute as enum type by
[@&#8203;yasuaki640](https://github.com/yasuaki640) in
[honojs/hono#3118
- fix(types): allow `string[] | File[]` for RPC form value by
[@&#8203;yusukebe](https://github.com/yusukebe) in
[honojs/hono#3117
- fix(validator-types): type Alignment with Web Standards by
[@&#8203;EdamAme-x](https://github.com/EdamAme-x) in
[honojs/hono#3120
- fix(types): `app.use(path, mw)` return correct schema type by
[@&#8203;yusukebe](https://github.com/yusukebe) in
[honojs/hono#3128

**Full Changelog**:
honojs/hono@v4.4.12...v4.4.13

### [`v4.4.12`](https://github.com/honojs/hono/releases/tag/v4.4.12)

[Compare
Source](https://github.com/honojs/hono/compare/v4.4.11...v4.4.12)

#### What's Changed

- fix(aws-lambda): set cookies with comma is bugged by
[@&#8203;NamesMT](https://github.com/NamesMT) in
[honojs/hono#3084
- fix(types): infer `path` when chaining after `use` by
[@&#8203;yusukebe](https://github.com/yusukebe) in
[honojs/hono#3087
- chore: update outdated links in JSDoc by
[@&#8203;ryuapp](https://github.com/ryuapp) in
[honojs/hono#3089
- fix(jsx): changes behavior when `download` attribute is set to a
boolean value. by [@&#8203;oon00b](https://github.com/oon00b) in
[honojs/hono#3094
- chore: add the triage label by
[@&#8203;mvares](https://github.com/mvares) in
[honojs/hono#3092
- feat(types): improve JSONParsed by
[@&#8203;m-shaka](https://github.com/m-shaka) in
[honojs/hono#3074
- fix(helper/streaming): remove slow types by
[@&#8203;yusukebe](https://github.com/yusukebe) in
[honojs/hono#3100
- chore(utils/jwt): add `@module` docs by
[@&#8203;yusukebe](https://github.com/yusukebe) in
[honojs/hono#3101

#### New Contributors

- [@&#8203;oon00b](https://github.com/oon00b) made their first
contribution in
[honojs/hono#3094

**Full Changelog**:
honojs/hono@v4.4.11...v4.4.12

### [`v4.4.11`](https://github.com/honojs/hono/releases/tag/v4.4.11)

[Compare
Source](https://github.com/honojs/hono/compare/v4.4.10...v4.4.11)

#### What's Changed

- refactor: remove unnecessary async keyword from router tests by
[@&#8203;K-tecchan](https://github.com/K-tecchan) in
[honojs/hono#3061
- fix(validator): don't return a FormData if formData is cached by
[@&#8203;yusukebe](https://github.com/yusukebe) in
[honojs/hono#3067
- fix(client): Add Query Parameter Support to WebSocket Client in
`hono/client` by [@&#8203;naporin0624](https://github.com/naporin0624)
in
[honojs/hono#3066
- refactor(types): move `HandlerInterface`'s `(path, handler)`s
overloads down by [@&#8203;NamesMT](https://github.com/NamesMT) in
[honojs/hono#3072
- test(helper/dev): fix typo of test case name by
[@&#8203;yasuaki640](https://github.com/yasuaki640) in
[honojs/hono#3073
- fix(stream): Fixed a problem that onAbort() is called even if request
is normally closed in deno by
[@&#8203;usualoma](https://github.com/usualoma) in
[honojs/hono#3079

#### New Contributors

- [@&#8203;K-tecchan](https://github.com/K-tecchan) made their first
contribution in
[honojs/hono#3061

**Full Changelog**:
honojs/hono@v4.4.10...v4.4.11

### [`v4.4.10`](https://github.com/honojs/hono/releases/tag/v4.4.10)

[Compare
Source](https://github.com/honojs/hono/compare/v4.4.9...v4.4.10)

#### What's Changed

- chore(jsr): export JWT utils by
[@&#8203;ryuapp](https://github.com/ryuapp) in
[honojs/hono#3056
- fix(streaming): call stream.abort() explicitly when request is aborted
by [@&#8203;usualoma](https://github.com/usualoma) in
[honojs/hono#3042
- fix(client): set Path as the default of Original by
[@&#8203;m-shaka](https://github.com/m-shaka) in
[honojs/hono#3058

#### New Contributors

- [@&#8203;m-shaka](https://github.com/m-shaka) made their first
contribution in
[honojs/hono#3058

**Full Changelog**:
honojs/hono@v4.4.9...v4.4.10

### [`v4.4.9`](https://github.com/honojs/hono/releases/tag/v4.4.9)

[Compare
Source](https://github.com/honojs/hono/compare/v4.4.8...v4.4.9)

#### What's Changed

- perf(context): improve initializing `Context` by
[@&#8203;yusukebe](https://github.com/yusukebe) in
[honojs/hono#3046
- fix(types): correct inferring env when routes channing by
[@&#8203;yusukebe](https://github.com/yusukebe) in
[honojs/hono#3051
- docs: update the description of `package.json` and README by
[@&#8203;yusukebe](https://github.com/yusukebe) in
[honojs/hono#3052
- fix(timing): prevent duplicate applications by
[@&#8203;yusukebe](https://github.com/yusukebe) in
[honojs/hono#3054

**Full Changelog**:
honojs/hono@v4.4.8...v4.4.9

### [`v4.4.8`](https://github.com/honojs/hono/releases/tag/v4.4.8)

[Compare
Source](https://github.com/honojs/hono/compare/v4.4.7...v4.4.8)

#### What's Changed

- fix(jsx): add an explicit type by
[@&#8203;yusukebe](https://github.com/yusukebe) in
[honojs/hono#3007
- ci: use `env` for codecov GitHub Actions by
[@&#8203;yusukebe](https://github.com/yusukebe) in
[honojs/hono#3010
- chore: Fix typos in JSDoc by
[@&#8203;NicoPlyley](https://github.com/NicoPlyley) in
[honojs/hono#3002
- fix: change to allow use of websocket options by
[@&#8203;EdamAme-x](https://github.com/EdamAme-x) in
[honojs/hono#2999
- perf: parseAccept without spread operator by
[@&#8203;Jayllyz](https://github.com/Jayllyz) in
[honojs/hono#3003
- test: add tests for buffer.ts by
[@&#8203;yasuaki640](https://github.com/yasuaki640) in
[honojs/hono#3004
- chore: upload bun test coverage to CodeCov by
[@&#8203;exoego](https://github.com/exoego) in
[honojs/hono#3022
- refactor: remove unneeded import statements by
[@&#8203;EdamAme-x](https://github.com/EdamAme-x) in
[honojs/hono#3014
- perf(utils/buffer): use promise all for better performance by
[@&#8203;yasuaki640](https://github.com/yasuaki640) in
[honojs/hono#3031

**Full Changelog**:
honojs/hono@v4.4.7...v4.4.8

</details>

---

### Configuration

📅 **Schedule**: Branch creation - "before 4am on Monday" in timezone
America/Chicago, Automerge - At any time (no schedule defined).

🚦 **Automerge**: Disabled by config. Please merge this manually once you
are satisfied.

♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the
rebase/retry checkbox.

🔕 **Ignore**: Close this PR and you won't be reminded about this update
again.

---

- [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check
this box

---

This PR was generated by [Mend
Renovate](https://www.mend.io/free-developer-tools/renovate/). View the
[repository job log](https://developer.mend.io/github/autoblocksai/cli).

<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzNy40MjUuMSIsInVwZGF0ZWRJblZlciI6IjM3LjQzOC4wIiwidGFyZ2V0QnJhbmNoIjoibWFpbiIsImxhYmVscyI6W119-->

Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants