mirror of
https://github.com/astral-sh/ruff.git
synced 2025-08-03 18:28:24 +00:00
perf(logical-lines): Various small perf improvements (#4022)
This commit is contained in:
parent
cab65b25da
commit
f3e6ddda62
15 changed files with 349 additions and 536 deletions
|
@ -24,14 +24,14 @@ struct LineIndexInner {
|
|||
impl LineIndex {
|
||||
/// Builds the [`LineIndex`] from the source text of a file.
|
||||
pub fn from_source_text(text: &str) -> Self {
|
||||
assert!(u32::try_from(text.len()).is_ok());
|
||||
|
||||
let mut line_starts: Vec<TextSize> = Vec::with_capacity(text.len() / 88);
|
||||
line_starts.push(TextSize::default());
|
||||
|
||||
let bytes = text.as_bytes();
|
||||
let mut utf8 = false;
|
||||
|
||||
assert!(u32::try_from(bytes.len()).is_ok());
|
||||
|
||||
for (i, byte) in bytes.iter().enumerate() {
|
||||
utf8 |= !byte.is_ascii();
|
||||
|
||||
|
@ -39,7 +39,9 @@ impl LineIndex {
|
|||
// Only track one line break for `\r\n`.
|
||||
b'\r' if bytes.get(i + 1) == Some(&b'\n') => continue,
|
||||
b'\n' | b'\r' => {
|
||||
line_starts.push(TextSize::try_from(i + 1).unwrap());
|
||||
// SAFETY: Assertion above guarantees `i <= u32::MAX`
|
||||
#[allow(clippy::cast_possible_truncation)]
|
||||
line_starts.push(TextSize::from(i as u32) + TextSize::from(1));
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
|
|
|
@ -167,6 +167,7 @@ pub enum TokenKind {
|
|||
}
|
||||
|
||||
impl TokenKind {
|
||||
#[inline]
|
||||
pub const fn is_whitespace_needed(&self) -> bool {
|
||||
matches!(
|
||||
self,
|
||||
|
@ -197,6 +198,7 @@ impl TokenKind {
|
|||
)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub const fn is_whitespace_optional(&self) -> bool {
|
||||
self.is_arithmetic()
|
||||
|| matches!(
|
||||
|
@ -210,6 +212,7 @@ impl TokenKind {
|
|||
)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub const fn is_unary(&self) -> bool {
|
||||
matches!(
|
||||
self,
|
||||
|
@ -221,6 +224,7 @@ impl TokenKind {
|
|||
)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub const fn is_keyword(&self) -> bool {
|
||||
matches!(
|
||||
self,
|
||||
|
@ -261,6 +265,7 @@ impl TokenKind {
|
|||
)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub const fn is_operator(&self) -> bool {
|
||||
matches!(
|
||||
self,
|
||||
|
@ -313,10 +318,12 @@ impl TokenKind {
|
|||
)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub const fn is_singleton(&self) -> bool {
|
||||
matches!(self, TokenKind::False | TokenKind::True | TokenKind::None)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub const fn is_skip_comment(&self) -> bool {
|
||||
matches!(
|
||||
self,
|
||||
|
@ -328,6 +335,7 @@ impl TokenKind {
|
|||
)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub const fn is_arithmetic(&self) -> bool {
|
||||
matches!(
|
||||
self,
|
||||
|
@ -340,6 +348,7 @@ impl TokenKind {
|
|||
)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub const fn is_soft_keyword(&self) -> bool {
|
||||
matches!(self, TokenKind::Match | TokenKind::Case)
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue