From 44d2bfeb657e0a6e2bada38b2e1a966189df289e Mon Sep 17 00:00:00 2001 From: Charlie Marsh Date: Mon, 20 Jan 2025 20:53:12 -0500 Subject: [PATCH] Remove allocation in Git SHA truncation (#10801) --- crates/uv-distribution/src/index/built_wheel_index.rs | 2 +- crates/uv-distribution/src/source/mod.rs | 4 ++-- crates/uv-git/src/sha.rs | 11 ++++++++--- 3 files changed, 11 insertions(+), 6 deletions(-) diff --git a/crates/uv-distribution/src/index/built_wheel_index.rs b/crates/uv-distribution/src/index/built_wheel_index.rs index 45a04d939..8d51003b2 100644 --- a/crates/uv-distribution/src/index/built_wheel_index.rs +++ b/crates/uv-distribution/src/index/built_wheel_index.rs @@ -170,7 +170,7 @@ impl<'a> BuiltWheelIndex<'a> { let cache_shard = self.cache.shard( CacheBucket::SourceDistributions, - WheelCache::Git(&source_dist.url, &git_sha.to_short_string()).root(), + WheelCache::Git(&source_dist.url, git_sha.as_short_str()).root(), ); // If there are build settings, we need to scope to a cache shard. diff --git a/crates/uv-distribution/src/source/mod.rs b/crates/uv-distribution/src/source/mod.rs index 8faa3844f..dc5bdb57e 100644 --- a/crates/uv-distribution/src/source/mod.rs +++ b/crates/uv-distribution/src/source/mod.rs @@ -1426,7 +1426,7 @@ impl<'a, T: BuildContext> SourceDistributionBuilder<'a, T> { let git_sha = fetch.git().precise().expect("Exact commit after checkout"); let cache_shard = self.build_context.cache().shard( CacheBucket::SourceDistributions, - WheelCache::Git(resource.url, &git_sha.to_short_string()).root(), + WheelCache::Git(resource.url, git_sha.as_short_str()).root(), ); let metadata_entry = cache_shard.entry(METADATA); @@ -1564,7 +1564,7 @@ impl<'a, T: BuildContext> SourceDistributionBuilder<'a, T> { let git_sha = fetch.git().precise().expect("Exact commit after checkout"); let cache_shard = self.build_context.cache().shard( CacheBucket::SourceDistributions, - WheelCache::Git(resource.url, &git_sha.to_short_string()).root(), + WheelCache::Git(resource.url, git_sha.as_short_str()).root(), ); let metadata_entry = cache_shard.entry(METADATA); diff --git a/crates/uv-git/src/sha.rs b/crates/uv-git/src/sha.rs index 7e36ee719..8adf8d4e7 100644 --- a/crates/uv-git/src/sha.rs +++ b/crates/uv-git/src/sha.rs @@ -8,9 +8,14 @@ use thiserror::Error; pub struct GitSha(GitOid); impl GitSha { - /// Convert the SHA to a truncated representation, i.e., the first 16 characters of the SHA. - pub fn to_short_string(&self) -> String { - self.0.to_string()[0..16].to_string() + /// Return the Git SHA as a string. + pub fn as_str(&self) -> &str { + self.0.as_str() + } + + /// Return a truncated representation, i.e., the first 16 characters of the SHA. + pub fn as_short_str(&self) -> &str { + &self.0.as_str()[..16] } }