diff --git a/extensions/scarb-cairo-run/src/main.rs b/extensions/scarb-cairo-run/src/main.rs index 250339434..4a935a593 100644 --- a/extensions/scarb-cairo-run/src/main.rs +++ b/extensions/scarb-cairo-run/src/main.rs @@ -43,6 +43,10 @@ struct Args { #[arg(long, default_value_t = false)] print_full_memory: bool, + /// Print detailed resources. + #[arg(long, default_value_t = false)] + print_resource_usage: bool, + /// Do not rebuild the package. #[arg(long, default_value_t = false)] no_build: bool, @@ -150,6 +154,7 @@ fn main_inner(ui: &Ui, args: Args) -> Result<()> { result, print_full_memory: args.print_full_memory, gas_defined: available_gas.is_defined(), + detailed_resources: args.print_resource_usage, }); Ok(()) @@ -228,6 +233,7 @@ struct Summary { result: RunResultStarknet, print_full_memory: bool, gas_defined: bool, + detailed_resources: bool, } impl Message for Summary { @@ -272,6 +278,18 @@ impl Message for Summary { } println!("]"); } + + if self.detailed_resources { + let resources = self.result.used_resources.basic_resources; + let sorted_builtins = sort_by_value(&resources.builtin_instance_counter); + let sorted_syscalls = sort_by_value(&self.result.used_resources.syscalls); + + println!("Resources:"); + println!("\tsteps: {}", resources.n_steps); + println!("\tmemory holes: {}", resources.n_memory_holes); + println!("\tbuiltins: ({})", format_items(&sorted_builtins)); + println!("\tsyscalls: ({})", format_items(&sorted_syscalls)); + } } fn structured(self, _ser: S) -> Result @@ -282,6 +300,28 @@ impl Message for Summary { } } +fn sort_by_value<'a, K, V, M>(map: M) -> Vec<(&'a K, &'a V)> +where + M: IntoIterator, + V: Ord, +{ + let mut sorted: Vec<_> = map.into_iter().collect(); + sorted.sort_by(|a, b| b.1.cmp(a.1)); + sorted +} + +fn format_items(items: &[(K, V)]) -> String +where + K: std::fmt::Debug, + V: std::fmt::Display, +{ + items + .iter() + .map(|(key, value)| format!("{key:?}: {value}")) + .collect::>() + .join(", ") +} + enum GasLimit { Disabled, Unlimited,