From a09f2957d7947e3265a8021783769cf4c8fea122 Mon Sep 17 00:00:00 2001 From: Rob Hogan <2590098+robhogan@users.noreply.github.com> Date: Mon, 9 Nov 2026 17:30:01 +0000 Subject: [PATCH] metro-file-map: Avoid a generator per directory in matchFiles Same approach as in https://github.com/react/metro/pull/2206, applied to `matchFiles` (used on file changes to invalidate `require.context`). `matchFiles` nests a generator for each directory level, so each path is passed up through every level above it. This walks the tree with one generator and an explicit stack instead. It's still lazy, in the same order and with the same results. ## Benchmark AI-driven. Paired against `main`, 16 interleaved rounds, with a byte-identical copy of `main` as a noise control. | | `main` | This PR [95% CI] | Control [95% CI] | |---|---|---|---| | `matchFiles`, context (`src/`, `.tsx?`) | 0.17 ms | -13.4% [-16.7, -12.5] | -0.1% [-2.9, +0.9] | | Context, `follow: true` | 0.17 ms | -14.8% [-16.2, -13.9] | -1.8% [-2.9, +1.1] | | Whole tree | 17.7 ms | -27.2% [-28.8, -25.9] | -0.7% [-1.3, +1.0] | | Following symlinks under `node_modules/@react-navigation` | 0.09 ms | -40.6% [-41.2, -39.9] | -0.1% [-0.6, +1.3] | | Trace replay, steady | 56.9 ms | -0.9% [-2.0, +1.1] | -1.3% [-1.9, +0.4] | | Cold build | 136 ms | +1.1% [-2.3, +3.6] | +1.4% [-0.6, +3.2] | Changelog: Internal ## Test plan Same benchmark app and method as https://github.com/react/metro/pull/2204, with identical `matchFiles` results on every arm. Fuzzed against `main` on 20,000 random symlinked trees, including `follow`, non-recursive and ancestor-of-root queries, with no differences. --- packages/metro-file-map/src/lib/TreeFS.js | 75 +++++++++++++++-------- 1 file changed, 49 insertions(+), 26 deletions(-) diff --git a/packages/metro-file-map/src/lib/TreeFS.js b/packages/metro-file-map/src/lib/TreeFS.js index 019f0b4456..41b495029a 100644 --- a/packages/metro-file-map/src/lib/TreeFS.js +++ b/packages/metro-file-map/src/lib/TreeFS.js @@ -1358,17 +1358,32 @@ export default class TreeFS implements MutableFileSystem { followedLinks: ReadonlySet = new Set(), ): Iterable { const pathSep = opts.alwaysYieldPosix ? '/' : path.sep; - const prefixWithSep = pathPrefix === '' ? pathPrefix : pathPrefix + pathSep; - for (const [name, node] of this.#directoryNodeIterator( - iterationRootNode, - iterationRootParentNode, - ancestorOfRootIdx, - )) { + const stack = [ + { + entries: this.#directoryNodeIterator( + iterationRootNode, + iterationRootParentNode, + ancestorOfRootIdx, + ), + parentNode: iterationRootParentNode, + ancestorOfRootIdx, + prefixWithSep: pathPrefix === '' ? '' : pathPrefix + pathSep, + followedLinks, + }, + ]; + while (stack.length > 0) { + const frame = stack[stack.length - 1]; + const next = frame.entries.next(); + if (next.done === true) { + stack.pop(); + continue; + } + const [name, node] = next.value; if (opts.subtreeOnly && name === '..') { continue; } - const nodePath = prefixWithSep + name; + const nodePath = frame.prefixWithSep + name; if (!isDirectory(node)) { if (isRegularFile(node)) { // regular file @@ -1408,31 +1423,39 @@ export default class TreeFS implements MutableFileSystem { } else if ( opts.recursive && opts.follow && - !followedLinks.has(node) + !frame.followedLinks.has(node) ) { // Symlink points to a directory - iterate over its contents using // the path where we found the symlink as a prefix. - yield* this.#pathIterator( - target, - resolved.parentNode, - resolved.ancestorOfRootIdx, - opts, - nodePath, - new Set([...followedLinks, node]), - ); + stack.push({ + entries: this.#directoryNodeIterator( + target, + resolved.parentNode, + resolved.ancestorOfRootIdx, + ), + parentNode: resolved.parentNode, + ancestorOfRootIdx: resolved.ancestorOfRootIdx, + prefixWithSep: nodePath + pathSep, + followedLinks: new Set([...frame.followedLinks, node]), + }); } } } else if (opts.recursive) { - yield* this.#pathIterator( - node, - iterationRootParentNode, - ancestorOfRootIdx != null && ancestorOfRootIdx > 0 - ? ancestorOfRootIdx - 1 - : null, - opts, - nodePath, - followedLinks, - ); + const childAncestorOfRootIdx = + frame.ancestorOfRootIdx != null && frame.ancestorOfRootIdx > 0 + ? frame.ancestorOfRootIdx - 1 + : null; + stack.push({ + entries: this.#directoryNodeIterator( + node, + frame.parentNode, + childAncestorOfRootIdx, + ), + parentNode: frame.parentNode, + ancestorOfRootIdx: childAncestorOfRootIdx, + prefixWithSep: nodePath + pathSep, + followedLinks: frame.followedLinks, + }); } } }