From f283df5497e0d4387911359a529fafa979a7bb14 Mon Sep 17 00:00:00 2001 From: Nitzan Uziely Date: Fri, 5 Mar 2021 14:37:57 +0200 Subject: [PATCH] fs: improve fsPromises writeFile performance Increase the write chunk size in fsPromises writeFile to improve performance. PR-URL: https://github.com/nodejs/node/pull/37610 Reviewed-By: James M Snell Reviewed-By: Darshan Sen Reviewed-By: Benjamin Gruenbaum Reviewed-By: Anna Henningsen Reviewed-By: Antoine du Hamel --- lib/internal/fs/promises.js | 4 +++- lib/internal/fs/utils.js | 5 +++++ test/parallel/test-fs-promises-file-handle-writeFile.js | 2 +- 3 files changed, 9 insertions(+), 2 deletions(-) diff --git a/lib/internal/fs/promises.js b/lib/internal/fs/promises.js index 39418446bd9c15..16efc1c6398abb 100644 --- a/lib/internal/fs/promises.js +++ b/lib/internal/fs/promises.js @@ -7,7 +7,6 @@ const kIoMaxLength = 2 ** 31 - 1; // Note: This is different from kReadFileBufferLength used for non-promisified // fs.readFile. const kReadFileMaxChunkSize = 2 ** 14; -const kWriteFileMaxChunkSize = 2 ** 14; // 2 ** 32 - 1 const kMaxUserId = 4294967295; @@ -44,6 +43,9 @@ const { const { isArrayBufferView } = require('internal/util/types'); const { rimrafPromises } = require('internal/fs/rimraf'); const { + constants: { + kWriteFileMaxChunkSize + }, copyObject, getDirents, getOptions, diff --git a/lib/internal/fs/utils.js b/lib/internal/fs/utils.js index d3f4c621e4dd56..6f30d1b020da9f 100644 --- a/lib/internal/fs/utils.js +++ b/lib/internal/fs/utils.js @@ -113,6 +113,8 @@ const kMaximumCopyMode = COPYFILE_EXCL | COPYFILE_FICLONE | COPYFILE_FICLONE_FORCE; +const kWriteFileMaxChunkSize = 512 * 1024; + const isWindows = process.platform === 'win32'; let fs; @@ -815,6 +817,9 @@ const validatePosition = hideStackFrames((position, name) => { }); module.exports = { + constants: { + kWriteFileMaxChunkSize + }, assertEncoding, BigIntStats, // for testing copyObject, diff --git a/test/parallel/test-fs-promises-file-handle-writeFile.js b/test/parallel/test-fs-promises-file-handle-writeFile.js index a4ae7fd054b53d..3f5e9a20c4d470 100644 --- a/test/parallel/test-fs-promises-file-handle-writeFile.js +++ b/test/parallel/test-fs-promises-file-handle-writeFile.js @@ -31,7 +31,7 @@ async function validateWriteFile() { async function doWriteAndCancel() { const filePathForHandle = path.resolve(tmpDir, 'dogs-running.txt'); const fileHandle = await open(filePathForHandle, 'w+'); - const buffer = Buffer.from('dogs running'.repeat(10000), 'utf8'); + const buffer = Buffer.from('dogs running'.repeat(512 * 1024), 'utf8'); const controller = new AbortController(); const { signal } = controller; process.nextTick(() => controller.abort());