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 5, 2021
1 parent 55493f2 commit 03d36bb
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 9 deletions.
3 changes: 1 addition & 2 deletions lib/internal/fs/promises.js
@@ -1,7 +1,5 @@
'use strict';

const kWriteFileMaxChunkSize = 2 ** 14;

const {
ArrayPrototypePush,
Error,
Expand Down Expand Up @@ -40,6 +38,7 @@ const {
kMaxUserId,
kReadFileBufferLength,
kReadFileUnknownBufferLength,
kWriteFileMaxChunkSize,
},
copyObject,
getDirents,
Expand Down
3 changes: 3 additions & 0 deletions lib/internal/fs/utils.js
Expand Up @@ -126,6 +126,8 @@ const kIoMaxLength = 2 ** 31 - 1;
const kReadFileUnknownBufferLength = 64 * 1024;
const kReadFileBufferLength = 512 * 1024;

const kWriteFileMaxChunkSize = 512 * 1024;

const kMaxUserId = 2 ** 32 - 1;

const isWindows = process.platform === 'win32';
Expand Down Expand Up @@ -835,6 +837,7 @@ module.exports = {
kMaxUserId,
kReadFileBufferLength,
kReadFileUnknownBufferLength,
kWriteFileMaxChunkSize,
},
assertEncoding,
BigIntStats, // for testing
Expand Down
18 changes: 11 additions & 7 deletions test/parallel/test-fs-promises-file-handle-writeFile.js
Expand Up @@ -34,13 +34,17 @@ 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 controller = new AbortController();
const { signal } = controller;
process.nextTick(() => controller.abort());
await assert.rejects(writeFile(fileHandle, buffer, { signal }), {
name: 'AbortError'
});
try {
const buffer = Buffer.from('dogs running'.repeat(512 * 1024), 'utf8');
const controller = new AbortController();
const { signal } = controller;
process.nextTick(() => controller.abort());
await assert.rejects(writeFile(fileHandle, buffer, { signal }), {
name: 'AbortError'
});
} finally {
await fileHandle.close();
}
}

const dest = path.resolve(tmpDir, 'tmp.txt');
Expand Down

0 comments on commit 03d36bb

Please sign in to comment.