Repository navigation
Conversation
147f9fe to
9a1cd59
Compare
mcollina
left a comment
There was a problem hiding this comment.
lgtm
I know that probably @KhafraDev would object to some of those. This might take a while to land.
cc @jasnell
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #6260 +/- ##
=======================================
Coverage 91.77% 91.78%
=======================================
Files 113 113
Lines 44884 44927 +43
=======================================
+ Hits 41194 41236 +42
- Misses 3690 3691 +1 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
|
I object to all of them, actually. If the intent is for Claude to maintain undici in perpetuity... |
9a1cd59 to
9fa7fab
Compare
|
-1. This PR defers signal registration, but the relationship between the source signal and the child signal needs to be weak in both directions to avoid memory leaks. Once weak references are introduced, the cost appears to be roughly equivalent to the current implementation. If so, it is unclear what practical benefit this PR would provide. |
I might be wrong, but I though our tests cover the leaks, or maybe not with a signal? This PR does also avoid a few bits of data copying, isn’t that worth doing? |
|
I checked whether the abort-listener leak protection from #5285 is still covered with this change. Behaviour looks right: listener counts on the caller's signal match Coverage is thin, though. Only I opened #6269 with tests for:
They pass on |
I do not think that alone justifies the change unless the performance improvement can be achieved in a maintainable way without violating the specification.
Sorry, but that is not what I meant. The test added in #6269 does not cover this point. That test is fine as it is, so there is no need to add another one. What I was referring to is the fact that |
Background
While profiling Node.js HTTP servers built on the Web Fetch API (such as
@remix-run/node-fetch-server, which constructs a nativenew Request(url, { method, headers, signal, body })for every incoming HTTP request), two hot-path allocations stood out inRequestconstruction:createDependentSignal()runs eagerly in theRequestconstructor even whenrequest.signalis never read, allocating a secondAbortController,WeakRef,WeakMapentries,FinalizationRegistryregistration, andaddAbortListener/removeEventListenerchurn on GC.inputis a string, theRequestconstructor callsmakeRequest()twice — allocating and immediately discarding a 40-property internal request object, aHeadersList, aMap, and aurlListarray on every call.Both optimizations preserve Undici's HTTP client behavior and also speed up client
fetch()calls.Summary of changes
AbortSignalinRequest(lib/web/fetch/request.js,lib/web/fetch/index.js):#sourceSignalin theRequestconstructor and defercreateDependentSignal()untilrequest.signalis accessed via a private#getSignal()helper (avoiding public getter interception when copying or cloning).request.signalis read, accessingrequest.signalstill materializes an already-aborted signal with the originalreason.fetch(), checkhasRequestSignal(requestObject)before registering abort listeners so signal-less clientfetch(url)calls skipAbortControllerandaddAbortListenersetup as well.makeRequest()andHeadersListallocations inRequest(lib/web/fetch/request.js):makeRequest()twice.Requestinputs, skip cloninginput.#state.headersListwheninit.headersreplaces it immediately in step 32, and skip materializinginput.#getSignal()wheninit.signaloverrides it.Benchmarks
Measured on Apple M5 Pro, Node.js v24.18.0 (150k iterations per benchmark, with
global.gc()between runs):mainnew Request(url)0.301 µs0.207 µsnew Request(url, { method, headers })0.848 µs0.691 µsnew Request(url, { method, headers, signal })2.227 µs0.717 µs