mirror of
https://github.com/astral-sh/ruff.git
synced 2025-10-01 14:21:53 +00:00
perf: Reduce best fitting allocations (#7411)
This commit is contained in:
parent
2421805033
commit
3336d23f48
5 changed files with 173 additions and 83 deletions
|
@ -9,9 +9,7 @@ use Tag::*;
|
|||
use crate::format_element::tag::{Condition, Tag};
|
||||
use crate::prelude::tag::{DedentMode, GroupMode, LabelId};
|
||||
use crate::prelude::*;
|
||||
use crate::{
|
||||
format_element, write, Argument, Arguments, FormatContext, FormatOptions, GroupId, TextSize,
|
||||
};
|
||||
use crate::{write, Argument, Arguments, FormatContext, FormatOptions, GroupId, TextSize};
|
||||
use crate::{Buffer, VecBuffer};
|
||||
|
||||
/// A line break that only gets printed if the enclosing `Group` doesn't fit on a single line.
|
||||
|
@ -2543,15 +2541,12 @@ impl<Context> Format<Context> for BestFitting<'_, Context> {
|
|||
fn fmt(&self, f: &mut Formatter<Context>) -> FormatResult<()> {
|
||||
let variants = self.variants.items();
|
||||
|
||||
let mut formatted_variants = Vec::with_capacity(variants.len());
|
||||
let mut buffer = VecBuffer::with_capacity(variants.len() * 8, f.state_mut());
|
||||
|
||||
for variant in variants {
|
||||
let mut buffer = VecBuffer::with_capacity(8, f.state_mut());
|
||||
buffer.write_element(FormatElement::Tag(StartEntry));
|
||||
buffer.write_element(FormatElement::Tag(StartBestFittingEntry));
|
||||
buffer.write_fmt(Arguments::from(variant))?;
|
||||
buffer.write_element(FormatElement::Tag(EndEntry));
|
||||
|
||||
formatted_variants.push(buffer.into_vec().into_boxed_slice());
|
||||
buffer.write_element(FormatElement::Tag(EndBestFittingEntry));
|
||||
}
|
||||
|
||||
// SAFETY: The constructor guarantees that there are always at least two variants. It's, therefore,
|
||||
|
@ -2559,9 +2554,7 @@ impl<Context> Format<Context> for BestFitting<'_, Context> {
|
|||
#[allow(unsafe_code)]
|
||||
let element = unsafe {
|
||||
FormatElement::BestFitting {
|
||||
variants: format_element::BestFittingVariants::from_vec_unchecked(
|
||||
formatted_variants,
|
||||
),
|
||||
variants: BestFittingVariants::from_vec_unchecked(buffer.into_vec()),
|
||||
mode: self.mode,
|
||||
}
|
||||
};
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue