Skip to content

Commit

Permalink
fix: Initialize vector before calling .set_len()
Browse files Browse the repository at this point in the history
The previous use of `.set_len()` did not uphold the safety contract,
resulting in undefined behavior. [0, 1] Initializing the spare capacity
before setting the length makes the behavior defined.

[0] https://users.rust-lang.org/t/58755
[1] https://users.rust-lang.org/t/38248
  • Loading branch information
jan-ferdinand committed Jun 6, 2024
1 parent d5ef951 commit 3727f02
Showing 1 changed file with 7 additions and 7 deletions.
14 changes: 7 additions & 7 deletions triton-vm/src/table/master_table.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::borrow::Borrow;
use std::mem::MaybeUninit;
use std::ops::Mul;
use std::ops::MulAssign;
use std::ops::Range;
Expand Down Expand Up @@ -314,25 +315,24 @@ where
if should_cache {
profiler!(start "resize");
assert!(extended_trace.capacity() >= num_elements);
extended_trace
.spare_capacity_mut()
.par_iter_mut()
.for_each(|e| *e = MaybeUninit::new(FF::zero()));

unsafe {
// Speed up initialization through parallelization.
//
// SAFETY:
// 1. The capacity is sufficiently large – see above `assert!`.
// 2. The length is set to equal (or less than) the capacity.
// 3. Each element is over-written before being read in the
// immediately following lines.
// 3. Each element in the spare capacity is initialized.
extended_trace.set_len(num_elements);
}
let mut extended_columns =
Array2::from_shape_vec([num_rows, Self::NUM_COLUMNS], extended_trace).unwrap();
profiler!(stop "resize");

// Speeds up the subsequent parallel iteration. Maybe because of page faults?
profiler!(start "touch memory");
extended_columns.par_mapv_inplace(|_| FF::zero());
profiler!(stop "touch memory");

profiler!(start "evaluation");
Zip::from(extended_columns.axis_iter_mut(Axis(1)))
.and(interpolation_polynomials.axis_iter(Axis(0)))
Expand Down

0 comments on commit 3727f02

Please sign in to comment.