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

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.

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>
@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
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