Uh oh!
There was an error while loading. Please reload this page.
fs: use sized reads for large files in readFileUtf8 - #65328
Merged
nodejs-github-bot merged 1 commit intoAug 24, 2026
Conversation
codebytereforce-pushed
the
perf/fs-readfile-utf8-sized-reads
branch
2 times, most recently
from
August 16, 2026 20:30
39c515d to
ff70ea5CompareCodecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@## main #65328 +/- ##
==========================================
- Coverage 90.13% 90.11% -0.03%
==========================================
Files 752 751 -1 Lines 251568 252570 +1002 Branches 47270 47526 +256 ==========================================
+ Hits 226759 227604 +845 - Misses 16168 16247 +79 - Partials 8641 8719 +78
🚀 New features to boost your workflow:
|
codebytere
requested review from
anonrig and avivkeller
and removed request for
RafaelGSSAugust 19, 2026 08:55
Uh oh!
There was an error while loading. Please reload this page.
codebytereforce-pushed
the
perf/fs-readfile-utf8-sized-reads
branch
from
August 20, 2026 17:18
ff70ea5 to
d47ea46Comparenodejs-github-bot
commented
Aug 21, 2026
Collaborator
nodejs-github-bot
commented
Aug 21, 2026
Collaborator
nodejs-github-bot
commented
Aug 21, 2026
Collaborator
codebytereforce-pushed
the
perf/fs-readfile-utf8-sized-reads
branch
from
August 22, 2026 14:46
d47ea46 to
f6970e8Comparenodejs-github-bot
commented
Aug 22, 2026
Collaborator
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>
codebytereforce-pushed
the
perf/fs-readfile-utf8-sized-reads
branch
from
August 22, 2026 18:39
f6970e8 to
60bf6b9Compareanonrig
approved these changes
Aug 22, 2026
nodejs-github-bot
commented
Aug 23, 2026
Collaborator
nodejs-github-bot
commented
Aug 23, 2026
Collaborator
Uh oh!
There was an error while loading. Please reload this page.
nodejs-github-bot
commented
Aug 24, 2026
Collaborator
Landed in 18fb35a |
aduh95 pushed a commit
that referenced
this pull request
Aug 25, 2026
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> PR-URL: #65328 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
aduh95 pushed a commit
that referenced
this pull request
Aug 25, 2026
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> PR-URL: #65328 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
aduh95 pushed a commit
that referenced
this pull request
Aug 27, 2026
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> PR-URL: #65328 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for freeto join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.(Linux x64, 30 runs. A 300 KB file goes from 39 syscalls to 5.)
ReadFileUtf8- behindreadFileSync(…, 'utf8')and therefore every CommonJS module load - read in 8 KiBread()s, appending each to astd::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 nofstat), then sized fromfstat()if that fills too, with geometric growth as the fallback. The size is only an allocation hint - reading continues untilread()returns 0, so procfs/sysfs and concurrently changing files behave as before andStringBytes::Encode()sees exactly the bytes read. Allocation failure throwsERR_MEMORY_ALLOCATION_FAILED.Tests:
test-fs-readfilesync-utf8-sizes.js(new): equalsreadFileSync(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/mapsand 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.