Skip to content

src: consolidate C++ ReadFileSync/WriteFileSync utilities#61662

Open
joyeecheung wants to merge 2 commits intonodejs:mainfrom
joyeecheung:file-utils
Open

src: consolidate C++ ReadFileSync/WriteFileSync utilities#61662
joyeecheung wants to merge 2 commits intonodejs:mainfrom
joyeecheung:file-utils

Conversation

@joyeecheung
Copy link
Member

@joyeecheung joyeecheung commented Feb 3, 2026

This patch moves ReadFileSync and WriteFileSync from src/util.cc to src/node_file_utils.cc, consolidates the implementation to reuse code, and adds a few more enhancements:

For ReadFileSync:

  • Use fstat-based pre-allocation to minimize buffer resizing and repeated reads for bigger files.
  • Handle various potential overflows in size conversions.
  • Handle fallback for 0-byte special files.

For WriteFileSync:

  • Handle potential partial writes for big enough files and support non-seekable files (with -1 as offset).
  • Handle 0-byte writes correctly.

In both cases, this now avoids hard aborts and return error code for the caller to handle as much as possible, except std::vector<char> ReadFileSync(FILE* fp) which is part of the embedder API.

This patch uses the new ReadFileSync to address a TODO in node_sea.bin.cc.

Some numbers from --build-sea (note that most of the time is spent on parsing the executable and building the SEA, but even so there's a measurable difference just from the file I/O because the binary is ~120MB).

$ hyperfine "out/Release/node --build-sea sea-config.json" "./node_main --build-sea sea-config.json" --warmup 5
Benchmark 1: out/Release/node --build-sea sea-config.json
  Time (mean ± σ):      1.696 s ±  0.040 s    [User: 1.509 s, System: 0.173 s]
  Range (min … max):    1.651 s …  1.756 s    10 runs

Benchmark 2: ./node_main --build-sea sea-config.json
  Time (mean ± σ):      1.730 s ±  0.030 s    [User: 1.530 s, System: 0.183 s]
  Range (min … max):    1.681 s …  1.787 s    10 runs

Summary
  out/Release/node --build-sea sea-config.json ran
    1.02 ± 0.03 times faster than ./node_main --build-sea sea-config.json

This patch moves `ReadFileSync` and `WriteFileSync` from
`src/util.cc` to `src/node_file_utils.cc`, consolidates
the implementation to reuse code, and adds a few more
enhancements:

For `ReadFileSync`:

- Use fstat-based pre-allocation to minimize buffer resizing
  and repeated reads for bigger files.
- Handle various potential overflows in size conversions.
- Handle fallback for 0-byte special files.

For `WriteFileSync`:

- Handle potential partial writes for big enough files and
  support non-seekable files (with -1 as offset).
- Handle 0-byte writes correctly.

In both cases, this now avoids hard aborts and return error code
for the caller to handle as much as possible, except
`std::vector<char> ReadFileSync(FILE* fp)` which is part of the
embedder API.

This patch uses the new `ReadFileSync` to address a TODO in
node_sea.bin.cc.
@nodejs-github-bot
Copy link
Collaborator

Review requested:

  • @nodejs/gyp
  • @nodejs/single-executable

@nodejs-github-bot nodejs-github-bot added c++ Issues and PRs that require attention from people who are familiar with C++. lib / src Issues and PRs related to general changes in the lib or src directory. needs-ci PRs that need a full CI run. labels Feb 3, 2026
@joyeecheung
Copy link
Member Author

cc @nodejs/cpp-reviewers

@joyeecheung joyeecheung added the request-ci Add this label to start a Jenkins CI on a PR. label Feb 3, 2026
@github-actions github-actions bot removed the request-ci Add this label to start a Jenkins CI on a PR. label Feb 3, 2026
@nodejs-github-bot
Copy link
Collaborator

@codecov
Copy link

codecov bot commented Feb 3, 2026

Codecov Report

❌ Patch coverage is 62.85714% with 52 lines in your changes missing coverage. Please review.
✅ Project coverage is 89.72%. Comparing base (8e41b8d) to head (48ed3ef).
⚠️ Report is 26 commits behind head on main.

Files with missing lines Patch % Lines
src/node_file_utils.cc 62.31% 30 Missing and 22 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main   #61662      +/-   ##
==========================================
- Coverage   89.74%   89.72%   -0.03%     
==========================================
  Files         674      675       +1     
  Lines      204348   204472     +124     
  Branches    39271    39290      +19     
==========================================
+ Hits       183396   183460      +64     
- Misses      13262    13303      +41     
- Partials     7690     7709      +19     
Files with missing lines Coverage Δ
src/node_internals.h 82.35% <ø> (ø)
src/node_sea_bin.cc 41.51% <100.00%> (-0.22%) ⬇️
src/util.cc 87.44% <ø> (+1.34%) ⬆️
src/util.h 90.09% <ø> (ø)
src/node_file_utils.cc 62.31% <62.31%> (ø)

... and 35 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Copy link
Member

@Qard Qard left a comment

Choose a reason for hiding this comment

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

LGTM. A few small code quality improvement ideas, but I'm fine landing as-is. These are just minor suggestions to simplify things slightly. 🙂

Co-authored-by: Stephen Belanger <admin@stephenbelanger.com>
@joyeecheung
Copy link
Member Author

Thanks for the suggestion. @Qard @RafaelGSS Can you take a look again?

@addaleax addaleax added author ready PRs that have at least one approval, no pending requests for changes, and a CI started. request-ci Add this label to start a Jenkins CI on a PR. labels Feb 5, 2026
@github-actions github-actions bot removed the request-ci Add this label to start a Jenkins CI on a PR. label Feb 5, 2026
@nodejs-github-bot
Copy link
Collaborator

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

author ready PRs that have at least one approval, no pending requests for changes, and a CI started. c++ Issues and PRs that require attention from people who are familiar with C++. lib / src Issues and PRs related to general changes in the lib or src directory. needs-ci PRs that need a full CI run.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants