Skip to content

Commit b6f2646

Browse files
authored
refactor: make IncrementalCache accept a CacheDBHash (denoland#27570)
1 parent 6750aa6 commit b6f2646

File tree

7 files changed

+22
-17
lines changed

7 files changed

+22
-17
lines changed

cli/cache/cache_db.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,12 @@ impl CacheDBHash {
2525
Self(hash)
2626
}
2727

28-
pub fn from_source(source: impl std::hash::Hash) -> Self {
28+
pub fn from_hashable(hashable: impl std::hash::Hash) -> Self {
2929
Self::new(
3030
// always write in the deno version just in case
3131
// the clearing on deno version change doesn't work
3232
FastInsecureHasher::new_deno_versioned()
33-
.write_hashable(source)
33+
.write_hashable(hashable)
3434
.finish(),
3535
)
3636
}

cli/cache/incremental.rs

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,16 @@ pub static INCREMENTAL_CACHE_DB: CacheDBConfiguration = CacheDBConfiguration {
3434
pub struct IncrementalCache(IncrementalCacheInner);
3535

3636
impl IncrementalCache {
37-
pub fn new<TState: std::hash::Hash>(
37+
pub fn new(
3838
db: CacheDB,
39-
state: &TState,
39+
state_hash: CacheDBHash,
4040
initial_file_paths: &[PathBuf],
4141
) -> Self {
42-
IncrementalCache(IncrementalCacheInner::new(db, state, initial_file_paths))
42+
IncrementalCache(IncrementalCacheInner::new(
43+
db,
44+
state_hash,
45+
initial_file_paths,
46+
))
4347
}
4448

4549
pub fn is_file_same(&self, file_path: &Path, file_text: &str) -> bool {
@@ -67,12 +71,11 @@ struct IncrementalCacheInner {
6771
}
6872

6973
impl IncrementalCacheInner {
70-
pub fn new<TState: std::hash::Hash>(
74+
pub fn new(
7175
db: CacheDB,
72-
state: &TState,
76+
state_hash: CacheDBHash,
7377
initial_file_paths: &[PathBuf],
7478
) -> Self {
75-
let state_hash = CacheDBHash::from_source(state);
7679
let sql_cache = SqlIncrementalCache::new(db, state_hash);
7780
Self::from_sql_incremental_cache(sql_cache, initial_file_paths)
7881
}
@@ -112,13 +115,13 @@ impl IncrementalCacheInner {
112115

113116
pub fn is_file_same(&self, file_path: &Path, file_text: &str) -> bool {
114117
match self.previous_hashes.get(file_path) {
115-
Some(hash) => *hash == CacheDBHash::from_source(file_text),
118+
Some(hash) => *hash == CacheDBHash::from_hashable(file_text),
116119
None => false,
117120
}
118121
}
119122

120123
pub fn update_file(&self, file_path: &Path, file_text: &str) {
121-
let hash = CacheDBHash::from_source(file_text);
124+
let hash = CacheDBHash::from_hashable(file_text);
122125
if let Some(previous_hash) = self.previous_hashes.get(file_path) {
123126
if *previous_hash == hash {
124127
return; // do not bother updating the db file because nothing has changed
@@ -262,7 +265,7 @@ mod test {
262265
let sql_cache = SqlIncrementalCache::new(conn, CacheDBHash::new(1));
263266
let file_path = PathBuf::from("/mod.ts");
264267
let file_text = "test";
265-
let file_hash = CacheDBHash::from_source(file_text);
268+
let file_hash = CacheDBHash::from_hashable(file_text);
266269
sql_cache.set_source_hash(&file_path, file_hash).unwrap();
267270
let cache = IncrementalCacheInner::from_sql_incremental_cache(
268271
sql_cache,

cli/cache/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,7 @@ impl Loader for FetchCacher {
298298
module_info: &deno_graph::ModuleInfo,
299299
) {
300300
log::debug!("Caching module info for {}", specifier);
301-
let source_hash = CacheDBHash::from_source(source);
301+
let source_hash = CacheDBHash::from_hashable(source);
302302
let result = self.module_info_cache.set_module_info(
303303
specifier,
304304
media_type,

cli/cache/module_info.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ impl<'a> ModuleInfoCacheModuleAnalyzer<'a> {
194194
source: &Arc<str>,
195195
) -> Result<ModuleInfo, deno_ast::ParseDiagnostic> {
196196
// attempt to load from the cache
197-
let source_hash = CacheDBHash::from_source(source);
197+
let source_hash = CacheDBHash::from_hashable(source);
198198
if let Some(info) =
199199
self.load_cached_module_info(specifier, media_type, source_hash)
200200
{
@@ -228,7 +228,7 @@ impl<'a> deno_graph::ModuleAnalyzer for ModuleInfoCacheModuleAnalyzer<'a> {
228228
media_type: MediaType,
229229
) -> Result<ModuleInfo, deno_ast::ParseDiagnostic> {
230230
// attempt to load from the cache
231-
let source_hash = CacheDBHash::from_source(&source);
231+
let source_hash = CacheDBHash::from_hashable(&source);
232232
if let Some(info) =
233233
self.load_cached_module_info(specifier, media_type, source_hash)
234234
{

cli/node.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ impl CliCjsCodeAnalyzer {
6868
specifier: &ModuleSpecifier,
6969
source: &str,
7070
) -> Result<CliCjsAnalysis, AnyError> {
71-
let source_hash = CacheDBHash::from_source(source);
71+
let source_hash = CacheDBHash::from_hashable(source);
7272
if let Some(analysis) =
7373
self.cache.get_cjs_analysis(specifier.as_str(), source_hash)
7474
{

cli/tools/fmt.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ use crate::args::FmtOptions;
4343
use crate::args::FmtOptionsConfig;
4444
use crate::args::ProseWrap;
4545
use crate::args::UnstableFmtOptions;
46+
use crate::cache::CacheDBHash;
4647
use crate::cache::Caches;
4748
use crate::cache::IncrementalCache;
4849
use crate::colors;
@@ -202,7 +203,7 @@ async fn format_files(
202203
let paths = paths_with_options.paths;
203204
let incremental_cache = Arc::new(IncrementalCache::new(
204205
caches.fmt_incremental_cache_db(),
205-
&(&fmt_options.options, &fmt_options.unstable), // cache key
206+
CacheDBHash::from_hashable((&fmt_options.options, &fmt_options.unstable)),
206207
&paths,
207208
));
208209
formatter

cli/tools/lint/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ use crate::args::Flags;
3939
use crate::args::LintFlags;
4040
use crate::args::LintOptions;
4141
use crate::args::WorkspaceLintOptions;
42+
use crate::cache::CacheDBHash;
4243
use crate::cache::Caches;
4344
use crate::cache::IncrementalCache;
4445
use crate::colors;
@@ -291,7 +292,7 @@ impl WorkspaceLinter {
291292
lint_rules.incremental_cache_state().map(|state| {
292293
Arc::new(IncrementalCache::new(
293294
self.caches.lint_incremental_cache_db(),
294-
&state,
295+
CacheDBHash::from_hashable(&state),
295296
&paths,
296297
))
297298
});

0 commit comments

Comments
 (0)