From 370f31a284763604f1940d01dfc722502e4d0595 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=C3=89lo=C3=AFs?= <c@elo.tf>
Date: Mon, 11 Jul 2022 17:24:26 +0200
Subject: [PATCH] perf(claim_uds): remove one comparison per reeval (!85)

* perf(claim_uds): remove one comparison per reeval
---
 pallets/universal-dividend/src/compute_claim_uds.rs     | 9 ++++++---
 runtime/common/src/weights/pallet_universal_dividend.rs | 2 +-
 2 files changed, 7 insertions(+), 4 deletions(-)

diff --git a/pallets/universal-dividend/src/compute_claim_uds.rs b/pallets/universal-dividend/src/compute_claim_uds.rs
index 5e214b977..02b381603 100644
--- a/pallets/universal-dividend/src/compute_claim_uds.rs
+++ b/pallets/universal-dividend/src/compute_claim_uds.rs
@@ -26,12 +26,15 @@ pub(super) fn compute_claim_uds<Balance: AtLeast32BitUnsigned>(
     let mut total_amount = Zero::zero();
     let mut total_count = 0;
     for (ud_index, ud_amount) in past_reevals.rev() {
-        let count = current_ud_index - core::cmp::max(ud_index, first_ud_index);
-        total_amount += Balance::from(count) * ud_amount;
-        total_count += count;
         if ud_index <= first_ud_index {
+            let count = current_ud_index - first_ud_index;
+            total_amount += Balance::from(count) * ud_amount;
+            total_count += count;
             break;
         } else {
+            let count = current_ud_index - ud_index;
+            total_amount += Balance::from(count) * ud_amount;
+            total_count += count;
             current_ud_index = ud_index;
         }
     }
diff --git a/runtime/common/src/weights/pallet_universal_dividend.rs b/runtime/common/src/weights/pallet_universal_dividend.rs
index 9f196cb7f..9c9efb8f9 100644
--- a/runtime/common/src/weights/pallet_universal_dividend.rs
+++ b/runtime/common/src/weights/pallet_universal_dividend.rs
@@ -76,7 +76,7 @@ impl<T: frame_system::Config> pallet_universal_dividend::WeightInfo for WeightIn
 	fn claim_uds(n: u32) -> Weight {
 		(1_228_876_000 as Weight)
 			// Standard Error: 958_000
-			.saturating_add((7_709_000 as Weight).saturating_mul(n as Weight))
+			.saturating_add((7_551_000 as Weight).saturating_mul(n as Weight))
 			.saturating_add(T::DbWeight::get().reads(4 as Weight))
 			.saturating_add(T::DbWeight::get().writes(1 as Weight))
 	}
-- 
GitLab