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(trie-router): optimize and remove unnecessary processes #3647

Merged
merged 3 commits into from
Nov 12, 2024

Conversation

EdamAme-x
Copy link
Contributor

@EdamAme-x EdamAme-x commented Nov 8, 2024

  1. The following process is not necessary and has been removed.

https://github.com/honojs/hono/pull/3647/files#diff-bf1549ba9405f87c376d2d338883f54bfeb6a91f40b28e62dd2ce5e9d4edd4e5L66-L69

  1. Since it will be minified, I changed the name to be easy to understand.

Related: #3596

https://github.com/honojs/hono/pull/3647/files#diff-bf1549ba9405f87c376d2d338883f54bfeb6a91f40b28e62dd2ce5e9d4edd4e5L85

  1. Since only the type number is used for the key, an empty object should be created instead of Object.create(null). It is faster and smaller.

Node and Bun (8x~ faster)

runtime: node 20.18.0 (x64-linux)

benchmark              avg (min … max) p75   p99    (min … top 1%)
-------------------------------------- -------------------------------
= Object.create(null)    16.67 ns/iter  15.13 ns  █                   
                 (13.54 ns … 96.35 ns)  42.71 ns ▇█▂▁▂▂▁▁▂▁▁▁▁▁▁▁▁▁▁▁▁
= {}                      2.41 ns/iter   2.22 ns  █                   
                  (1.84 ns … 95.40 ns)   8.21 ns ▁█▂▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁
runtime: deno 2.0.5 (x86_64-unknown-linux-gnu)

benchmark              avg (min … max) p75   p99    (min … top 1%)
-------------------------------------- -------------------------------
= Object.create(null)    16.64 ns/iter  14.93 ns  █                   
                (12.03 ns … 137.93 ns)  33.21 ns ▁██▃▁▁▁▁▁▁▁▁▁▁▅▂▁▁▁▁▁
= {}                      2.70 ns/iter   2.19 ns █                    
                  (2.03 ns … 62.84 ns)  16.17 ns █▂▂▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁

In Bun, the speed of both is almost the same.

https://github.com/honojs/hono/pull/3647/files#diff-bf1549ba9405f87c376d2d338883f54bfeb6a91f40b28e62dd2ce5e9d4edd4e5L95

4. Multiple index accesses are eliminated and the size is reduced.

https://github.com/honojs/hono/pull/3647/files#diff-bf1549ba9405f87c376d2d338883f54bfeb6a91f40b28e62dd2ce5e9d4edd4e5R130

  1. Remove as it is not a necessary check

https://github.com/honojs/hono/pull/3647/files#diff-bf1549ba9405f87c376d2d338883f54bfeb6a91f40b28e62dd2ce5e9d4edd4e5L178

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

@EdamAme-x EdamAme-x marked this pull request as draft November 8, 2024 14:17
Copy link

codecov bot commented Nov 8, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 91.69%. Comparing base (8a8a576) to head (9028f2f).
Report is 2 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #3647      +/-   ##
==========================================
+ Coverage   91.68%   91.69%   +0.01%     
==========================================
  Files         159      159              
  Lines       10135    10138       +3     
  Branches     2878     2880       +2     
==========================================
+ Hits         9292     9296       +4     
+ Misses        841      840       -1     
  Partials        2        2              

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

@EdamAme-x EdamAme-x marked this pull request as ready for review November 8, 2024 14:30
@EdamAme-x EdamAme-x changed the title perf(trie-router): remove unnecessary processes and speed up perf(trie-router): optimize and remove unnecessary processes Nov 8, 2024
@yusukebe
Copy link
Member

yusukebe commented Nov 9, 2024

Hi @EdamAme-x !

It looks awesome! I'd like to know how much this change affects performance. Did you benchmark it? And how much did you reduce the bundle size?

PS.

To benchmark the req/res performance, you can follow the method used in this repo: https://github.com/SaltyAom/bun-http-framework-benchmarkhttps://github.com/SaltyAom/bun-http-framework-benchmark

@EdamAme-x
Copy link
Contributor Author

EdamAme-x commented Nov 9, 2024

Report

Revert (4): The bundle size is smaller, but we got strange benchmark results that one index access is slower than multiple index accesses, so I reverted.

runtime: deno 2.0.5 (x86_64-pc-windows-msvc)

benchmark              avg (min … max) p75   p99    (min … top 1%)
-------------------------------------- -------------------------------
Once                    193.31 ps/iter 195.31 ps ▇  █
                (170.90 ps … 70.87 ns) 341.80 ps █▁▁█▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁
Multi                   136.00 ps/iter 146.48 ps     █
                 (97.66 ps … 22.39 ns) 219.73 ps ▁▁▁▁█▁▁▁▄▁▁▁▂▁▁▁▂▁▁▁▁

Bundle Size

19.34kb => 19.28kb (1kb = 1024b)

gitpod@honojs-hono-0oouxesc90f:/workspace/hono/sandbox$ ls compare -l
total 40
-rw-r--r-- 1 gitpod gitpod 19288 Nov  9 07:09 after.js
-rw-r--r-- 1 gitpod gitpod 19344 Nov  9 07:11 before.js

Benchmark

In Deno

There was not much change.
(+0.3%)

|  Framework       | Runtime | Average | Ping       | Query      | Body       |
| ---------------- | ------- | ------- | ---------- | ---------- | ---------- |
| after | deno | 41,225.407 | 50,386.78 | 38,903.5 | 34,385.94 |
| before | deno | 41,079.073 | 50,923.38 | 39,116.56 | 33,197.28 |

@yusukebe
Copy link
Member

@EdamAme-x

Have you ever tried to benchmark frameworks on Bun, not only Deno?

@EdamAme-x
Copy link
Contributor Author

I'll benchmark with several approaches.

@EdamAme-x
Copy link
Contributor Author

EdamAme-x commented Nov 11, 2024

I measured the performance of router itself using node, bun, and mitata, but did not see any significant performance improvement that we could see.

In Deno sample

clk: ~3.03 GHz
cpu: AMD EPYC 7B13
runtime: deno 2.0.5 (x86_64-unknown-linux-gnu)

benchmark              avg (min … max) p75   p99    (min … top 1%)
-------------------------------------- -------------------------------
Before Trie               3.55 µs/iter   3.57 µs   ▅ █▅██   ▅         
                   (3.47 µs … 3.70 µs)   3.68 µs ▄▇█▄████▄▄▄█▁▄▇▁▁▁▄▁▁
After Trie                3.51 µs/iter   3.52 µs   ▄▆▂█▂▄             
                   (3.45 µs … 3.71 µs)   3.67 µs ▇▅██████▃▃▁▁▁▃▁▁▁▁▁▁▁

@yusukebe
Copy link
Member

@EdamAme-x

Thanks. Anyway, this is a good improvement. I'll merge this now.

@yusukebe yusukebe merged commit 7e17b76 into honojs:main Nov 12, 2024
16 checks passed
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.

2 participants