Skip to content
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: add isFile and isDirectory methods #46345

Closed
wants to merge 6 commits into from

Conversation

debadree25
Copy link
Member

Attempted to add a isFile, isDirectory apis as per the referenced issue, basically this would avoid the creation of the stats object so I think this should provide somewhat of a speed up, I believe we could speedup from the cpp side too but not much luck in finding what to improve there, any help would be appreciated!

Refs: nodejs/performance#46

@debadree25
Copy link
Member Author

Attempting to add a benchmark, any preliminary reviews would be appreciated

cc @anonrig

lib/fs.js Outdated
Comment on lines 1591 to 1624
const stats = binding.stat(pathModule.toNamespacedPath(path), true, undefined, ctx);
if (hasNoEntryError(ctx)) {
return false;
}
return validateIfFileFromStats(stats);
Copy link
Member

Choose a reason for hiding this comment

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

What if a different error occurs?

Copy link
Member Author

Choose a reason for hiding this comment

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

Have updated the error handling

Copy link
Member

Choose a reason for hiding this comment

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

Same problem in isDirectory.

Copy link
Member Author

Choose a reason for hiding this comment

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

Fixed

@tniessen
Copy link
Member

basically this would avoid the creation of the stats object so I think this should provide somewhat of a speed up, I believe we could speedup from the cpp side too but not much luck in finding what to improve there

Such claims really need realistic benchmarks. I would expect sync disk I/O to far outweigh the cost of creating the JS object, except perhaps when repeatedly accessing the same disk block on an SSD in a loop.

This also promotes the anti-pattern of fs operations that result in race conditions.

@debadree25
Copy link
Member Author

debadree25 commented Jan 25, 2023

Such claims really need realistic benchmarks. I would expect sync disk I/O to far outweigh the cost of creating the JS object, except perhaps when repeatedly accessing the same disk block on an SSD in a loop.

Have added a benchmark for this, should also commit the comparison benchmark file?
also how could we make it realistic if you could say? maybe randomly open multiple files?

@debadree25
Copy link
Member Author

have run the benchmarks on my local machine the results look something like this:

fs/bench-isFile-stat.js
fs/bench-isFile-stat.js fileName="/Users/debadreechatterjee/Documents/Personal/node/benchmark/fs/bench-isFile-stat.js" n=1000000: 192,685.4785343927
fs/bench-isFile-stat.js fileName="./not-exist.txt" n=1000000: 444,116.0411600369

fs/bench-isFile.js
fs/bench-isFile.js fileName="/Users/debadreechatterjee/Documents/Personal/node/benchmark/fs/bench-isFile.js" n=1000000: 231,165.77255325063
fs/bench-isFile.js fileName="./not-exist.txt" n=1000000: 484,097.7516883041

So looks somewhat faster?

also i am unable to understand how to run the benchmark with statistics for proper comparison so any help for that would be appreciated

@debadree25
Copy link
Member Author

debadree25 commented Jan 25, 2023

This also promotes the anti-pattern of fs operations that result in race conditions.

couldnt get you here 😅😅 could you elaborate?

@tniessen
Copy link
Member

also how could we make it realistic if you could say? maybe randomly open multiple files?

That is going to be tricky. OS kernels are extremely effective at caching inodes and since this feature does not depend on actual file contents, any I/O during a benchmark will likely hit the inode cache. And even if it doesn't, it'll probably hit the kernel's disk cache. And if it doesn't, most SSDs maintain their own internal cache. In other words, as long as the benchmark accesses a predictable and/or smallish part of the disk only (and assuming the disk is not a shared medium, network-attached storage, etc.), it won't reflect actual I/O times.

This also promotes the anti-pattern of fs operations that result in race conditions.

couldnt get you here 😅😅 could you elaborate?

APIs such as fs.exists are problematic because they often introduce race conditions in programs. The result is often useless because the file might be deleted (or created) before the application is even able to observe the result. There are some justified use cases, of course, but it is an anti-pattern in general. See #39968 for a related discussion.

