Skip to content

Commit 82d957f

Browse files
authored
metro-file-map: Track only directories and symlinks in FallbackWatcher (#2107)
`FallbackWatcher` keeps an entry for every file it has seen: https://github.com/react/metro/blob/56efab302200568c919ac1b3c28dacf8d84fc692/packages/metro-file-map/src/watchers/FallbackWatcher.js#L53-L56 It only reads them when something is deleted. Deleting `src/a.js` is reported because there's an entry for it, and deleting `src` reports `src/a.js`, `src/b.js` and so on from the entries, then `src` itself. Neither needs an entry per file: - A file's name says whether it was reported: `src/a.js` passes the include checks, `src/README.md` doesn't. - Told that `src` is deleted, the file map removes everything under it (that's how `NativeWatcher` reports it), so one `delete` is enough. So this keeps entries only for directories (including ones it couldn't watch) and symlinks. Those are reported whatever their name, so once `src/components` is gone, only an entry says whether it was a directory or a file the include checks left out. ## Benchmark This takes another 22-30% off watch-mode startup on Linux and Windows (Linux: 969ms -> 695ms), and 3MB (10%) off the heap after startup on this app. | | `main` | #2106 | This PR | vs `main` | vs #2106 | |---|---|---|---|---|---| | Linux: Watch-mode start, warm cache | 1,887ms | 969ms | 695ms | -63.2% (-63.6% to -62.8%) | -28.2% (-29.2% to -27.6%) | | Windows: Watch-mode start, warm cache | 2,577ms | 935ms | 727ms | -71.8% (-71.9% to -71.7%) | -22.2% (-22.4% to -21.9%) | | Linux: Watch-mode start, cold cache | 1,885ms | 931ms | 652ms | -65.4% (-65.6% to -65.1%) | -30.0% (-30.9% to -29.2%) | | Windows: Watch-mode start, cold cache | 2,562ms | 917ms | 701ms | -72.7% (-72.8% to -72.5%) | -23.6% (-23.8% to -23.1%) | | Linux: Heap after a watch-mode start | 31.7MB | 31.2MB | 28.1MB | -11.2% (-11.2% to -11.2%) | -9.9% (-9.9% to -9.9%) | | Windows: Heap after a watch-mode start | 31.7MB | 31.3MB | 28.2MB | -11.3% (-11.3% to -11.3%) | -9.9% (-9.9% to -9.9%) | ## Changelog ``` - **[Performance]**: metro-file-map: Start the file watcher faster, and with less memory, on Linux and Windows without Watchman ``` ## Test plan New unit tests, and a `watch-integration-test.js` that drives FileMap in watch mode over a real directory, deleting directories and symlinks and moving directories out (with `FallbackWatcher`, or `NativeWatcher` on macOS). Benchmark as in #2104.
1 parent d032fa5 commit 82d957f

5 files changed

Lines changed: 414 additions & 128 deletions

File tree

Lines changed: 175 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
1+
/**
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*
7+
* @flow strict-local
8+
* @format
9+
* @oncall react_native
10+
*/
11+
12+
import type {ChangeEvent, FileSystem} from '../flow-types';
13+
14+
import FileMap from '../index';
15+
import fs from 'node:fs';
16+
import os from 'node:os';
17+
import path from 'node:path';
18+
19+
// A FileMap in watch mode, with the watcher backend it uses on this platform
20+
// (FallbackWatcher, or NativeWatcher on macOS), against a real file system.
21+
// Changes are made after the initial crawl and checked through the file system
22+
// the build returned.
23+
24+
jest.useRealTimers();
25+
jest.setTimeout(30000);
26+
27+
let rootDir: string;
28+
let fileMap: FileMap;
29+
let fileSystem: FileSystem;
30+
let removedFiles: Set<string>;
31+
32+
const abs = (normalPath: string) => path.join(rootDir, normalPath);
33+
34+
function write(normalPath: string, content: string = '') {
35+
fs.mkdirSync(path.dirname(abs(normalPath)), {recursive: true});
36+
fs.writeFileSync(abs(normalPath), content);
37+
}
38+
39+
async function until(label: string, check: () => boolean): Promise<void> {
40+
const deadline = Date.now() + 10000;
41+
while (!check()) {
42+
if (Date.now() > deadline) {
43+
throw new Error(`Timed out waiting for: ${label}`);
44+
}
45+
await new Promise(resolve => setTimeout(resolve, 25));
46+
}
47+
}
48+
49+
const exists = (normalPath: string) => fileSystem.exists(abs(normalPath));
50+
51+
function createFileMap({retainAllFiles}: {retainAllFiles: boolean}) {
52+
return new FileMap({
53+
cacheManagerFactory: () => ({
54+
read: async () => null,
55+
write: async () => {},
56+
end: async () => {},
57+
}),
58+
enableSymlinks: true,
59+
extensions: ['js'],
60+
healthCheck: {
61+
enabled: false,
62+
interval: 10000,
63+
timeout: 1000,
64+
filePrefix: '.metro-file-map-health-check',
65+
},
66+
ignorePattern: /ignored-dir/,
67+
maxWorkers: 1,
68+
retainAllFiles,
69+
rootDir,
70+
roots: [rootDir],
71+
useWatchman: false,
72+
watch: true,
73+
});
74+
}
75+
76+
describe('FileMap in watch mode', () => {
77+
beforeEach(async () => {
78+
rootDir = fs.realpathSync(
79+
fs.mkdtempSync(path.join(os.tmpdir(), 'metro-file-map-watch-')),
80+
);
81+
write('src/deep/a/b/c/one.js');
82+
write('src/two.js');
83+
write('pkg/real/index.js');
84+
write('ignored-dir/x.js');
85+
for (let i = 0; i < 20; i++) {
86+
write(`many/d${i}/f.js`);
87+
}
88+
fs.mkdirSync(abs('node_modules'));
89+
fs.symlinkSync(
90+
path.join('..', 'pkg', 'real'),
91+
abs('node_modules/linked'),
92+
'junction',
93+
);
94+
95+
removedFiles = new Set();
96+
fileMap = createFileMap({retainAllFiles: true});
97+
({fileSystem} = await fileMap.build());
98+
fileMap.on('change', ({changes}: ChangeEvent) => {
99+
for (const [canonicalPath] of changes.removedFiles) {
100+
removedFiles.add(canonicalPath);
101+
}
102+
});
103+
});
104+
105+
afterEach(async () => {
106+
await fileMap.end();
107+
fs.rmSync(rootDir, {recursive: true, force: true});
108+
});
109+
110+
test('reports a new file in a directory that existed at startup', async () => {
111+
write('src/deep/a/b/c/new.js');
112+
await until('new.js added', () => exists('src/deep/a/b/c/new.js'));
113+
});
114+
115+
test('watches every directory that existed at startup', async () => {
116+
for (let i = 0; i < 20; i++) {
117+
write(`many/d${i}/g.js`);
118+
}
119+
await until('all added', () =>
120+
Array.from({length: 20}, (_, i) => exists(`many/d${i}/g.js`)).every(
121+
Boolean,
122+
),
123+
);
124+
});
125+
126+
test('removes the files under a deleted directory', async () => {
127+
fs.rmSync(abs('src/deep'), {recursive: true});
128+
await until('one.js removed', () => !exists('src/deep/a/b/c/one.js'));
129+
expect(removedFiles).toContain(
130+
path.join('src', 'deep', 'a', 'b', 'c', 'one.js'),
131+
);
132+
expect(exists('src/two.js')).toBe(true);
133+
});
134+
135+
test('removes the files under a directory moved out of the root', async () => {
136+
const outside = fs.mkdtempSync(
137+
path.join(os.tmpdir(), 'metro-file-map-out-'),
138+
);
139+
try {
140+
fs.renameSync(abs('many/d7'), path.join(outside, 'd7'));
141+
await until('f.js removed', () => !exists('many/d7/f.js'));
142+
expect(exists('many/d8/f.js')).toBe(true);
143+
} finally {
144+
fs.rmSync(outside, {recursive: true, force: true});
145+
}
146+
});
147+
148+
test('reports files in a new directory, and files added to it later', async () => {
149+
write('fresh/sub/new.js');
150+
await until('new.js added', () => exists('fresh/sub/new.js'));
151+
write('fresh/sub/later.js');
152+
await until('later.js added', () => exists('fresh/sub/later.js'));
153+
});
154+
155+
test('removes a deleted symlink', async () => {
156+
expect(fileSystem.linkStats(abs('node_modules/linked'))).toMatchObject({
157+
fileType: 'l',
158+
});
159+
// A junction on Windows, which is removed as a directory
160+
(process.platform === 'win32' ? fs.rmdirSync : fs.unlinkSync)(
161+
abs('node_modules/linked'),
162+
);
163+
await until(
164+
'link removed',
165+
() => fileSystem.linkStats(abs('node_modules/linked')) == null,
166+
);
167+
});
168+
169+
test('ignores changes in an ignored directory', async () => {
170+
write('ignored-dir/y.js');
171+
write('src/marker.js');
172+
await until('marker.js added', () => exists('src/marker.js'));
173+
expect(exists('ignored-dir/y.js')).toBe(false);
174+
});
175+
});

0 commit comments

Comments
 (0)