Remove unnecessary heap allocations in `PrettyPrinter::print` for style components

This commit is contained in:
rhysd 2023-01-15 19:04:16 +09:00
parent 5e3abcad07
commit 4cc2a489d0
2 changed files with 21 additions and 12 deletions

View File

@ -10,7 +10,7 @@ use crate::{
error::Result, error::Result,
input, input,
line_range::{HighlightedLineRanges, LineRange, LineRanges}, line_range::{HighlightedLineRanges, LineRange, LineRanges},
style::{StyleComponent, StyleComponents}, style::StyleComponent,
SyntaxMapping, WrappingMode, SyntaxMapping, WrappingMode,
}; };
@ -20,6 +20,7 @@ use crate::paging::PagingMode;
#[derive(Default)] #[derive(Default)]
struct ActiveStyleComponents { struct ActiveStyleComponents {
header: bool, header: bool,
#[cfg(feature = "git")]
vcs_modification_markers: bool, vcs_modification_markers: bool,
grid: bool, grid: bool,
rule: bool, rule: bool,
@ -269,31 +270,31 @@ impl<'a> PrettyPrinter<'a> {
.term_width .term_width
.unwrap_or_else(|| Term::stdout().size().1 as usize); .unwrap_or_else(|| Term::stdout().size().1 as usize);
let mut style_components = vec![]; self.config.style_components.clear();
if self.active_style_components.grid { if self.active_style_components.grid {
style_components.push(StyleComponent::Grid); self.config.style_components.insert(StyleComponent::Grid);
} }
if self.active_style_components.rule { if self.active_style_components.rule {
style_components.push(StyleComponent::Rule); self.config.style_components.insert(StyleComponent::Rule);
} }
if self.active_style_components.header { if self.active_style_components.header {
style_components.push(StyleComponent::Header); self.config.style_components.insert(StyleComponent::Header);
} }
if self.active_style_components.line_numbers { if self.active_style_components.line_numbers {
style_components.push(StyleComponent::LineNumbers); self.config
.style_components
.insert(StyleComponent::LineNumbers);
} }
if self.active_style_components.snip { if self.active_style_components.snip {
style_components.push(StyleComponent::Snip); self.config.style_components.insert(StyleComponent::Snip);
} }
#[cfg(feature = "git")]
if self.active_style_components.vcs_modification_markers { if self.active_style_components.vcs_modification_markers {
#[cfg(feature = "git")] self.config.style_components.insert(StyleComponent::Changes);
style_components.push(StyleComponent::Changes);
} }
self.config.style_components = StyleComponents::new(&style_components);
// Collect the inputs to print // Collect the inputs to print
let mut inputs: Vec<Input> = vec![]; let inputs = std::mem::take(&mut self.inputs);
std::mem::swap(&mut inputs, &mut self.inputs);
// Run the controller // Run the controller
let controller = Controller::new(&self.config, &self.assets); let controller = Controller::new(&self.config, &self.assets);

View File

@ -129,4 +129,12 @@ impl StyleComponents {
pub fn plain(&self) -> bool { pub fn plain(&self) -> bool {
self.0.iter().all(|c| c == &StyleComponent::Plain) self.0.iter().all(|c| c == &StyleComponent::Plain)
} }
pub fn insert(&mut self, component: StyleComponent) {
self.0.insert(component);
}
pub fn clear(&mut self) {
self.0.clear();
}
} }