Skip to content

Commit e2c4b66

Browse files
Merge pull request #1209 from gemini-testing/TESTPLANE-903.restrict_selectivity_fs_parallelism
fix(selectivity): reduce master-thread RAM usage
2 parents 474d074 + d989eaf commit e2c4b66

9 files changed

Lines changed: 121 additions & 108 deletions

File tree

package-lock.json

Lines changed: 64 additions & 70 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@
9595
"looks-same": "10.0.1",
9696
"micromatch": "4.0.5",
9797
"mocha": "10.2.0",
98+
"p-limit": "3.1.0",
9899
"pirates": "4.0.7",
99100
"plugins-loader": "1.3.4",
100101
"png-validator": "1.1.0",

src/browser/cdp/selectivity/fs-cache.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import os from "node:os";
22
import path from "node:path";
3+
import pLimit from "p-limit";
34
import lockfile from "proper-lockfile";
45
import fs from "fs-extra";
56
import { getMD5 } from "../../../utils/crypto";
@@ -16,13 +17,16 @@ type CacheTypeValue = (typeof CacheType)[keyof typeof CacheType];
1617
const processStartTime = Number(new Date());
1718
const tmpDir = path.join(os.tmpdir(), SELECTIVITY_CACHE_DIRECTIRY);
1819

20+
// https://nodejs.org/api/cli.html#uv_threadpool_sizesize
21+
const libUVLimited = pLimit((process.env.UV_THREADPOOL_SIZE && Number(process.env.UV_THREADPOOL_SIZE)) || 16);
22+
1923
const ensureSelectivityCacheDirectory = async (): Promise<void> => {
20-
await fs.ensureDir(tmpDir);
24+
await libUVLimited(() => fs.ensureDir(tmpDir));
2125
};
2226

2327
const wasModifiedAfterProcessStart = async (flagFilePath: string): Promise<boolean> => {
2428
try {
25-
const stats = await fs.stat(flagFilePath);
29+
const stats = await libUVLimited(() => fs.stat(flagFilePath));
2630
return stats.mtimeMs >= processStartTime;
2731
} catch {
2832
return false;
@@ -51,7 +55,7 @@ export const getCachedSelectivityFile = async (cacheType: CacheTypeValue, key: s
5155
const flagFilePath = cacheFilePath + SELECTIVITY_CACHE_READY_SUFFIX;
5256

5357
if (await wasModifiedAfterProcessStart(flagFilePath)) {
54-
const cacheContents = await fs.readFile(cacheFilePath, "utf8").catch(() => null);
58+
const cacheContents = await libUVLimited(() => fs.readFile(cacheFilePath, "utf8")).catch(() => null);
5559

5660
return cacheContents;
5761
}
@@ -60,7 +64,7 @@ export const getCachedSelectivityFile = async (cacheType: CacheTypeValue, key: s
6064
if (await wasModifiedAfterProcessStart(cacheFilePath)) {
6165
for (let i = 0; i < 10; i++) {
6266
if (await wasModifiedAfterProcessStart(flagFilePath)) {
63-
const cacheContents = await fs.readFile(cacheFilePath, "utf8").catch(() => null);
67+
const cacheContents = await libUVLimited(() => fs.readFile(cacheFilePath, "utf8")).catch(() => null);
6468

6569
return cacheContents;
6670
}
@@ -113,12 +117,12 @@ export const setCachedSelectivityFile = async (
113117
}
114118

115119
try {
116-
await fs.writeFile(cacheFilePath, utf8Contents, { encoding: "utf8" }).catch(cause => {
120+
await libUVLimited(() => fs.writeFile(cacheFilePath, utf8Contents, { encoding: "utf8" })).catch(cause => {
117121
throw new Error(`Couldn't write cache to "${cacheFilePath}"`, { cause });
118122
});
119123

120124
// Using "writeFile" to trigger "mtime" update even if file exists
121-
await fs.writeFile(flagFilePath, "").catch(cause => {
125+
await libUVLimited(() => fs.writeFile(flagFilePath, "")).catch(cause => {
122126
throw new Error(`Couldn't mark cache as fresh at "${cacheFilePath}"`, { cause });
123127
});
124128
} finally {

src/browser/cdp/selectivity/hash-provider.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import path from "node:path";
22
import crypto from "node:crypto";
33
import fs from "node:fs";
4+
import pLimit from "p-limit";
45

56
const calculateFileMd5Hash = (filePath: string): Promise<string> =>
67
new Promise((resolve, reject) => {
@@ -17,6 +18,7 @@ const calculateFileMd5Hash = (filePath: string): Promise<string> =>
1718
export class HashProvider {
1819
private static readonly _fileHashStore: Map<string, Promise<string>> = new Map();
1920
private static readonly _patternHashStore: Map<string, Promise<string>> = new Map();
21+
private static readonly _limited = pLimit(10);
2022

2123
async calculateForFile(filePath: string): Promise<string> {
2224
const cachedHash = HashProvider._fileHashStore.get(filePath);
@@ -25,7 +27,7 @@ export class HashProvider {
2527
return cachedHash;
2628
}
2729

28-
const hashPromise = calculateFileMd5Hash(filePath);
30+
const hashPromise = HashProvider._limited(() => calculateFileMd5Hash(filePath));
2931

3032
HashProvider._fileHashStore.set(filePath, hashPromise);
3133

@@ -55,7 +57,7 @@ export class HashProvider {
5557
let promiseQue = Promise.resolve();
5658

5759
for (const filePath of filesSorted) {
58-
const fileHashPromise = calculateFileMd5Hash(filePath);
60+
const fileHashPromise = HashProvider._limited(() => calculateFileMd5Hash(filePath));
5961
const cwdRelativePath = path.relative(cwd, filePath);
6062
const posixRelativePath =
6163
path.sep === path.posix.sep

src/browser/cdp/selectivity/js-selectivity.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -240,12 +240,12 @@ export class JSSelectivity {
240240
);
241241
}
242242

243-
const sourceString = isCachedOnFs(source)
244-
? await getCachedSelectivityFile(CacheType.Asset, sourceUrl)
245-
: source;
246-
const sourceMapsString = isCachedOnFs(sourceMaps)
247-
? await getCachedSelectivityFile(CacheType.Asset, sourceMapUrl as string)
248-
: sourceMaps;
243+
const [sourceString, sourceMapsString] = await Promise.all([
244+
isCachedOnFs(source) ? getCachedSelectivityFile(CacheType.Asset, sourceUrl) : source,
245+
isCachedOnFs(sourceMaps)
246+
? getCachedSelectivityFile(CacheType.Asset, sourceMapUrl as string)
247+
: sourceMaps,
248+
]);
249249

250250
if (!sourceString || !sourceMapsString) {
251251
throw new Error(`JS Selectivity: fs-cache is broken for ${sourceUrl}`);
@@ -261,7 +261,7 @@ export class JSSelectivity {
261261
});
262262
});
263263

264-
const dependingSourceFiles = await extractSourceFilesDeps(
264+
const dependingSourceFiles = extractSourceFilesDeps(
265265
sourceString,
266266
sourceMapsString,
267267
Array.from(startOffsetsSet),

0 commit comments

Comments
 (0)