Skip to content

Commit b1c26b3

Browse files
author
Samuel Dare
committed
feat: clippy
1 parent abc7798 commit b1c26b3

File tree

3 files changed

+49
-62
lines changed

3 files changed

+49
-62
lines changed

pallets/subtensor/src/epoch.rs

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ impl<T: Config> Pallet<T> {
3232
// Inactive mask.
3333
let inactive: Vec<bool> = last_update
3434
.iter()
35-
.map(|updated| *updated + activity_cutoff < current_block)
35+
.map(|updated| updated.saturating_add(activity_cutoff) < current_block)
3636
.collect();
3737
log::trace!("Inactive:\n{:?}\n", inactive.clone());
3838

@@ -193,7 +193,7 @@ impl<T: Config> Pallet<T> {
193193
let combined_emission: Vec<I32F32> = incentive
194194
.iter()
195195
.zip(dividends.clone())
196-
.map(|(ii, di)| ii + di)
196+
.map(|(ii, di)| ii.saturating_add(di))
197197
.collect();
198198
let emission_sum: I32F32 = combined_emission.iter().sum();
199199

@@ -223,7 +223,7 @@ impl<T: Config> Pallet<T> {
223223

224224
let server_emission: Vec<I96F32> = normalized_server_emission
225225
.iter()
226-
.map(|se: &I32F32| I96F32::from_num(*se) * float_rao_emission)
226+
.map(|se: &I32F32| I96F32::from_num(*se).saturating_mul(float_rao_emission))
227227
.collect();
228228
let server_emission: Vec<u64> = server_emission
229229
.iter()
@@ -232,7 +232,7 @@ impl<T: Config> Pallet<T> {
232232

233233
let validator_emission: Vec<I96F32> = normalized_validator_emission
234234
.iter()
235-
.map(|ve: &I32F32| I96F32::from_num(*ve) * float_rao_emission)
235+
.map(|ve: &I32F32| I96F32::from_num(*ve).saturating_mul(float_rao_emission))
236236
.collect();
237237
let validator_emission: Vec<u64> = validator_emission
238238
.iter()
@@ -242,7 +242,7 @@ impl<T: Config> Pallet<T> {
242242
// Used only to track combined emission in the storage.
243243
let combined_emission: Vec<I96F32> = normalized_combined_emission
244244
.iter()
245-
.map(|ce: &I32F32| I96F32::from_num(*ce) * float_rao_emission)
245+
.map(|ce: &I32F32| I96F32::from_num(*ce).saturating_mul(float_rao_emission))
246246
.collect();
247247
let combined_emission: Vec<u64> = combined_emission
248248
.iter()
@@ -371,7 +371,7 @@ impl<T: Config> Pallet<T> {
371371
// Inactive mask.
372372
let inactive: Vec<bool> = last_update
373373
.iter()
374-
.map(|updated| *updated + activity_cutoff < current_block)
374+
.map(|updated| updated.saturating_add(activity_cutoff) < current_block)
375375
.collect();
376376
log::trace!("Inactive: {:?}", inactive.clone());
377377

@@ -551,7 +551,7 @@ impl<T: Config> Pallet<T> {
551551
let combined_emission: Vec<I32F32> = incentive
552552
.iter()
553553
.zip(dividends.clone())
554-
.map(|(ii, di)| ii + di)
554+
.map(|(ii, di)| ii.saturating_add(di))
555555
.collect();
556556
let emission_sum: I32F32 = combined_emission.iter().sum();
557557

@@ -581,7 +581,7 @@ impl<T: Config> Pallet<T> {
581581

582582
let server_emission: Vec<I96F32> = normalized_server_emission
583583
.iter()
584-
.map(|se: &I32F32| I96F32::from_num(*se) * float_rao_emission)
584+
.map(|se: &I32F32| I96F32::from_num(*se).saturating_mul(float_rao_emission))
585585
.collect();
586586
let server_emission: Vec<u64> = server_emission
587587
.iter()
@@ -590,7 +590,7 @@ impl<T: Config> Pallet<T> {
590590

591591
let validator_emission: Vec<I96F32> = normalized_validator_emission
592592
.iter()
593-
.map(|ve: &I32F32| I96F32::from_num(*ve) * float_rao_emission)
593+
.map(|ve: &I32F32| I96F32::from_num(*ve).saturating_mul(float_rao_emission))
594594
.collect();
595595
let validator_emission: Vec<u64> = validator_emission
596596
.iter()
@@ -600,7 +600,7 @@ impl<T: Config> Pallet<T> {
600600
// Only used to track emission in storage.
601601
let combined_emission: Vec<I96F32> = normalized_combined_emission
602602
.iter()
603-
.map(|ce: &I32F32| I96F32::from_num(*ce) * float_rao_emission)
603+
.map(|ce: &I32F32| I96F32::from_num(*ce).saturating_mul(float_rao_emission))
604604
.collect();
605605
let combined_emission: Vec<u64> = combined_emission
606606
.iter()
@@ -706,7 +706,7 @@ impl<T: Config> Pallet<T> {
706706
I32F32::from_num(Self::get_rho(netuid))
707707
}
708708
pub fn get_float_kappa(netuid: u16) -> I32F32 {
709-
I32F32::from_num(Self::get_kappa(netuid)) / I32F32::from_num(u16::MAX)
709+
I32F32::from_num(Self::get_kappa(netuid)).saturating_div(I32F32::from_num(u16::MAX))
710710
}
711711

712712
pub fn get_normalized_stake(netuid: u16) -> Vec<I32F32> {
@@ -855,8 +855,10 @@ impl<T: Config> Pallet<T> {
855855

856856
// Calculate the intercept 'b' of the logistic function.
857857
// b = ln((1 / alpha_low - 1)) + a * consensus_low
858-
let b = safe_ln((I32F32::from_num(1.0) / alpha_low).saturating_sub(I32F32::from_num(1.0)))
859-
.saturating_add(a.saturating_mul(consensus_low));
858+
let b = safe_ln(
859+
(I32F32::from_num(1.0).saturating_div(alpha_low)).saturating_sub(I32F32::from_num(1.0)),
860+
)
861+
.saturating_add(a.saturating_mul(consensus_low));
860862
log::trace!("b: {:?}", b);
861863

862864
// Return the calculated slope 'a' and intercept 'b'.

pallets/subtensor/src/math.rs

Lines changed: 28 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1148,36 +1148,6 @@ pub fn sparse_threshold(w: &[Vec<(u16, I32F32)>], threshold: I32F32) -> Vec<Vec<
11481148
.collect()
11491149
}
11501150

1151-
// /// Calculates the exponential moving average (EMA) for a sparse matrix using dynamic alpha values.
1152-
// #[allow(dead_code)]
1153-
// pub fn mat_ema_alpha_vec_sparse(
1154-
// new: &Vec<Vec<(u16, I32F32)>>,
1155-
// old: &Vec<Vec<(u16, I32F32)>>,
1156-
// alpha: &Vec<I32F32>,
1157-
// ) -> Vec<Vec<(u16, I32F32)>> {
1158-
// assert!(new.len() == old.len());
1159-
// let n = new.len(); // assume square matrix, rows=cols
1160-
// let zero: I32F32 = I32F32::from_num(0.0);
1161-
// let mut result: Vec<Vec<(u16, I32F32)>> = vec![vec![]; n];
1162-
// for i in 0..new.len() {
1163-
// let mut row: Vec<I32F32> = vec![zero; n];
1164-
// for (j, value) in new[i].iter() {
1165-
// let alpha_val: I32F32 = alpha[*j as usize];
1166-
// row[*j as usize] += alpha_val * value;
1167-
// }
1168-
// for (j, value) in old[i].iter() {
1169-
// let one_minus_alpha: I32F32 = I32F32::from_num(1.0) - alpha[*j as usize];
1170-
// row[*j as usize] += one_minus_alpha * value;
1171-
// }
1172-
// for (j, value) in row.iter().enumerate() {
1173-
// if *value > zero {
1174-
// result[i].push((j as u16, *value))
1175-
// }
1176-
// }
1177-
// }
1178-
// result
1179-
// }
1180-
11811151
/// Calculates the exponential moving average (EMA) for a sparse matrix using dynamic alpha values.
11821152
#[allow(dead_code)]
11831153
pub fn mat_ema_alpha_vec_sparse(
@@ -1192,36 +1162,39 @@ pub fn mat_ema_alpha_vec_sparse(
11921162
let mut result: Vec<Vec<(u16, I32F32)>> = vec![vec![]; n];
11931163

11941164
// Iterate over each row of the matrices.
1195-
for i in 0..new.len() {
1165+
for (i, (new_row, old_row)) in new.iter().zip(old).enumerate() {
11961166
// Initialize a row of zeros for the result matrix.
11971167
let mut row: Vec<I32F32> = vec![zero; n];
11981168

11991169
// Process the new matrix values.
1200-
for (j, value) in new[i].iter() {
1170+
for (j, value) in new_row.iter() {
12011171
// Retrieve the alpha value for the current column.
1202-
let alpha_val: I32F32 = alpha[*j as usize];
1172+
let alpha_val: I32F32 = alpha.get(*j as usize).copied().unwrap_or(zero);
12031173
// Compute the EMA component for the new value using saturating multiplication.
1204-
row[*j as usize] = alpha_val.saturating_mul(*value);
1174+
if let Some(row_val) = row.get_mut(*j as usize) {
1175+
*row_val = alpha_val.saturating_mul(*value);
1176+
}
12051177
log::trace!(
12061178
"new[{}][{}] * alpha[{}] = {} * {} = {}",
12071179
i,
12081180
j,
12091181
j,
12101182
value,
12111183
alpha_val,
1212-
row[*j as usize]
1184+
row.get(*j as usize).unwrap_or(&zero)
12131185
);
12141186
}
12151187

12161188
// Process the old matrix values.
1217-
for (j, value) in old[i].iter() {
1189+
for (j, value) in old_row.iter() {
12181190
// Retrieve the alpha value for the current column.
1219-
let alpha_val: I32F32 = alpha[*j as usize];
1191+
let alpha_val: I32F32 = alpha.get(*j as usize).copied().unwrap_or(zero);
12201192
// Calculate the complement of the alpha value using saturating subtraction.
12211193
let one_minus_alpha: I32F32 = I32F32::from_num(1.0).saturating_sub(alpha_val);
12221194
// Compute the EMA component for the old value and add it to the row using saturating operations.
1223-
row[*j as usize] =
1224-
row[*j as usize].saturating_add(one_minus_alpha.saturating_mul(*value));
1195+
if let Some(row_val) = row.get_mut(*j as usize) {
1196+
*row_val = row_val.saturating_add(one_minus_alpha.saturating_mul(*value));
1197+
}
12251198
log::trace!(
12261199
"old[{}][{}] * (1 - alpha[{}]) = {} * {} = {}",
12271200
i,
@@ -1236,15 +1209,18 @@ pub fn mat_ema_alpha_vec_sparse(
12361209
// Collect the non-zero values into the result matrix.
12371210
for (j, value) in row.iter().enumerate() {
12381211
if *value > zero {
1239-
result[i].push((j as u16, *value));
1240-
log::trace!("result[{}][{}] = {}", i, j, value);
1212+
if let Some(result_row) = result.get_mut(i) {
1213+
result_row.push((j as u16, *value));
1214+
log::trace!("result[{}][{}] = {}", i, j, value);
1215+
}
12411216
}
12421217
}
12431218
}
12441219

12451220
// Return the computed EMA sparse matrix.
12461221
result
12471222
}
1223+
12481224
/// Return matrix exponential moving average: `alpha_j * a_ij + one_minus_alpha_j * b_ij`.
12491225
/// `alpha_` is the EMA coefficient passed as a vector per column.
12501226
#[allow(dead_code)]
@@ -1254,13 +1230,13 @@ pub fn mat_ema_alpha_vec(
12541230
alpha: &[I32F32],
12551231
) -> Vec<Vec<I32F32>> {
12561232
// Check if the new matrix is empty or its first row is empty.
1257-
if new.is_empty() || new[0].is_empty() {
1233+
if new.is_empty() || new.first().map_or(true, |row| row.is_empty()) {
12581234
return vec![vec![]; 1];
12591235
}
12601236

12611237
// Ensure the dimensions of the new and old matrices match.
12621238
assert!(new.len() == old.len());
1263-
assert!(new[0].len() == alpha.len());
1239+
assert!(new.first().map_or(0, |row| row.len()) == alpha.len());
12641240

12651241
// Initialize the result matrix with zeros, having the same dimensions as the new matrix.
12661242
let mut result: Vec<Vec<I32F32>> =
@@ -1277,9 +1253,15 @@ pub fn mat_ema_alpha_vec(
12771253
let one_minus_alpha = I32F32::from_num(1.0).saturating_sub(alpha_val);
12781254

12791255
// Compute the EMA for the current element using saturating operations.
1280-
result[i][j] = alpha_val
1281-
.saturating_mul(new_row[j])
1282-
.saturating_add(one_minus_alpha.saturating_mul(old_row[j]));
1256+
if let (Some(new_val), Some(old_val), Some(result_val)) = (
1257+
new_row.get(j),
1258+
old_row.get(j),
1259+
result.get_mut(i).and_then(|row| row.get_mut(j)),
1260+
) {
1261+
*result_val = alpha_val
1262+
.saturating_mul(*new_val)
1263+
.saturating_add(one_minus_alpha.saturating_mul(*old_val));
1264+
}
12831265
}
12841266
}
12851267

pallets/subtensor/tests/math.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1-
#![allow(clippy::unwrap_used)]
2-
#![allow(clippy::panic)]
3-
#![allow(clippy::indexing_slicing)]
1+
#![allow(
2+
clippy::unwrap_used,
3+
clippy::panic,
4+
clippy::indexing_slicing,
5+
clippy::arithmetic_side_effects
6+
)]
47
use substrate_fixed::types::{I32F32, I64F64};
58

69
use pallet_subtensor::math::*;

0 commit comments

Comments
 (0)