Skip to content

Commit

Permalink
fs: read full size if known in promises.readFile
Browse files Browse the repository at this point in the history
If we have an estimate of the file size available from the previous
stat call, use that for the size of the first chunk to be read.
This increases performance by reading more data (and, most likely,
all data) at once without incurring memory overhead in most situations.

PR-URL: #37127
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Darshan Sen <raisinten@gmail.com>
Reviewed-By: Zijian Liu <lxxyxzj@gmail.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Juan José Arboleda <soyjuanarbol@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
  • Loading branch information
addaleax committed Feb 1, 2021
1 parent 186feb4 commit a126cb5
Showing 1 changed file with 6 additions and 5 deletions.
11 changes: 6 additions & 5 deletions lib/internal/fs/promises.js
Original file line number Diff line number Diff line change
Expand Up @@ -315,20 +315,21 @@ async function readFileHandle(filehandle, options) {
throw new ERR_FS_FILE_TOO_LARGE(size);

const chunks = [];
const chunkSize = size === 0 ?
kReadFileMaxChunkSize :
MathMin(size, kReadFileMaxChunkSize);
let isFirstChunk = true;
const firstChunkSize = size === 0 ? kReadFileMaxChunkSize : size;
const chunkSize = MathMin(firstChunkSize, kReadFileMaxChunkSize);
let endOfFile = false;
do {
if (signal?.aborted) {
throw lazyDOMException('The operation was aborted', 'AbortError');
}
const buf = Buffer.alloc(chunkSize);
const buf = Buffer.alloc(isFirstChunk ? firstChunkSize : chunkSize);
const { bytesRead, buffer } =
await read(filehandle, buf, 0, chunkSize, -1);
await read(filehandle, buf, 0, buf.length, -1);
endOfFile = bytesRead === 0;
if (bytesRead > 0)
ArrayPrototypePush(chunks, buffer.slice(0, bytesRead));
isFirstChunk = false;
} while (!endOfFile);

const result = chunks.length === 1 ? chunks[0] : Buffer.concat(chunks);
Expand Down

0 comments on commit a126cb5

Please sign in to comment.