Merge pull request #2447 from rhysd/reuse-styles

Remove unnecessary heap allocations in `PrettyPrinter::print` for style components
This commit is contained in:
David Peter 2023-01-17 15:26:18 +01:00 committed by GitHub
commit 09ab1905d0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
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();
}
} }