Skip to content

Commit e9020d3

Browse files
committed
Avoid allocations for default cache keys
1 parent 029b9e1 commit e9020d3

File tree

1 file changed

+16
-11
lines changed

1 file changed

+16
-11
lines changed

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

Lines changed: 16 additions & 11 deletions
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};
@@ -35,7 +36,7 @@ pub struct CacheInfo {
3536
env: BTreeMap<String, Option<String>>,
3637
/// The timestamp or inode of any directories that should be considered in the cache key.
3738
#[serde(default)]
38-
directories: BTreeMap<String, Option<DirectoryTimestamp>>,
39+
directories: BTreeMap<Cow<'static, str>, Option<DirectoryTimestamp>>,
3940
}
4041

4142
impl CacheInfo {
@@ -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,18 @@ 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
102+
.as_ref()
103+
.chars()
104+
.any(|c| matches!(c, '*' | '?' | '[' | '{'))
105+
{
101106
// Defer globs to a separate pass.
102107
globs.push(file);
103108
continue;
104109
}
105110

106111
// Treat the path as a file.
107-
let path = directory.join(&file);
112+
let path = directory.join(file.as_ref());
108113
let metadata = match path.metadata() {
109114
Ok(metadata) => metadata,
110115
Err(err) if err.kind() == std::io::ErrorKind::NotFound => {
@@ -126,7 +131,7 @@ impl CacheInfo {
126131
}
127132
CacheKey::Directory { dir } => {
128133
// Treat the path as a directory.
129-
let path = directory.join(&dir);
134+
let path = directory.join(dir.as_ref());
130135
let metadata = match path.metadata() {
131136
Ok(metadata) => metadata,
132137
Err(err) if err.kind() == std::io::ErrorKind::NotFound => {
@@ -296,11 +301,11 @@ struct ToolUv {
296301
#[serde(untagged, rename_all = "kebab-case", deny_unknown_fields)]
297302
pub enum CacheKey {
298303
/// Ex) `"Cargo.lock"` or `"**/*.toml"`
299-
Path(String),
304+
Path(Cow<'static, str>),
300305
/// Ex) `{ file = "Cargo.lock" }` or `{ file = "**/*.toml" }`
301-
File { file: String },
306+
File { file: Cow<'static, str> },
302307
/// Ex) `{ dir = "src" }`
303-
Directory { dir: String },
308+
Directory { dir: Cow<'static, str> },
304309
/// Ex) `{ git = true }` or `{ git = { commit = true, tags = false } }`
305310
Git { git: GitPattern },
306311
/// Ex) `{ env = "UV_CACHE_INFO" }`

0 commit comments

Comments
 (0)