Skip to content

Commit eb2d416

Browse files
committed
fs: improve readFile performance
This increases the maximum buffer size per read to 512kb when using `fs.readFile`. This is important to improve the read performance for bigger files. PR-URL: #27063 Refs: #25741 Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Jamie Davis <[email protected]>
1 parent 97c0a34 commit eb2d416

File tree

2 files changed

+25
-21
lines changed

2 files changed

+25
-21
lines changed

lib/fs.js

+5-7
Original file line numberDiff line numberDiff line change
@@ -266,19 +266,17 @@ function readFileAfterStat(err, stats) {
266266

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

269-
if (size === 0) {
270-
context.buffers = [];
271-
context.read();
272-
return;
273-
}
274-
275269
if (size > kMaxLength) {
276270
err = new ERR_FS_FILE_TOO_LARGE(size);
277271
return context.close(err);
278272
}
279273

280274
try {
281-
context.buffer = Buffer.allocUnsafeSlow(size);
275+
if (size === 0) {
276+
context.buffers = [];
277+
} else {
278+
context.buffer = Buffer.allocUnsafeSlow(size);
279+
}
282280
} catch (err) {
283281
return context.close(err);
284282
}

lib/internal/fs/read_file_context.js

+20-14
Original file line numberDiff line numberDiff line change
@@ -6,27 +6,32 @@ const { Buffer } = require('buffer');
66

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

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

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

1421
if (err)
1522
return context.close(err);
1623

17-
if (bytesRead === 0)
18-
return context.close();
19-
2024
context.pos += bytesRead;
2125

22-
if (context.size !== 0) {
23-
if (context.pos === context.size)
24-
context.close();
25-
else
26-
context.read();
26+
if (context.pos === context.size || bytesRead === 0) {
27+
context.close();
2728
} else {
28-
// Unknown size, just read until we don't get bytes.
29-
context.buffers.push(context.buffer.slice(0, bytesRead));
29+
if (context.size === 0) {
30+
// Unknown size, just read until we don't get bytes.
31+
const buffer = bytesRead === kReadFileUnknownBufferLength ?
32+
context.buffer : context.buffer.slice(0, bytesRead);
33+
context.buffers.push(buffer);
34+
}
3035
context.read();
3136
}
3237
}
@@ -60,7 +65,7 @@ class ReadFileContext {
6065
constructor(callback, encoding) {
6166
this.fd = undefined;
6267
this.isUserFd = undefined;
63-
this.size = undefined;
68+
this.size = 0;
6469
this.callback = callback;
6570
this.buffers = null;
6671
this.buffer = null;
@@ -75,9 +80,10 @@ class ReadFileContext {
7580
let length;
7681

7782
if (this.size === 0) {
78-
buffer = this.buffer = Buffer.allocUnsafeSlow(kReadFileBufferLength);
83+
buffer = Buffer.allocUnsafeSlow(kReadFileUnknownBufferLength);
7984
offset = 0;
80-
length = kReadFileBufferLength;
85+
length = kReadFileUnknownBufferLength;
86+
this.buffer = buffer;
8187
} else {
8288
buffer = this.buffer;
8389
offset = this.pos;

0 commit comments

Comments
 (0)