MCPcopy Create free account
hub / github.com/bytecodealliance/wasmtime / handle_on_cache_get

Method handle_on_cache_get

crates/cache/src/worker.rs:268–394  ·  view source on GitHub ↗

Increases the usage counter and recompresses the file if the usage counter reached configurable threshold.

(&self, path: PathBuf)

Source from the content-addressed store, hash-verified

266 /// Increases the usage counter and recompresses the file
267 /// if the usage counter reached configurable threshold.
268 fn handle_on_cache_get(&self, path: PathBuf) {
269 trace!("handle_on_cache_get() for path: {}", path.display());
270
271 // construct .stats file path
272 let filename = path.file_name().unwrap().to_str().unwrap();
273 let stats_path = path.with_file_name(format!("{filename}.stats"));
274
275 // load .stats file (default if none or error)
276 let mut stats = read_stats_file(stats_path.as_ref())
277 .unwrap_or_else(|| ModuleCacheStatistics::default(&self.cache_config));
278
279 // step 1: update the usage counter & write to the disk
280 // it's racy, but it's fine (the counter will be just smaller,
281 // sometimes will retrigger recompression)
282 stats.usages += 1;
283 if !write_stats_file(stats_path.as_ref(), &stats) {
284 return;
285 }
286
287 // step 2: recompress if there's a need
288 let opt_compr_lvl = self.cache_config.optimized_compression_level();
289 if stats.compression_level >= opt_compr_lvl
290 || stats.usages
291 < self
292 .cache_config
293 .optimized_compression_usage_counter_threshold()
294 {
295 return;
296 }
297
298 let lock_path = if let Some(p) = acquire_task_fs_lock(
299 path.as_ref(),
300 self.cache_config.optimizing_compression_task_timeout(),
301 self.cache_config
302 .allowed_clock_drift_for_files_from_future(),
303 ) {
304 p
305 } else {
306 return;
307 };
308
309 trace!("Trying to recompress file: {}", path.display());
310
311 // recompress, write to other file, rename (it's atomic file content exchange)
312 // and update the stats file
313 let compressed_cache_bytes = unwrap_or_warn!(
314 fs::read(&path),
315 return,
316 "Failed to read old cache file",
317 path
318 );
319
320 let cache_bytes = unwrap_or_warn!(
321 zstd::decode_all(&compressed_cache_bytes[..]),
322 return,
323 "Failed to decompress cached code",
324 path
325 );

Callers 1

runMethod · 0.80

Calls 7

read_stats_fileFunction · 0.85
write_stats_fileFunction · 0.85
acquire_task_fs_lockFunction · 0.85
to_strMethod · 0.80
file_nameMethod · 0.80
unwrapMethod · 0.45
as_refMethod · 0.45

Tested by

no test coverage detected