Skip to content

Commit

Permalink
Use reserve() in VZV mutation code so we are dealing with the appropr…
Browse files Browse the repository at this point in the history
…iate allocation
  • Loading branch information
Manishearth committed Jun 13, 2023
1 parent 36b6089 commit 5518e5f
Showing 1 changed file with 3 additions and 2 deletions.
5 changes: 3 additions & 2 deletions utils/zerovec/src/varzerovec/owned.rs
Original file line number Diff line number Diff line change
Expand Up @@ -279,14 +279,15 @@ impl<T: VarULE + ?Sized, F: VarZeroVecFormat> VarZeroVecOwned<T, F> {
any::type_name::<F>()
);
}
self.entire_slice.reserve(shift as usize);
self.entire_slice.resize(new_slice_len, 0);
}

// Now that we've ensured there's enough space, we can shift the data around.
{
// Note: There are no references introduced between pointer creation and pointer use, and all
// raw pointers are derived from a single &mut. This preserves pointer provenance.
let slice_range = self.entire_slice.as_mut_ptr_range();
let old_slice_end = slice_range.start.offset(slice_len as isize);
let data_start = slice_range
.start
.add(LENGTH_WIDTH + METADATA_WIDTH + len * F::INDEX_WIDTH);
Expand Down Expand Up @@ -316,7 +317,7 @@ impl<T: VarULE + ?Sized, F: VarZeroVecFormat> VarZeroVecOwned<T, F> {

// Shift data after the element to its new position.
shift_bytes(
prev_element_p.end..slice_range.end,
prev_element_p.end..old_slice_end,
prev_element_p
.start
.offset((new_size as i64 + index_shift) as isize),
Expand Down

0 comments on commit 5518e5f

Please sign in to comment.