From bcbf3374fd75505395d33a09dd98962b054dd486 Mon Sep 17 00:00:00 2001 From: Brooks Prumo Date: Tue, 13 Dec 2022 10:20:14 -0500 Subject: [PATCH] Avoids copying hashes when computing merkle root (#29225) --- runtime/src/accounts_db.rs | 2 +- runtime/src/accounts_hash.rs | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/runtime/src/accounts_db.rs b/runtime/src/accounts_db.rs index 89c8c33bbe..ac393efe3b 100644 --- a/runtime/src/accounts_db.rs +++ b/runtime/src/accounts_db.rs @@ -10496,7 +10496,7 @@ pub mod tests { let (storages, raw_expected) = sample_storages_and_accounts(&db); let expected_hash = AccountsHasher::compute_merkle_root_loop(raw_expected.clone(), MERKLE_FANOUT, |item| { - item.hash + &item.hash }); let sum = raw_expected.iter().map(|item| item.lamports).sum(); let result = db diff --git a/runtime/src/accounts_hash.rs b/runtime/src/accounts_hash.rs index 7d000ed1ab..77af53ed42 100644 --- a/runtime/src/accounts_hash.rs +++ b/runtime/src/accounts_hash.rs @@ -538,12 +538,12 @@ impl AccountsHasher { } pub fn compute_merkle_root(hashes: Vec<(Pubkey, Hash)>, fanout: usize) -> Hash { - Self::compute_merkle_root_loop(hashes, fanout, |t| t.1) + Self::compute_merkle_root_loop(hashes, fanout, |t| &t.1) } // this function avoids an infinite recursion compiler error pub fn compute_merkle_root_recurse(hashes: Vec, fanout: usize) -> Hash { - Self::compute_merkle_root_loop(hashes, fanout, |t: &Hash| *t) + Self::compute_merkle_root_loop(hashes, fanout, |t| t) } pub fn div_ceil(x: usize, y: usize) -> usize { @@ -558,7 +558,7 @@ impl AccountsHasher { // Using extractor allows us to avoid an unnecessary array copy on the first iteration. pub fn compute_merkle_root_loop(hashes: Vec, fanout: usize, extractor: F) -> Hash where - F: Fn(&T) -> Hash + std::marker::Sync, + F: Fn(&T) -> &Hash + std::marker::Sync, T: std::marker::Sync, { if hashes.is_empty() { @@ -806,7 +806,7 @@ impl AccountsHasher { pub fn accumulate_account_hashes(mut hashes: Vec<(Pubkey, Hash)>) -> Hash { Self::sort_hashes_by_pubkey(&mut hashes); - Self::compute_merkle_root_loop(hashes, MERKLE_FANOUT, |i| i.1) + Self::compute_merkle_root_loop(hashes, MERKLE_FANOUT, |i| &i.1) } pub fn sort_hashes_by_pubkey(hashes: &mut Vec<(Pubkey, Hash)>) {