Skip to content
This repository has been archived by the owner on Jul 5, 2024. It is now read-only.

Commit

Permalink
rw table sort chronological by rw counter
Browse files Browse the repository at this point in the history
  • Loading branch information
hero78119 committed Oct 9, 2023
1 parent feab0b1 commit aab44aa
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions zkevm-circuits/src/witness/rw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,27 +128,28 @@ impl RwMap {
target_len: usize,
is_first_row_padding: bool,
) -> (Vec<Rw>, usize) {
// Remove Start rows as we will add them from scratch.
let rows: Vec<Rw> = rows
// Remove Start/Padding rows as we will add them from scratch.
let rows_trimmed: Vec<Rw> = rows
.iter()
.skip_while(|rw| matches!(rw, Rw::Start { .. }) || matches!(rw, Rw::Padding { .. }))
.filter(|rw| !(matches!(rw, Rw::Start { .. }) || matches!(rw, Rw::Padding { .. })))
.cloned()
.collect();
let padding_length = {
let length = Self::padding_len(rows.len(), target_len);
let length = Self::padding_len(rows_trimmed.len(), target_len);
if is_first_row_padding {
length.saturating_sub(1)
} else {
length
}
};
let start_padding_rw_counter = rows.last().map(|row| row.rw_counter()).unwrap_or(1) + 1;
let start_padding_rw_counter =
rows_trimmed.last().map(|row| row.rw_counter()).unwrap_or(1) + 1;
let padding = (start_padding_rw_counter..start_padding_rw_counter + padding_length)
.map(|rw_counter| Rw::Padding { rw_counter });
(
iter::once(Rw::Start { rw_counter: 1 })
.take(if is_first_row_padding { 1 } else { 0 })
.chain(rows.into_iter())
.chain(rows_trimmed.into_iter())
.chain(padding.into_iter())
.collect(),
padding_length,
Expand All @@ -157,7 +158,9 @@ impl RwMap {
/// Build Rws for assignment
pub fn table_assignments(&self, keep_chronological_order: bool) -> Vec<Rw> {
let mut rows: Vec<Rw> = self.0.values().flatten().cloned().collect();
if !keep_chronological_order {
if keep_chronological_order {
rows.sort_by_key(|row| row.rw_counter());
} else {
rows.sort_by_key(|row| {
(
row.tag() as u64,
Expand All @@ -169,6 +172,7 @@ impl RwMap {
)
});
}

rows
}
}
Expand Down

0 comments on commit aab44aa

Please sign in to comment.