Skip to content

Commit 7f79b7a

Browse files
committed
metro-file-map: Skip the per-file worker pass when no plugin needs it
With lazy SHA-1s (Metro's default), a file only needs a worker at startup if a plugin computes data for it as it's crawled. With Metro's defaults none does, since #2220 leaves out the Haste plugin without Haste config. Even so, `processBatch` goes through every crawled file to find that out, checking its path for `node_modules` and building an array of the plugins to run on it. This returns straight away when there's no SHA-1 to compute and every plugin's worker is lazy (`lazy: true`, ie it computes data on demand). It doesn't change the cache or any API. ## Benchmark This takes 5-16% off applying the crawl on a cold start (macOS: 15.4ms -> 13.0ms, Linux: 68ms -> 64ms), and 2-3% off a cold start on Linux and Windows (Linux: 297ms -> 289ms). On macOS, cache writes are 0.4ms slower. The serialise itself takes the same time, so the difference is in the rest of the write. | | `main` | #2220 | This PR | vs `main` | vs #2220 | |---|---|---|---|---|---| | macOS: Applying the crawl in a cold start | 19.2ms | 15.4ms | 13.0ms | -32.5% (-34.5% to -30.3%) | -15.9% (-19.0% to -12.8%) | | Linux: Applying the crawl in a cold start | 83ms | 68ms | 64ms | -23.5% (-25.3% to -20.4%) | -6.5% (-8.3% to -4.1%) | | Windows: Applying the crawl in a cold start | 47ms | 34ms | 32ms | -32.4% (-33.4% to -31.5%) | -5.3% (-7.7% to -2.4%) | | macOS: Cache write in a cold start | 7.6ms | 4.4ms | 4.8ms | -36.4% (-38.9% to -33.8%) | +10.3% (+7.5% to +12.9%) | | Linux: Cache write in a cold start | 32ms | 15.0ms | 14.4ms | -55.0% (-56.4% to -52.2%) | -3.7% (-12.9% to +2.5%) | | Windows: Cache write in a cold start | 20ms | 10.6ms | 9.4ms | -53.7% (-54.6% to -52.7%) | -11.4% (-35.3% to -6.2%) | | macOS: Cold start | 146ms | 90ms | 92ms | -37.1% (-43.4% to -32.7%) | +1.7% (-9.6% to +13.9%) | | Linux: Cold start | 647ms | 297ms | 289ms | -55.3% (-55.7% to -54.9%) | -2.6% (-4.2% to -1.3%) | | Windows: Cold start | 358ms | 162ms | 158ms | -55.9% (-56.5% to -55.6%) | -2.6% (-4.4% to -1.7%) | ## Changelog ``` - **[Performance]**: metro-file-map: Skip the per-file worker pass when no plugin needs it ``` ## Test plan New unit tests. Benchmark as in #2104 (cold starts only), with 12 rounds on macOS.
1 parent c489a4b commit 7f79b7a

2 files changed

Lines changed: 53 additions & 6 deletions

File tree

‎packages/metro-file-map/src/lib/FileProcessor.js‎

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ const NODE_MODULES_SEP = 'node_modules' + sep;
5353
const MAX_FILES_PER_WORKER = 100;
5454

5555
export class FileProcessor {
56+
// Whether any plugin's worker runs on a batch, rather than on demand.
57+
#hasEagerPluginWorker: boolean;
5658
#maxFilesPerWorker: number;
5759
#maxWorkers: number;
5860
#perfLogger: ?PerfLogger;
@@ -72,6 +74,9 @@ export class FileProcessor {
7274
this.#maxFilesPerWorker = opts.maxFilesPerWorker ?? MAX_FILES_PER_WORKER;
7375
this.#maxWorkers = opts.maxWorkers;
7476
this.#pluginWorkers = opts.pluginWorkers ?? [];
77+
this.#hasEagerPluginWorker = this.#pluginWorkers.some(
78+
plugin => plugin.lazy !== true,
79+
);
7580
this.#inBandWorker = new Worker({
7681
plugins: this.#pluginWorkers.map(plugin => plugin.worker),
7782
});
@@ -90,6 +95,17 @@ export class FileProcessor {
9095
}> {
9196
const errors = [];
9297

98+
if (req.maybeReturnContent) {
99+
throw new Error(
100+
'Batch processing does not support returning file contents',
101+
);
102+
}
103+
104+
if (!req.computeSha1 && !this.#hasEagerPluginWorker) {
105+
// No file needs a worker.
106+
return {errors};
107+
}
108+
93109
const workerJobs = files
94110
.map(([normalFilePath, fileMetadata]) => {
95111
const maybeWorkerInput = this.#getWorkerInput(
@@ -110,12 +126,6 @@ export class FileProcessor {
110126
);
111127
const batchWorker = this.#getBatchWorker(numWorkers);
112128

113-
if (req.maybeReturnContent) {
114-
throw new Error(
115-
'Batch processing does not support returning file contents',
116-
);
117-
}
118-
119129
await Promise.all(
120130
workerJobs.map(([workerInput, fileMetadata]) => {
121131
return batchWorker

‎packages/metro-file-map/src/lib/__tests__/FileProcessor-test.js‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,43 @@ describe('processBatch', () => {
276276
expect(mockWorkerFn).not.toHaveBeenCalled();
277277
});
278278

279+
test.each([
280+
['no plugins', [] as ReadonlyArray<FileMapPluginWorker>],
281+
[
282+
'only lazy plugins',
283+
[
284+
{
285+
worker: {modulePath: 'mock-lazy-plugin', setupArgs: {}},
286+
filter: () => true,
287+
lazy: true,
288+
},
289+
],
290+
],
291+
])(
292+
'reads no file without a SHA-1 to compute and with %s',
293+
async (_, pluginWorkers) => {
294+
const readKeys: Array<string | symbol> = [];
295+
const fileMetadata = new Proxy<FileMetadata>(
296+
[123, 234, 0, null, 0, null],
297+
{
298+
get(target, key, receiver) {
299+
readKeys.push(key);
300+
return Reflect.get(target, key, receiver);
301+
},
302+
},
303+
);
304+
const processor = new FileProcessor({...defaultOptions, pluginWorkers});
305+
306+
await processor.processBatch([[p('src/index.js'), fileMetadata]], {
307+
computeSha1: false,
308+
maybeReturnContent: false,
309+
});
310+
311+
expect(readKeys).toEqual([]);
312+
expect(mockWorkerFn).not.toHaveBeenCalled();
313+
},
314+
);
315+
279316
describe('node_modules files', () => {
280317
const nodeModulesFiles = (): Array<[string, FileMetadata]> => [
281318
[p('node_modules/pkg/index.js'), [123, 234, 0, null, 0, null]],

0 commit comments

Comments
 (0)