Skip to content

Commit

Permalink
feat(profiler): allow to preserve original stack trace order
Browse files Browse the repository at this point in the history
  • Loading branch information
m-kus committed Oct 4, 2024
1 parent 98fb56b commit d9a7e0e
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 24 deletions.
2 changes: 1 addition & 1 deletion crates/cairo-lang-runner/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ impl SierraCasmRunner {
// function).
// The value is the weight of the stack trace so far, not including the pending weight being
// tracked at the time.
let mut stack_trace_weights = UnorderedHashMap::default();
let mut stack_trace_weights = OrderedHashMap::default();
let mut end_of_program_reached = false;
// The total weight of each Sierra statement.
// Note the header and footer (CASM instructions added for running the program by the
Expand Down
35 changes: 12 additions & 23 deletions crates/cairo-lang-runner/src/profiling.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use cairo_lang_sierra_generator::db::SierraGenGroup;
use cairo_lang_utils::ordered_hash_map::OrderedHashMap;
use cairo_lang_utils::unordered_hash_map::UnorderedHashMap;
use cairo_lang_utils::{require, LookupIntern};
use itertools::Itertools;
use smol_str::SmolStr;

#[cfg(test)]
Expand Down Expand Up @@ -309,39 +310,27 @@ impl<'a> ProfilingInfoProcessor<'a> {
raw_profiling_info: &ProfilingInfo,
params: &ProfilingInfoProcessorParams,
) -> StackTraceWeights {
let resolve_names = |(idx_stack_trace, weight): (&Vec<usize>, &usize)| {
(index_stack_trace_to_name_stack_trace(&self.sierra_program, &idx_stack_trace), *weight)
};

let sierra_stack_trace_weights = params.process_by_stack_trace.then(|| {
raw_profiling_info
.stack_trace_weights
.iter_sorted_by_key(|(idx_stack_trace, weight)| {
(usize::MAX - **weight, (*idx_stack_trace).clone())
})
.map(|(idx_stack_trace, weight)| {
(
index_stack_trace_to_name_stack_trace(
&self.sierra_program,
idx_stack_trace,
),
*weight,
)
})
.iter()
.sorted_by_key(|&(trace, weight)| (usize::MAX - *weight, trace.clone()))
.map(resolve_names)
.collect()
});

let cairo_stack_trace_weights = params.process_by_cairo_stack_trace.then(|| {
let db = self.db.expect("DB must be set with `process_by_cairo_stack_trace=true`.");
raw_profiling_info
.stack_trace_weights
.filter_cloned(|trace, _| is_cairo_trace(db, &self.sierra_program, trace))
.into_iter_sorted_by_key(|(trace, weight)| (usize::MAX - *weight, trace.clone()))
.map(|(idx_stack_trace, weight)| {
(
index_stack_trace_to_name_stack_trace(
&self.sierra_program,
&idx_stack_trace,
),
weight,
)
})
.iter()
.filter(|(trace, _)| is_cairo_trace(db, &self.sierra_program, trace))
.sorted_by_key(|&(trace, weight)| (usize::MAX - *weight, trace.clone()))
.map(resolve_names)
.collect()
});

Expand Down

0 comments on commit d9a7e0e

Please sign in to comment.