Skip to content

Commit 47670bc

Browse files
committed
Avoid allocations for default cache keys
1 parent 8eb68c5 commit 47670bc

File tree

1 file changed

+11
-10
lines changed

1 file changed

+11
-10
lines changed

crates/uv-cache-info/src/cache_info.rs

+11-10
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use std::borrow::Cow;
12
use std::cmp::max;
23
use std::collections::BTreeMap;
34
use std::path::{Path, PathBuf};
@@ -83,11 +84,11 @@ impl CacheInfo {
8384
// If no cache keys were defined, use the defaults.
8485
let cache_keys = cache_keys.unwrap_or_else(|| {
8586
vec![
86-
CacheKey::Path("pyproject.toml".to_string()),
87-
CacheKey::Path("setup.py".to_string()),
88-
CacheKey::Path("setup.cfg".to_string()),
87+
CacheKey::Path(Cow::Borrowed("pyproject.toml")),
88+
CacheKey::Path(Cow::Borrowed("setup.py")),
89+
CacheKey::Path(Cow::Borrowed("setup.cfg")),
8990
CacheKey::Directory {
90-
dir: "src".to_string(),
91+
dir: Cow::Borrowed("src"),
9192
},
9293
]
9394
});
@@ -97,14 +98,14 @@ impl CacheInfo {
9798
for cache_key in cache_keys {
9899
match cache_key {
99100
CacheKey::Path(file) | CacheKey::File { file } => {
100-
if file.chars().any(|c| matches!(c, '*' | '?' | '[' | '{')) {
101+
if file.as_ref().chars().any(|c| matches!(c, '*' | '?' | '[' | '{')) {
101102
// Defer globs to a separate pass.
102103
globs.push(file);
103104
continue;
104105
}
105106

106107
// Treat the path as a file.
107-
let path = directory.join(&file);
108+
let path = directory.join(file.as_ref());
108109
let metadata = match path.metadata() {
109110
Ok(metadata) => metadata,
110111
Err(err) if err.kind() == std::io::ErrorKind::NotFound => {
@@ -126,7 +127,7 @@ impl CacheInfo {
126127
}
127128
CacheKey::Directory { dir } => {
128129
// Treat the path as a directory.
129-
let path = directory.join(&dir);
130+
let path = directory.join(dir.as_ref());
130131
let metadata = match path.metadata() {
131132
Ok(metadata) => metadata,
132133
Err(err) if err.kind() == std::io::ErrorKind::NotFound => {
@@ -287,11 +288,11 @@ struct ToolUv {
287288
#[serde(untagged, rename_all = "kebab-case", deny_unknown_fields)]
288289
pub enum CacheKey {
289290
/// Ex) `"Cargo.lock"` or `"**/*.toml"`
290-
Path(String),
291+
Path(Cow<'static, str>),
291292
/// Ex) `{ file = "Cargo.lock" }` or `{ file = "**/*.toml" }`
292-
File { file: String },
293+
File { file: Cow<'static, str> },
293294
/// Ex) `{ dir = "src" }`
294-
Directory { dir: String },
295+
Directory { dir: Cow<'static, str> },
295296
/// Ex) `{ git = true }` or `{ git = { commit = true, tags = false } }`
296297
Git { git: GitPattern },
297298
/// Ex) `{ env = "UV_CACHE_INFO" }`

0 commit comments

Comments
 (0)