Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refac(pallets/subspace): making code more robust #38

Merged
merged 9 commits into from
Mar 18, 2024
Merged

Conversation

Supremesource
Copy link
Member

Simplifying the code, fixing clones, making more readable

@aripiprazole
Copy link
Member

I would rename the title so the name of the squash commit would be more explicit

for i in x.iter_mut() {
*i /= x_sum;
if x_sum != I32F32::from_num(0) {
x.iter_mut().for_each(|i| *i /= x_sum);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would still use for instead of for_each

pallets/subspace/src/step.rs Show resolved Hide resolved
pallets/subspace/src/step.rs Show resolved Hide resolved

if is_founder_registered {
let founder_share: u16 = Self::get_founder_share(netuid);
if founder_share > 0 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The same thing here with nested ifs

pallets/subspace/src/weights.rs Show resolved Hide resolved
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should replace this prints with asserts instead of just replacing with logs

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But this can be left for further and another PRs

pallets/subspace/tests/weights.rs Show resolved Hide resolved
@Supremesource Supremesource changed the title Refac v1.4.0 Refactoring code complexity Mar 16, 2024
@Supremesource
Copy link
Member Author

d4b3f23
@aripiprazole Applied most of the things you asked for

@@ -31,7 +31,6 @@ impl<T: Config> Pallet<T> {
}
}

// TODO: make sure there are checks for all values

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was this addressed?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@@ -99,6 +98,9 @@ impl<T: Config> Pallet<T> {
}

pub fn set_global_params(params: GlobalParams) {
// Check if the params are valid
Self::check_global_params(params.clone()).unwrap();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm pretty sure check_global_params could receive &GlobalParams instead and avoid cloning it here.

Change it to an expect statement. Unwraps are usually unhelpful.

Suggested change
Self::check_global_params(params.clone()).unwrap();
Self::check_global_params(params.clone()).expect("global params are invalid");

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Were the storage items removed or relocated? I can't find Key2Controller. This will most probably mess with the genesis building stage, was this tested?

@@ -43,12 +43,12 @@ pub fn mask_diag_sparse(sparse_matrix: &[Vec<(u16, I32F32)>]) -> Vec<Vec<(u16, I

/// Normalizes (sum to 1 except 0) each row (dim=0) of a sparse matrix in-place.
pub fn inplace_row_normalize_sparse(sparse_matrix: &mut [Vec<(u16, I32F32)>]) {
for sparse_row in sparse_matrix.iter_mut() {
sparse_matrix.iter_mut().for_each(|sparse_row| {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A for statement is the idiomatic way to do this here, we can leave it the way it was

Comment on lines 28 to 30
let normalized_share =
(share as u64 * u16::MAX as u64 / total_shares as u64) as u16;
normalized_share

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
let normalized_share =
(share as u64 * u16::MAX as u64 / total_shares as u64) as u16;
normalized_share
(share as u64 * u16::MAX as u64 / total_shares as u64) as u16

pallets/subspace/src/registration.rs Outdated Show resolved Hide resolved

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Change all &Vec<_> to &[_]

pallets/subspace/tests/delegate_staking.rs Outdated Show resolved Hide resolved
pallets/subspace/tests/delegate_staking.rs Outdated Show resolved Hide resolved
pallets/subspace/tests/delegate_staking.rs Outdated Show resolved Hide resolved
@Supremesource
Copy link
Member Author

@saiintbrisson
14e5e65
I did the refactors you requested

@Supremesource Supremesource added the enhancement New feature or request label Mar 17, 2024
@Supremesource Supremesource changed the title Refactoring code complexity refac: making code more robust Mar 17, 2024
pallets/subspace/src/module.rs Outdated Show resolved Hide resolved
pallets/subspace/src/module.rs Outdated Show resolved Hide resolved
pallets/subspace/src/weights.rs Show resolved Hide resolved
if sum == 0 {
return weights;
}
weights.iter_mut().for_each(|x| {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use for instead of for_each

@Supremesource
Copy link
Member Author

@aripiprazole
74d279f
refac

@Supremesource Supremesource changed the title refac: making code more robust refac(pallets/subspace): making code more robust Mar 17, 2024
@saiintbrisson saiintbrisson merged commit b5e9e81 into main Mar 18, 2024
1 check passed
@Supremesource Supremesource deleted the refac-v1.4.0 branch March 23, 2024 21:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants