diff options
author | Ruben Bridgewater <ruben@bridgewater.de> | 2022-10-06 20:03:56 +0300 |
---|---|---|
committer | Danielle Adams <adamzdanielle@gmail.com> | 2022-10-11 16:08:29 +0300 |
commit | 67fb76519a054c0b5266e0bfb1a7600660893f2e (patch) | |
tree | 2f54e61dce49a0fb5b550b922bddcfe36af624fe /test | |
parent | 9c7e66478ca21746f749b5b433266e7bed857e24 (diff) |
fs: improve promise based readFile performance for big files
This significantly reduces the peak memory for the promise
based readFile operation by reusing a single memory chunk after
each read and strinigifying that chunk immediately.
Signed-off-by: Ruben Bridgewater <ruben@bridgewater.de>
PR-URL: https://github.com/nodejs/node/pull/44295
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Diffstat (limited to 'test')
-rw-r--r-- | test/parallel/test-fs-promises-readfile.js | 18 |
1 files changed, 15 insertions, 3 deletions
diff --git a/test/parallel/test-fs-promises-readfile.js b/test/parallel/test-fs-promises-readfile.js index a7fc46e5a65..fd1bfabf88d 100644 --- a/test/parallel/test-fs-promises-readfile.js +++ b/test/parallel/test-fs-promises-readfile.js @@ -1,3 +1,4 @@ +// Flags: --expose-internals 'use strict'; const common = require('../common'); @@ -6,15 +7,15 @@ const assert = require('assert'); const path = require('path'); const { writeFile, readFile } = require('fs').promises; const tmpdir = require('../common/tmpdir'); +const { internalBinding } = require('internal/test/binding'); +const fsBinding = internalBinding('fs'); tmpdir.refresh(); const fn = path.join(tmpdir.path, 'large-file'); // Creating large buffer with random content const largeBuffer = Buffer.from( - Array.apply(null, { length: 16834 * 2 }) - .map(Math.random) - .map((number) => (number * (1 << 8))) + Array.from({ length: 1024 ** 2 + 19 }, (_, index) => index) ); async function createLargeFile() { @@ -69,6 +70,16 @@ async function validateWrongSignalParam() { } +async function validateZeroByteLiar() { + const originalFStat = fsBinding.fstat; + fsBinding.fstat = common.mustCall( + () => (/* stat fields */ [0, 1, 2, 3, 4, 5, 6, 7, 0 /* size */]) + ); + const readBuffer = await readFile(fn); + assert.strictEqual(readBuffer.toString(), largeBuffer.toString()); + fsBinding.fstat = originalFStat; +} + (async () => { await createLargeFile(); await validateReadFile(); @@ -76,4 +87,5 @@ async function validateWrongSignalParam() { await validateReadFileAbortLogicBefore(); await validateReadFileAbortLogicDuring(); await validateWrongSignalParam(); + await validateZeroByteLiar(); })().then(common.mustCall()); |