Skip to content

perf: skip re-hashing unchanged watched files on every build check - #625

Merged
ElMassimo merged 2 commits into
ElMassimo:mainfrom
mokevnin:perf/cache-watched-file-digests
Sep 16, 2026
Merged

ElMassimo merged 2 commits into
ElMassimo:mainfrom
mokevnin:perf/cache-watched-file-digests

Conversation

@mokevnin

Copy link
Copy Markdown
Contributor

Description 📖

This pull request makes the autoBuild staleness check stop re-reading watched files that have not changed. Closes #624, where the measurements are written up in full.

Background 📜

Every call to a vite_* helper goes through Manifest#lookup, which calls builder.build, which calls watched_files_digest — a glob over config.watched_paths plus Digest::SHA1.file on every one of them. @last_digest_at memoizes the result, but only for a second.

On an app with ~1850 watched files (72 MB under app/javascript) one pass costs:

Dir[] File.stat × 1846 Digest::SHA1.file × 1846
Linux, native filesystem 2.1 ms 1.9 ms 104.7 ms
Linux container, macOS bind mount 56.3 ms 3.5 ms 364.5 ms

The traversal is cheap; reading 72 MB to hash it is the whole cost, and the answer is nearly always "not stale".

The one-second memo decides how often that is paid, and it works against you exactly when things are slow: the slower the render, the more likely the next helper call falls outside the window. Our layout makes five lookups that reach the manifest. Locally they coalesce into one pass; on our CI runner they do not — a captured log shows 540 build calls in one run.

The Fix 🔨

By keeping each file's id under its mtime and size, and reading the file again only once that changes.

repeated pass, no file changes before after
Linux, native filesystem 99.8 ms 6.3 ms
Linux container, macOS bind mount ~460 ms ~58 ms

In one integration-test file (29 tests, autoBuild: true) the time spent inside the staleness check went from 2052 ms to 232 ms, over the same 64 build calls.

Three things worth calling out:

  • The digest stays content-based. A fresh checkout that changes mtimes but not contents produces the same digest and triggers no build. That is why the cache is keyed on the stat instead of the digest itself being replaced by one.
  • No new class of missed edit. mtime plus size can collide only for an edit landing within the clock resolution of an earlier one, and recently_modified? re-reads exactly those files — the rule Git applies to racily clean index entries.
  • Build#stale? and the contents of last-build-*.json are untouched. The cache lives on the Builder instance and is rebuilt on each pass, so deleted files drop out of it on their own.

Two tests are added: one asserts that a second pass reads nothing from disk and that touching a file re-reads that file and only that file; the other asserts that changed contents still change the digest.

#624 also names a second, smaller cost — Manifest#manifest re-reading and re-parsing manifest.json on every lookup when auto_build is on. That one is not in this pull request; happy to send it separately if you want it.

mokevnin and others added 2 commits September 15, 2026 22:02
With autoBuild on, every asset helper asks the builder whether the build
is stale, and each check reads and hashes every watched file. On an app
with ~1850 watched files (72 MB) one check costs ~100 ms on a native
filesystem and ~460 ms on a bind mount, and the answer is almost always
"not stale". The one-second memo decides how often that is paid, and it
works against you exactly when things are slow: the slower the render,
the more likely the next helper call falls outside the window.

Caching each file's id under its mtime and size drops the same check to
~6 ms. The digest stays content-based, so a checkout that changes mtimes
but not contents does not trigger a build; files touched within the last
second are read again regardless, the way Git resolves a racily clean
index entry.

Closes ElMassimo#624
@ElMassimo

Copy link
Copy Markdown
Owner

Hi Kirill!

I like this optimization, it's small and self-contained, so it feels like a reasonable addition.

That said, I notice that you mention auto_build being triggered in the CI, which seems wasteful.

As mentioned in the guide, it's recommended to set VITE_RUBY_AUTO_BUILD="false" in your CI to prevent the check from ever running.

@ElMassimo
ElMassimo merged commit e0ad345 into ElMassimo:main Sep 16, 2026
17 checks passed
@mokevnin

Copy link
Copy Markdown
Contributor Author

Thanks for merging, and for the pointer on VITE_RUBY_AUTO_BUILD — agreed, we've since turned auto-build off in the test environment, CI included.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

autoBuild: the staleness check re-hashes every watched file on each helper call

2 participants