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: improve readFile performance #27063

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 5 additions & 7 deletions lib/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -264,19 +264,17 @@ function readFileAfterStat(err, stats) {

const size = context.size = isFileType(stats, S_IFREG) ? stats[8] : 0;

if (size === 0) {
context.buffers = [];
context.read();
return;
}

if (size > kMaxLength) {
err = new ERR_FS_FILE_TOO_LARGE(size);
return context.close(err);
}

try {
context.buffer = Buffer.allocUnsafeSlow(size);
if (size === 0) {
context.buffers = [];
} else {
context.buffer = Buffer.allocUnsafeSlow(size);
}
} catch (err) {
return context.close(err);
}
Expand Down
34 changes: 20 additions & 14 deletions lib/internal/fs/read_file_context.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,32 @@ const { Buffer } = require('buffer');

const { FSReqCallback, close, read } = internalBinding('fs');

const kReadFileBufferLength = 8 * 1024;
// Use 64kb in case the file type is not a regular file and thus do not know the
// actual file size. Increasing the value further results in more frequent over
// allocation for small files and consumes CPU time and memory that should be
// used else wise.
// Use up to 512kb per read otherwise to partition reading big files to prevent
// blocking other threads in case the available threads are all in use.
const kReadFileUnknownBufferLength = 64 * 1024;
const kReadFileBufferLength = 512 * 1024;

function readFileAfterRead(err, bytesRead) {
const context = this.context;

if (err)
return context.close(err);

if (bytesRead === 0)
return context.close();

context.pos += bytesRead;

if (context.size !== 0) {
if (context.pos === context.size)
context.close();
else
context.read();
if (context.pos === context.size || bytesRead === 0) {
context.close();
} else {
// Unknown size, just read until we don't get bytes.
context.buffers.push(context.buffer.slice(0, bytesRead));
if (context.size === 0) {
// Unknown size, just read until we don't get bytes.
const buffer = bytesRead === kReadFileUnknownBufferLength ?
context.buffer : context.buffer.slice(0, bytesRead);
context.buffers.push(buffer);
}
context.read();
}
}
Expand Down Expand Up @@ -58,7 +63,7 @@ class ReadFileContext {
constructor(callback, encoding) {
this.fd = undefined;
this.isUserFd = undefined;
this.size = undefined;
this.size = 0;
this.callback = callback;
this.buffers = null;
this.buffer = null;
Expand All @@ -73,9 +78,10 @@ class ReadFileContext {
let length;

if (this.size === 0) {
buffer = this.buffer = Buffer.allocUnsafeSlow(kReadFileBufferLength);
buffer = Buffer.allocUnsafeSlow(kReadFileUnknownBufferLength);
offset = 0;
length = kReadFileBufferLength;
length = kReadFileUnknownBufferLength;
this.buffer = buffer;
} else {
buffer = this.buffer;
offset = this.pos;
Expand Down