[ty] Reduce size of TypeInference (#19435)

This commit is contained in:
Micha Reiser 2025-07-22 11:36:36 +02:00 committed by GitHub
parent af62d0368f
commit 5e29278aa2
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 678 additions and 164 deletions

1
Cargo.lock generated
View file

@ -2851,7 +2851,6 @@ dependencies = [
"anstyle", "anstyle",
"arc-swap", "arc-swap",
"camino", "camino",
"countme",
"dashmap", "dashmap",
"dunce", "dunce",
"etcetera", "etcetera",

View file

@ -25,7 +25,6 @@ ty_static = { workspace = true }
anstyle = { workspace = true } anstyle = { workspace = true }
arc-swap = { workspace = true } arc-swap = { workspace = true }
camino = { workspace = true } camino = { workspace = true }
countme = { workspace = true }
dashmap = { workspace = true } dashmap = { workspace = true }
dunce = { workspace = true } dunce = { workspace = true }
filetime = { workspace = true } filetime = { workspace = true }
@ -59,6 +58,11 @@ tempfile = { workspace = true }
cache = ["ruff_cache"] cache = ["ruff_cache"]
junit = ["dep:quick-junit"] junit = ["dep:quick-junit"]
os = ["ignore", "dep:etcetera"] os = ["ignore", "dep:etcetera"]
serde = ["camino/serde1", "dep:serde", "dep:serde_json", "ruff_diagnostics/serde"] serde = [
"camino/serde1",
"dep:serde",
"dep:serde_json",
"ruff_diagnostics/serde",
]
# Exposes testing utilities. # Exposes testing utilities.
testing = ["tracing-subscriber"] testing = ["tracing-subscriber"]

View file

@ -1,7 +1,6 @@
use std::fmt; use std::fmt;
use std::sync::Arc; use std::sync::Arc;
use countme::Count;
use dashmap::mapref::entry::Entry; use dashmap::mapref::entry::Entry;
pub use file_root::{FileRoot, FileRootKind}; pub use file_root::{FileRoot, FileRootKind};
pub use path::FilePath; pub use path::FilePath;
@ -312,11 +311,6 @@ pub struct File {
/// the file has been deleted is to change the status to `Deleted`. /// the file has been deleted is to change the status to `Deleted`.
#[default] #[default]
status: FileStatus, status: FileStatus,
/// Counter that counts the number of created file instances and active file instances.
/// Only enabled in debug builds.
#[default]
count: Count<File>,
} }
// The Salsa heap is tracked separately. // The Salsa heap is tracked separately.

View file

@ -1,8 +1,6 @@
use std::ops::Deref; use std::ops::Deref;
use std::sync::Arc; use std::sync::Arc;
use countme::Count;
use ruff_notebook::Notebook; use ruff_notebook::Notebook;
use ruff_python_ast::PySourceType; use ruff_python_ast::PySourceType;
use ruff_source_file::LineIndex; use ruff_source_file::LineIndex;
@ -38,11 +36,7 @@ pub fn source_text(db: &dyn Db, file: File) -> SourceText {
}; };
SourceText { SourceText {
inner: Arc::new(SourceTextInner { inner: Arc::new(SourceTextInner { kind, read_error }),
kind,
read_error,
count: Count::new(),
}),
} }
} }
@ -125,8 +119,6 @@ impl std::fmt::Debug for SourceText {
#[derive(Eq, PartialEq, get_size2::GetSize)] #[derive(Eq, PartialEq, get_size2::GetSize)]
struct SourceTextInner { struct SourceTextInner {
#[get_size(ignore)]
count: Count<SourceText>,
kind: SourceTextKind, kind: SourceTextKind,
read_error: Option<SourceTextError>, read_error: Option<SourceTextError>,
} }

View file

@ -104,7 +104,10 @@ pub fn check_types(db: &dyn Db, file: File) -> Vec<Diagnostic> {
for scope_id in index.scope_ids() { for scope_id in index.scope_ids() {
let result = infer_scope_types(db, scope_id); let result = infer_scope_types(db, scope_id);
diagnostics.extend(result.diagnostics());
if let Some(scope_diagnostics) = result.diagnostics() {
diagnostics.extend(scope_diagnostics);
}
} }
diagnostics.extend_diagnostics( diagnostics.extend_diagnostics(
@ -116,7 +119,7 @@ pub fn check_types(db: &dyn Db, file: File) -> Vec<Diagnostic> {
check_suppressions(db, file, &mut diagnostics); check_suppressions(db, file, &mut diagnostics);
diagnostics.into_vec() diagnostics.into_diagnostics()
} }
/// Infer the type of a binding. /// Infer the type of a binding.

View file

@ -32,7 +32,7 @@ use crate::{
/// ## Consuming /// ## Consuming
/// It's important that the context is explicitly consumed before dropping by calling /// It's important that the context is explicitly consumed before dropping by calling
/// [`InferContext::finish`] and the returned diagnostics must be stored /// [`InferContext::finish`] and the returned diagnostics must be stored
/// on the current [`TypeInference`](super::infer::TypeInference) result. /// on the current [`TypeInferenceBuilder`](super::infer::TypeInferenceBuilder) result.
pub(crate) struct InferContext<'db, 'ast> { pub(crate) struct InferContext<'db, 'ast> {
db: &'db dyn Db, db: &'db dyn Db,
scope: ScopeId<'db>, scope: ScopeId<'db>,

View file

@ -1671,18 +1671,26 @@ impl TypeCheckDiagnostics {
self.diagnostics.shrink_to_fit(); self.diagnostics.shrink_to_fit();
} }
pub(crate) fn into_vec(self) -> Vec<Diagnostic> { pub(crate) fn into_diagnostics(self) -> Vec<Diagnostic> {
self.diagnostics self.diagnostics
} }
pub(crate) fn is_empty(&self) -> bool {
self.diagnostics.is_empty() && self.used_suppressions.is_empty()
}
pub fn iter(&self) -> std::slice::Iter<'_, Diagnostic> { pub fn iter(&self) -> std::slice::Iter<'_, Diagnostic> {
self.diagnostics.iter() self.diagnostics().iter()
}
fn diagnostics(&self) -> &[Diagnostic] {
self.diagnostics.as_slice()
} }
} }
impl std::fmt::Debug for TypeCheckDiagnostics { impl std::fmt::Debug for TypeCheckDiagnostics {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
self.diagnostics.fmt(f) self.diagnostics().fmt(f)
} }
} }
@ -1691,7 +1699,7 @@ impl IntoIterator for TypeCheckDiagnostics {
type IntoIter = std::vec::IntoIter<Diagnostic>; type IntoIter = std::vec::IntoIter<Diagnostic>;
fn into_iter(self) -> Self::IntoIter { fn into_iter(self) -> Self::IntoIter {
self.diagnostics.into_iter() self.into_diagnostics().into_iter()
} }
} }
@ -1699,8 +1707,9 @@ impl<'a> IntoIterator for &'a TypeCheckDiagnostics {
type Item = &'a Diagnostic; type Item = &'a Diagnostic;
type IntoIter = std::slice::Iter<'a, Diagnostic>; type IntoIter = std::slice::Iter<'a, Diagnostic>;
#[inline]
fn into_iter(self) -> Self::IntoIter { fn into_iter(self) -> Self::IntoIter {
self.diagnostics.iter() self.iter()
} }
} }

File diff suppressed because it is too large Load diff