Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 33 additions & 37 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ anyhow = { version = "1.0.80" }
arc-swap = { version = "1.7.1" }
argfile = { version = "1.0.0" }
assert_fs = { version = "1.1.0" }
bitcode = { version = "0.6.9" }
bincode = { version = "2.0.0" }
bitflags = { version = "2.5.0" }
bitvec = { version = "1.0.1", default-features = false, features = [
"alloc",
Expand Down
2 changes: 1 addition & 1 deletion crates/ruff/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ ruff_workspace = { workspace = true }

anyhow = { workspace = true }
argfile = { workspace = true }
bitcode = { workspace = true, features = ["serde"] }
bincode = { workspace = true, features = ["serde"] }
bitflags = { workspace = true }
cachedir = { workspace = true }
clap = { workspace = true, features = ["derive", "env", "wrap_help"] }
Expand Down
38 changes: 20 additions & 18 deletions crates/ruff/src/cache.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::fmt::Debug;
use std::fs;
use std::fs::{self, File};
use std::hash::Hasher;
use std::io::{self, Write};
use std::io::{self, BufReader, Write};
use std::path::{Path, PathBuf};
use std::sync::Mutex;
use std::sync::atomic::{AtomicU64, Ordering};
Expand Down Expand Up @@ -97,8 +97,8 @@ impl Cache {
let key = format!("{}", cache_key(&package_root, settings));
let path = PathBuf::from_iter([&settings.cache_dir, Path::new(VERSION), Path::new(&key)]);

let serialized = match fs::read(&path) {
Ok(serialized) => serialized,
let file = match File::open(&path) {
Ok(file) => file,
Err(err) if err.kind() == io::ErrorKind::NotFound => {
// No cache exist yet, return an empty cache.
return Cache::empty(path, package_root);
Expand All @@ -109,13 +109,14 @@ impl Cache {
}
};

let mut package: PackageCache = match bitcode::deserialize(&serialized) {
Ok(package) => package,
Err(err) => {
warn_user!("Failed parse cache file `{}`: {err}", path.display());
return Cache::empty(path, package_root);
}
};
let mut package: PackageCache =
match bincode::decode_from_reader(BufReader::new(file), bincode::config::standard()) {
Ok(package) => package,
Err(err) => {
warn_user!("Failed parse cache file `{}`: {err}", path.display());
return Cache::empty(path, package_root);
}
};

// Sanity check.
if package.package_root != package_root {
Expand Down Expand Up @@ -167,7 +168,8 @@ impl Cache {

// Serialize to in-memory buffer because hyperfine benchmark showed that it's faster than
// using a `BufWriter` and our cache files are small enough that streaming isn't necessary.
let serialized = bitcode::serialize(&self.package).context("Failed to serialize cache")?;
let serialized = bincode::encode_to_vec(&self.package, bincode::config::standard())
.context("Failed to serialize cache data")?;
temp_file
.write_all(&serialized)
.context("Failed to write serialized cache to temporary file.")?;
Expand Down Expand Up @@ -296,8 +298,8 @@ impl Cache {
}
}

/// Runtime representation of a cache of a package.
#[derive(Debug, serde::Serialize, serde::Deserialize)]
/// On disk representation of a cache of a package.
#[derive(bincode::Encode, Debug, bincode::Decode)]
struct PackageCache {
/// Path to the root of the package.
///
Expand All @@ -308,8 +310,8 @@ struct PackageCache {
files: FxHashMap<RelativePathBuf, FileCache>,
}

/// Runtime representation of the cache per source file.
#[derive(Debug, serde::Serialize, serde::Deserialize)]
/// On disk representation of the cache per source file.
#[derive(bincode::Decode, Debug, bincode::Encode)]
pub(crate) struct FileCache {
/// Key that determines if the cached item is still valid.
key: u64,
Expand All @@ -329,7 +331,7 @@ impl FileCache {
}
}

#[derive(Debug, Default, serde::Serialize, serde::Deserialize)]
#[derive(Debug, Default, bincode::Decode, bincode::Encode)]
struct FileCacheData {
linted: bool,
formatted: bool,
Expand Down Expand Up @@ -570,7 +572,7 @@ mod tests {
expected_diagnostics += diagnostics;
}
}
assert_ne!(paths, &[] as &[PathBuf], "no files checked");
assert_ne!(paths, &[] as &[std::path::PathBuf], "no files checked");

cache.persist().unwrap();

Expand Down
Loading