Skip to content

Commit

Permalink
fix: refactor duplicate code
Browse files Browse the repository at this point in the history
  • Loading branch information
leruaa committed Nov 13, 2024
1 parent f64ff32 commit 69c34f1
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 29 deletions.
23 changes: 23 additions & 0 deletions crates/core/executor/src/events/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,26 @@ where
{
format!("{count:>width$} {label}")
}

/// Returns sorted and formatted rows of a table of counts (e.g. `opcode_counts`).
///
/// The table is sorted first by count (descending) and then by label (ascending).
/// The first column consists of the counts, is right-justified, and is padded precisely
/// enough to fit all the numbers. The second column consists of the labels (e.g. `OpCode`s).
/// The columns are separated by a single space character.
///
/// It's possible to hide rows with 0 count by setting `hide_zeros` to true.
pub fn generate_execution_report<'a, K, V>(
table: impl IntoIterator<Item = (K, &'a V)> + 'a,
hide_zeros: bool,
) -> impl Iterator<Item = String> + 'a
where
K: Ord + Display + 'a,
V: Ord + PartialEq<u64> + Display + 'a,
{
let (width, lines) = sorted_table_lines(table);

lines
.filter(move |(_, count)| !hide_zeros || **count != 0_u64)
.map(move |(label, count)| format!(" {}", format_table_line(&width, &label, count)))
}
20 changes: 5 additions & 15 deletions crates/core/executor/src/report.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,7 @@ use std::{
use enum_map::{EnumArray, EnumMap};
use hashbrown::HashMap;

use crate::{
events::{format_table_line, sorted_table_lines},
syscalls::SyscallCode,
Opcode,
};
use crate::{events::generate_execution_report, syscalls::SyscallCode, Opcode};

/// An execution report.
#[derive(Default, Debug, Clone, PartialEq, Eq)]
Expand Down Expand Up @@ -72,19 +68,13 @@ impl Add for ExecutionReport {
impl Display for ExecutionReport {
fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
writeln!(f, "opcode counts ({} total instructions):", self.total_instruction_count())?;
let (width, lines) = sorted_table_lines(self.opcode_counts.as_ref());
for (label, count) in lines {
writeln!(f, " {}", format_table_line(&width, &label, count))?;
for line in generate_execution_report(self.opcode_counts.as_ref(), false) {
writeln!(f, " {line}")?;
}

writeln!(f, "syscall counts ({} total syscall instructions):", self.total_syscall_count())?;
let (width, lines) = sorted_table_lines(self.syscall_counts.as_ref());
for (label, count) in lines {
if *count == 0 {
break;
}

writeln!(f, " {}", format_table_line(&width, &label, count))?;
for line in generate_execution_report(self.syscall_counts.as_ref(), true) {
writeln!(f, " {line}")?;
}

Ok(())
Expand Down
19 changes: 5 additions & 14 deletions crates/core/machine/src/utils/prove.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,7 @@ use crate::{
riscv::cost::CostEstimator,
utils::{chunk_vec, concurrency::TurnBasedSync},
};
use sp1_core_executor::{
events::{format_table_line, sorted_table_lines},
ExecutionState,
};
use sp1_core_executor::{events::generate_execution_report, ExecutionState};
use sp1_primitives::io::SP1PublicValues;

use sp1_core_executor::{
Expand Down Expand Up @@ -677,19 +674,13 @@ where
// Print the opcode and syscall count tables like `du`: sorted by count (descending) and
// with the count in the first column.
tracing::info!("execution report (opcode counts):");
let (width, lines) = sorted_table_lines(report_aggregate.opcode_counts.as_ref());
for (label, count) in lines {
tracing::info!(" {}", format_table_line(&width, &label, count));
for line in generate_execution_report(report_aggregate.opcode_counts.as_ref(), false) {
tracing::info!(" {line}");
}

tracing::info!("execution report (syscall counts):");
let (width, lines) = sorted_table_lines(report_aggregate.syscall_counts.as_ref());
for (label, count) in lines {
if *count == 0 {
break;
}

tracing::info!(" {}", format_table_line(&width, &label, count));
for line in generate_execution_report(report_aggregate.syscall_counts.as_ref(), true) {
tracing::info!(" {line}");
}

let proof = MachineProof::<SC> { shard_proofs };
Expand Down

0 comments on commit 69c34f1

Please sign in to comment.