@debadree25
Copy link
Member Author

That is going to be tricky. OS kernels are extremely effective at caching inodes and since this feature does not depend on actual file contents, any I/O during a benchmark will likely hit the inode cache. And even if it doesn't, it'll probably hit the kernel's disk cache. And if it doesn't, most SSDs maintain their own internal cache. In other words, as long as the benchmark accesses a predictable and/or smallish part of the disk only (and assuming the disk is not a shared medium, network-attached storage, etc.), it won't reflect actual I/O times.

Thank you so much for explaining much appreciated

@debadree25
Copy link
Member Author

debadree25 commented Jan 25, 2023

APIs such as fs.exists are problematic because they often introduce race conditions in programs. The result is often useless because the file might be deleted (or created) before the application is even able to observe the result. There are some justified use cases, of course, but it is an anti-pattern in general. See #39968 for a related discussion.

Went through the discussion it seems like its DX vs Discouraging anti-patterns issue and I don't think there is any consensus on that yet?
so should I close this PR or leave it open for any discussions for now?

@debadree25
Copy link
Member Author

Also for the original intent of the issue nodejs/performance#46 (comment) of speeding up checking during module resolution do you think this could speed it up? since (i think please correct me if am wrong 😅) during module resolution, we would be accessing predictable parts of the disk multiple times?

Copy link
Contributor

@cjihrig cjihrig left a comment

Choose a reason for hiding this comment

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

I'll wait for benchmark numbers, but I really don't think we should add these as public API.

lib/fs.js Outdated
Comment on lines 3005 to 3006
isFile,
isDirectory,
Copy link
Contributor

Choose a reason for hiding this comment

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

It seems like these should have "sync" as part of their name.

Copy link
Member Author

Choose a reason for hiding this comment

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

Refactoring

Copy link
Member Author

Choose a reason for hiding this comment

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

Done

Copy link
Member Author

Choose a reason for hiding this comment

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

Also, could you say how I could post the comparison benchmarks?

@RaisinTen
Copy link
Contributor

during module resolution

If you're referring to the ESM loader alone, I wonder why we aren't using the InternalModuleStat function there like we are using it in the CJS loader. Maybe that would speed things up without exposing more public APIs 🤔

@debadree25
Copy link
Member Author

during module resolution

If you're referring to the ESM loader alone, I wonder why we aren't using the InternalModuleStat function there like we are using it in the CJS loader. Maybe that would speed things up without exposing more public APIs 🤔

Oh ok didn't know this could explore in a separate PR I guess, I created this one for the general comment in nodejs/performance#46 (comment)

@debadree25
Copy link
Member Author

Ok I tried replacing the implementation of isFileSync with InternalModuleStat the results seem to be significantly faster now especially, if a file doesn't exist

Running on my local machine:
using isFileSync:

fs/bench-isFile.js fileName="/Users/debadreechatterjee/Documents/Personal/node/benchmark/fs/bench-isFile.js" n=1000000: 261,749.895896177
fs/bench-isFile.js fileName="./not-exist.txt" n=1000000: 729,323.8410444844

and using statSync().isFile()

fs/bench-isFile-stat.js fileName="/Users/debadreechatterjee/Documents/Personal/node/benchmark/fs/bench-isFile-stat.js" n=1000000: 198,631.47703315382
fs/bench-isFile-stat.js fileName="./not-exist.txt" n=1000000: 467,543.81614458084

@RaisinTen

@debadree25
Copy link
Member Author

since there isn't any consensus on making this change yet, and also the benchmark perf isn't hugely better closing this for now

I think it may be possible to improve the stats object itself it was mentioned in nodejs/performance#39 (comment) but not sure how to do that 😅 but it seems that way can really yield perf gains ex of bun https://twitter.com/jarredsumner/status/1622839218094641152

@debadree25 debadree25 closed this Feb 15, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants