News for hackers. Stuff that matters.
Re:fs.promises.readFile is 40% slower than fs.readFile by wcarss on Wednesday March 03, 2021 @06:12PM (#26336174)
I'm brand new to the code, but I don't think it is as simple as you state.

The 'one function' you linked to just passes a ReadFileContext into binding.open[1]. The ReadFileContext appears to have its own read that uses its own allocated Buffers[2] under some circumstances, but under others they are mysteriously already there[3]. Then binding.open refers to an internalBinding of fs[4], which I'm unsure of, is that js or C++ code? This[5] suggests C++ is involved, but there is also an open in lib/fs.js[6] with that signature, though it also just calls binding.open at the end with the same signature, so I think it's off into C++ by then. At that point, I lose the trail. Maybe C++ is even doing the allocating.

This is not easy code to grok in its full extent.

1 - https://github.com/nodejs/node/blob/master/lib/fs.js#L356

2 - https://github.com/nodejs/node/blob/master/lib/internal/fs/r...

3 - https://github.com/nodejs/node/blob/master/lib/internal/fs/r...

4 - https://github.com/nodejs/node/blob/master/lib/fs.js#L74

5 - https://github.com/nodejs/node/blob/3b2863db12818778671713b5...

6 - https://github.com/nodejs/node/blob/master/lib/fs.js#L472

edit: I was curious so I kept reading, It seems like open is probably C++ here[7], which makes an AsyncCall or SyncCall to "open", which are just wrappers around syscalls[8]. Still not sure about other places the buffer comes from, though.

7 - https://github.com/nodejs/node/blob/master/src/node_file.cc#...

8 - https://github.com/nodejs/node/blob/606df7c4e79324b9725bfcfe...

There are no comments on this comment yet.