Skip to content

Commit

Permalink
Remove lifetime parameter in Row.
Browse files Browse the repository at this point in the history
  • Loading branch information
chriseth committed Jul 1, 2024
1 parent 562154c commit de8ddd8
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 64 deletions.
4 changes: 2 additions & 2 deletions executor/src/witgen/block_processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ pub struct BlockProcessor<'a, 'b, 'c, T: FieldElement, Q: QueryCallback<T>> {
impl<'a, 'b, 'c, T: FieldElement, Q: QueryCallback<T>> BlockProcessor<'a, 'b, 'c, T, Q> {
pub fn new(
row_offset: RowIndex,
data: FinalizableData<'a, T>,
data: FinalizableData<T>,
mutable_state: &'c mut MutableState<'a, 'b, T, Q>,
identities: &'c [&'a Identity<Expression<T>>],
fixed_data: &'a FixedData<'a, T>,
Expand Down Expand Up @@ -99,7 +99,7 @@ impl<'a, 'b, 'c, T: FieldElement, Q: QueryCallback<T>> BlockProcessor<'a, 'b, 'c
}
}

pub fn finish(self) -> FinalizableData<'a, T> {
pub fn finish(self) -> FinalizableData<T> {
self.processor.finish()
}
}
Expand Down
32 changes: 16 additions & 16 deletions executor/src/witgen/data_structures/finalizable_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<T: FieldElement> {
/// The row is still in progress, and range constraints are still available.
InProgress(Row<'a, T>),
InProgress(Row<T>),
Finalized(FinalizedRow<T>),
}

Expand Down Expand Up @@ -42,21 +42,21 @@ impl<T> FinalizedRow<T> {
/// 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<T: FieldElement> {
/// The list of rows (either in progress or finalized)
data: Vec<Entry<'a, T>>,
data: Vec<Entry<T>>,
/// The list of column IDs (in sorted order), used to index finalized rows.
column_ids: Vec<PolyID>,
}

impl<'a, T: FieldElement> FinalizableData<'a, T> {
impl<T: FieldElement> FinalizableData<T> {
pub fn new(column_ids: &HashSet<PolyID>) -> Self {
Self::with_initial_rows_in_progress(column_ids, [].into_iter())
}

pub fn with_initial_rows_in_progress(
column_ids: &HashSet<PolyID>,
rows: impl Iterator<Item = Row<'a, T>>,
rows: impl Iterator<Item = Row<T>>,
) -> Self {
let mut column_ids = column_ids.iter().cloned().collect::<Vec<_>>();
column_ids.sort();
Expand All @@ -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<T>) {
self.data.push(Entry::InProgress(row));
}

pub fn pop(&mut self) -> Option<Row<'a, T>> {
pub fn pop(&mut self) -> Option<Row<T>> {
match self.data.pop() {
Some(Entry::InProgress(row)) => Some(row),
Some(Entry::Finalized(_)) => panic!("Row already finalized."),
Expand All @@ -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<T> {
match self.data.remove(i) {
Entry::InProgress(row) => row,
Entry::Finalized(_) => panic!("Row {i} already finalized."),
Expand All @@ -99,22 +99,22 @@ 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<T>> {
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<T>> {
match self.data.last() {
Some(Entry::InProgress(row)) => Some(row),
Some(Entry::Finalized(_)) => panic!("Last row already finalized."),
None => None,
}
}

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<T>, &mut Row<T>) {
let (before, after) = self.data.split_at_mut(i + 1);
let current = before.last_mut().unwrap();
let next = after.first_mut().unwrap();
Expand Down Expand Up @@ -191,8 +191,8 @@ impl<'a, T: FieldElement> FinalizableData<'a, T> {
}
}

impl<'a, T: FieldElement> Index<usize> for FinalizableData<'a, T> {
type Output = Row<'a, T>;
impl<T: FieldElement> Index<usize> for FinalizableData<T> {
type Output = Row<T>;

fn index(&self, index: usize) -> &Self::Output {
match &self.data[index] {
Expand All @@ -202,7 +202,7 @@ impl<'a, T: FieldElement> Index<usize> for FinalizableData<'a, T> {
}
}

impl<'a, T: FieldElement> IndexMut<usize> for FinalizableData<'a, T> {
impl<T: FieldElement> IndexMut<usize> for FinalizableData<T> {
fn index_mut(&mut self, index: usize) -> &mut Self::Output {
match &mut self.data[index] {
Entry::InProgress(row) => row,
Expand Down
11 changes: 5 additions & 6 deletions executor/src/witgen/generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@ use super::{EvalResult, FixedData, MutableState, QueryCallback};

struct ProcessResult<'a, T: FieldElement> {
eval_value: EvalValue<&'a AlgebraicReference, T>,
block: FinalizableData<'a, T>,
block: FinalizableData<T>,
}

pub struct Generator<'a, T: FieldElement> {
connecting_identities: BTreeMap<u64, &'a Identity<Expression<T>>>,
fixed_data: &'a FixedData<'a, T>,
identities: Vec<&'a Identity<Expression<T>>>,
witnesses: HashSet<PolyID>,
data: FinalizableData<'a, T>,
data: FinalizableData<T>,
latch: Option<Expression<T>>,
name: String,
}
Expand Down Expand Up @@ -159,7 +159,7 @@ impl<'a, T: FieldElement> Generator<'a, T> {
fn compute_partial_first_row<Q: QueryCallback<T>>(
&self,
mutable_state: &mut MutableState<'a, '_, T, Q>,
) -> Row<'a, T> {
) -> Row<T> {
// 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.
Expand Down Expand Up @@ -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<T>>(
&self,
first_row: Row<'a, T>,
first_row: Row<T>,
row_offset: DegreeType,
mutable_state: &mut MutableState<'a, 'b, T, Q>,
outer_query: Option<OuterQuery<'a, 'b, T>>,
Expand Down
10 changes: 5 additions & 5 deletions executor/src/witgen/machines/block_machine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<T>, 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<T>, updates: EvalValue<&'a AlgebraicReference, T>) -> Self {
match updates.is_complete() {
true => ProcessResult::Success(data, updates),
false => ProcessResult::Incomplete(updates),
Expand Down Expand Up @@ -109,7 +109,7 @@ pub struct BlockMachine<'a, T: FieldElement> {
/// The internal identities
identities: Vec<&'a Identity<Expression<T>>>,
/// The data of the machine.
data: FinalizableData<'a, T>,
data: FinalizableData<T>,
/// The set of witness columns that are actually part of this machine.
witness_cols: HashSet<PolyID>,
/// Cache that states the order in which to evaluate identities
Expand Down Expand Up @@ -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<T> {
// 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()]
Expand Down Expand Up @@ -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<T>> {
fn append_block(&mut self, mut new_block: FinalizableData<T>) -> Result<(), EvalError<T>> {
assert!(
(self.rows() + self.block_size as DegreeType) < self.fixed_data.degree,
"Block machine is full (this should have been checked before)"
Expand Down
12 changes: 6 additions & 6 deletions executor/src/witgen/processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ pub struct Processor<'a, 'b, 'c, T: FieldElement, Q: QueryCallback<T>> {
/// 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<T>,
/// The mutable state
mutable_state: &'c mut MutableState<'a, 'b, T, Q>,
/// The fixed data (containing information about all columns)
Expand All @@ -94,7 +94,7 @@ pub struct Processor<'a, 'b, 'c, T: FieldElement, Q: QueryCallback<T>> {
impl<'a, 'b, 'c, T: FieldElement, Q: QueryCallback<T>> Processor<'a, 'b, 'c, T, Q> {
pub fn new(
row_offset: RowIndex,
data: FinalizableData<'a, T>,
data: FinalizableData<T>,
mutable_state: &'c mut MutableState<'a, 'b, T, Q>,
fixed_data: &'a FixedData<'a, T>,
witness_cols: &'c HashSet<PolyID>,
Expand Down Expand Up @@ -160,7 +160,7 @@ impl<'a, 'b, 'c, T: FieldElement, Q: QueryCallback<T>> Processor<'a, 'b, 'c, T,
.unwrap_or(true)
}

pub fn finish(self) -> FinalizableData<'a, T> {
pub fn finish(self) -> FinalizableData<T> {
self.data
}

Expand Down Expand Up @@ -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<T> {
&self.data[i]
}

Expand All @@ -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<T>) {
if i < self.data.len() {
self.data[i] = row;
} else {
Expand All @@ -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<T>,
identity: &'a Identity<Expression<T>>,
// This could be computed from the identity, but should be pre-computed for performance reasons.
has_next_reference: bool,
Expand Down
Loading

0 comments on commit de8ddd8

Please sign in to comment.