From 4892272a08708a0f0ea92ba1e138e70e88072c03 Mon Sep 17 00:00:00 2001 From: Michael Dyer Date: Wed, 28 Feb 2024 15:27:00 +0100 Subject: [PATCH] Return earlier if original_length < 2 and remove unnecessary call to saturating_sub() --- gitbutler-diff/src/signature.rs | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/gitbutler-diff/src/signature.rs b/gitbutler-diff/src/signature.rs index 47f840adb..36c1ebb09 100644 --- a/gitbutler-diff/src/signature.rs +++ b/gitbutler-diff/src/signature.rs @@ -81,13 +81,16 @@ impl Signature { panic!("unsupported signature version"); } - let original_length = u32::from_le_bytes(self.0[1..5].try_into().unwrap()); + let original_length = u32::from_le_bytes(self.0[1..5].try_into().expect("invalid length")); + if original_length < 2 { + return 0.0; + } let other = other.as_ref(); let other_string: String = other.chars().filter(|&x| !char::is_whitespace(x)).collect(); let other = other_string.as_bytes(); - if original_length < 2 || other.len() < 2 { + if other.len() < 2 { return 0.0; } @@ -100,7 +103,7 @@ impl Signature { let right = right >> SHIFT; let index = ((left as usize) << BITS) | (right as usize); if self_buckets[index] > 0 { - self_buckets[index] = self_buckets[index].saturating_sub(1); + self_buckets[index] = self_buckets[index] - 1; matching_bigrams += 1; } }