Skip to content

fix: reduce Context code#4100

Merged
yusukebe merged 19 commits intonextfrom
perf/reduce-context
Jun 16, 2025
Merged

fix: reduce Context code#4100
yusukebe merged 19 commits intonextfrom
perf/reduce-context

Conversation

@yusukebe
Copy link
Member

@yusukebe yusukebe commented Apr 21, 2025

This PR reduces the code size of src/context.ts and fixes some bugs.

Reducing the code size

The current implementation uses #preparedHeaders and #isFresh to avoid generating Headers and Response objects as much as possible. This increases the amount of code and makes the code more complex.

In this PR, we have removed them and shortened the code.

This change has reduced the bundle size by about 900B with a minified minimum app using the hono/tiny.

CleanShot 2025-06-07 at 07 46 32@2x

Fixes weird behaviors

There are some bugs in the current implementation of context.ts that cause weird behaviors. The following tests have failed. This PR allows those tests to succeed.

f4cb6ac

Therefore, the following Issues are fixed.

Fixes honojs/node-server#226
Fixes #3736 (maybe)

Performance

This PR shows a slight decrease in performance related to application speed. Benchmark results using this script are as follows.

CleanShot 2025-06-07 at 07 43 54@2x

Performance is very important to us. So we are not sure if we can accept this PR or not. However, the difference is slight, and the smaller file size is expected to improve performance in a serverless environment.

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

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@codecov
Copy link

codecov bot commented Apr 21, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 91.27%. Comparing base (548d73f) to head (50b7819).
Report is 21 commits behind head on next.

Additional details and impacted files
@@            Coverage Diff             @@
##             next    #4100      +/-   ##
==========================================
- Coverage   91.32%   91.27%   -0.06%     
==========================================
  Files         168      168              
  Lines       10802    10723      -79     
  Branches     3073     3043      -30     
==========================================
- Hits         9865     9787      -78     
+ Misses        936      935       -1     
  Partials        1        1              

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

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@yusukebe
Copy link
Member Author

Whether we merge this or not is a difficult thing. In the c.header(), using c.res to store the header values will introduce a significant decrease in performance when using c.header(). Hmm.

@github-actions

This comment has been minimized.

Co-authored-by: Kentaro Suzuki <71284054+sushichan044@users.noreply.github.com>
Co-authored-by: Fuxiao Peng <xx1124961758@126.com>
@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@yusukebe yusukebe marked this pull request as ready for review June 6, 2025 23:05
@yusukebe yusukebe changed the title perf: reduce Context code fix: reduce Context code Jun 6, 2025
@yusukebe
Copy link
Member Author

yusukebe commented Jun 6, 2025

Hello @usualoma @sushichan044 !

What do you think of this? If this makes sense to you, can you review it?

@yusukebe yusukebe added the v4.8 label Jun 6, 2025
Copy link
Member

@usualoma usualoma left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great, I think it's wonderful work!

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

Copy link
Contributor

@sushichan044 sushichan044 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had the same concern as @usualoma .
Since it has already been corrected, I think it looks good now!

I did a quick check on my end, and it seemed to handle common corner cases without any issues.

@yusukebe yusukebe requested a review from usualoma June 7, 2025 08:42
@sushichan044
Copy link
Contributor

sushichan044 commented Jun 7, 2025

@yusukebe
As a nitpick, it might be good to have a test like this to demonstrate the behavior when we input empty array as header value.
This test isn't essential, as the behavior is predictable from the implementation.

  it('Should remove existing header when new value is empty array', async () => {
    c.header('X-Test', 'existing')
    const res = c.json({ test: 'data' }, 200, {
      'X-Test': [],
    })
    expect(res.headers.get('X-Test')).toBeNull()
  })

Copy link
Member

@usualoma usualoma left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

Co-authored-by: Kentaro Suzuki <71284054+sushichan044@users.noreply.github.com>
@github-actions
Copy link

github-actions bot commented Jun 8, 2025

Bundle size check

main (4a1dd5f) #4100 (36744c6) +/-
Bundle Size (B) 19,027B 18,157B -870B
Bundle Size (KB) 18.58K 17.73K -0.85K

Compiler Diagnostics (typescript-go)

main (4a1dd5f) #4100 (36744c6) +/-
Files 231 231 0
Lines 106,340 106,248 -92
Identifiers 106,183 106,137 -46
Symbols 371,551 371,525 -26
Types 293,004 292,994 -10
Instantiations 3,568,489 3,568,491 2
Memory used 229,864K 229,650K -214K
Memory allocs 10,003,691 10,003,331 -360
Parse time 0.066s 0.068s 0.002s
Bind time 0.014s 0.04s 0.026s
Check time 1.343s 1.39s 0.047s
Emit time 0s 0s 0s
Total time 1.425s 1.5s 0.075s

Reported by octocov

@github-actions
Copy link

github-actions bot commented Jun 8, 2025

Bundle size check

main (4a1dd5f) #4100 (36744c6) +/-
Bundle Size (B) 19,027B 18,157B -870B
Bundle Size (KB) 18.58K 17.73K -0.85K

Compiler Diagnostics (tsc)

main (4a1dd5f) #4100 (36744c6) +/-
Files 261 261 0
Lines 116,441 116,349 -92
Identifiers 114,443 114,397 -46
Symbols 259,900 259,888 -12
Types 162,567 162,559 -8
Instantiations 3,039,293 3,039,295 2
Memory used 272,070K 273,661K 1,591K
I/O read 0.02s 0.02s 0s
I/O write 0s 0s 0s
Parse time 0.65s 0.72s 0.07s
Bind time 0.27s 0.28s 0.01s
Check time 3.74s 3.7s -0.04s
Emit time 0s 0s 0s
Total time 4.66s 4.7s 0.04s

Reported by octocov

@yusukebe
Copy link
Member Author

yusukebe commented Jun 8, 2025

@usualoma @sushichan044 Thanks!

@sushichan044 Yes. The test you mentioned is good. I added it.

I'll include this in the next minor release.

@yusukebe yusukebe changed the base branch from main to next June 16, 2025 05:15
@yusukebe yusukebe merged commit 9cdfdf3 into next Jun 16, 2025
17 checks passed
@yusukebe yusukebe deleted the perf/reduce-context branch June 16, 2025 22:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: serveStatic({ precompressed: true }) returns text/plain Hono not picking up MIME types correctly (I think)

3 participants