Skip to content

perf(util/parseHeaders): If the header name is buffer - #2501

Merged
ronag merged 34 commits into
nodejs:mainfrom
tsctx:perf/improve-parseheaders-buffer
Dec 8, 2023
Merged

ronag merged 34 commits into
nodejs:mainfrom
tsctx:perf/improve-parseheaders-buffer

Conversation

@tsctx

@tsctx tsctx commented Dec 6, 2023 •

Copy link
Copy Markdown
Member

Fixes #2498

Benchmark

  • main
• parseHeaders
---------------------------------------------------------------- -----------------------------
parseHeaders               26.12 µs/iter    (21.1 µs … 19.62 ms)   22.5 µs   56.6 µs   72.6 µs
parseHeaders (irregular)   29.83 µs/iter    (25.9 µs … 874.1 µs)   29.3 µs   73.7 µs   80.4 µs

summary for parseHeaders
  parseHeaders
   1.14x faster than parseHeaders (irregular)
  • this patch
• parseHeaders
---------------------------------------------------------------- -----------------------------
parseHeaders               14.34 µs/iter    (12.5 µs … 436.2 µs)   13.3 µs   33.5 µs     45 µs
parseHeaders (irregular)   13.99 µs/iter    (12.4 µs … 368.8 µs)   13.1 µs   31.5 µs   39.9 µs

summary for parseHeaders
  parseHeaders (irregular)
   1.03x faster than parseHeaders

Comment thread lib/core/tree.js Resolved
Comment thread lib/core/tree.js Resolved
Comment thread lib/core/tree.js Resolved
@tsctx tsctx changed the title perf(util/parse): If the header name is buffer perf(util/parseHeaders): If the header name is buffer Dec 6, 2023
@codecov-commenter

codecov-commenter commented Dec 6, 2023 •

Copy link
Copy Markdown

Codecov Report

Attention: 153 lines in your changes are missing coverage. Please review.

Comparison is base (e39a632) 85.54% compared to head (6efff14) 84.50%.
Report is 143 commits behind head on main.

Files Patch % Lines
lib/fetch/index.js 57.01% 49 Missing ⚠️
lib/fetch/util.js 33.33% 46 Missing ⚠️
lib/handler/RetryHandler.js 74.35% 30 Missing ⚠️
lib/api/readable.js 86.48% 5 Missing ⚠️
lib/compat/dispatcher-weakref.js 28.57% 5 Missing ⚠️
lib/fetch/headers.js 88.63% 5 Missing ⚠️
lib/client.js 92.68% 3 Missing ⚠️
lib/core/tree.js 94.23% 3 Missing ⚠️
lib/core/util.js 94.28% 2 Missing ⚠️
lib/fetch/request.js 90.47% 2 Missing ⚠️
... and 3 more
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2501      +/-   ##
==========================================
- Coverage   85.54%   84.50%   -1.05%     
==========================================
  Files          76       79       +3     
  Lines        6858     7216     +358     
==========================================
+ Hits         5867     6098     +231     
- Misses        991     1118     +127     

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

@tsctx

tsctx commented Dec 6, 2023 •

Copy link
Copy Markdown
Member Author

@ronag
Is it safe because it is verified by llhttp?

undici/lib/client.js

Lines 768 to 774 in 0c3c6f8

if (key.length === 10 && key.toString().toLowerCase() === 'keep-alive') {
this.keepAlive += buf.toString()
} else if (key.length === 10 && key.toString().toLowerCase() === 'connection') {
this.connection += buf.toString()
} else if (key.length === 14 && key.toString().toLowerCase() === 'content-length') {
this.contentLength += buf.toString()
}

@metcoder95

Copy link
Copy Markdown
Member

Is it safe because it is verified by llhttp?

It should. It might not be a benefit trying to optimize that path

Comment thread lib/core/util.js Resolved

@ronag ronag left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Can you add a benchmark as well?

@tsctx

tsctx commented Dec 6, 2023

Copy link
Copy Markdown
Member Author

Can you add a benchmark as well?

Which benchmark library should I use?

@tsctx
tsctx marked this pull request as ready for review December 6, 2023 11:49
@ronag

ronag commented Dec 6, 2023

Copy link
Copy Markdown
Member

As far as I'm concerned. Whatever you prefer.

@tsctx

tsctx commented Dec 6, 2023

Copy link
Copy Markdown
Member Author

@ronag Done.

@ronag

ronag commented Dec 6, 2023

