Skip to content

Commit ddd02ef

Browse files
committed
remove copy
1 parent 8f27218 commit ddd02ef

File tree

1 file changed

+16
-11
lines changed

1 file changed

+16
-11
lines changed

accounts-db/src/accounts_hash.rs

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,8 @@ impl CumulativeHashesFromFiles {
332332
}
333333

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

345346
let mut result_bytes: Vec<u8> = vec![];
346347
data.read_to_end(&mut result_bytes).unwrap();
347-
let result: Vec<Hash> = bytemuck::cast_slice(&result_bytes).to_vec();
348-
Arc::new(result)
348+
Arc::new(result_bytes)
349349
}
350350
}
351351

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

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

571572
let data_len = data.len();
572573

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

@@ -599,7 +601,8 @@ impl<'a> AccountsHasher<'a> {
599601
for i in start_index..end_index {
600602
if data_index >= data_len {
601603
// we exhausted our data, fetch next slice starting at i
602-
data = get_hash_slice_starting_at_index(i);
604+
data_bytes = get_hash_slice_starting_at_index(i);
605+
data = bytemuck::cast_slice(&data_bytes);
603606
data_len = data.len();
604607
data_index = 0;
605608
}
@@ -654,7 +657,8 @@ impl<'a> AccountsHasher<'a> {
654657
for _k in 0..end {
655658
if data_index >= data_len {
656659
// we exhausted our data, fetch next slice starting at i
657-
data = get_hash_slice_starting_at_index(i);
660+
data_bytes = get_hash_slice_starting_at_index(i);
661+
data = bytemuck::cast_slice(&data_bytes);
658662
data_len = data.len();
659663
data_index = 0;
660664
}
@@ -1314,7 +1318,7 @@ impl<'a> AccountsHasher<'a> {
13141318

13151319
let _guard = self.active_stats.activate(ActiveStatItem::HashMerkleTree);
13161320
let mut hash_time = Measure::start("hash");
1317-
let (hash, _) = Self::compute_merkle_root_from_start(
1321+
let (hash, _) = Self::compute_merkle_root_from_start::<_, Hash>(
13181322
cumulative.total_count(),
13191323
MERKLE_FANOUT,
13201324
None,
@@ -1628,7 +1632,8 @@ mod tests {
16281632
let mut cumulative_start = start;
16291633
// read all data
16301634
while retrieved.len() < (len - start) {
1631-
let this_one = cumulative.get_data(cumulative_start);
1635+
let this_one_bytes = cumulative.get_data(cumulative_start);
1636+
let this_one = bytemuck::cast_slice(&this_one_bytes);
16321637
retrieved.extend(this_one.iter());
16331638
cumulative_start += this_one.len();
16341639
assert_ne!(0, this_one.len());

0 commit comments

Comments
 (0)