From de8ddd870649399da813df1bdae3d359345ede52 Mon Sep 17 00:00:00 2001 From: chriseth Date: Mon, 1 Jul 2024 17:49:51 +0000 Subject: [PATCH] Remove lifetime parameter in Row. --- executor/src/witgen/block_processor.rs | 4 +- .../data_structures/finalizable_data.rs | 32 ++++++------- executor/src/witgen/generator.rs | 11 ++--- executor/src/witgen/machines/block_machine.rs | 10 ++-- executor/src/witgen/processor.rs | 12 ++--- executor/src/witgen/rows.rs | 47 +++++++++---------- executor/src/witgen/vm_processor.rs | 6 +-- 7 files changed, 58 insertions(+), 64 deletions(-) diff --git a/executor/src/witgen/block_processor.rs b/executor/src/witgen/block_processor.rs index 1dd26cc73..2285caece 100644 --- a/executor/src/witgen/block_processor.rs +++ b/executor/src/witgen/block_processor.rs @@ -28,7 +28,7 @@ pub struct BlockProcessor<'a, 'b, 'c, T: FieldElement, Q: QueryCallback> { impl<'a, 'b, 'c, T: FieldElement, Q: QueryCallback> BlockProcessor<'a, 'b, 'c, T, Q> { pub fn new( row_offset: RowIndex, - data: FinalizableData<'a, T>, + data: FinalizableData, mutable_state: &'c mut MutableState<'a, 'b, T, Q>, identities: &'c [&'a Identity>], fixed_data: &'a FixedData<'a, T>, @@ -99,7 +99,7 @@ impl<'a, 'b, 'c, T: FieldElement, Q: QueryCallback> BlockProcessor<'a, 'b, 'c } } - pub fn finish(self) -> FinalizableData<'a, T> { + pub fn finish(self) -> FinalizableData { self.processor.finish() } } diff --git a/executor/src/witgen/data_structures/finalizable_data.rs b/executor/src/witgen/data_structures/finalizable_data.rs index d4075b8aa..9faf53df8 100644 --- a/executor/src/witgen/data_structures/finalizable_data.rs +++ b/executor/src/witgen/data_structures/finalizable_data.rs @@ -4,16 +4,16 @@ use std::{ }; use bit_vec::BitVec; -use powdr_ast::analyzed::{PolyID}; +use powdr_ast::analyzed::PolyID; use powdr_number::FieldElement; use crate::witgen::rows::Row; /// A row entry in [FinalizableData]. #[derive(Clone)] -enum Entry<'a, T: FieldElement> { +enum Entry { /// The row is still in progress, and range constraints are still available. - InProgress(Row<'a, T>), + InProgress(Row), Finalized(FinalizedRow), } @@ -42,21 +42,21 @@ impl FinalizedRow { /// Once a row has been finalized, any operation trying to access it again will fail at runtime. /// [FinalizableData::take_transposed] can be used to access the final cells. #[derive(Clone)] -pub struct FinalizableData<'a, T: FieldElement> { +pub struct FinalizableData { /// The list of rows (either in progress or finalized) - data: Vec>, + data: Vec>, /// The list of column IDs (in sorted order), used to index finalized rows. column_ids: Vec, } -impl<'a, T: FieldElement> FinalizableData<'a, T> { +impl FinalizableData { pub fn new(column_ids: &HashSet) -> Self { Self::with_initial_rows_in_progress(column_ids, [].into_iter()) } pub fn with_initial_rows_in_progress( column_ids: &HashSet, - rows: impl Iterator>, + rows: impl Iterator>, ) -> Self { let mut column_ids = column_ids.iter().cloned().collect::>(); column_ids.sort(); @@ -72,11 +72,11 @@ impl<'a, T: FieldElement> FinalizableData<'a, T> { self.data.is_empty() } - pub fn push(&mut self, row: Row<'a, T>) { + pub fn push(&mut self, row: Row) { self.data.push(Entry::InProgress(row)); } - pub fn pop(&mut self) -> Option> { + pub fn pop(&mut self) -> Option> { match self.data.pop() { Some(Entry::InProgress(row)) => Some(row), Some(Entry::Finalized(_)) => panic!("Row already finalized."), @@ -88,7 +88,7 @@ impl<'a, T: FieldElement> FinalizableData<'a, T> { self.data.extend(other.data); } - pub fn remove(&mut self, i: usize) -> Row<'a, T> { + pub fn remove(&mut self, i: usize) -> Row { match self.data.remove(i) { Entry::InProgress(row) => row, Entry::Finalized(_) => panic!("Row {i} already finalized."), @@ -99,14 +99,14 @@ impl<'a, T: FieldElement> FinalizableData<'a, T> { self.data.truncate(len); } - pub fn get_mut(&mut self, i: usize) -> Option<&mut Row<'a, T>> { + pub fn get_mut(&mut self, i: usize) -> Option<&mut Row> { match &mut self.data[i] { Entry::InProgress(row) => Some(row), Entry::Finalized(_) => panic!("Row {i} already finalized."), } } - pub fn last(&self) -> Option<&Row<'a, T>> { + pub fn last(&self) -> Option<&Row> { match self.data.last() { Some(Entry::InProgress(row)) => Some(row), Some(Entry::Finalized(_)) => panic!("Last row already finalized."), @@ -114,7 +114,7 @@ impl<'a, T: FieldElement> FinalizableData<'a, T> { } } - pub fn mutable_row_pair(&mut self, i: usize) -> (&mut Row<'a, T>, &mut Row<'a, T>) { + pub fn mutable_row_pair(&mut self, i: usize) -> (&mut Row, &mut Row) { let (before, after) = self.data.split_at_mut(i + 1); let current = before.last_mut().unwrap(); let next = after.first_mut().unwrap(); @@ -191,8 +191,8 @@ impl<'a, T: FieldElement> FinalizableData<'a, T> { } } -impl<'a, T: FieldElement> Index for FinalizableData<'a, T> { - type Output = Row<'a, T>; +impl Index for FinalizableData { + type Output = Row; fn index(&self, index: usize) -> &Self::Output { match &self.data[index] { @@ -202,7 +202,7 @@ impl<'a, T: FieldElement> Index for FinalizableData<'a, T> { } } -impl<'a, T: FieldElement> IndexMut for FinalizableData<'a, T> { +impl IndexMut for FinalizableData { fn index_mut(&mut self, index: usize) -> &mut Self::Output { match &mut self.data[index] { Entry::InProgress(row) => row, diff --git a/executor/src/witgen/generator.rs b/executor/src/witgen/generator.rs index a83f021cb..9111ada09 100644 --- a/executor/src/witgen/generator.rs +++ b/executor/src/witgen/generator.rs @@ -18,7 +18,7 @@ use super::{EvalResult, FixedData, MutableState, QueryCallback}; struct ProcessResult<'a, T: FieldElement> { eval_value: EvalValue<&'a AlgebraicReference, T>, - block: FinalizableData<'a, T>, + block: FinalizableData, } pub struct Generator<'a, T: FieldElement> { @@ -26,7 +26,7 @@ pub struct Generator<'a, T: FieldElement> { fixed_data: &'a FixedData<'a, T>, identities: Vec<&'a Identity>>, witnesses: HashSet, - data: FinalizableData<'a, T>, + data: FinalizableData, latch: Option>, name: String, } @@ -159,7 +159,7 @@ impl<'a, T: FieldElement> Generator<'a, T> { fn compute_partial_first_row>( &self, mutable_state: &mut MutableState<'a, '_, T, Q>, - ) -> Row<'a, T> { + ) -> Row { // Use `BlockProcessor` + `DefaultSequenceIterator` using a "block size" of 0. Because `BlockProcessor` // expects `data` to include the row before and after the block, this means we'll run the // solver on exactly one row pair. @@ -202,14 +202,13 @@ impl<'a, T: FieldElement> Generator<'a, T> { DefaultSequenceIterator::new(0, identities_with_next_reference.len(), None), ); processor.solve(&mut sequence_iterator).unwrap(); - let first_row = processor.finish().remove(1); - first_row + processor.finish().remove(1) } fn process<'b, Q: QueryCallback>( &self, - first_row: Row<'a, T>, + first_row: Row, row_offset: DegreeType, mutable_state: &mut MutableState<'a, 'b, T, Q>, outer_query: Option>, diff --git a/executor/src/witgen/machines/block_machine.rs b/executor/src/witgen/machines/block_machine.rs index 93218a220..af1a8f6db 100644 --- a/executor/src/witgen/machines/block_machine.rs +++ b/executor/src/witgen/machines/block_machine.rs @@ -24,12 +24,12 @@ use powdr_ast::parsed::visitor::ExpressionVisitable; use powdr_number::{DegreeType, FieldElement}; enum ProcessResult<'a, T: FieldElement> { - Success(FinalizableData<'a, T>, EvalValue<&'a AlgebraicReference, T>), + Success(FinalizableData, EvalValue<&'a AlgebraicReference, T>), Incomplete(EvalValue<&'a AlgebraicReference, T>), } impl<'a, T: FieldElement> ProcessResult<'a, T> { - fn new(data: FinalizableData<'a, T>, updates: EvalValue<&'a AlgebraicReference, T>) -> Self { + fn new(data: FinalizableData, updates: EvalValue<&'a AlgebraicReference, T>) -> Self { match updates.is_complete() { true => ProcessResult::Success(data, updates), false => ProcessResult::Incomplete(updates), @@ -109,7 +109,7 @@ pub struct BlockMachine<'a, T: FieldElement> { /// The internal identities identities: Vec<&'a Identity>>, /// The data of the machine. - data: FinalizableData<'a, T>, + data: FinalizableData, /// The set of witness columns that are actually part of this machine. witness_cols: HashSet, /// Cache that states the order in which to evaluate identities @@ -460,7 +460,7 @@ impl<'a, T: FieldElement> BlockMachine<'a, T> { RowIndex::from_i64(self.rows() as i64 - 1, self.fixed_data.degree) } - fn get_row(&self, row: RowIndex) -> &Row<'a, T> { + fn get_row(&self, row: RowIndex) -> &Row { // The first block is a dummy block corresponding to rows (-block_size, 0), // so we have to add the block size to the row index. &self.data[(row + self.block_size).into()] @@ -612,7 +612,7 @@ impl<'a, T: FieldElement> BlockMachine<'a, T> { /// the last row of its previous block is merged with the one we have already. /// This is necessary to handle non-rectangular block machines, which already use /// unused cells in the previous block. - fn append_block(&mut self, mut new_block: FinalizableData<'a, T>) -> Result<(), EvalError> { + fn append_block(&mut self, mut new_block: FinalizableData) -> Result<(), EvalError> { assert!( (self.rows() + self.block_size as DegreeType) < self.fixed_data.degree, "Block machine is full (this should have been checked before)" diff --git a/executor/src/witgen/processor.rs b/executor/src/witgen/processor.rs index 44d247020..0b7d49134 100644 --- a/executor/src/witgen/processor.rs +++ b/executor/src/witgen/processor.rs @@ -73,7 +73,7 @@ pub struct Processor<'a, 'b, 'c, T: FieldElement, Q: QueryCallback> { /// The global index of the first row of [Processor::data]. row_offset: RowIndex, /// The rows that are being processed. - data: FinalizableData<'a, T>, + data: FinalizableData, /// The mutable state mutable_state: &'c mut MutableState<'a, 'b, T, Q>, /// The fixed data (containing information about all columns) @@ -94,7 +94,7 @@ pub struct Processor<'a, 'b, 'c, T: FieldElement, Q: QueryCallback> { impl<'a, 'b, 'c, T: FieldElement, Q: QueryCallback> Processor<'a, 'b, 'c, T, Q> { pub fn new( row_offset: RowIndex, - data: FinalizableData<'a, T>, + data: FinalizableData, mutable_state: &'c mut MutableState<'a, 'b, T, Q>, fixed_data: &'a FixedData<'a, T>, witness_cols: &'c HashSet, @@ -160,7 +160,7 @@ impl<'a, 'b, 'c, T: FieldElement, Q: QueryCallback> Processor<'a, 'b, 'c, T, .unwrap_or(true) } - pub fn finish(self) -> FinalizableData<'a, T> { + pub fn finish(self) -> FinalizableData { self.data } @@ -468,7 +468,7 @@ Known values in current row (local: {row_index}, global {global_row_index}): self.data.finalize_range(range); } - pub fn row(&self, i: usize) -> &Row<'a, T> { + pub fn row(&self, i: usize) -> &Row { &self.data[i] } @@ -477,7 +477,7 @@ Known values in current row (local: {row_index}, global {global_row_index}): } /// Sets the ith row, extending the data if necessary. - pub fn set_row(&mut self, i: usize, row: Row<'a, T>) { + pub fn set_row(&mut self, i: usize, row: Row) { if i < self.data.len() { self.data[i] = row; } else { @@ -490,7 +490,7 @@ Known values in current row (local: {row_index}, global {global_row_index}): pub fn check_row_pair( &mut self, row_index: usize, - proposed_row: &Row<'a, T>, + proposed_row: &Row, identity: &'a Identity>, // This could be computed from the identity, but should be pre-computed for performance reasons. has_next_reference: bool, diff --git a/executor/src/witgen/rows.rs b/executor/src/witgen/rows.rs index 88951ac43..2602f9448 100644 --- a/executor/src/witgen/rows.rs +++ b/executor/src/witgen/rows.rs @@ -167,14 +167,12 @@ impl CellValue { } #[derive(Clone, Default, PartialEq)] -pub struct Row<'a, T: FieldElement> { +pub struct Row { /// The values in the row, zero if unknown. values: WitnessColumnMap>, - // TODO remove this. - names: Vec<&'a str>, } -impl<'a, T: FieldElement> Row<'a, T> { +impl Row { pub fn value_or_zero(&self, poly_id: &PolyID) -> T { self.values[poly_id].unwrap_or_zero() } @@ -192,7 +190,7 @@ impl<'a, T: FieldElement> Row<'a, T> { /// Merges two rows, updating the first. /// Range constraints from the second row are ignored. - pub fn merge_with(&mut self, other: &Row<'a, T>) -> Result<(), ()> { + pub fn merge_with(&mut self, other: &Row) -> Result<(), ()> { // First check for conflicts, otherwise we would have to roll back changes. if self .values @@ -242,9 +240,9 @@ impl<'a, T: FieldElement> Row<'a, T> { } } -impl<'a, T: FieldElement> Row<'a, T> { +impl Row { /// Creates a "fresh" row, i.e., one that is empty but initialized with the global range constraints. - pub fn fresh(fixed_data: &'a FixedData<'a, T>, row: RowIndex) -> Row<'a, T> { + pub fn fresh(fixed_data: &FixedData<'_, T>, row: RowIndex) -> Row { // TODO this instance could be computed exactly once (per column set) and then cloned. // TODO and we could copy in the external witnesses later on // TODO we should really only have a subset of the columns. @@ -265,10 +263,7 @@ impl<'a, T: FieldElement> Row<'a, T> { } }), ); - Self { - values, - names: vec![], - } + Self { values } } /// Builds a string representing the current row @@ -330,7 +325,7 @@ impl<'a, T: FieldElement> Row<'a, T> { } } -impl From> for WitnessColumnMap { +impl From> for WitnessColumnMap { /// Builds a map from polynomial ID to value. Unknown values are set to zero. fn from(row: Row) -> Self { WitnessColumnMap::from(row.values.values_into_iter().map(|c| c.unwrap_or_zero())) @@ -338,16 +333,16 @@ impl From> for WitnessColumnMap { } /// A pair of mutable row references which knows how to apply updates. -pub struct RowUpdater<'row, 'a, T: FieldElement> { - current: &'row mut Row<'a, T>, - next: &'row mut Row<'a, T>, +pub struct RowUpdater<'row, T: FieldElement> { + current: &'row mut Row, + next: &'row mut Row, current_row_index: RowIndex, } -impl<'row, 'a, T: FieldElement> RowUpdater<'row, 'a, T> { +impl<'row, T: FieldElement> RowUpdater<'row, T> { pub fn new( - current: &'row mut Row<'a, T>, - next: &'row mut Row<'a, T>, + current: &'row mut Row, + next: &'row mut Row, current_row_index: RowIndex, ) -> Self { Self { @@ -379,7 +374,7 @@ impl<'row, 'a, T: FieldElement> RowUpdater<'row, 'a, T> { self.get_row_mut(poly.next).apply_update(&poly.poly_id, c); } - fn get_row_mut<'b>(&'b mut self, next: bool) -> &'b mut Row<'a, T> { + fn get_row_mut(&mut self, next: bool) -> &mut Row { match next { false => self.current, true => self.next, @@ -405,8 +400,8 @@ pub enum UnknownStrategy { /// A pair of row references which knows which value / range constraint /// to return for a given [AlgebraicReference]. pub struct RowPair<'row, 'a, T: FieldElement> { - pub current: &'row Row<'a, T>, - pub next: Option<&'row Row<'a, T>>, + pub current: &'row Row, + pub next: Option<&'row Row>, pub current_row_index: RowIndex, fixed_data: &'a FixedData<'a, T>, unknown_strategy: UnknownStrategy, @@ -414,8 +409,8 @@ pub struct RowPair<'row, 'a, T: FieldElement> { impl<'row, 'a, T: FieldElement> RowPair<'row, 'a, T> { /// Creates a new row pair. pub fn new( - current: &'row Row<'a, T>, - next: &'row Row<'a, T>, + current: &'row Row, + next: &'row Row, current_row_index: RowIndex, fixed_data: &'a FixedData<'a, T>, unknown_strategy: UnknownStrategy, @@ -431,7 +426,7 @@ impl<'row, 'a, T: FieldElement> RowPair<'row, 'a, T> { /// Creates a new row pair from a single row, setting the next row to None. pub fn from_single_row( - current: &'row Row<'a, T>, + current: &'row Row, current_row_index: RowIndex, fixed_data: &'a FixedData<'a, T>, unknown_strategy: UnknownStrategy, @@ -449,7 +444,7 @@ impl<'row, 'a, T: FieldElement> RowPair<'row, 'a, T> { self.get_row_mut(poly.next).value_is_known(&poly.poly_id) } - fn get_row_mut(&self, next: bool) -> &Row<'a, T> { + fn get_row_mut(&self, next: bool) -> &Row { match next { false => self.current, true => self @@ -458,7 +453,7 @@ impl<'row, 'a, T: FieldElement> RowPair<'row, 'a, T> { } } - fn get_row(&self, next: bool) -> &Row<'a, T> { + fn get_row(&self, next: bool) -> &Row { match next { false => self.current, true => self diff --git a/executor/src/witgen/vm_processor.rs b/executor/src/witgen/vm_processor.rs index 6b04159b2..a0c3ec00e 100644 --- a/executor/src/witgen/vm_processor.rs +++ b/executor/src/witgen/vm_processor.rs @@ -67,7 +67,7 @@ impl<'a, 'b, 'c, T: FieldElement, Q: QueryCallback> VmProcessor<'a, 'b, 'c, T fixed_data: &'a FixedData<'a, T>, identities: &[&'a Identity>], witnesses: &'c HashSet, - data: FinalizableData<'a, T>, + data: FinalizableData, mutable_state: &'c mut MutableState<'a, 'b, T, Q>, ) -> Self { let (identities_with_next, identities_without_next): (Vec<_>, Vec<_>) = identities @@ -101,7 +101,7 @@ impl<'a, 'b, 'c, T: FieldElement, Q: QueryCallback> VmProcessor<'a, 'b, 'c, T Self { processor, ..self } } - pub fn finish(self) -> FinalizableData<'a, T> { + pub fn finish(self) -> FinalizableData { self.processor.finish() } @@ -502,7 +502,7 @@ impl<'a, 'b, 'c, T: FieldElement, Q: QueryCallback> VmProcessor<'a, 'b, 'c, T /// Verifies the proposed values for the next row. /// TODO this is bad for machines because we might introduce rows in the machine that are then /// not used. - fn try_proposed_row(&mut self, row_index: DegreeType, proposed_row: Row<'a, T>) -> bool { + fn try_proposed_row(&mut self, row_index: DegreeType, proposed_row: Row) -> bool { let constraints_valid = self.identities_with_next_ref.iter().all(|i| { self.processor .check_row_pair(row_index as usize, &proposed_row, i, true)