Skip to content

fs: use sized reads for large files in readFileUtf8 - #65328

Open
codebytere wants to merge 1 commit into
nodejs:mainfrom
codebytere:perf/fs-readfile-utf8-sized-reads
Open

fs: use sized reads for large files in readFileUtf8#65328
codebytere wants to merge 1 commit into
nodejs:mainfrom
codebytere:perf/fs-readfile-utf8-sized-reads

Conversation

@codebytere

@codebytere codebytere commented Aug 16, 2026

Copy link
Copy Markdown
Member

fs.readFileSync(path, 'utf8') gets +19…42 % on 4 MiB files and +60…231 % on 256 KiB files, with small files untouched, by
reading large files into one right-sized buffer instead of 8 KiB at a time.

fs/readfile-utf8-fastpath.js source='path' content='ascii'      size=262144    ***   231.14 %  ±2.91%
fs/readfile-utf8-fastpath.js source='path' content='utf8_mixed' size=262144    ***    60.16 %  ±0.93%
fs/readfile-utf8-fastpath.js source='path' content='ascii'      size=4194304   ***    41.86 %  ±5.85%
fs/readfile-utf8-fastpath.js source='path' content='utf8_mixed' size=4194304   ***    19.06 %  ±2.92%
fs/readfile-utf8-fastpath.js source='fd'   *                    size=4194304   ***   +22…27 %
fs/readfile-utf8-fastpath.js *             *                    size≤16384            0…+2 %
fs/readFileSync.js (1 KiB)                                                              ~0 %  n.s.

(Linux x64, 30 runs. A 300 KB file goes from 39 syscalls to 5.)

ReadFileUtf8 - behind readFileSync(…, 'utf8') and therefore every CommonJS module load - read in 8 KiB read()s,
appending each to a std::string. Files that fit the first 8 KiB read behave exactly as before. Once a read fills the stack
buffer, the rest goes straight into one heap buffer: a 64 KiB step first (so files up to 64 KiB take the same reads as today
and no fstat), then sized from fstat() if that fills too, with geometric growth as the fallback. The size is only an
allocation hint - reading continues until read() returns 0, so procfs/sysfs and concurrently changing files behave as
before and StringBytes::Encode() sees exactly the bytes read. Allocation failure throws ERR_MEMORY_ALLOCATION_FAILED.

Tests: test-fs-readfilesync-utf8-sizes.js (new): equals readFileSync(path).toString('utf8') around every internal
boundary (0 … 8 MiB+5) with multi-byte characters straddling chunk edges; by fd from start / at EOF / mid-file; binary input;
/proc/self/maps and friends; EISDIR. fs, module and require suites pass.


Disclosure: the code, test, measurements and this description were written by Claude Code, directed and reviewed by @codebytere.

@nodejs-github-bot nodejs-github-bot added c++ Issues and PRs that require attention from people who are familiar with C++. fs Issues and PRs related to the fs subsystem / file system. needs-ci PRs that need a full CI run. labels Aug 16, 2026
@codebytere
codebytere force-pushed the perf/fs-readfile-utf8-sized-reads branch from 50aa5f1 to 39c515d Compare August 16, 2026 19:23
fs.readFileSync(path, 'utf8') read the whole file in 8 KiB read() calls
appended to a std::string, i.e. one syscall and a potential
reallocation per 8 KiB (an 8 MiB file took ~1400 read() calls).

Keep the exact old sequence for small files (one read into the 8 KiB
stack buffer, one read reporting EOF). Once a read fills the stack
buffer, read the rest directly into one heap buffer sized from fstat()
(plus one byte so that the EOF read does not force growth), growing
geometrically only when the size is unavailable or wrong. The size is
only an allocation hint: reading continues until read() reports EOF,
so procfs/sysfs files, FIFOs, files that change while being read and
file descriptors positioned mid-file behave as before, and the bytes
handed to StringBytes::Encode() are exactly the ones read.

Signed-off-by: Shelley Vohr <shelley.vohr@gmail.com>
@codebytere
codebytere force-pushed the perf/fs-readfile-utf8-sized-reads branch from 39c515d to ff70ea5 Compare August 16, 2026 20:30
@codecov

codecov Bot commented Aug 16, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 78.43137% with 11 lines in your changes missing coverage. Please review.
✅ Project coverage is 90.12%. Comparing base (30bff4a) to head (ff70ea5).
⚠️ Report is 6 commits behind head on main.

Files with missing lines Patch % Lines
src/node_file.cc 78.43% 4 Missing and 7 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main   #65328      +/-   ##
==========================================
- Coverage   90.13%   90.12%   -0.02%     
==========================================
  Files         752      752              
  Lines      251568   251618      +50     
  Branches    47270    47283      +13     
==========================================
+ Hits       226759   226764       +5     
- Misses      16168    16190      +22     
- Partials     8641     8664      +23     
Files with missing lines Coverage Δ
src/node_file.cc 74.47% <78.43%> (+0.27%) ⬆️

... and 32 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.

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

Labels

c++ Issues and PRs that require attention from people who are familiar with C++. fs Issues and PRs related to the fs subsystem / file system. needs-ci PRs that need a full CI run.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants