@samitouri / QOS-React-2 / commits / 77c535ce25

[snap] Make watch mode go fast(er)

While optimizing per @josephsavona's suggestions in #1592, I noticed that we were clearing quite a few require cache entries. As of this PR, `Object.keys(require.cache)` holds - 1258 entries total - 67 files compiled from Forget source code (this is what `ts.createWatchCompilerHost` modifies) - 1120 babel source files (from node_modules) When working on watch mode, I'm almost always making changes to Forget source or test fixture files. It's a bit faster to just clear those entries (assuming that babel has no global state we need to invalidate). On my computer, re-running tests in watch mode (triggered by source code changes) takes: | | All tests | One test (filter) | |-- |--------|----------| | current | 4.7s | 1.8s | | this PR | 1.8s | 0.1s |

mofeiZ committed May 11, 2023 at 14:58 UTC 77c535ce25575e0679d7df8d40afe4dcce1d3c1f
2 files changed +7 -16
compiler/forget/packages/snap/src/compiler-worker.ts
+5 -15
@@ -11,24 +11,14 @@ import { exists } from "./utils";
11
12 const originalConsoleError = console.error;
13
14 -// Subpaths to ignore when clearing the require cache
15 -const ignoredRequireSubpaths: Array<string> = [
16 - // compiler worker runner files
17 - "node_modules/jest-worker",
18 - // snap source files
19 - "packages/snap",
20 -];
21 -const ignoredRequirePaths: Set<string> = new Set(
22 - Object.keys(require.cache).filter(
23 - (path) =>
24 - !ignoredRequireSubpaths.every((ignored) => !path.includes(ignored))
25 - )
26 -);
27 -
14 +// Try to avoid clearing the entire require cache, which (as of this PR)
15 +// contains ~1250 files. This assumes that no dependencies have global caches
16 +// that may need to be invalidated across Forget reloads.
17 +const invalidationSubpath = "react-forget/forget/dist";
18 let version: number | null = null;
19 export function clearRequireCache() {
20 Object.keys(require.cache).forEach(function (path) {
31 - if (!ignoredRequirePaths.has(path)) {
21 + if (path.includes(invalidationSubpath)) {
22 delete require.cache[path];
23 }
24 });
compiler/forget/packages/snap/src/runner.ts
+2 -1
@@ -441,13 +441,14 @@ export async function main(opts: RunnerOptions): Promise<void> {
441 const start = performance.now();
442 clearConsole();
443 console.log("Running tests...");
444 + // we don't clear console after this point, since
445 + // it may contain debug console logging
446 const results = await run(
447 worker,
448 opts,
449 filterMode ? testFilter : null,
450 compilerVersion
451 );
450 - clearConsole();
452 if (mode === Mode.Update) {
453 update(results);
454 } else {