From 69d38a5f8e8baf274e30dbb2b26f5cc7e6f1cefd Mon Sep 17 00:00:00 2001 From: Philipp Paulweber Date: Fri, 22 Apr 2022 15:55:59 +0200 Subject: [PATCH] Refactor MarkupExporter to omit intermediate table result creation and simplify MarkupFormatter emitting * see: https://github.com/sharkdp/hyperfine/pull/490#pullrequestreview-932424652 --- src/export/markdown.rs | 8 +---- src/export/markup.rs | 67 ++++++++++++++++-------------------------- 2 files changed, 27 insertions(+), 48 deletions(-) diff --git a/src/export/markdown.rs b/src/export/markdown.rs index f91a5b2..b8f307d 100644 --- a/src/export/markdown.rs +++ b/src/export/markdown.rs @@ -37,13 +37,7 @@ impl Exporter for MarkdownExporter { } let formatter = MarkdownFormatter::default(); - let data_vvs = formatter.results(&entries.unwrap(), unit); - let data_vas: Vec> = data_vvs - .iter() - .map(|list| list.iter().map(AsRef::as_ref).collect::>()) - .collect(); - let data: Vec<&[&str]> = data_vas.iter().map(AsRef::as_ref).collect(); - let table = formatter.table(&data); + let table = formatter.table_results(&entries.unwrap(), unit); Ok(table.as_bytes().to_vec()) } } diff --git a/src/export/markup.rs b/src/export/markup.rs index 9005eca..674b5ed 100644 --- a/src/export/markup.rs +++ b/src/export/markup.rs @@ -3,45 +3,23 @@ use crate::output::format::format_duration_value; use crate::util::units::Unit; pub trait MarkupFormatter { - fn table(&self, data: &[&[&str]]) -> String { - let head: &[&str] = data.first().unwrap(); - let tail = &data[1..]; + fn table_results(&self, entries: &[BenchmarkResultWithRelativeSpeed], unit: Unit) -> String { + // prepare table header strings + let notation = format!("[{}]", unit.short_name()); + let head: [&str; 5] = [ + "Command", + &format!("Mean {}", notation), + &format!("Min {}", notation), + &format!("Max {}", notation), + "Relative", + ]; // emit header - let table = &mut self.table_data(head); + let mut table = self.table_data(&head); // emit horizontal line table.push_str(&self.table_line(head.len())); - // emit data rows - for row in tail { - table.push_str(&self.table_data(row)) - } - - table.to_string() - } - - fn table_data(&self, data: &[&str]) -> String; - - fn table_line(&self, size: usize) -> String; - - fn command(&self, size: &str) -> String; - - fn results( - &self, - entries: &[BenchmarkResultWithRelativeSpeed], - unit: Unit, - ) -> Vec> { - // prepare table header strings - let notation = format!("[{}]", unit.short_name()); - let mut data: Vec> = vec![vec![ - "Command".to_string(), - format!("Mean {}", notation), - format!("Min {}", notation), - format!("Max {}", notation), - "Relative".to_string(), - ]]; - for entry in entries { let measurement = &entry.result; // prepare data row strings @@ -63,15 +41,22 @@ pub trait MarkupFormatter { "".into() }; // prepare table row entries - data.push(vec![ - self.command(&cmd_str), - format!("{}{}", mean_str, stddev_str), - min_str, - max_str, - format!("{}{}", rel_str, rel_stddev_str), - ]) + let data: [&str; 5] = [ + &self.command(&cmd_str), + &format!("{}{}", mean_str, stddev_str), + &min_str, + &max_str, + &format!("{}{}", rel_str, rel_stddev_str), + ]; + table.push_str(&self.table_data(&data)) } - data + table } + + fn table_data(&self, data: &[&str]) -> String; + + fn table_line(&self, size: usize) -> String; + + fn command(&self, size: &str) -> String; }