Copy link
Copy Markdown
Member

@ronag Done.

Are the results in the description the latest ones?

@ronag
ronag requested review from anonrig and mcollina December 6, 2023 12:13
@tsctx

tsctx commented Dec 6, 2023

Copy link
Copy Markdown
Member Author

No. I'll update it later.

@ronag

ronag commented Dec 6, 2023 •

Copy link
Copy Markdown
Member

@lemire @anonrig Can you think of some other data structure or other way to implement Tree that might be faster? I think the main bottleneck here is the heap-allocated nodes.

@ronag ronag left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Did you try with Map?

Comment thread lib/core/tree.js Resolved
@ronag
ronag requested review from anonrig and lemire December 7, 2023 16:47
@ronag

ronag commented Dec 7, 2023

Copy link
Copy Markdown
Member

@mcollina @lemire @anonrig PTAL

@lemire

lemire commented Dec 7, 2023

Copy link
Copy Markdown
Member

I’m planning a blog post on this issue… hopefully today.

@ronag

ronag commented Dec 7, 2023

Copy link
Copy Markdown
Member

Regarding memory usage. This will increase minimum usage but also decrease maximum usage as concurrent responses would share memory.

Comment thread lib/core/tree.js Resolved
Comment thread test/tree.js Resolved
Comment thread test/tree.js Resolved
@lemire

lemire commented Dec 8, 2023

Copy link
Copy Markdown
Member

@ronag I finally had time tonight to run some experiments. The blog post is up: Fast Buffer-to-String conversion in JavaScript with a Lookup Table.

@ronag

ronag commented Dec 8, 2023

Copy link
Copy Markdown
Member

@lemire I assume you are ok with the current state of this PR then?

@ronag
ronag merged commit 0690568 into nodejs:main Dec 8, 2023
@tsctx
tsctx deleted the perf/improve-parseheaders-buffer branch December 8, 2023 09:05
@lemire

lemire commented Dec 8, 2023

Copy link
Copy Markdown
Member

@ronag It checks out!

