Remove source position from FormatElement::DynamicText (#4619)

This commit is contained in:
Micha Reiser 2023-05-24 16:36:14 +02:00 committed by GitHub
parent 85f094f592
commit 6943beee66
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
20 changed files with 165 additions and 145 deletions

View file

@ -1,17 +1,15 @@
pub mod document;
pub mod tag;
use crate::format_element::tag::{LabelId, Tag};
use std::borrow::Cow;
#[cfg(target_pointer_width = "64")]
use crate::static_assert;
use crate::{TagKind, TextSize};
use ruff_text_size::TextRange;
use std::hash::{Hash, Hasher};
use std::ops::Deref;
use std::rc::Rc;
use crate::format_element::tag::{LabelId, Tag};
use crate::TagKind;
use ruff_text_size::{TextRange, TextSize};
/// Language agnostic IR for formatting source code.
///
/// Use the helper functions like [crate::builders::space], [crate::builders::soft_line_break] etc. defined in this file to create elements.
@ -26,16 +24,19 @@ pub enum FormatElement {
/// Forces the parent group to print in expanded mode.
ExpandParent,
/// Indicates the position of the elements coming after this element in the source document.
/// The printer will create a source map entry from this position in the source document to the
/// formatted position.
SourcePosition(TextSize),
/// Token constructed by the formatter from a static string
StaticText { text: &'static str },
/// Token constructed from the input source as a dynamic
/// string with its start position in the input document.
/// string.
DynamicText {
/// There's no need for the text to be mutable, using `Box<str>` safes 8 bytes over `String`.
text: Box<str>,
/// The start position of the dynamic token in the unformatted source code
source_position: TextSize,
},
/// Token constructed by slicing a defined range from a static string.
@ -80,6 +81,9 @@ impl std::fmt::Debug for FormatElement {
fmt.debug_list().entries(interned.deref()).finish()
}
FormatElement::Tag(tag) => fmt.debug_tuple("Tag").field(tag).finish(),
FormatElement::SourcePosition(position) => {
fmt.debug_tuple("SourcePosition").field(position).finish()
}
}
}
}
@ -241,9 +245,10 @@ impl FormatElements for FormatElement {
// Traverse into the most flat version because the content is guaranteed to expand when even
// the most flat version contains some content that forces a break.
FormatElement::BestFitting(best_fitting) => best_fitting.most_flat().will_break(),
FormatElement::LineSuffixBoundary | FormatElement::Space | FormatElement::Tag(_) => {
false
}
FormatElement::LineSuffixBoundary
| FormatElement::Space
| FormatElement::Tag(_)
| FormatElement::SourcePosition(_) => false,
}
}
@ -369,20 +374,26 @@ mod tests {
}
#[cfg(target_pointer_width = "64")]
static_assert!(std::mem::size_of::<ruff_text_size::TextRange>() == 8usize);
mod sizes {
// Increasing the size of FormatElement has serious consequences on runtime performance and memory footprint.
// Is there a more efficient way to encode the data to avoid increasing its size? Can the information
// be recomputed at a later point in time?
// You reduced the size of a format element? Excellent work!
#[cfg(target_pointer_width = "64")]
static_assert!(std::mem::size_of::<crate::format_element::tag::VerbatimKind>() == 8usize);
use crate::format_element::BestFitting;
use crate::prelude::tag::VerbatimKind;
use crate::prelude::Interned;
use ruff_text_size::TextRange;
use static_assertions::assert_eq_size;
#[cfg(not(debug_assertions))]
#[cfg(target_pointer_width = "64")]
static_assert!(std::mem::size_of::<crate::format_element::Tag>() == 16usize);
assert_eq_size!(TextRange, [u8; 8]);
assert_eq_size!(VerbatimKind, [u8; 8]);
assert_eq_size!(Interned, [u8; 16]);
assert_eq_size!(BestFitting, [u8; 16]);
// Increasing the size of FormatElement has serious consequences on runtime performance and memory footprint.
// Is there a more efficient way to encode the data to avoid increasing its size? Can the information
// be recomputed at a later point in time?
// You reduced the size of a format element? Excellent work!
#[cfg(not(debug_assertions))]
assert_eq_size!(crate::format_element::Tag, [u8; 16]);
#[cfg(not(debug_assertions))]
#[cfg(target_pointer_width = "64")]
static_assert!(std::mem::size_of::<crate::FormatElement>() == 32usize);
#[cfg(not(debug_assertions))]
assert_eq_size!(crate::FormatElement, [u8; 32]);
}