-
-
Notifications
You must be signed in to change notification settings - Fork 34.7k
src: consolidate C++ ReadFileSync/WriteFileSync utilities #61662
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
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.
|
Review requested:
|
|
cc @nodejs/cpp-reviewers |
Codecov Report❌ Patch coverage is
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
🚀 New features to boost your workflow:
|
Qard
left a comment
There was a problem hiding this 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>
|
Thanks for the suggestion. @Qard @RafaelGSS Can you take a look again? |
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. PR-URL: #61662 Reviewed-By: Rafael Gonzaga <rafael.nunu@hotmail.com> Reviewed-By: Stephen Belanger <admin@stephenbelanger.com> Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: James M Snell <jasnell@gmail.com>
|
Landed in 9bc369f...3ab9dd8 |
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. PR-URL: #61662 Reviewed-By: Rafael Gonzaga <rafael.nunu@hotmail.com> Reviewed-By: Stephen Belanger <admin@stephenbelanger.com> Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: James M Snell <jasnell@gmail.com>
This patch moves
ReadFileSyncandWriteFileSyncfromsrc/util.cctosrc/node_file_utils.cc, consolidates the implementation to reuse code, and adds a few more enhancements:For
ReadFileSync:For
WriteFileSync: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
ReadFileSyncto 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).