Skip to content

Commit

Permalink
Wrap cache reader in BufReader for speed. (#166)
Browse files Browse the repository at this point in the history
Profiling reading from the cache in the download of a 1GB file showed
that 48% of the time was spent in system read calls. This PR removes
that bottleneck by simply wrapping the read calls in a BufReader. This
eliminates that bottleneck and showed a 15-20% overall speed improvement
restoring the 1GB file from cache (based on a few non-rigorous bash
`time` runs).
  • Loading branch information
hoytak authored Feb 6, 2025
1 parent f21b60c commit 169c368
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions chunk_cache/src/disk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -264,15 +264,17 @@ impl DiskCache {
}
}

let Ok(header) = CacheFileHeader::deserialize(&mut file)
let mut file_reader = std::io::BufReader::new(file);

let Ok(header) = CacheFileHeader::deserialize(&mut file_reader)
.debug_error(format!("failed to deserialize cache file header on path: {path:?}"))
else {
self.remove_item(key, &cache_item)?;
continue;
};

let start = cache_item.range.start;
let result_buf = get_range_from_cache_file(&header, &mut file, range, start)?;
let result_buf = get_range_from_cache_file(&header, &mut file_reader, range, start)?;
return Ok(Some(result_buf));
}
}
Expand Down

0 comments on commit 169c368

Please sign in to comment.