-
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.promises.readFile is 40% slower than fs.readFile #37583
Comments
Would you be interested in sending a PR to improve the implementation? |
But no one said |
Well that's right, but no one said it had to be slower either. I think all code should always be as fast as possible. |
My guess is that the promise version is idling when IO capacity is available due to scheduling? Is it implemented in an event driven way internally, and if it is (for example using epoll) is the epoll event able to interrupt and cause the promise to continue to be evaluated or does it have to wait for the vm to get around to scheduling it again? |
What about other resources(CPU/memory) consumption? |
I dont think it's that, but cant be discarded. The best option here it's to reproduce. |
Note that it looks like there are some other differences as well, although I'm not sure how much they affect performance:
Also, the 'utf8' in your |
This signals the benchmark flaw. as it's same to stating that Promises naturally will always be slower than callbacks (extra objects to garbage collect and artificial ticks are not free)
I can expect it when running 1 to 1, but I'd expect callback version to be more efficient (faster) if we start to process multiple files and take advantage of parallel execution. If that's not the case, I'd see it as other flaw on Node.js side |
This is an amazing finding, @Jarred-Sumner |
Improve the fsPromises readFile performance by allocating only one buffer, when size is known, increase the size of the readbuffer chunks, and dont read more data if size bytes have been read refs: nodejs#37583
Quite sure it's npm package "benchmark", sorry if I did not understand your question and you meant something else |
Improve the fsPromises readFile performance by allocating only one buffer, when size is known, increase the size of the readbuffer chunks, and dont read more data if size bytes have been read Refs: #37583 PR-URL: #37608 Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Anna Henningsen <anna@addaleax.net>
Improve the fsPromises readFile performance by allocating only one buffer, when size is known, increase the size of the readbuffer chunks, and dont read more data if size bytes have been read Refs: #37583 PR-URL: #37608 Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Anna Henningsen <anna@addaleax.net>
Improve the fsPromises readFile performance by allocating only one buffer, when size is known, increase the size of the readbuffer chunks, and dont read more data if size bytes have been read refs: nodejs#37583
Improve the fsPromises readFile performance by allocating only one buffer, when size is known, increase the size of the readbuffer chunks, and dont read more data if size bytes have been read refs: nodejs#37583 Backport-PR-URL: nodejs#37608
Improve the fsPromises readFile performance by allocating only one buffer, when size is known, increase the size of the readbuffer chunks, and dont read more data if size bytes have been read refs: nodejs#37583 Backport-PR-URL: nodejs#37703 PR-URL: nodejs#37608
Improve the fsPromises readFile performance by allocating only one buffer, when size is known, increase the size of the readbuffer chunks, and dont read more data if size bytes have been read refs: nodejs#37583 Backport-PR-URL: nodejs#37703 PR-URL: nodejs#37608
Hello, is there something I can do to get the changes for this into node v14, or is this blocked by something else? |
@targos I thought that this was already backported to Node 14? If not, I'll reopen my backport PR that I closed |
@Linkgoron the original PR is marked backport-requested. In that case, there are two options:
|
@targos Thanks, originally it didn't land cleanly because of the AbortSignal stuff, but I think that now it should as you backported the AbortSignal stuff (that's why I thought that it was automatically merged). I'll check that it works out, and if it doesn't I'll reopen my backport PR. |
Improve the fsPromises readFile performance by allocating only one buffer, when size is known, increase the size of the readbuffer chunks, and dont read more data if size bytes have been read. Also moves constants to internal/fs/utils. refs: nodejs#37583 (Old) Backport-PR-URL: nodejs#37703 PR-URL: nodejs#37608
Improve the fsPromises readFile performance by allocating only one buffer, when size is known, increase the size of the readbuffer chunks, and dont read more data if size bytes have been read. Also moves constants to internal/fs/utils. refs: nodejs#37583 (Old) Backport-PR-URL: nodejs#37703 PR-URL: nodejs#37608
Improve the fsPromises readFile performance by allocating only one buffer, when size is known, increase the size of the readbuffer chunks, and dont read more data if size bytes have been read Refs: nodejs#37583 PR-URL: nodejs#37608 Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Anna Henningsen <anna@addaleax.net>
Improve the fsPromises readFile performance by allocating only one buffer, when size is known, increase the size of the readbuffer chunks, and dont read more data if size bytes have been read Refs: nodejs#37583 PR-URL: nodejs#37608 Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Anna Henningsen <anna@addaleax.net>
Improve the fsPromises readFile performance by allocating only one buffer, when size is known, increase the size of the readbuffer chunks, and dont read more data if size bytes have been read Refs: nodejs#37583 PR-URL: nodejs#37608 Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Anna Henningsen <anna@addaleax.net>
Improve the fsPromises readFile performance by allocating only one buffer, when size is known, increase the size of the readbuffer chunks, and dont read more data if size bytes have been read Refs: nodejs#37583 PR-URL: nodejs#37608 Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Anna Henningsen <anna@addaleax.net>
Improve the fsPromises readFile performance by allocating only one buffer, when size is known, increase the size of the readbuffer chunks, and dont read more data if size bytes have been read Refs: nodejs#37583 PR-URL: nodejs#37608 Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Anna Henningsen <anna@addaleax.net>
Improve the fsPromises readFile performance by allocating only one buffer, when size is known, increase the size of the readbuffer chunks, and dont read more data if size bytes have been read Refs: #37583 PR-URL: #37608 Backport-PR-URL: #39838 Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Anna Henningsen <anna@addaleax.net>
As observed in nodejs/node#37583 this is a fair bit faster than the promises version.
I guess it's fixed? Feel free to (or ask to) reopen if it isn't. |
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as spam.
This comment was marked as spam.
This comment was marked as off-topic.
This comment was marked as off-topic.
bro fixed it in bun |
Hey I'm locking this thread for non contributors leaving off topic comments. I also blocked the last person who left an off topic comment. |
What steps will reproduce the bug?
Run this benchmark on a 1 MB file (
big.file
):To create a 1 MB file (~40% slower):
To create a 20 KB file (~55% slower):
How often does it reproduce? Is there a required condition?
Always.
What is the expected behavior?
fs.promises.readFile
should perform similarly tofs.readFile
What do you see instead?
Additional information
I suspect the cause is right here: https://github.com/nodejs/node/blob/master/lib/internal/fs/promises.js#L319-L339
Instead of creating a new
Buffer
for each chunk, it could allocate a single Buffer and write to that buffer. I don't thinkBuffer.concat
or temporary arrays are necessary.The text was updated successfully, but these errors were encountered: