Skip to content

fs: optimize fs.cpSync js calls - #53614

Merged
nodejs-github-bot merged 1 commit into
nodejs:mainfrom
anonrig:optimize-cp-sync
Jul 22, 2024
Merged

fs: optimize fs.cpSync js calls#53614
nodejs-github-bot merged 1 commit into
nodejs:mainfrom
anonrig:optimize-cp-sync

Conversation

@anonrig

@anonriganonrig commented Jun 27, 2024

Copy link
Copy Markdown
Member

#53612 is required to do a benchmark

A different approach to #53541

cc @nodejs/performance @nodejs/cpp-reviewers @billywhizz@jasnell @nodejs/fs

Benchmark CI: https://ci.nodejs.org/view/Node.js%20benchmark/job/benchmark-node-micro-benchmarks/1573/

Small win on happy path, and probably high win on error path:

 confidence improvement accuracy (*) (**) (***)
fs/bench-cpSync.js n=1 *** 21.96 % ±6.85% ±9.14% ±11.97%
fs/bench-cpSync.js n=100 *** 14.91 % ±2.86% ±3.81% ±4.97%
fs/bench-cpSync.js n=10000 *** 9.74 % ±1.28% ±1.71% ±2.23%

@nodejs-github-botnodejs-github-bot added c++ Issues and PRs that require attention from people who are familiar with C++. lib / src Issues and PRs related to general changes in the lib or src directory. needs-ci PRs that need a full CI run. labels Jun 27, 2024
@anonrig
anonrig requested a review from lemireJune 27, 2024 18:44
@anonrig
anonrigforce-pushed the optimize-cp-sync branch 2 times, most recently from bebf131 to 2d5fb50CompareJune 27, 2024 21:23
@anonrig

Copy link
Copy Markdown
MemberAuthor

cc @nodejs/tsc @nodejs/fs appreciate your input on which path to take.

previous pr:

  • is a sem-ver major
  • introduces 2 different implementations (js only and cpp)
  • had limitations due to std::filesystem::copy

this pr:

  • is not a sem-ver major
  • optimizes all flags passed to cpSync
  • optimizes error cases as same as the previous pr
  • has a lot more optimization to do

@nodejs-github-bot

This comment was marked as outdated.

@mcollinamcollina left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

@aduh95

Copy link
Copy Markdown
Contributor

Windows compilation is failing:

C:\workspace\node-compile-windows-debug\node\deps\v8\src\base\bits.h(477,31): warning C4146: unary minus operator applied to unsigned type, result still unsigned [C:\workspace\node-compile-windows-debug\node\tools\v8_gypfiles\mksnapshot.vcxproj]
embedded-file-writer.cc
mksnapshot.cc
embedded-empty.cc
platform-embedded-file-writer-base.cc
platform-embedded-file-writer-generic.cc
platform-embedded-file-writer-win.cc
platform-embedded-file-writer-aix.cc
platform-embedded-file-writer-mac.cc
snapshot-empty.cc
static-roots-gen.cc
Creating library ..\..\out\Debug\mksnapshot.lib and object ..\..\out\Debug\mksnapshot.exp
mksnapshot.vcxproj -> ..\..\out\Debug\\mksnapshot.exe
generating: "..\..\out\Debug\obj\v8_snapshot\/snapshot.cc" "..\..\out\Debug\obj\v8_snapshot\/embedded.S"
Assembling ..\..\out\Debug\obj\v8_snapshot\\embedded.S...
setup-isolate-deserialize.cc
C:\workspace\node-compile-windows-debug\node\deps\v8\src\base\bits.h(477,31): warning C4146: unary minus operator applied to unsigned type, result still unsigned [C:\workspace\node-compile-windows-debug\node\tools\v8_gypfiles\v8_snapshot.vcxproj]
snapshot.cc
C:\workspace\node-compile-windows-debug\node\deps\v8\src\base\bits.h(477,31): warning C4146: unary minus operator applied to unsigned type, result still unsigned [C:\workspace\node-compile-windows-debug\node\tools\v8_gypfiles\v8_snapshot.vcxproj]
v8_snapshot.vcxproj -> ..\..\out\Debug\lib\v8_snapshot.lib

