diff --git a/Cargo.toml b/Cargo.toml index 4eb8a3705..83b29252d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -81,6 +81,7 @@ manual_assert = "warn" manual_instant_elapsed = "warn" manual_let_else = "warn" manual_ok_or = "warn" +manual_string_new = "warn" many_single_char_names = "warn" match_wild_err_arm = "warn" mismatching_type_param_order = "warn" diff --git a/triton-vm/src/instruction.rs b/triton-vm/src/instruction.rs index 263728c99..18c6c5a68 100644 --- a/triton-vm/src/instruction.rs +++ b/triton-vm/src/instruction.rs @@ -81,10 +81,10 @@ pub struct TypeHint { impl Display for TypeHint { fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult { let variable = &self.variable_name; - let type_name = match self.type_name { - Some(ref type_name) => format!(": {type_name}"), - None => "".to_string(), - }; + + let format_type = |t| format!(": {t}"); + let maybe_type = self.type_name.as_ref(); + let type_name = maybe_type.map(format_type).unwrap_or_default(); let start = self.starting_index; let range = match self.length { diff --git a/triton-vm/src/profiler.rs b/triton-vm/src/profiler.rs index e9b1b094f..f793952c3 100644 --- a/triton-vm/src/profiler.rs +++ b/triton-vm/src/profiler.rs @@ -15,6 +15,7 @@ use colored::Color; use colored::ColoredString; use colored::Colorize; use criterion::profiler::Profiler; +use itertools::Itertools; use unicode_width::UnicodeWidthStr; const GET_PROFILE_OUTPUT_AS_YOU_GO_ENV_VAR_NAME: &str = "PROFILE_AS_YOU_GO"; @@ -355,38 +356,18 @@ impl Weight { } } - fn color(&self) -> Color { - use Color::*; - use Weight::*; - match self { - LikeNothing => TrueColor { - r: 120, - g: 120, - b: 120, - }, - VeryLittle => TrueColor { - r: 200, - g: 200, - b: 200, - }, - Light => White, - Noticeable => TrueColor { - r: 255, - g: 255, - b: 120, - }, - Heavy => TrueColor { - r: 255, - g: 150, - b: 0, - }, - Massive => TrueColor { - r: 255, - g: 75, - b: 0, - }, - SuperMassive => Red, - } + fn color(self) -> Color { + let [r, g, b] = match self { + Self::LikeNothing => [120; 3], + Self::VeryLittle => [200; 3], + Self::Light => [255; 3], + Self::Noticeable => [255, 255, 120], + Self::Heavy => [255, 150, 0], + Self::Massive => [255, 75, 0], + Self::SuperMassive => [255, 0, 0], + }; + + Color::TrueColor { r, g, b } } } @@ -451,7 +432,7 @@ impl Display for Report { .map(|t| t.name.width() + 2 * t.depth) .max() .expect("No tasks to generate report from."); - let max_category_name_width = self + let max_category_width = self .category_times .keys() .map(|k| k.width()) @@ -460,86 +441,74 @@ impl Display for Report { let title = format!("### {}", self.name).bold(); let max_width = max(max_name_width, title.width()); + let title = format!("{title: "".to_string(), - false => "Category".to_string(), - } - .bold(); + true => ColoredString::default(), + false => "Category".bold(), + }; writeln!( f, - "{title}{separation} {total_time_string} {share_string} {category_string}" + "{title} {total_time_string} {share_string} {category_string}" )?; for task in &self.tasks { for &ancestor_index in &task.ancestors { - let mut spacer: ColoredString = if self.tasks[ancestor_index].is_last_sibling { - " ".normal() - } else { - "│ ".normal() - }; - let uncle_weight = &self.tasks[ancestor_index].younger_max_weight; - spacer = spacer.color(uncle_weight.color()); - + let ancestor = &self.tasks[ancestor_index]; + let spacer_color = ancestor.younger_max_weight.color(); + let is_last_sibling = ancestor.is_last_sibling; + let spacer = if is_last_sibling { " " } else { "│ " }.color(spacer_color); write!(f, "{spacer}")?; } - let tracer = if task.is_last_sibling { - "└".normal() - } else { - "├".normal() - } - .color(max(task.weight, task.younger_max_weight).color()); + let is_last_sibling = task.is_last_sibling; + let tracer = if is_last_sibling { "└" } else { "├" } + .color(max(task.weight, task.younger_max_weight).color()); let dash = "─".color(task.weight.color()); write!(f, "{tracer}{dash}")?; - let padding_length = max_width - task.name.len() - 2 * task.depth; - assert!( - padding_length < (1 << 60), - "max width: {max_name_width}, width: {}", - task.name.len(), - ); + let task_name_area = max_width - 2 * task.depth; let task_name_colored = task.name.color(task.weight.color()); - let mut task_time = Report::display_time_aligned(task.time); - while task_time.width() < 10 { - task_time.push(b' '.into()); - } + let task_name_colored = format!("{task_name_colored:6}", format!("{:2.2}%", 100.0 * task.relative_time)); let relative_time_string_colored = relative_time_string.color(task.weight.color()); - let category_name = task.category.as_deref().unwrap_or(""); let relative_category_time = task .relative_category_time .map(|t| format!("{:>6}", format!("{:2.2}%", 100.0 * t))) - .unwrap_or("".to_string()); - let category_and_relative_time = match task.category.is_some() { - true => format!( - "({category_name: "".to_string(), - }; + .unwrap_or_default(); + let maybe_category = task.category.as_ref(); + let category_and_relative_time = maybe_category + .map(|cat| format!("({cat:>(); - category_times_and_names_sorted_by_time.sort_by_key(|&(_, time)| time); - category_times_and_names_sorted_by_time.reverse(); - for (category, category_time) in category_times_and_names_sorted_by_time { + .sorted_by_key(|(_, &time)| time) + .rev(); + for (category, &category_time) in category_times_and_names_sorted_by_time { let category_relative_time = category_time.as_secs_f64() / self.total_time.as_secs_f64(); let category_color = Weight::weigh(category_relative_time).color(); @@ -547,16 +516,10 @@ impl Display for Report { format!("{:>6}", format!("{:2.2}%", 100.0 * category_relative_time)); let category_time = Report::display_time_aligned(category_time); - let padding_length = max_category_name_width - category.width(); - let padding = String::from_utf8(vec![b' '; padding_length]).unwrap(); - - let category = category.color(category_color); + let category = format!("{category: String { let alphabet = "abcdefghijklmnopqrstuvwxyz_"; - let length = (rng.next_u32() as usize % 10) + 2; + let length = rng.gen_range(2..12); (0..length) .map(|_| (rng.next_u32() as usize) % alphabet.len()) diff --git a/triton-vm/src/stark.rs b/triton-vm/src/stark.rs index f6a5046c7..3d237b6d4 100644 --- a/triton-vm/src/stark.rs +++ b/triton-vm/src/stark.rs @@ -1352,7 +1352,7 @@ pub(crate) mod tests { let nia = if curr_instruction.has_arg() { next_instruction_or_arg.to_string() } else { - "".to_string() + String::new() }; (curr_instruction.name().to_string(), nia) } diff --git a/triton-vm/src/vm.rs b/triton-vm/src/vm.rs index b1fff27d0..1be6a25d2 100644 --- a/triton-vm/src/vm.rs +++ b/triton-vm/src/vm.rs @@ -924,7 +924,7 @@ impl Display for VMState { let buffer_width = total_width - tab_width - clk_width - 7; let print_row = |f: &mut Formatter, s: String| writeln!(f, "│ {s: