Skip to content

Commit

Permalink
Avoid cloning Name when looking up function and class types
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaReiser committed Nov 4, 2024
1 parent 012f385 commit 6a6131b
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 4 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion crates/red_knot_python_semantic/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ license = { workspace = true }
[dependencies]
ruff_db = { workspace = true }
ruff_index = { workspace = true }
ruff_python_ast = { workspace = true }
ruff_python_ast = { workspace = true, features = ["salsa"] }
ruff_python_stdlib = { workspace = true }
ruff_source_file = { workspace = true }
ruff_text_size = { workspace = true }
Expand Down
4 changes: 2 additions & 2 deletions crates/red_knot_python_semantic/src/types/infer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -766,7 +766,7 @@ impl<'db> TypeInferenceBuilder<'db> {
};
let function_ty = Type::FunctionLiteral(FunctionType::new(
self.db,
name.id.clone(),
&*name.id,
function_kind,
definition,
decorator_tys,
Expand Down Expand Up @@ -875,7 +875,7 @@ impl<'db> TypeInferenceBuilder<'db> {

let class_ty = Type::ClassLiteral(ClassType::new(
self.db,
name.id.clone(),
&*name.id,
definition,
body_scope,
maybe_known_class,
Expand Down
9 changes: 8 additions & 1 deletion crates/ruff_python_ast/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,20 @@ is-macro = { workspace = true }
itertools = { workspace = true }
memchr = { workspace = true }
rustc-hash = { workspace = true }
salsa = { workspace = true, optional = true }
schemars = { workspace = true, optional = true }
serde = { workspace = true, optional = true }

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

[lints]
workspace = true
Expand Down
15 changes: 15 additions & 0 deletions crates/ruff_python_ast/src/name.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,21 @@ impl schemars::JsonSchema for Name {
}
}

#[cfg(feature = "salsa")]
impl salsa::plumbing::interned::Lookup<Name> for &str {
fn hash<H: Hasher>(&self, h: &mut H) {
std::hash::Hash::hash(self, h);
}

fn eq(&self, data: &Name) -> bool {
self == data
}

fn into_owned(self) -> Name {
Name::new(self)
}
}

/// A representation of a qualified name, like `typing.List`.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct QualifiedName<'a>(SegmentsVec<'a>);
Expand Down

0 comments on commit 6a6131b

Please sign in to comment.