| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
Sorry, something went wrong.
Codecov Report❌ Patch coverage is 78.26087% with 10 lines in your changes missing coverage. Please review.
@@ 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
... and 121 files with indirect coverage changes 🚀 New features to boost your workflow:
|
Sorry, something went wrong.
There was a problem hiding this comment.
lgtm
Sorry, something went wrong.
Sorry, something went wrong.
Sorry, something went wrong.
Sorry, something went wrong.
Sorry, something went wrong.
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>
Sorry, something went wrong.
Sorry, something went wrong.
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>
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>
| Back | FazBrowse Home | New Git URL |
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 - 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.