From a8bd926e0fc22c706d01fe44db2d9f6ea2a552a4 Mon Sep 17 00:00:00 2001 From: John Ericson Date: Thu, 30 Mar 2023 19:32:45 -0400 Subject: refactor(nix-compat): Avoid encoding round trip When building store paths we can just construct the thing. Change-Id: Ife5d461d6a440ecbb22f32a86a6d51d212a2035b Reviewed-on: https://cl.tvl.fyi/c/depot/+/8409 Reviewed-by: flokli Tested-by: BuildkiteCI --- tvix/nix-compat/src/store_path/utils.rs | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) (limited to 'tvix') diff --git a/tvix/nix-compat/src/store_path/utils.rs b/tvix/nix-compat/src/store_path/utils.rs index 449117717260..fa1908ee3552 100644 --- a/tvix/nix-compat/src/store_path/utils.rs +++ b/tvix/nix-compat/src/store_path/utils.rs @@ -7,7 +7,7 @@ use super::Error; /// compress_hash takes an arbitrarily long sequence of bytes (usually /// a hash digest), and returns a sequence of bytes of length -/// output_size. +/// OUTPUT_SIZE. /// /// It's calculated by rotating through the bytes in the output buffer /// (zero- initialized), and XOR'ing with each byte of the passed @@ -15,11 +15,11 @@ use super::Error; /// value in the output buffer. /// /// This mimics equivalent functionality in C++ Nix. -pub fn compress_hash(input: &[u8], output_size: usize) -> Vec { - let mut output: Vec = vec![0; output_size]; +pub fn compress_hash(input: &[u8]) -> [u8; OUTPUT_SIZE] { + let mut output = [0; OUTPUT_SIZE]; for (ii, ch) in input.iter().enumerate() { - output[ii % output_size] ^= ch; + output[ii % OUTPUT_SIZE] ^= ch; } output @@ -56,8 +56,12 @@ pub fn build_store_path_from_fingerprint( let hasher = Sha256::new_with_prefix(fingerprint); hasher.finalize() }; - let compressed = compress_hash(&digest, 20); - StorePath::from_string(format!("{}-{}", nixbase32::encode(&compressed), name).as_str()) + let compressed = compress_hash::<20>(&digest); + StorePath::validate_name(name)?; + Ok(StorePath { + digest: compressed, + name: name.to_string(), + }) } /// This contains the Nix logic to create "text hash strings", which are used -- cgit 1.4.1