Skip to content

Commit

Permalink
Use CompactString for Identifier
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaReiser committed Jun 29, 2024
1 parent f0a541e commit 1695155
Show file tree
Hide file tree
Showing 11 changed files with 102 additions and 24 deletions.
26 changes: 25 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ colored = { version = "2.1.0" }
console_error_panic_hook = { version = "0.1.7" }
console_log = { version = "1.0.0" }
countme = { version = "3.0.1" }
compact_str = "0.7.1"
criterion = { version = "0.5.1", default-features = false }
crossbeam = { version = "0.8.4" }
dashmap = { version = "5.5.3" }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,12 @@ pub(crate) fn private_member_access(checker: &mut Checker, expr: &Expr) {
return;
}

if checker.settings.flake8_self.ignore_names.contains(&attr.id) {
if checker
.settings
.flake8_self
.ignore_names
.contains(attr.id())
{
return;
}

Expand Down
4 changes: 2 additions & 2 deletions crates/ruff_linter/src/rules/pyflakes/rules/strings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -747,7 +747,7 @@ pub(crate) fn string_dot_format_extra_named_arguments(
let missing: Vec<(usize, &Name)> = keywords
.enumerate()
.filter_map(|(index, keyword)| {
if summary.keywords.contains(&keyword.id) {
if summary.keywords.contains(keyword.id()) {
None
} else {
Some((index, &keyword.id))
Expand Down Expand Up @@ -863,7 +863,7 @@ pub(crate) fn string_dot_format_missing_argument(
.iter()
.filter_map(|k| {
let Keyword { arg, .. } = &k;
arg.as_ref().map(|identifier| &identifier.id)
arg.as_ref().map(|identifier| identifier.id())
})
.collect();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ fn get_undecorated_methods(checker: &mut Checker, class_stmt: &Stmt, method_type
..
}) = stmt
{
let Some(decorator_call_statement) = explicit_decorator_calls.get(&name.id) else {
let Some(decorator_call_statement) = explicit_decorator_calls.get(name.id()) else {
continue;
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ pub(crate) fn repeated_global(checker: &mut Checker, mut suite: &[Stmt]) {
Stmt::Nonlocal(stmt) => &stmt.names,
_ => unreachable!(),
})
.map(|identifier| &identifier.id)
.map(|identifier| identifier.id())
.format(", ")
),
range,
Expand Down
4 changes: 2 additions & 2 deletions crates/ruff_python_ast/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ once_cell = { workspace = true }
rustc-hash = { workspace = true }
schemars = { workspace = true, optional = true }
serde = { workspace = true, optional = true }
smol_str = { workspace = true }
compact_str = { workspace = true }

[features]
serde = ["dep:serde", "ruff_text_size/serde", "dep:ruff_cache", "smol_str/serde", "dep:ruff_macros", "dep:schemars"]
serde = ["dep:serde", "ruff_text_size/serde", "dep:ruff_cache", "compact_str/serde", "dep:ruff_macros", "dep:schemars"]

[lints]
workspace = true
Expand Down
68 changes: 55 additions & 13 deletions crates/ruff_python_ast/src/name.rs
Original file line number Diff line number Diff line change
@@ -1,30 +1,32 @@
use crate::{nodes, Expr};
use std::borrow::Borrow;
use std::borrow::{Borrow, Cow};
use std::fmt::{Debug, Display, Formatter, Write};
use std::hash::{Hash, Hasher};
use std::ops::Deref;

#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
use crate::{nodes, Expr};

#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
#[cfg_attr(
feature = "serde",
derive(serde::Serialize, serde::Deserialize, ruff_macros::CacheKey,)
)]
pub struct Name(smol_str::SmolStr);
pub struct Name(compact_str::CompactString);

impl Name {
#[inline]
pub fn empty() -> Self {
Self(smol_str::SmolStr::new_inline(""))
Self(compact_str::CompactString::default())
}

#[inline]
pub fn new(name: impl AsRef<str>) -> Self {
Self(smol_str::SmolStr::new(name))
Self(compact_str::CompactString::new(name))
}

#[inline]
pub fn new_static(name: &'static str) -> Self {
Self(smol_str::SmolStr::new_static(name))
// TODO: Use ComactString::const_new once we upgrade to 0.8 https://github.com/ParkMyCar/compact_str/pull/336
Self(compact_str::CompactString::from(name))
}

pub fn as_str(&self) -> &str {
Expand Down Expand Up @@ -55,12 +57,52 @@ impl Borrow<str> for Name {
}
}

impl<T> From<T> for Name
where
T: Into<smol_str::SmolStr>,
{
fn from(value: T) -> Self {
Self(value.into())
impl<'a> From<&'a str> for Name {
#[inline]
fn from(s: &'a str) -> Self {
Name(s.into())
}
}

impl From<String> for Name {
#[inline]
fn from(s: String) -> Self {
Name(s.into())
}
}

impl<'a> From<&'a String> for Name {
#[inline]
fn from(s: &'a String) -> Self {
Name(s.into())
}
}

impl<'a> From<Cow<'a, str>> for Name {
#[inline]
fn from(cow: Cow<'a, str>) -> Self {
Name(cow.into())
}
}

impl From<Box<str>> for Name {
#[inline]
fn from(b: Box<str>) -> Self {
Name(b.into())
}
}

impl From<compact_str::CompactString> for Name {
#[inline]
fn from(value: compact_str::CompactString) -> Self {
Self(value)
}
}

impl From<Name> for compact_str::CompactString {
#[inline]
fn from(name: Name) -> Self {
name.0
}
}

Expand Down
4 changes: 4 additions & 0 deletions crates/ruff_python_ast/src/nodes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3777,6 +3777,10 @@ impl Identifier {
}
}

pub fn id(&self) -> &Name {
&self.id
}

pub fn is_valid(&self) -> bool {
!self.id.is_empty()
}
Expand Down
1 change: 1 addition & 0 deletions crates/ruff_python_parser/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ ruff_text_size = { workspace = true }

bitflags = { workspace = true }
bstr = { workspace = true }
compact_str = { workspace = true }
memchr = { workspace = true }
rustc-hash = { workspace = true }
static_assertions = { workspace = true }
Expand Down
7 changes: 4 additions & 3 deletions crates/ruff_python_parser/src/parser/statement.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use compact_str::CompactString;
use std::fmt::Display;

use rustc_hash::{FxBuildHasher, FxHashSet};
Expand Down Expand Up @@ -624,7 +625,7 @@ impl<'src> Parser<'src> {
let range = self.node_range(start);
return ast::Alias {
name: ast::Identifier {
id: "*".into(),
id: Name::new_static("*"),
range,
},
asname: None,
Expand Down Expand Up @@ -670,7 +671,7 @@ impl<'src> Parser<'src> {
fn parse_dotted_name(&mut self) -> ast::Identifier {
let start = self.node_start();

let mut dotted_name = self.parse_identifier().id.to_string();
let mut dotted_name: CompactString = self.parse_identifier().id.into();
let mut progress = ParserProgress::default();

while self.eat(TokenKind::Dot) {
Expand All @@ -687,7 +688,7 @@ impl<'src> Parser<'src> {
// import a.b.c
// import a . b . c
ast::Identifier {
id: Name::new(dotted_name),
id: Name::from(dotted_name),
range: self.node_range(start),
}
}
Expand Down

0 comments on commit 1695155

Please sign in to comment.