@tsctx tsctx mentioned this pull request Dec 13, 2023
kodiakhq Bot referenced this pull request in X-oss-byte/Canary-nextjs Dec 21, 2023
[![Mend Renovate](https://app.renovatebot.com/images/banner.svg)](https://renovatebot.com)

This PR contains the following updates:

| Package | Change | Age | Adoption | Passing | Confidence |
|---|---|---|---|---|---|
| [undici](https://undici.nodejs.org) ([source](https://togithub.com/nodejs/undici)) | [`6.0.1` -> `6.2.0`](https://renovatebot.com/diffs/npm/undici/6.0.1/6.2.0) | [![age](https://developer.mend.io/api/mc/badges/age/npm/undici/6.2.0?slim=true)](https://docs.renovatebot.com/merge-confidence/) | [![adoption](https://developer.mend.io/api/mc/badges/adoption/npm/undici/6.2.0?slim=true)](https://docs.renovatebot.com/merge-confidence/) | [![passing](https://developer.mend.io/api/mc/badges/compatibility/npm/undici/6.0.1/6.2.0?slim=true)](https://docs.renovatebot.com/merge-confidence/) | [![confidence](https://developer.mend.io/api/mc/badges/confidence/npm/undici/6.0.1/6.2.0?slim=true)](https://docs.renovatebot.com/merge-confidence/) |

---

> [!WARNING]
> Some dependencies could not be looked up. Check the Dependency Dashboard for more information.

---

### Release Notes

<details>
<summary>nodejs/undici (undici)</summary>

### [`v6.2.0`](https://togithub.com/nodejs/undici/releases/tag/v6.2.0)

[Compare Source](https://togithub.com/nodejs/undici/compare/v6.1.0...v6.2.0)

#### What's Changed

-   Remove FinalizationRegistry from Agent by [@&#8203;mcollina](https://togithub.com/mcollina) in [https://github.com/nodejs/undici/pull/2530](https://togithub.com/nodejs/undici/pull/2530)

**Full Changelog**: nodejs/undici@v6.1.0...v6.2.0

### [`v6.1.0`](https://togithub.com/nodejs/undici/releases/tag/v6.1.0)

[Compare Source](https://togithub.com/nodejs/undici/compare/v6.0.1...v6.1.0)

#### What's Changed

-   fix: more sensible stack trace from dump error by [@&#8203;ronag](https://togithub.com/ronag) in [https://github.com/nodejs/undici/pull/2503](https://togithub.com/nodejs/undici/pull/2503)
-   refactor: remove some node compat by [@&#8203;ronag](https://togithub.com/ronag) in [https://github.com/nodejs/undici/pull/2502](https://togithub.com/nodejs/undici/pull/2502)
-   refactor: version cleanup by [@&#8203;tsctx](https://togithub.com/tsctx) in [https://github.com/nodejs/undici/pull/2507](https://togithub.com/nodejs/undici/pull/2507)
-   perf(fetch): Improve fetch of detaurl by [@&#8203;tsctx](https://togithub.com/tsctx) in [https://github.com/nodejs/undici/pull/2479](https://togithub.com/nodejs/undici/pull/2479)
-   feat: expose parseHeader by [@&#8203;ronag](https://togithub.com/ronag) in [https://github.com/nodejs/undici/pull/2511](https://togithub.com/nodejs/undici/pull/2511)
-   perf(fetch): optimize call `dispatch` by [@&#8203;tsctx](https://togithub.com/tsctx) in [https://github.com/nodejs/undici/pull/2493](https://togithub.com/nodejs/undici/pull/2493)
-   perf(util/parseHeaders): If the header name is buffer by [@&#8203;tsctx](https://togithub.com/tsctx) in [https://github.com/nodejs/undici/pull/2501](https://togithub.com/nodejs/undici/pull/2501)
-   perf: twice faster method check by [@&#8203;tsctx](https://togithub.com/tsctx) in [https://github.com/nodejs/undici/pull/2495](https://togithub.com/nodejs/undici/pull/2495)
-   refactor: remove Error.captureStackTrace by [@&#8203;tsctx](https://togithub.com/tsctx) in [https://github.com/nodejs/undici/pull/2509](https://togithub.com/nodejs/undici/pull/2509)
-   perf: Improve processHeader by [@&#8203;tsctx](https://togithub.com/tsctx) in [https://github.com/nodejs/undici/pull/2513](https://togithub.com/nodejs/undici/pull/2513)
-   perf: reduce `String#toLowerCase` call by [@&#8203;tsctx](https://togithub.com/tsctx) in [https://github.com/nodejs/undici/pull/2516](https://togithub.com/nodejs/undici/pull/2516)
-   perf: optimize consumeEnd by [@&#8203;tsctx](https://togithub.com/tsctx) in [https://github.com/nodejs/undici/pull/2510](https://togithub.com/nodejs/undici/pull/2510)
-   perf: reduce tst built time by [@&#8203;tsctx](https://togithub.com/tsctx) in [https://github.com/nodejs/undici/pull/2517](https://togithub.com/nodejs/undici/pull/2517)
-   feat: allow customization of build environment by [@&#8203;khardix](https://togithub.com/khardix) in [https://github.com/nodejs/undici/pull/2403](https://togithub.com/nodejs/undici/pull/2403)
-   fix: clear cache by [@&#8203;tsctx](https://togithub.com/tsctx) in [https://github.com/nodejs/undici/pull/2519](https://togithub.com/nodejs/undici/pull/2519)
-   feat: Add resource timing entries for connection, request and response by [@&#8203;ToshB](https://togithub.com/ToshB) in [https://github.com/nodejs/undici/pull/2481](https://togithub.com/nodejs/undici/pull/2481)
-   Call fg.unregister() after a dispatcher is done, adds UNDICI_NO_FG to… by [@&#8203;mcollina](https://togithub.com/mcollina) in [https://github.com/nodejs/undici/pull/2527](https://togithub.com/nodejs/undici/pull/2527)
-   feat: expose headerNameToString by [@&#8203;tsctx](https://togithub.com/tsctx) in [https://github.com/nodejs/undici/pull/2525](https://togithub.com/nodejs/undici/pull/2525)

#### New Contributors

-   [@&#8203;khardix](https://togithub.com/khardix) made their first contribution in [https://github.com/nodejs/undici/pull/2403](https://togithub.com/nodejs/undici/pull/2403)

**Full Changelog**: nodejs/undici@v6.0.1...v6.1.0

</details>

---

### Configuration

📅 **Schedule**: Branch creation - At any time (no schedule defined), 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.

---

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

---

This PR has been generated by [Mend Renovate](https://www.mend.io/free-developer-tools/renovate/). View repository job log [here](https://developer.mend.io/github/X-oss-byte/Canary-nextjs).
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.

Request to improve performance of stringifying in internal codes

7 participants