Skip to content

Commit

Permalink
remove copy
Browse files Browse the repository at this point in the history
  • Loading branch information
HaoranYi committed Nov 13, 2024
1 parent 8f27218 commit ddd02ef
Showing 1 changed file with 16 additions and 11 deletions.
27 changes: 16 additions & 11 deletions accounts-db/src/accounts_hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,8 @@ impl CumulativeHashesFromFiles {
}

// return the biggest hash data possible that starts at the overall index 'start'
fn get_data(&self, start: usize) -> Arc<Vec<Hash>> {
// start is the index of hashes
fn get_data(&self, start: usize) -> Arc<Vec<u8>> {
let (start, offset) = self.cumulative.find(start);
let data_source_index = offset.index[0];
let mut data = self.readers[data_source_index].lock().unwrap();
Expand All @@ -344,8 +345,7 @@ impl CumulativeHashesFromFiles {

let mut result_bytes: Vec<u8> = vec![];
data.read_to_end(&mut result_bytes).unwrap();
let result: Vec<Hash> = bytemuck::cast_slice(&result_bytes).to_vec();
Arc::new(result)
Arc::new(result_bytes)
}
}

Expand Down Expand Up @@ -546,8 +546,8 @@ impl<'a> AccountsHasher<'a> {
specific_level_count: Option<usize>,
) -> (Hash, Vec<Hash>)
where
// returns a vec hashes starting at the given overall index
F: Fn(usize) -> Arc<Vec<T>> + std::marker::Sync,
// returns a vec hash bytes starting at the given overall index
F: Fn(usize) -> Arc<Vec<u8>> + std::marker::Sync,
T: AsRef<[u8]> + std::marker::Send + std::marker::Sync + bytemuck::Pod,
{
if total_hashes == 0 {
Expand All @@ -566,7 +566,8 @@ impl<'a> AccountsHasher<'a> {
let chunks = Self::div_ceil(total_hashes, num_hashes_per_chunk);

// initial fetch - could return entire slice
let data = get_hash_slice_starting_at_index(0);
let data_bytes = get_hash_slice_starting_at_index(0);
let data: &[T] = bytemuck::cast_slice(&data_bytes);

let data_len = data.len();

Expand All @@ -589,7 +590,8 @@ impl<'a> AccountsHasher<'a> {
// if we exhaust our data, then we will request a new slice, and data_index resets to 0, the beginning of the new slice
let mut data_index = start_index;
// source data, which we may refresh when we exhaust
let mut data = data.clone();
let mut data_bytes = data_bytes.clone();
let mut data: &[T] = bytemuck::cast_slice(&data_bytes);
// len of the source data
let mut data_len = data_len;

Expand All @@ -599,7 +601,8 @@ impl<'a> AccountsHasher<'a> {
for i in start_index..end_index {
if data_index >= data_len {
// we exhausted our data, fetch next slice starting at i
data = get_hash_slice_starting_at_index(i);
data_bytes = get_hash_slice_starting_at_index(i);
data = bytemuck::cast_slice(&data_bytes);
data_len = data.len();
data_index = 0;
}
Expand Down Expand Up @@ -654,7 +657,8 @@ impl<'a> AccountsHasher<'a> {
for _k in 0..end {
if data_index >= data_len {
// we exhausted our data, fetch next slice starting at i
data = get_hash_slice_starting_at_index(i);
data_bytes = get_hash_slice_starting_at_index(i);
data = bytemuck::cast_slice(&data_bytes);
data_len = data.len();
data_index = 0;
}
Expand Down Expand Up @@ -1314,7 +1318,7 @@ impl<'a> AccountsHasher<'a> {

let _guard = self.active_stats.activate(ActiveStatItem::HashMerkleTree);
let mut hash_time = Measure::start("hash");
let (hash, _) = Self::compute_merkle_root_from_start(
let (hash, _) = Self::compute_merkle_root_from_start::<_, Hash>(
cumulative.total_count(),
MERKLE_FANOUT,
None,
Expand Down Expand Up @@ -1628,7 +1632,8 @@ mod tests {
let mut cumulative_start = start;
// read all data
while retrieved.len() < (len - start) {
let this_one = cumulative.get_data(cumulative_start);
let this_one_bytes = cumulative.get_data(cumulative_start);
let this_one = bytemuck::cast_slice(&this_one_bytes);
retrieved.extend(this_one.iter());
cumulative_start += this_one.len();
assert_ne!(0, this_one.len());
Expand Down

0 comments on commit ddd02ef

Please sign in to comment.