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: optimize HeadersList.clear #2457

Closed
wants to merge 2 commits into from

Conversation

tsctx
Copy link
Member

@tsctx tsctx commented Nov 25, 2023

Rationale

Checking the Map size and not clearing is thirty times faster if the map is empty.

Status

Copy link
Member

@KhafraDev KhafraDev left a comment

Choose a reason for hiding this comment

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

I'm all for optimizing, but when would someone ever clear an empty Headers? It seems like a case that would never happen.

@tsctx
Copy link
Member Author

tsctx commented Nov 26, 2023

@KhafraDev #2454 (comment)

@tsctx
Copy link
Member Author

tsctx commented Nov 26, 2023

The headersList is empty unless the first argument of the Request constructor is Request

@codecov-commenter
Copy link

codecov-commenter commented Nov 26, 2023

Codecov Report

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

Comparison is base (e39a632) 85.54% compared to head (29eabc3) 85.62%.
Report is 106 commits behind head on main.

Files Patch % Lines
lib/handler/RetryHandler.js 73.04% 31 Missing ⚠️
lib/fetch/index.js 64.51% 11 Missing ⚠️
lib/fetch/headers.js 88.09% 5 Missing ⚠️
lib/api/readable.js 80.00% 4 Missing ⚠️
lib/client.js 92.10% 3 Missing ⚠️
lib/core/util.js 90.47% 2 Missing ⚠️
lib/core/request.js 97.56% 1 Missing ⚠️
lib/fetch/request.js 83.33% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2457      +/-   ##
==========================================
+ Coverage   85.54%   85.62%   +0.07%     
==========================================
  Files          76       77       +1     
  Lines        6858     7055     +197     
==========================================
+ Hits         5867     6041     +174     
- Misses        991     1014      +23     

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

@KhafraDev
Copy link
Member

#2464 (comment)

@tsctx
Copy link
Member Author

tsctx commented Nov 28, 2023

@KhafraDev The same logic is used here.

undici/lib/fetch/headers.js

Lines 227 to 237 in 0437f69

get entries () {
const headers = {}
if (this[kHeadersMap].size) {
for (const { name, value } of this[kHeadersMap].values()) {
headers[name] = value
}
}
return headers
}

@ronag
Copy link
Member

ronag commented Nov 28, 2023

@KhafraDev The same logic is used here.

undici/lib/fetch/headers.js

Lines 227 to 237 in 0437f69

get entries () {
const headers = {}
if (this[kHeadersMap].size) {
for (const { name, value } of this[kHeadersMap].values()) {
headers[name] = value
}
}
return headers
}

That's not quite the same. It avoids creating an unnecessary iterator. But yea, I would argue it's unnecessary there as well.

@tsctx
Copy link
Member Author

tsctx commented Nov 28, 2023

On second thought, this does not seem necessary.

@tsctx tsctx closed this Nov 28, 2023
@tsctx tsctx deleted the perf/optimize-headerlist-clear branch November 28, 2023 10:07
@KhafraDev
Copy link
Member

That optimization makes much more sense because headers received from the server are immutable

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.

None yet

4 participants