From cb1a14334a7722e868058a6dda2176ae2f1bd52b Mon Sep 17 00:00:00 2001 From: Florian Klink Date: Sat, 14 Oct 2023 21:23:31 +0100 Subject: refactor(nix-compat/nixhash): box sha512 Limit the amount of memory consumed on the stack for NixHash. Sha512 isn't used that often, so it's fine if we heap-allocate it. Change-Id: I4a9eecd20c6184610124dc130c41bfa5d0dc04c5 Reviewed-on: https://cl.tvl.fyi/c/depot/+/9726 Tested-by: BuildkiteCI Autosubmit: flokli Reviewed-by: Connor Brewster --- tvix/nix-compat/src/nixhash/mod.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'tvix/nix-compat/src') diff --git a/tvix/nix-compat/src/nixhash/mod.rs b/tvix/nix-compat/src/nixhash/mod.rs index 727f1280eaa0..8cc918b04322 100644 --- a/tvix/nix-compat/src/nixhash/mod.rs +++ b/tvix/nix-compat/src/nixhash/mod.rs @@ -14,7 +14,7 @@ pub enum NixHash { Md5([u8; 16]), Sha1([u8; 20]), Sha256([u8; 32]), - Sha512([u8; 64]), + Sha512(Box<[u8; 64]>), } impl NixHash { @@ -34,7 +34,7 @@ impl NixHash { NixHash::Md5(digest) => digest, NixHash::Sha1(digest) => digest, NixHash::Sha256(digest) => digest, - NixHash::Sha512(digest) => digest, + NixHash::Sha512(digest) => digest.as_ref(), } } @@ -73,7 +73,7 @@ pub fn from_algo_and_digest(algo: HashAlgo, digest: &[u8]) -> Result NixHash::Md5(digest.try_into().unwrap()), HashAlgo::Sha1 => NixHash::Sha1(digest.try_into().unwrap()), HashAlgo::Sha256 => NixHash::Sha256(digest.try_into().unwrap()), - HashAlgo::Sha512 => NixHash::Sha512(digest.try_into().unwrap()), + HashAlgo::Sha512 => NixHash::Sha512(Box::new(digest.try_into().unwrap())), }) } @@ -307,7 +307,7 @@ mod tests { /// Test parsing a hash string in various formats, and also when/how the out-of-band algo is needed. #[test_case(&NixHash::Sha1(DIGEST_SHA1); "sha1")] #[test_case(&NixHash::Sha256(DIGEST_SHA256); "sha256")] - #[test_case(&NixHash::Sha512(DIGEST_SHA512); "sha512")] + #[test_case(&NixHash::Sha512(Box::new(DIGEST_SHA512)); "sha512")] #[test_case(&NixHash::Md5(DIGEST_MD5); "md5")] fn from_str(expected_hash: &NixHash) { let algo = &expected_hash.algo(); -- cgit 1.4.1