-
-
Notifications
You must be signed in to change notification settings - Fork 32.2k
fs: use fast api calls for existsSync
#49893
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
Closed
Closed
Changes from 2 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
f413971
fs: improve performance of `existsSync`
littledivy dd08af9
Format
littledivy ac4be44
Merge branch 'main' into perf-existsSync
littledivy 69736c2
Add test for fast call
littledivy 176cbdc
Fix lint js
littledivy 8d331ed
Add test for true existsSync
littledivy 7a37e48
Use SetFastMethod
littledivy e2e7341
Format
littledivy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,6 +31,7 @@ | |
#include "node_stat_watcher.h" | ||
#include "permission/permission.h" | ||
#include "util-inl.h" | ||
#include "v8-fast-api-calls.h" | ||
|
||
#include "tracing/trace_event.h" | ||
|
||
|
@@ -57,8 +58,11 @@ namespace fs { | |
|
||
using v8::Array; | ||
using v8::BigInt; | ||
using v8::CFunction; | ||
using v8::Context; | ||
using v8::EscapableHandleScope; | ||
using v8::FastApiCallbackOptions; | ||
using v8::FastOneByteString; | ||
using v8::Function; | ||
using v8::FunctionCallbackInfo; | ||
using v8::FunctionTemplate; | ||
|
@@ -1095,6 +1099,46 @@ static void ExistsSync(const FunctionCallbackInfo<Value>& args) { | |
args.GetReturnValue().Set(err == 0); | ||
} | ||
|
||
bool FastExistsSync(v8::Local<v8::Object> recv, | ||
const FastOneByteString& string, | ||
// NOLINTNEXTLINE(runtime/references) This is V8 api. | ||
FastApiCallbackOptions& options) { | ||
Environment* env = Environment::GetCurrent(recv->GetCreationContextChecked()); | ||
|
||
MaybeStackBuffer<char> path; | ||
|
||
path.AllocateSufficientStorage(string.length + 1); | ||
memcpy(path.out(), string.data, string.length); | ||
path.SetLengthAndZeroTerminate(string.length); | ||
|
||
if (UNLIKELY(!env->permission()->is_granted( | ||
permission::PermissionScope::kFileSystemRead, path.ToStringView()))) { | ||
options.fallback = true; | ||
return false; | ||
} | ||
|
||
uv_fs_t req; | ||
auto make = OnScopeLeave([&req]() { uv_fs_req_cleanup(&req); }); | ||
FS_SYNC_TRACE_BEGIN(access); | ||
int err = uv_fs_access(nullptr, &req, path.out(), 0, nullptr); | ||
FS_SYNC_TRACE_END(access); | ||
|
||
#ifdef _WIN32 | ||
// In case of an invalid symlink, `uv_fs_access` on win32 | ||
// will **not** return an error and is therefore not enough. | ||
// Double check with `uv_fs_stat()`. | ||
if (err == 0) { | ||
FS_SYNC_TRACE_BEGIN(stat); | ||
err = uv_fs_stat(nullptr, &req, path.out(), nullptr); | ||
FS_SYNC_TRACE_END(stat); | ||
} | ||
#endif // _WIN32 | ||
anonrig marked this conversation as resolved.
Show resolved
Hide resolved
Comment on lines
+1120
to
+1135
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The fs operation part can be wrapped in a helper and shared with the slow callback to avoid getting out of sync. |
||
|
||
return err == 0; | ||
} | ||
|
||
CFunction fast_exists_sync_(CFunction::Make(FastExistsSync)); | ||
|
||
// Used to speed up module loading. Returns an array [string, boolean] | ||
static void InternalModuleReadJSON(const FunctionCallbackInfo<Value>& args) { | ||
Environment* env = Environment::GetCurrent(args); | ||
|
@@ -3364,7 +3408,8 @@ static void CreatePerIsolateProperties(IsolateData* isolate_data, | |
SetMethodNoSideEffect(isolate, target, "accessSync", AccessSync); | ||
SetMethod(isolate, target, "close", Close); | ||
SetMethod(isolate, target, "closeSync", CloseSync); | ||
SetMethodNoSideEffect(isolate, target, "existsSync", ExistsSync); | ||
SetFastMethodNoSideEffect( | ||
tniessen marked this conversation as resolved.
Show resolved
Hide resolved
tniessen marked this conversation as resolved.
Show resolved
Hide resolved
|
||
isolate, target, "existsSync", ExistsSync, &fast_exists_sync_); | ||
SetMethod(isolate, target, "open", Open); | ||
SetMethod(isolate, target, "openSync", OpenSync); | ||
SetMethod(isolate, target, "openFileHandle", OpenFileHandle); | ||
|
@@ -3490,6 +3535,8 @@ void RegisterExternalReferences(ExternalReferenceRegistry* registry) { | |
registry->Register(Close); | ||
registry->Register(CloseSync); | ||
registry->Register(ExistsSync); | ||
registry->Register(FastExistsSync); | ||
registry->Register(fast_exists_sync_.GetTypeInfo()); | ||
registry->Register(Open); | ||
registry->Register(OpenSync); | ||
registry->Register(OpenFileHandle); | ||
|
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.
Uh oh!
There was an error while loading. Please reload this page.