Skip to content

Commit

Permalink
fs: improve fsPromises writeFile performance
Browse files Browse the repository at this point in the history
Increase the write chunk size in fsPromises writeFile
to improve performance.

PR-URL: nodejs#37610
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Darshan Sen <raisinten@gmail.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
  • Loading branch information
Linkgoron authored and MoritzLoewenstein committed Sep 4, 2021
1 parent f0b7b93 commit f283df5
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 2 deletions.
4 changes: 3 additions & 1 deletion lib/internal/fs/promises.js
Expand Up @@ -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;
Expand Down Expand Up @@ -44,6 +43,9 @@ const {
const { isArrayBufferView } = require('internal/util/types');
const { rimrafPromises } = require('internal/fs/rimraf');
const {
constants: {
kWriteFileMaxChunkSize
},
copyObject,
getDirents,
getOptions,
Expand Down
5 changes: 5 additions & 0 deletions lib/internal/fs/utils.js
Expand Up @@ -113,6 +113,8 @@ const kMaximumCopyMode = COPYFILE_EXCL |
COPYFILE_FICLONE |
COPYFILE_FICLONE_FORCE;

const kWriteFileMaxChunkSize = 512 * 1024;

const isWindows = process.platform === 'win32';

let fs;
Expand Down Expand Up @@ -815,6 +817,9 @@ const validatePosition = hideStackFrames((position, name) => {
});

module.exports = {
constants: {
kWriteFileMaxChunkSize
},
assertEncoding,
BigIntStats, // for testing
copyObject,
Expand Down
2 changes: 1 addition & 1 deletion test/parallel/test-fs-promises-file-handle-writeFile.js
Expand Up @@ -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());
Expand Down

0 comments on commit f283df5

Please sign in to comment.