mirror of
https://github.com/astral-sh/ruff.git
synced 2025-09-29 05:15:12 +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
|
@ -35,7 +35,10 @@ impl Document {
|
|||
enum Enclosing<'a> {
|
||||
Group(&'a tag::Group),
|
||||
ConditionalGroup(&'a tag::ConditionalGroup),
|
||||
FitsExpanded(&'a tag::FitsExpanded),
|
||||
FitsExpanded {
|
||||
tag: &'a tag::FitsExpanded,
|
||||
expands_before: bool,
|
||||
},
|
||||
BestFitting,
|
||||
}
|
||||
|
||||
|
@ -43,7 +46,7 @@ impl Document {
|
|||
match enclosing.last() {
|
||||
Some(Enclosing::Group(group)) => group.propagate_expand(),
|
||||
Some(Enclosing::ConditionalGroup(group)) => group.propagate_expand(),
|
||||
Some(Enclosing::FitsExpanded(fits_expanded)) => fits_expanded.propagate_expand(),
|
||||
Some(Enclosing::FitsExpanded { tag, .. }) => tag.propagate_expand(),
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
@ -85,23 +88,24 @@ impl Document {
|
|||
FormatElement::BestFitting { variants, mode: _ } => {
|
||||
enclosing.push(Enclosing::BestFitting);
|
||||
|
||||
for variant in variants {
|
||||
propagate_expands(variant, enclosing, checked_interned);
|
||||
}
|
||||
|
||||
// Best fitting acts as a boundary
|
||||
expands = false;
|
||||
propagate_expands(variants, enclosing, checked_interned);
|
||||
enclosing.pop();
|
||||
continue;
|
||||
}
|
||||
FormatElement::Tag(Tag::StartFitsExpanded(fits_expanded)) => {
|
||||
enclosing.push(Enclosing::FitsExpanded(fits_expanded));
|
||||
enclosing.push(Enclosing::FitsExpanded {
|
||||
tag: fits_expanded,
|
||||
expands_before: expands,
|
||||
});
|
||||
false
|
||||
}
|
||||
FormatElement::Tag(Tag::EndFitsExpanded) => {
|
||||
enclosing.pop();
|
||||
// Fits expanded acts as a boundary
|
||||
expands = false;
|
||||
if let Some(Enclosing::FitsExpanded { expands_before, .. }) =
|
||||
enclosing.pop()
|
||||
{
|
||||
expands = expands_before;
|
||||
}
|
||||
|
||||
continue;
|
||||
}
|
||||
FormatElement::Text {
|
||||
|
@ -338,14 +342,20 @@ impl Format<IrFormatContext<'_>> for &[FormatElement] {
|
|||
}
|
||||
|
||||
FormatElement::BestFitting { variants, mode } => {
|
||||
write!(f, [token("best_fitting([")])?;
|
||||
write!(f, [token("best_fitting(")])?;
|
||||
|
||||
if *mode != BestFittingMode::FirstLine {
|
||||
write!(f, [text(&std::format!("mode: {mode:?}, "), None)])?;
|
||||
}
|
||||
|
||||
write!(f, [token("[")])?;
|
||||
f.write_elements([
|
||||
FormatElement::Tag(StartIndent),
|
||||
FormatElement::Line(LineMode::Hard),
|
||||
]);
|
||||
|
||||
for variant in variants {
|
||||
write!(f, [&**variant, hard_line_break()])?;
|
||||
write!(f, [variant, hard_line_break()])?;
|
||||
}
|
||||
|
||||
f.write_elements([
|
||||
|
@ -353,13 +363,7 @@ impl Format<IrFormatContext<'_>> for &[FormatElement] {
|
|||
FormatElement::Line(LineMode::Hard),
|
||||
]);
|
||||
|
||||
write!(f, [token("]")])?;
|
||||
|
||||
if *mode != BestFittingMode::FirstLine {
|
||||
write!(f, [text(&std::format!(", mode: {mode:?}"), None),])?;
|
||||
}
|
||||
|
||||
write!(f, [token(")")])?;
|
||||
write!(f, [token("])")])?;
|
||||
}
|
||||
|
||||
FormatElement::Interned(interned) => {
|
||||
|
@ -594,10 +598,10 @@ impl Format<IrFormatContext<'_>> for &[FormatElement] {
|
|||
}
|
||||
}
|
||||
|
||||
StartEntry => {
|
||||
StartEntry | StartBestFittingEntry { .. } => {
|
||||
// handled after the match for all start tags
|
||||
}
|
||||
EndEntry => write!(f, [ContentArrayEnd])?,
|
||||
EndEntry | EndBestFittingEntry => write!(f, [ContentArrayEnd])?,
|
||||
|
||||
EndFill
|
||||
| EndLabelled
|
||||
|
|
|
@ -83,6 +83,9 @@ pub enum Tag {
|
|||
|
||||
StartFitsExpanded(FitsExpanded),
|
||||
EndFitsExpanded,
|
||||
|
||||
StartBestFittingEntry,
|
||||
EndBestFittingEntry,
|
||||
}
|
||||
|
||||
impl Tag {
|
||||
|
@ -103,6 +106,7 @@ impl Tag {
|
|||
| Tag::StartVerbatim(_)
|
||||
| Tag::StartLabelled(_)
|
||||
| Tag::StartFitsExpanded(_)
|
||||
| Tag::StartBestFittingEntry,
|
||||
)
|
||||
}
|
||||
|
||||
|
@ -129,6 +133,7 @@ impl Tag {
|
|||
StartVerbatim(_) | EndVerbatim => TagKind::Verbatim,
|
||||
StartLabelled(_) | EndLabelled => TagKind::Labelled,
|
||||
StartFitsExpanded { .. } | EndFitsExpanded => TagKind::FitsExpanded,
|
||||
StartBestFittingEntry { .. } | EndBestFittingEntry => TagKind::BestFittingEntry,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -152,6 +157,7 @@ pub enum TagKind {
|
|||
Verbatim,
|
||||
Labelled,
|
||||
FitsExpanded,
|
||||
BestFittingEntry,
|
||||
}
|
||||
|
||||
#[derive(Debug, Copy, Default, Clone, Eq, PartialEq)]
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue