fix(providence): [optimised-glob] use Array.concat for memory efficiency

This commit is contained in:
Thijs Louisse 2025-01-22 17:23:01 +01:00 committed by Thijs Louisse
parent a1d6dd90a8
commit 0f0991cd62
2 changed files with 17 additions and 7 deletions

View file

@ -0,0 +1,5 @@
---
'providence-analytics': patch
---
[optimised-glob] use Array.concat for memory efficiency

View file

@ -253,12 +253,16 @@ const getAllDirentsFromStartPath = memoize(
return dirents; return dirents;
} }
dirents.push( const direntResult = /** @type {* & DirentWithPath[]} */ (
// @ts-expect-error await fs.promises.readdir(startPath, {
...(await fs.promises.readdir(startPath, { withFileTypes: true, recursive: true })), withFileTypes: true,
// @ts-expect-error
recursive: true,
})
); );
cache[startPath] = dirents;
return dirents; cache[startPath] = direntResult;
return direntResult;
}, },
); );
@ -401,7 +405,7 @@ export async function optimisedGlob(globOrGlobs, providedOptions = {}) {
/** @type {RegExp[]} */ /** @type {RegExp[]} */
const matchRegexes = []; const matchRegexes = [];
/** @type {{dirent:nodeFs.Dirent;relativeToCwdPath:string}[]} */ /** @type {{dirent:nodeFs.Dirent;relativeToCwdPath:string}[]} */
const globEntries = []; let globEntries = [];
for (const glob of globs) { for (const glob of globs) {
const isNegative = glob.startsWith('!'); const isNegative = glob.startsWith('!');
@ -431,7 +435,8 @@ export async function optimisedGlob(globOrGlobs, providedOptions = {}) {
cwd, cwd,
}); });
globEntries.push(...allDirEntsRelativeToCwd); // N.B. `globEntries.push(...allDirEntsRelativeToCwd);` can give memory issues for large file trees
globEntries = globEntries.concat(allDirEntsRelativeToCwd);
} catch (e) { } catch (e) {
if (!options.suppressErrors) { if (!options.suppressErrors) {
throw e; throw e;