Skip to content

Commit e0ad345

Browse files
mokevninElMassimo
andauthored
perf: skip re-hashing unchanged watched files on every build check (#625)
* perf: skip re-hashing unchanged watched files on every build check 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 #624 * chore: rubocop --------- Co-authored-by: Maximo Mussini <maximomussini@gmail.com>
1 parent 49734a0 commit e0ad345

2 files changed

Lines changed: 89 additions & 2 deletions

File tree

‎test/builder_test.rb‎

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,13 @@
44
require "open3"
55

66
class BuilderTest < ViteRuby::Test
7+
include ActiveSupport::Testing::TimeHelpers
8+
79
delegate :builder, :manifest, to: "ViteRuby.instance"
810

11+
# A watched file, as the digest sees it: relative to the root.
12+
WATCHED_FILE = "app/frontend/entrypoints/application.js"
13+
914
def last_build
1015
builder.last_build_metadata
1116
end
@@ -83,6 +88,37 @@ def test_watched_files_digest
8388
assert_equal previous_digest, ViteRuby.digest
8489
end
8590

91+
# NOTE: Each digest is taken in a separate time travel, because the result is
92+
# memoized for a second.
93+
def test_watched_files_digest_reads_changed_files_only
94+
previous_digest = ViteRuby.digest
95+
96+
travel 1.minute do
97+
assert_empty hashed_files { assert_equal previous_digest, ViteRuby.digest }
98+
end
99+
100+
FileUtils.touch(watched_file)
101+
102+
travel 2.minutes do
103+
assert_equal [WATCHED_FILE], hashed_files {
104+
assert_equal previous_digest, ViteRuby.digest
105+
}
106+
end
107+
end
108+
109+
def test_watched_files_digest_detects_changed_contents
110+
previous_digest = ViteRuby.digest
111+
contents = watched_file.read
112+
113+
travel 1.minute do
114+
watched_file.write("#{contents}\n// Changed")
115+
116+
refute_equal previous_digest, ViteRuby.digest
117+
end
118+
ensure
119+
watched_file.write(contents)
120+
end
121+
86122
def test_external_env_variables
87123
assert_equal "production", vite_env["VITE_RUBY_MODE"]
88124
assert_equal Rails.root.to_s, vite_env["VITE_RUBY_ROOT"]
@@ -122,6 +158,21 @@ def test_build_cache
122158

123159
private
124160

161+
def watched_file
162+
Pathname.new(path_to_test_app).join(WATCHED_FILE)
163+
end
164+
165+
# Collects the files whose contents were read to compute the digest.
166+
def hashed_files(&block)
167+
files = []
168+
original = Digest::SHA1.method(:file)
169+
Digest::SHA1.stub(:file, ->(file) {
170+
files << file.to_s
171+
original.call(file)
172+
}, &block)
173+
files
174+
end
175+
125176
def stub_runner(errors: "", success: errors.empty?, &block)
126177
args = ["stdout", errors, MockProcessStatus.new(success: success)]
127178
ViteRuby::IO.stub(:capture, args, &block)

‎vite_ruby/lib/vite_ruby/builder.rb‎

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
class ViteRuby::Builder
77
def initialize(vite_ruby)
88
@vite_ruby = vite_ruby
9+
@file_digests = {}
910
end
1011

1112
# Public: Checks if the watched files have changed since the last compilation,
@@ -55,13 +56,48 @@ def watched_files_digest
5556
return @last_digest if @last_digest_at && Time.now - @last_digest_at < 1
5657

5758
config.within_root do
58-
files = Dir[*config.watched_paths].reject { |f| File.directory?(f) }
59-
file_ids = files.sort.map { |f| "#{File.basename(f)}/#{Digest::SHA1.file(f).hexdigest}" }
59+
previous_digests = @file_digests
60+
@file_digests = {}
61+
file_ids = Dir[*config.watched_paths].sort.filter_map { |file|
62+
file_digest(file, previous_digests)
63+
}
6064
@last_digest_at = Time.now
6165
@last_digest = Digest::SHA1.hexdigest(file_ids.join("/"))
6266
end
6367
end
6468

69+
# Internal: Returns the id of a watched file, or nil if it's a directory.
70+
#
71+
# NOTE: Reading and hashing every watched file dominates the cost of the
72+
# check, and the answer is almost always the same one as the last time, so the
73+
# contents are read again only once the file has been touched. The digest
74+
# stays content-based: a checkout that changes mtimes but not contents still
75+
# counts as unchanged, and no Vite build is triggered.
76+
def file_digest(file, previous_digests)
77+
stat = File.stat(file)
78+
return if stat.directory?
79+
80+
signature = [stat.mtime, stat.size]
81+
previous_signature, previous_id = previous_digests[file]
82+
id = if previous_signature == signature && !recently_modified?(stat)
83+
previous_id
84+
else
85+
"#{File.basename(file)}/#{Digest::SHA1.file(file).hexdigest}"
86+
end
87+
@file_digests[file] = [signature, id]
88+
id
89+
end
90+
91+
# Internal: Whether the file was modified too recently to trust its mtime.
92+
#
93+
# NOTE: A file edited twice within the resolution of the file system clock,
94+
# without changing its size, would keep the same signature. Rehashing the
95+
# files touched in the last second rules that out, the same way Git resolves
96+
# a racily clean entry in the index.
97+
def recently_modified?(stat)
98+
Time.now - stat.mtime < 1
99+
end
100+
65101
# Public: Initiates a Vite build command to generate assets.
66102
def build_with_vite(*args)
67103
logger.info "Building with Vite ⚡️"

0 commit comments

Comments
 (0)