From 6b36d6396915181d38598928e35a20cf2308c5aa Mon Sep 17 00:00:00 2001 From: Andreas Grois Date: Mon, 24 Oct 2022 09:04:41 +0200 Subject: Improve long division performance. In PasswordMaker, the numbers that are fed into long division can only decrease. Therefore, skipping leading zeros is a rather reasonable improvement. --- src/passwordmaker/base_conversion/remainders_impl.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/passwordmaker/base_conversion/remainders_impl.rs b/src/passwordmaker/base_conversion/remainders_impl.rs index 7bc6c0e..8f0a7f9 100644 --- a/src/passwordmaker/base_conversion/remainders_impl.rs +++ b/src/passwordmaker/base_conversion/remainders_impl.rs @@ -13,7 +13,7 @@ impl Division for [u32;N] { //uses mutation, because why not? self is owned after all :D let divisor : UsizeAndFour = *divisor as UsizeAndFour; - let remainder = self.iter_mut().fold(0 as UsizeAndFour,|carry, current| { + let remainder = self.iter_mut().skip_while(|x| **x == 0).fold(0 as UsizeAndFour,|carry, current| { debug_assert_eq!(carry, carry & (usize::MAX as UsizeAndFour)); //carry has to be lower than divisor, and divisor is usize. let carry_shifted = carry << 32; let dividend = (carry_shifted) + (*current as UsizeAndFour); -- cgit v1.2.3