mirror of
https://github.com/astral-sh/ruff.git
synced 2025-09-28 12:55:05 +00:00
Printer: Reserve buffer upfront (#6550)
This commit is contained in:
parent
9584f613b9
commit
910dbbd9b6
2 changed files with 18 additions and 9 deletions
|
@ -319,20 +319,20 @@ where
|
|||
Context: FormatContext,
|
||||
{
|
||||
pub fn print(&self) -> PrintResult<Printed> {
|
||||
let source_code = self.context.source_code();
|
||||
let print_options = self.context.options().as_print_options();
|
||||
let printed = Printer::new(source_code, print_options).print(&self.document)?;
|
||||
|
||||
Ok(printed)
|
||||
let printer = self.create_printer();
|
||||
printer.print(&self.document)
|
||||
}
|
||||
|
||||
pub fn print_with_indent(&self, indent: u16) -> PrintResult<Printed> {
|
||||
let printer = self.create_printer();
|
||||
printer.print_with_indent(&self.document, indent)
|
||||
}
|
||||
|
||||
fn create_printer(&self) -> Printer {
|
||||
let source_code = self.context.source_code();
|
||||
let print_options = self.context.options().as_print_options();
|
||||
let printed =
|
||||
Printer::new(source_code, print_options).print_with_indent(&self.document, indent)?;
|
||||
|
||||
Ok(printed)
|
||||
Printer::new(source_code, print_options)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -40,7 +40,7 @@ impl<'a> Printer<'a> {
|
|||
Self {
|
||||
source_code,
|
||||
options,
|
||||
state: PrinterState::default(),
|
||||
state: PrinterState::with_capacity(source_code.as_str().len()),
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -757,6 +757,15 @@ struct PrinterState<'a> {
|
|||
fits_queue: Vec<&'a [FormatElement]>,
|
||||
}
|
||||
|
||||
impl<'a> PrinterState<'a> {
|
||||
fn with_capacity(capacity: usize) -> Self {
|
||||
Self {
|
||||
buffer: String::with_capacity(capacity),
|
||||
..Self::default()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Tracks the mode in which groups with ids are printed. Stores the groups at `group.id()` index.
|
||||
/// This is based on the assumption that the group ids for a single document are dense.
|
||||
#[derive(Debug, Default)]
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue