-
Notifications
You must be signed in to change notification settings - Fork 29.8k
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
fs: use fast api calls for existsSync
#49893
Conversation
e5014d5
to
dd08af9
Compare
Can you resolve the conflict @littledivy? |
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.
Guess we have to add a test like in test/parallel/test-url-canParse-whatwg.js
?
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 |
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.
The fs operation part can be wrapped in a helper and shared with the slow callback to avoid getting out of sync.
// This test is to ensure that the v8 fast api works. | ||
const oneBytePath = 'hello.txt'; | ||
for (let i = 0; i < 1e5; i++) { | ||
assert(!fs.existsSync(oneBytePath)); |
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.
This should be moved to test/pummel
instead. But also, in general we need to avoid running tight loops in the tests to avoid introducing timeouts in the CI on the slower machines. Maybe it's already enough that the fast path is exercised in the benchmark..
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.
Can't we use V8 natives API like they do unit test fast calls? IIRC you need to %PrepareForOptimization(fn)
, call the function, %OptimizeOnNextCall(fn)
, and call it again. That last call should be optimized.
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.
Also, while I'm not good enough with C++ to suggest how to implement it, I think we can put something into place (maybe only in debug builds?). For example, the fast version, when called, increases some counter that we can get from JavaScript for an assertion.
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.
I think we can keep an array of booleans for all fast APIs to see if they are called, and expose them to the JS land, toggling a boolean shouldn't be very expensive.
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.
For this PR though doing %PrepareForOptimization()
and %OptimizeOnNextCall()
in the tests may be fine. But we also need to check in JS land if the optimizing compiler is enabled at all in the test to avoid failing on builds that turns optimizations off, which would be tricky..
Using `await fs.access` has couple of downsides. It creates unnecessary async contexts where async scope can be removed. Also, it creates the possibility of race conditions such as `Time-of-Check to Time-of-Use`. It would be nice if someone can benchmark this. I'm rooting for a performance improvement. Some updates from Node.js land: - There is an open pull request to add V8 Fast API to `existsSync` method - nodejs/node#49893 - Non-existing `existsSync` executions became 30% faster - nodejs/node#49593 Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
This needs a rebase. |
👋 Hey, this has approvals and a decent number of reviews, but currently has conflcits with the main branch. @aduh95 pointed out a few months ago that this needs a rebase, but no action has been taken since then. I've marked this PR as stalled for these reasons, LMK if this wasn't the right thing to do, and feel free to undo it :-). |
This issue/PR was marked as stalled, it will be automatically closed in 30 days. If it should remain open, please leave a comment explaining why it should remain open. |
Sorry, I haven't had a chance to rebase. Please feel free to take over and get this landed. |
You can always re-open, the stalled bot isn't working, so I was just doing some maintenance |
Currently, It takes the fast route when
path
string is represented as a OneByteString in V8.