Skip to content
This repository has been archived by the owner on Jan 22, 2025. It is now read-only.

Commit

Permalink
eliminate flatten and sort in hash calculation (#17802)
Browse files Browse the repository at this point in the history
* eliminate flatten and sort in hash calculation

* reduce critical section time

* remove now no-longer necessary test code

* conflict with reset bins to 0 pr
  • Loading branch information
jeffwashington authored Jun 21, 2021
1 parent 58e1152 commit bf97627
Show file tree
Hide file tree
Showing 2 changed files with 294 additions and 484 deletions.
25 changes: 22 additions & 3 deletions runtime/src/accounts_db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4613,18 +4613,21 @@ impl AccountsDb {
}

/// Scan through all the account storage in parallel
fn scan_account_storage_no_bank<F, B>(
fn scan_account_storage_no_bank<F, F2, B, C>(
accounts_cache_and_ancestors: Option<(
&AccountsCache,
&Ancestors,
&AccountInfoAccountsIndex,
)>,
snapshot_storages: &SortedStorages,
scan_func: F,
) -> Vec<B>
after_func: F2,
) -> Vec<C>
where
F: Fn(LoadedAccount, &mut B, Slot) + Send + Sync,
F2: Fn(B) -> C + Send + Sync,
B: Send + Default,
C: Send + Default,
{
// Without chunks, we end up with 1 output vec for each outer snapshot storage.
// This results in too many vectors to be efficient.
Expand Down Expand Up @@ -4669,7 +4672,7 @@ impl AccountsDb {
}
}
}
retval
after_func(retval)
})
.collect()
}
Expand Down Expand Up @@ -4847,6 +4850,7 @@ impl AccountsDb {
}
accum[pubkey_to_bin_index].push(source_item);
},
Self::sort_slot_storage_scan,
);

if check_hash && mismatch_found.load(Ordering::Relaxed) > 0 {
Expand All @@ -4863,6 +4867,19 @@ impl AccountsDb {
Ok(result)
}

fn sort_slot_storage_scan(
accum: Vec<Vec<CalculateHashIntermediate>>,
) -> Vec<Vec<CalculateHashIntermediate>> {
accum
.into_par_iter()
.map(|mut items| {
// sort_by vs unstable because slot and write_version are already in order
items.sort_by(AccountsHash::compare_two_hash_entries);
items
})
.collect()
}

// modeled after get_accounts_delta_hash
// intended to be faster than calculate_accounts_hash
pub fn calculate_accounts_hash_without_index(
Expand Down Expand Up @@ -4915,6 +4932,7 @@ impl AccountsDb {
&mut stats,
pass == num_scan_passes - 1,
previous_pass,
bins_per_pass,
);
previous_pass = for_next_pass;
final_result = (hash, lamports);
Expand Down Expand Up @@ -6643,6 +6661,7 @@ pub mod tests {
assert_eq!(slot_expected, slot);
accum.push(expected);
},
|a| a,
);
assert_eq!(calls.load(Ordering::Relaxed), 1);
assert_eq!(result, vec![vec![expected]]);
Expand Down
Loading

0 comments on commit bf97627

Please sign in to comment.