fs: allocate FSReqPromise stat arrays lazily · nodejs/node@c5cb6bc · GitHub
Skip to content

Commit c5cb6bc

Browse files
MarshallOfSoundaduh95
authored andcommitted
fs: allocate FSReqPromise stat arrays lazily
Every promise-based fs operation eagerly allocated two AliasedBuffers (a stats array and a statfs array) at request creation, although only stat-family resolutions ever read the first and only statfs() reads the second. Each allocation is an ArrayBuffer, a TypedArray and a strong v8::Global. The callback path has no equivalent cost since it resolves through a shared global array. Construct the arrays lazily in ResolveStat()/ResolveStatFs() instead. Once created the lifetime is unchanged, so deferred continuations still read from request-owned memory. Improves fs/promises throughput under concurrency: writeFile +53%, stat +26%, readFile +22% at 64 in-flight operations on tmpfs, with callback paths unchanged. Signed-off-by: Sam Attard <sattard@anthropic.com> PR-URL: #63886 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Edy Silva <edigleyssonsilva@gmail.com>
1 parent b405e9b commit c5cb6bc

3 files changed

Lines changed: 29 additions & 18 deletions

File tree

src/node_file-inl.h

Lines changed: 21 additions & 13 deletions

src/node_file.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -266,8 +266,11 @@ class FSReqPromise final : public FSReqBase {
266266
bool use_bigint);
267267

268268
bool finished_ = false;
269-
AliasedBufferT stats_field_array_;
270-
AliasedBufferT statfs_field_array_;
269+
// Constructed lazily in ResolveStat()/ResolveStatFs(): most operations
270+
// never resolve with stats, and eagerly allocating the backing stores
271+
// for every request is a significant per-request cost.
272+
std::optional<AliasedBufferT> stats_field_array_;
273+
std::optional<AliasedBufferT> statfs_field_array_;
271274
};
272275

273276
class FSReqAfterScope final {

test/pummel/test-heapdump-fs-promise.js

Lines changed: 3 additions & 3 deletions

0 commit comments

Comments
 (0)