@anonrig

Copy link
Copy Markdown
MemberAuthor

@nodejs/platform-windows any idea why this build is failing?

@H4ad

H4ad commented Jul 17, 2024

Copy link
Copy Markdown
Member

Building locally, I got this:

C:\Users\vinic\Desktop\Projects\node\src\node_file.cc(3036,15): error C2664: 'void node::Environment::ThrowUVException(int,const char *,const char *,const char *,const char *)': cannot convert argument 4 from
'const std::filesystem::path::value_type *' to 'const char *' [C:\Users\vinic\Desktop\Projects\node\libnode.vcxproj]

@StefanStojanovic

Copy link
Copy Markdown
Contributor

Building locally, I got this:

C:\Users\vinic\Desktop\Projects\node\src\node_file.cc(3036,15): error C2664: 'void node::Environment::ThrowUVException(int,const char *,const char *,const char *,const char *)': cannot convert argument 4 from
'const std::filesystem::path::value_type *' to 'const char *' [C:\Users\vinic\Desktop\Projects\node\libnode.vcxproj]

This is also in the log from the CI. Based on the error message, this looks very similar to what we had in #53600 because std::filesystem::path::value_type is different on POSIX and Windows.

@anonrig
anonrig requested a review from jasnellJuly 21, 2024 21:17
@nodejs-github-bot

This comment was marked as outdated.

@anonrig

Copy link
Copy Markdown
MemberAuthor

This is also in the log from the CI. Based on the error message, this looks very similar to what we had in #53600 because std::filesystem::path::value_type is different on POSIX and Windows.

It seems I forgot a src_path.c_str() there. Thank you! @StefanStojanovic@H4ad

@anonriganonrig added the author ready PRs that have at least one approval, no pending requests for changes, and a CI started. label Jul 21, 2024
@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

@jasnelljasnell left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Still LGTM

@anonriganonrig added the commit-queue Add this label to land a pull request using GitHub Actions. label Jul 22, 2024
@nodejs-github-botnodejs-github-bot removed the commit-queue Add this label to land a pull request using GitHub Actions. label Jul 22, 2024
@nodejs-github-bot
nodejs-github-bot merged commit 88027e8 into nodejs:mainJul 22, 2024
@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

Landed in 88027e8

targos pushed a commit that referenced this pull request Jul 30, 2024
PR-URL: #53614
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
@RafaelGSSRafaelGSS mentioned this pull request Jul 30, 2024
RafaelGSS pushed a commit that referenced this pull request Aug 5, 2024
PR-URL: #53614
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Comment threadsrc/node_file.cc
? std::filesystem::symlink_status(src_path, error_code)
: std::filesystem::status(src_path, error_code);
if (error_code) {
return env->ThrowUVException(EEXIST, "lstat", nullptr, src.out());

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks wrong, the error code should be properly translated instead of being directly mapped to EEXIST, there are many other reasons why this could fail.

Comment threadsrc/node_file.cc
ToNamespacedPath(env, &src);
THROW_IF_INSUFFICIENT_PERMISSIONS(
env, permission::PermissionScope::kFileSystemRead, src.ToStringView());
auto src_path = std::filesystem::path(src.ToStringView());

@joyeecheungjoyeecheungAug 22, 2024

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BufferValue converts the string to UTF8, on Windows this requires automatic conversion for char8_t to work which may not be supported by the version of MSVC we use, I think that may be the source of bugs like #54476

@joyeecheungjoyeecheungAug 22, 2024

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually no, this is simply broken on Windows, because this just gets returned in char. It needs to be returned as std::u8string/std::u8string_view instead.

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

Labels

author readyPRs that have at least one approval, no pending requests for changes, and a CI started.c++Issues and PRs that require attention from people who are familiar with C++.lib / srcIssues and PRs related to general changes in the lib or src directory.needs-ciPRs that need a full CI run.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

10 participants

@anonrig@nodejs-github-bot@aduh95@H4ad@StefanStojanovic@mcollina@jasnell@benjamingr@joyeecheung@targos