From 69402522fddac2073a16c87a59aab11ca8bd1f25 Mon Sep 17 00:00:00 2001 From: Anna Henningsen Date: Fri, 29 Jan 2021 15:14:09 +0100 Subject: [PATCH] fs: read full size if known in promises.readFile MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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: https://github.com/nodejs/node/pull/37127 Reviewed-By: Antoine du Hamel Reviewed-By: Colin Ihrig Reviewed-By: Darshan Sen Reviewed-By: Zijian Liu Reviewed-By: Benjamin Gruenbaum Reviewed-By: Juan José Arboleda Reviewed-By: Luigi Pinca Reviewed-By: James M Snell --- lib/internal/fs/promises.js | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/lib/internal/fs/promises.js b/lib/internal/fs/promises.js index 951e4ac214a9f2..4b568b52b8d106 100644 --- a/lib/internal/fs/promises.js +++ b/lib/internal/fs/promises.js @@ -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);