Skip to content
This repository was archived by the owner on Apr 18, 2025. It is now read-only.

Commit a6f2288

Browse files
committed
parallelize mpt table loading
1 parent 7e44461 commit a6f2288

File tree

2 files changed

+56
-6
lines changed

2 files changed

+56
-6
lines changed

zkevm-circuits/src/mpt_circuit.rs

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -138,12 +138,22 @@ impl SubCircuit<Fr> for MptCircuit<Fr> {
138138
layouter: &mut impl Layouter<Fr>,
139139
) -> Result<(), Error> {
140140
config.0.assign(layouter, &self.proofs, self.row_limit)?;
141-
config.1.load(
142-
layouter,
143-
&self.mpt_updates,
144-
self.row_limit,
145-
challenges.evm_word(),
146-
)?;
141+
let use_par = std::env::var("PARALLEL_SYN").map_or(false, |s| s == *"true");
142+
if use_par {
143+
config.1.load_par(
144+
layouter,
145+
&self.mpt_updates,
146+
self.row_limit,
147+
challenges.evm_word(),
148+
)?;
149+
} else {
150+
config.1.load(
151+
layouter,
152+
&self.mpt_updates,
153+
self.row_limit,
154+
challenges.evm_word(),
155+
)?;
156+
}
147157
Ok(())
148158
}
149159

zkevm-circuits/src/table.rs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -803,6 +803,46 @@ impl MptTable {
803803
)
804804
}
805805

806+
pub(crate) fn load_par<F: Field>(
807+
&self,
808+
layouter: &mut impl Layouter<F>,
809+
updates: &MptUpdates,
810+
max_mpt_rows: usize,
811+
randomness: Value<F>,
812+
) -> Result<(), Error> {
813+
let num_threads = std::thread::available_parallelism().unwrap().get();
814+
let chunk_size = (max_mpt_rows + num_threads - 1) / num_threads;
815+
let mpt_update_rows = updates
816+
.table_assignments(randomness)
817+
.into_iter()
818+
.chain(repeat(MptUpdateRow::padding()))
819+
.take(max_mpt_rows)
820+
.collect_vec();
821+
let mut is_first_passes = vec![true; num_threads];
822+
let assignments = mpt_update_rows
823+
.chunks(chunk_size)
824+
.zip(is_first_passes.iter_mut())
825+
.map(|(mpt_update_rows, is_first_pass)| {
826+
|mut region: Region<'_, F>| -> Result<(), Error> {
827+
if *is_first_pass {
828+
*is_first_pass = false;
829+
let last_off = mpt_update_rows.len() - 1;
830+
self.assign(&mut region, last_off, &mpt_update_rows[last_off])?;
831+
return Ok(());
832+
}
833+
for (offset, row) in mpt_update_rows.iter().enumerate() {
834+
self.assign(&mut region, offset, row)?;
835+
}
836+
Ok(())
837+
}
838+
})
839+
.collect_vec();
840+
841+
layouter.assign_regions(|| "mpt table zkevm", assignments)?;
842+
843+
Ok(())
844+
}
845+
806846
pub(crate) fn load_with_region<F: Field>(
807847
&self,
808848
region: &mut Region<'_, F>,

0 commit comments

Comments
 (0)