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: #37610
Backport-PR-URL: #39993
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 targos committed Sep 7, 2021
1 parent 007f9a0 commit d2834fb
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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 d2834fb

Please sign in to comment.