diff --git a/crates/specs/src/slice.rs b/crates/specs/src/slice.rs index 453cbf88d..b6232c40b 100644 --- a/crates/specs/src/slice.rs +++ b/crates/specs/src/slice.rs @@ -40,7 +40,7 @@ impl From for FrameTableSlice { impl FrameTableSlice { pub fn build_returned_lookup_mapping(&self) -> HashMap<(u32, u32), bool> { - let mut lookup_table = HashMap::new(); + let mut lookup_table = HashMap::with_capacity(self.called.len() + self.inherited.0.len()); for entry in self.called.iter() { lookup_table.insert((entry.0.frame_id, entry.0.callee_fid), entry.0.returned); } diff --git a/crates/zkwasm/src/circuits/utils/image_table.rs b/crates/zkwasm/src/circuits/utils/image_table.rs index bef06882f..8a4e725a7 100644 --- a/crates/zkwasm/src/circuits/utils/image_table.rs +++ b/crates/zkwasm/src/circuits/utils/image_table.rs @@ -1,3 +1,5 @@ +use std::borrow::Cow; + use anyhow::Error; use halo2_proofs::arithmetic::FieldExt; use num_bigint::BigUint; @@ -324,8 +326,12 @@ pub(crate) fn encode_compilation_table_values( let (ltype, offset) = layouter.memory_location_from_offset(*pos); let entry = if let Some(entry) = init_memory_table.try_find(ltype, offset) { - bn_to_field::(&ImageTableEncoder::InitMemory.encode(entry.encode())) + Cow::Owned(bn_to_field::( + &ImageTableEncoder::InitMemory.encode(entry.encode()), + )) } else if ltype == LocationType::Heap { + // Perf: Use default entry to enhance performance. + // Original code: // let entry = InitMemoryTableEntry { // ltype, // is_mutable: true, @@ -336,24 +342,24 @@ pub(crate) fn encode_compilation_table_values( // }; // bn_to_field::(&ImageTableEncoder::InitMemory.encode(entry.encode())) - //bn_to_field::(&entry.encode()) - init_memory_table_entry_encode_update_offset( + + Cow::Owned(init_memory_table_entry_encode_update_offset( default_memory_entry, || F::from(offset as u64), - ) + )) } else { - *empty_entry.get_or_insert_with(|| { + Cow::Borrowed(empty_entry.get_or_insert_with(|| { bn_to_field::( &ImageTableEncoder::InitMemory.encode(BigUint::zero()), ) - }) + })) }; let addr = address.as_ptr(); unsafe { let addr = addr as *mut F; - *addr.offset((pos + 1) as isize) = entry; + *addr.offset((pos + 1) as isize) = *entry; } } });