Skip to content

Commit 2a8ee32

Browse files
yangshunclaudebranchseer
authored
refactor: extract shared hash_content into execute/hash.rs (#282)
## Summary Extract the 8 KiB buffered xxHash3_64 hashing logic from `fingerprint.rs` and `glob_inputs.rs` into a shared `execute/hash.rs` module. > [!NOTE] > I used Claude to find this duplication, reasoned with it back-and-forth to see if it was really an improvement, concluded that it was just a small one. > > Both call sites were identical, but strictly speaking the logic doesn't need to be shared. If not desired, just close the PR. ## Test plan - [x] `cargo test -p vite_task` — all 27 tests pass Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com> Co-authored-by: branchseer <3612422+branchseer@users.noreply.github.com>
1 parent fa42ef9 commit 2a8ee32

File tree

4 files changed

+20
-36
lines changed

4 files changed

+20
-36
lines changed

crates/vite_task/src/session/execute/fingerprint.rs

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@
66
use std::{
77
collections::BTreeMap,
88
fs::File,
9-
hash::Hasher as _,
10-
io::{self, BufRead, Read},
9+
io::{self, BufRead},
1110
sync::Arc,
1211
};
1312

@@ -182,20 +181,6 @@ fn determine_folder_change_kind<'a>(
182181
}
183182
}
184183

185-
/// Hash file content using `xxHash3_64`
186-
fn hash_content(mut stream: impl Read) -> io::Result<u64> {
187-
let mut hasher = twox_hash::XxHash3_64::default();
188-
let mut buf = [0u8; 8192];
189-
loop {
190-
let n = stream.read(&mut buf)?;
191-
if n == 0 {
192-
break;
193-
}
194-
hasher.write(&buf[..n]);
195-
}
196-
Ok(hasher.finish())
197-
}
198-
199184
/// Check if a directory entry should be ignored in fingerprinting
200185
fn should_ignore_entry(name: &[u8]) -> bool {
201186
matches!(name, b"." | b".." | b".DS_Store") || name.eq_ignore_ascii_case(b"dist")
@@ -251,7 +236,7 @@ pub fn fingerprint_path(
251236
return process_directory(std_path, path_read);
252237
}
253238
}
254-
Ok(PathFingerprint::FileContentHash(hash_content(reader)?))
239+
Ok(PathFingerprint::FileContentHash(super::hash::hash_content(reader)?))
255240
}
256241

257242
/// Process a directory on Windows using `std::fs::read_dir`

crates/vite_task/src/session/execute/glob_inputs.rs

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,7 @@
55
//!
66
//! All glob patterns are workspace-root-relative (resolved at task graph stage).
77
8-
use std::{
9-
collections::BTreeMap,
10-
fs::File,
11-
hash::Hasher as _,
12-
io::{self, Read},
13-
};
8+
use std::{collections::BTreeMap, fs::File, io};
149

1510
#[cfg(test)]
1611
use vite_path::AbsolutePathBuf;
@@ -114,21 +109,9 @@ pub fn compute_globbed_inputs(
114109
Ok(result)
115110
}
116111

117-
/// Hash file content using `xxHash3_64`.
118112
#[expect(clippy::disallowed_types, reason = "receives std::path::Path from wax glob walker")]
119113
fn hash_file_content(path: &std::path::Path) -> io::Result<u64> {
120-
let file = File::open(path)?;
121-
let mut reader = io::BufReader::new(file);
122-
let mut hasher = twox_hash::XxHash3_64::default();
123-
let mut buf = [0u8; 8192];
124-
loop {
125-
let n = reader.read(&mut buf)?;
126-
if n == 0 {
127-
break;
128-
}
129-
hasher.write(&buf[..n]);
130-
}
131-
Ok(hasher.finish())
114+
super::hash::hash_content(io::BufReader::new(File::open(path)?))
132115
}
133116

134117
#[cfg(test)]
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
use std::{hash::Hasher as _, io};
2+
3+
/// Hash content using 8 KiB buffered `xxHash3_64`.
4+
pub(super) fn hash_content(mut stream: impl io::Read) -> io::Result<u64> {
5+
let mut hasher = twox_hash::XxHash3_64::default();
6+
let mut buf = [0u8; 8192];
7+
loop {
8+
let n = stream.read(&mut buf)?;
9+
if n == 0 {
10+
break;
11+
}
12+
hasher.write(&buf[..n]);
13+
}
14+
Ok(hasher.finish())
15+
}

crates/vite_task/src/session/execute/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
pub mod fingerprint;
22
pub mod glob_inputs;
3+
mod hash;
34
pub mod spawn;
45

56
use std::{collections::BTreeMap, io::Write as _, process::Stdio, sync::Arc};

0 commit comments

Comments
 (0)