Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion compiler/noirc_evaluator/src/acir/arrays.rs
Original file line number Diff line number Diff line change
Expand Up @@ -879,8 +879,14 @@ impl Context<'_> {
AcirValue::Array(values) => {
let flat_elem_type_sizes = calculate_element_type_sizes_array(values);

// If there's already a block with these same sizes, reuse it. It's fine do to so
// because the element type sizes array is never mutated.
if let Some(block_id) = self.type_sizes_to_blocks.get(&flat_elem_type_sizes) {
return Ok(*block_id);
}

// The final array should will the flattened index at each outer array index
let init_values = vecmap(flat_elem_type_sizes, |type_size| {
let init_values = vecmap(flat_elem_type_sizes.clone(), |type_size| {
let var = self.acir_context.add_constant(type_size);
AcirValue::Var(var, NumericType::NativeField)
});
Expand All @@ -891,6 +897,8 @@ impl Context<'_> {
Some(AcirValue::Array(init_values.into())),
)?;

self.type_sizes_to_blocks.insert(flat_elem_type_sizes, element_type_sizes);

Ok(element_type_sizes)
}

Expand Down
5 changes: 5 additions & 0 deletions compiler/noirc_evaluator/src/acir/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,10 @@ struct Context<'a> {
/// which utilizes this internal memory for ACIR generation.
element_type_sizes_blocks: HashMap<Id<Value>, BlockId>,

/// Maps type sizes to BlockId. This is used to reuse the same BlockId if different
/// non-homogenous arrays end up having the same type sizes layout.
type_sizes_to_blocks: HashMap<Vec<usize>, BlockId>,

/// Number of the next BlockId, it is used to construct
/// a new BlockId
max_block_id: u32,
Expand Down Expand Up @@ -125,6 +129,7 @@ impl<'a> Context<'a> {
initialized_arrays: HashSet::default(),
memory_blocks: HashMap::default(),
element_type_sizes_blocks: HashMap::default(),
type_sizes_to_blocks: HashMap::default(),
max_block_id: 0,
data_bus: DataBus::default(),
shared_context,
Expand Down
Loading