Skip to content

metro-file-map: Walk to the parent directory once in addOrModify - #2210

Closed
robhogan wants to merge 1 commit into
mainfrom
pr2210
Closed

robhogan wants to merge 1 commit into
mainfrom
pr2210

Conversation

@robhogan

@robhogan robhogan commented Nov 9, 2026

Copy link
Copy Markdown
Collaborator

addOrModify walks to the file's parent directory, then hands the file to bulkAddOrModify, which walks to the same directory again. This is paid on every watched file change. This adds the file to the directory node from the first walk instead, which makes addOrModify about 45% faster.

It also fixes the file's path when its parent is a symlink to the project root. The parent's canonical path is then '', and joining it to the basename gave an absolute path (/new.js), so the file was added outside the project, at ../../new.js from a project at /project/root. @kitten spotted this in expo/expo#52449 (comment).

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]
addOrModify, existing file 1207 ns -47.1% [-48.0, -46.4] -0.3% [-1.2, +1.4]
addOrModify, new file 1323 ns -44.5% [-44.8, -43.7] +0.6% [-0.2, +1.3]
remove 710 ns -0.9% [-2.3, -0.5] +0.9% [-0.3, +1.8]
Cold build 137 ms +0.5% [-1.0, +1.2] +0.1% [-1.1, +0.6]
Warm start 133 ms +0.6% [-1.1, +2.2] +1.0% [-2.2, +3.1]

bulkAddOrModify is unchanged (0.455 ms on main, 0.456 ms with this PR, 200 calls in a fresh process each).

Changelog

 - **[Fix]**: Fix the path of a file added through a symlink to the project root

Test plan

New unit test, failing on main. Same benchmark app and method as #2204.

Fuzzed against main on 20,000 random symlinked trees, comparing results, errors and change events. 16 trees differ, each adding through a symlink to the project root, where main adds the file outside the project.

`addOrModify` walks to the file's parent directory, then hands the file to `bulkAddOrModify`, which walks to the same directory again. This is paid on every watched file change. This adds the file to the directory node from the first walk instead, which makes `addOrModify` about 45% faster.

It also fixes the file's path when its parent is a symlink to the project root. The parent's canonical path is then `''`, and joining it to the basename gave an absolute path (`/new.js`), so the file was added outside the project, at `../../new.js` from a project at `/project/root`. @kitten spotted this in expo/expo#52449 (comment).

## 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] |
|---|---|---|---|
| `addOrModify`, existing file | 1207 ns | -47.1% [-48.0, -46.4] | -0.3% [-1.2, +1.4] |
| `addOrModify`, new file | 1323 ns | -44.5% [-44.8, -43.7] | +0.6% [-0.2, +1.3] |
| `remove` | 710 ns | -0.9% [-2.3, -0.5] | +0.9% [-0.3, +1.8] |
| Cold build | 137 ms | +0.5% [-1.0, +1.2] | +0.1% [-1.1, +0.6] |
| Warm start | 133 ms | +0.6% [-1.1, +2.2] | +1.0% [-2.2, +3.1] |

`bulkAddOrModify` is unchanged (0.455 ms on `main`, 0.456 ms with this PR, 200 calls in a fresh process each).

## Changelog

```
 - **[Fix]**: Fix the path of a file added through a symlink to the project root
```

## Test plan

New unit test, failing on `main`. Same benchmark app and method as #2204.

Fuzzed against `main` on 20,000 random symlinked trees, comparing results, errors and change events. 16 trees differ, each adding through a symlink to the project root, where `main` adds the file outside the project.
@robhogan
robhogan requested review from huntie and vzaidman November 9, 2026 20:11
@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Nov 9, 2026
@github-actions

github-actions Bot commented Nov 9, 2026

Copy link
Copy Markdown

Metro review: Looks good

Risk: medium · Commit: dffa25a · 7 commands run

addOrModify now adds the file to the parent node from its first walk, so it no longer walks to the same directory again through bulkAddOrModify. It also builds the canonical path without going through #normalizePath. That fixes files added through a symlink to the project root, which used to land outside the project. A differential fuzz against base found differences only in that symlink-to-root case, and an interleaved benchmark reproduces the claimed ~45% speedup, so this looks good.

No findings.

Verified
  • ✅ yarn jest --no-watchman packages/metro-file-map/src/lib/__tests__/TreeFS-test.js — 275 tests pass, run on both win32 and posix path modules.
  • ✅ New PR test 'adds a file through a symlink to the root at its canonical path', run with base TreeFS.js copied over the head version (failed as expected) — Fails on both win32 and posix with base code. The head file was restored afterwards.
  • ✅ yarn jest --no-watchman packages/metro-file-map/src/lib/__tests__/TreeFS-review-fuzz-test.js — Head vs base fuzz: 20k random symlinked trees per platform, comparing results, errors, events and files. 155 of 18,714 valid runs differ, all through a symlink to the project root. Other diffs needed a target above the FS root, which normalization never produces.
  • ✅ node bench/addOrModify.js — Interleaved head/base benchmark of addOrModify; see benchmarks.
  • ✅ yarn eslint packages/metro-file-map/src/lib/TreeFS.js packages/metro-file-map/src/lib/__tests__/TreeFS-test.js
  • ✅ yarn flow check
  • ❌ yarn jest --no-watchman packages/metro-file-map — 800 pass. All 13 failures are Watchman integration tests, and Watchman can't run in the sandbox. They're unrelated to this change.

Benchmarks (base vs this PR)

Benchmark Base This PR Change
TreeFS.addOrModify, existing file (modification) 431 ns/call (426-447) 224 ns/call (216-230) -48%
TreeFS.addOrModify, new file (addition) 493 ns/call (487-498) 261 ns/call (256-265) -47%
Method
  • TreeFS.addOrModify, existing file (modification): One Node process loads base and head TreeFS through metro-babel-register. Fixture: 10,000 files (50x20x10 dirs), rootDir /project/root, 200k addOrModify calls per sample over 1,000 absolute paths, with a no-op listener. 1 warm-up, then 14 alternating ABAB rounds. Median with range. Script: bench/addOrModify.js
  • TreeFS.addOrModify, new file (addition): Same script and fixture as above (bench/addOrModify.js), adding 1,000 new basenames across existing directories. 14 interleaved rounds, median with range.

Not run: I didn't rerun the PR's cold-build, warm-start and remove numbers. The change touches only addOrModify, and both the crawl build and bulkAddOrModify are unchanged. The win32 path behavior was tested only with Node's path.win32 under Jest, not on a real Windows machine; none was available. Watchman integration tests can't run in the sandbox. vzaidman's question about changing bulkAddOrModify instead is a design choice for the author, so this review doesn't repeat it.

Automated review by Claude. Advisory only: it never approves or blocks, and a maintainer makes the call.

@robhogan

robhogan commented Nov 9, 2026

Copy link
Copy Markdown
Collaborator Author

I think tbh, this isn't a sufficiently hot path to justify the duplication - it's only used on file changes so typically on a small batch.

It was motivated by this being one of the last areas where @expo/metro-file-map is faster, and I'm trying to unfork, but this one's not meaningful to a user.

Thanks @vzaidman for the nudge :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants