Skip to content
Draft
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
5 changes: 4 additions & 1 deletion src/executor/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,9 @@ impl AotContractExecutor {

// Compile the Sierra program.
let NativeModule {
module, registry, ..
module,
registry,
metadata,
} = context.compile(
program,
true,
Expand Down Expand Up @@ -310,6 +312,7 @@ impl AotContractExecutor {
GenStatement::Return(_) => continue,
}
}
stats.sierra_declared_types_freq = metadata.types_frequencies();
}

// Generate mappings between the entry point's selectors and their function indexes.
Expand Down
14 changes: 13 additions & 1 deletion src/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@
//! - Pass extra compilation info to the libfunc generators (ex.
//! [TailRecursionMeta](self::tail_recursion)).

use cairo_lang_sierra::ids::ConcreteTypeId;
use std::{
any::{Any, TypeId},
collections::{hash_map::Entry, HashMap},
collections::{hash_map::Entry, BTreeMap, HashMap},
};

pub mod auto_breakpoint;
Expand All @@ -32,6 +33,8 @@ pub mod trace_dump;
#[derive(Debug)]
pub struct MetadataStorage {
entries: HashMap<TypeId, Box<dyn Any>>,
/// Number of times each declared type is built
types_freqs: BTreeMap<u64, u64>,
}

impl MetadataStorage {
Expand Down Expand Up @@ -110,13 +113,22 @@ impl MetadataStorage {
.downcast_mut::<T>()
.expect("the given type does not match the actual")
}

pub fn increment_frequency(&mut self, type_id: &ConcreteTypeId) {
*self.types_freqs.entry(type_id.id).or_insert(0) += 1;
}

pub fn types_frequencies(&self) -> BTreeMap<u64, u64> {
self.types_freqs.clone()
}
}

#[cfg(feature = "with-debug-utils")]
impl Default for MetadataStorage {
fn default() -> Self {
let mut metadata = Self {
entries: Default::default(),
types_freqs: Default::default(),
};

metadata.insert(debug_utils::DebugUtils::default());
Expand Down
2 changes: 2 additions & 0 deletions src/statistics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ pub struct Statistics {
pub sierra_func_stats: BTreeMap<u64, SierraFuncStats>,
/// Number of statements for each distinct libfunc.
pub sierra_libfunc_frequency: BTreeMap<String, u128>,
/// Number of times a Sierra declared type was built
pub sierra_declared_types_freq: BTreeMap<u64, u64>,
/// Number of times each circuit gate is used.
pub sierra_circuit_gates_count: CircuitGatesStats,
/// Number of MLIR operations generated.
Expand Down
1 change: 1 addition & 0 deletions src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ impl TypeBuilder for CoreTypeConcrete {
metadata: &mut MetadataStorage,
self_ty: &ConcreteTypeId,
) -> Result<Type<'ctx>, Self::Error> {
metadata.increment_frequency(self_ty);
match self {
Self::Array(info) => self::array::build(
context,
Expand Down
Loading