next.js icon indicating copy to clipboard operation
next.js copied to clipboard

[turbopack] Skip invalidation for writes that leave file contents unchanged

Open GriffinCanCode opened this issue 4 months ago • 0 comments

What?

An opt-in flag, TURBO_TASKS_SUPPRESS_UNCHANGED_WRITES=1, that makes the Turbopack filesystem watcher skip invalidation for a write that leaves a file's bytes unchanged. Off by default; nothing changes unless you set it.

Why?

A lot of the writes my machine sees all day don't change anything. A formatter that reformatted nothing, a generator that reran, git checkout restoring identical bytes, an editor saving on focus loss. The mtime moves either way, so the watcher invalidates every task that read the file and the dev server runs a full update cycle to land on exactly the output it already had.

webpack lets you fix this from userland, because compiler.watchFileSystem is settable; Vite lets you fix it with server.watcher plus handleHotUpdate. Turbopack has no equivalent seam, so there is nowhere to hang it. That is what #96520 and discussion #96540 are about.

Measured against this branch, hello-world app, counting built on the HMR socket:

20 byte-identical writes 3 real edits
canary 20 update cycles 3
flag on 0 update cycles 3

Real edits still emit serverComponentChanges and still rebuild in both arms. Reproduction: https://github.com/GriffinCanCode/turbopack-noop-write-repro

How?

BatchedInvalidations::drop_unchanged_writes runs when a batch flushes, before the invalidator lock is taken; it reads and hashes files, and holding the lock across that would stall every other fs operation. For each path it hashes length, permissions, and contents with Xxh3Hash64Hasher, streaming in 64 KiB chunks, and compares against what that path hashed to the last time it was invalidated. A match clears the path's flags, which leaves its readers registered in the invalidator map instead of consuming them, so the next real change still reaches them.

Deliberately conservative. A path is only eligible if the batch holds nothing for it but a content modification, so all of these still invalidate:

  • creates, removals, renames; they change a directory listing and whether a path exists
  • symlinks; read_link resolves the link itself, not what it points at
  • permission changes; metadata() sees those
  • the first write after startup; there is no baseline to compare against
  • anything unreadable; an error must never read as "unchanged"

Atomic saves (temp file plus rename) carry a create, so they are not eligible either.

Eight unit tests in watcher.rs cover each of those, plus the case where the bytes really did change and the case where a removal has to forget the old baseline. I mutation-tested them: deleting any one guard turns a test red.

No e2e test. The observable difference is "an update cycle did not happen", which needs write-then-wait timing, and I would rather not add a flaky test for a flag that is off by default. The unit tests pin the decision logic, and the numbers above reproduce with the linked repo.

No telemetry, matching TURBO_TASKS_FORCE_WATCH_MODE, the neighbouring knob, which also has none. Both are documented now in contributing/turbopack/watching.md.

Happy to flip the default, or move it to a turbopack config key instead of an env var, if you would rather; the env var just matched the existing knob.

This contribution came from the combination of Griffin and his agent Billy, a product of the Billy Company.

GriffinCanCode avatar Aug 03 '26 22:08 GriffinCanCode