Skip to content
Open
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 2 additions & 5 deletions Cargo.lock

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

1 change: 0 additions & 1 deletion crates/ruff_benchmark/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -88,5 +88,4 @@ mimalloc = { workspace = true }
tikv-jemallocator = { workspace = true }

[dev-dependencies]
rustc-hash = { workspace = true }
rayon = { workspace = true }
7 changes: 3 additions & 4 deletions crates/ruff_benchmark/benches/ty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ use std::ops::Range;

use criterion::{BatchSize, Criterion, criterion_group, criterion_main};
use rayon::ThreadPoolBuilder;
use rustc_hash::FxHashSet;

use ruff_benchmark::TestFile;
use ruff_db::diagnostic::{Diagnostic, DiagnosticId, Severity};
Expand All @@ -18,7 +17,7 @@ use ruff_python_ast::PythonVersion;
use ty_project::metadata::options::{EnvironmentOptions, Options};
use ty_project::metadata::value::{RangedValue, RelativePathBuf};
use ty_project::watch::{ChangeEvent, ChangedKind};
use ty_project::{CheckMode, Db, ProjectDatabase, ProjectMetadata};
use ty_project::{CheckMode, Db, FxIndexSet, ProjectDatabase, ProjectMetadata};

struct Case {
db: ProjectDatabase,
Expand Down Expand Up @@ -89,7 +88,7 @@ fn setup_tomllib_case() -> Case {
});

let mut db = ProjectDatabase::new(metadata, system).unwrap();
let mut tomllib_files = FxHashSet::default();
let mut tomllib_files = FxIndexSet::default();
let mut re: Option<File> = None;

for test_file in &TOMLLIB_FILES {
Expand Down Expand Up @@ -240,7 +239,7 @@ fn setup_micro_case(code: &str) -> Case {

db.set_check_mode(CheckMode::OpenFiles);
db.project()
.set_open_files(&mut db, FxHashSet::from_iter([file]));
.set_open_files(&mut db, FxIndexSet::from_iter([file]));

let file_path = file.path(&db).as_system_path().unwrap().to_owned();

Expand Down
1 change: 0 additions & 1 deletion crates/ty_ide/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ get-size2 = { workspace = true }
itertools = { workspace = true }
rayon = { workspace = true }
regex = { workspace = true }
rustc-hash = { workspace = true }
salsa = { workspace = true, features = ["compact_str"] }
smallvec = { workspace = true }
tracing = { workspace = true }
Expand Down
4 changes: 2 additions & 2 deletions crates/ty_ide/src/importer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ The main differences here are:
3. It doesn't have as many facilities as `ruff_linter`'s importer.
*/

use rustc_hash::FxHashMap;
use ty_python_semantic::FxHashMap;

use ruff_db::files::File;
use ruff_db::parsed::ParsedModuleRef;
Expand Down Expand Up @@ -325,7 +325,7 @@ impl<'ast> MembersInScope<'ast> {
let model = SemanticModel::new(db, file);
let map = model
.members_in_scope_at(node)
.into_iter()
.stable_into_iter()
.map(|(name, memberdef)| {
let Some(def) = memberdef.definition else {
return (name, MemberInScope::other(memberdef.ty));
Expand Down
4 changes: 2 additions & 2 deletions crates/ty_ide/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@ use ruff_db::{
vendored::VendoredPath,
};
use ruff_text_size::{Ranged, TextRange};
use rustc_hash::FxHashSet;
use std::ops::{Deref, DerefMut};
use ty_project::Db;
use ty_python_semantic::FxHashSet;
use ty_python_semantic::types::{Type, TypeDefinition};

/// Information associated with a text range.
Expand Down Expand Up @@ -216,7 +216,7 @@ impl NavigationTargets {
if unique.is_empty() {
Self::empty()
} else {
let mut targets = unique.into_iter().collect::<Vec<_>>();
let mut targets = unique.unstable_into_iter().collect::<Vec<_>>();
targets.sort_by_key(|target| (target.file, target.focus_range.start()));
Self(targets.into())
}
Expand Down
2 changes: 1 addition & 1 deletion crates/ty_project/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,13 @@ colored = { workspace = true }
crossbeam = { workspace = true }
get-size2 = { workspace = true }
globset = { workspace = true }
indexmap = { workspace = true }
notify = { workspace = true }
pep440_rs = { workspace = true, features = ["version-ranges"] }
ordermap = { workspace = true, features = ["serde"] }
rayon = { workspace = true }
regex = { workspace = true }
regex-automata = { workspace = true }
rustc-hash = { workspace = true }
salsa = { workspace = true }
schemars = { workspace = true, optional = true }
serde = { workspace = true }
Expand Down
6 changes: 4 additions & 2 deletions crates/ty_project/src/db/changes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ use ruff_db::Db as _;
use ruff_db::file_revision::FileRevision;
use ruff_db::files::{File, FileRootKind, Files};
use ruff_db::system::SystemPath;
use rustc_hash::FxHashSet;
use salsa::Setter;
use ty_python_semantic::FxHashSet;
use ty_python_semantic::Program;

/// Represents the result of applying changes to the project database.
Expand Down Expand Up @@ -317,7 +317,9 @@ impl ProjectDatabase {
}
}

let diagnostics = if let Some(walker) = ProjectFilesWalker::incremental(self, added_paths) {
let diagnostics = if let Some(walker) =
ProjectFilesWalker::incremental(self, added_paths.stable_iter())
{
// Use directory walking to discover newly added files.
let (files, diagnostics) = walker.collect_vec(self);

Expand Down
18 changes: 9 additions & 9 deletions crates/ty_project/src/files.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ use std::marker::PhantomData;
use std::ops::Deref;
use std::sync::Arc;

use rustc_hash::FxHashSet;
use salsa::Setter;
use ty_python_semantic::FxIndexSet;

use ruff_db::files::File;

Expand Down Expand Up @@ -127,7 +127,7 @@ impl<'db> LazyFiles<'db> {
/// Sets the indexed files of a package to `files`.
pub(super) fn set(
mut self,
files: FxHashSet<File>,
files: FxIndexSet<File>,
diagnostics: Vec<IOErrorDiagnostic>,
) -> Indexed<'db> {
let files = Indexed {
Expand All @@ -152,7 +152,7 @@ pub struct Indexed<'db> {

#[derive(Debug, get_size2::GetSize)]
struct IndexedInner {
files: FxHashSet<File>,
files: FxIndexSet<File>,
diagnostics: Vec<IOErrorDiagnostic>,
}

Expand All @@ -167,14 +167,14 @@ impl Indexed<'_> {
}

impl Deref for Indexed<'_> {
type Target = FxHashSet<File>;
type Target = FxIndexSet<File>;

fn deref(&self) -> &Self::Target {
&self.inner.files
}
}

pub(super) type IndexedIter<'a> = std::iter::Copied<std::collections::hash_set::Iter<'a, File>>;
pub(super) type IndexedIter<'a> = std::iter::Copied<indexmap::set::Iter<'a, File>>;

impl<'a> IntoIterator for &'a Indexed<'_> {
type Item = File;
Expand All @@ -187,7 +187,7 @@ impl<'a> IntoIterator for &'a Indexed<'_> {

/// A Mutable view of a project's indexed files.
///
/// Allows in-place mutation of the files without deep cloning the hash set.
/// Allows in-place mutation of the files without deep cloning the index set.
/// The changes are written back when the mutable view is dropped or by calling [`Self::set`] manually.
pub(super) struct IndexedMut<'db> {
db: Option<&'db mut dyn Db>,
Expand All @@ -207,7 +207,7 @@ impl IndexedMut<'_> {
}

pub(super) fn remove(&mut self, file: File) -> bool {
if self.inner_mut().files.remove(&file) {
if self.inner_mut().files.swap_remove(&file) {
self.did_change = true;
true
} else {
Expand Down Expand Up @@ -251,7 +251,7 @@ impl Drop for IndexedMut<'_> {

#[cfg(test)]
mod tests {
use rustc_hash::FxHashSet;
use ty_python_semantic::FxIndexSet;

use crate::ProjectMetadata;
use crate::db::Db;
Expand All @@ -273,7 +273,7 @@ mod tests {
let file = system_path_to_file(&db, "test.py").unwrap();

let files = match project.file_set(&db).get() {
Index::Lazy(lazy) => lazy.set(FxHashSet::from_iter([file]), Vec::new()),
Index::Lazy(lazy) => lazy.set(FxIndexSet::from_iter([file]), Vec::new()),
Index::Indexed(files) => files,
};

Expand Down
19 changes: 9 additions & 10 deletions crates/ty_project/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,9 @@ use ruff_db::files::{File, FileRootKind};
use ruff_db::parsed::parsed_module;
use ruff_db::source::{SourceTextError, source_text};
use ruff_db::system::{SystemPath, SystemPathBuf};
use rustc_hash::FxHashSet;
use salsa::Durability;
use salsa::Setter;
use std::backtrace::BacktraceStatus;
use std::collections::hash_set;
use std::iter::FusedIterator;
use std::panic::{AssertUnwindSafe, UnwindSafe};
use std::sync::Arc;
Expand All @@ -31,6 +29,7 @@ use tracing::error;
use ty_python_semantic::add_inferred_python_version_hint_to_diagnostic;
use ty_python_semantic::lint::RuleSelection;
use ty_python_semantic::types::check_types;
pub use ty_python_semantic::{FxIndexMap, FxIndexSet};

mod db;
mod files;
Expand All @@ -56,7 +55,7 @@ pub struct Project {
/// The files that are open in the project, [`None`] if there are no open files.
#[returns(ref)]
#[default]
open_fileset: FxHashSet<File>,
open_fileset: FxIndexSet<File>,

/// The first-party files of this project.
#[default]
Expand Down Expand Up @@ -355,7 +354,7 @@ impl Project {
tracing::debug!("Closing file `{}`", file.path(db));

let mut open_files = self.take_open_files(db);
let removed = open_files.remove(&file);
let removed = open_files.swap_remove(&file);

if removed {
self.set_open_files(db, open_files);
Expand Down Expand Up @@ -399,25 +398,25 @@ impl Project {
}

/// Returns the open files in the project or `None` if there are no open files.
pub fn open_files(self, db: &dyn Db) -> &FxHashSet<File> {
pub fn open_files(self, db: &dyn Db) -> &FxIndexSet<File> {
self.open_fileset(db)
}

/// Sets the open files in the project.
#[tracing::instrument(level = "debug", skip(self, db))]
pub fn set_open_files(self, db: &mut dyn Db, open_files: FxHashSet<File>) {
pub fn set_open_files(self, db: &mut dyn Db, open_files: FxIndexSet<File>) {
tracing::debug!("Set open project files (count: {})", open_files.len());

self.set_open_fileset(db).to(open_files);
}

/// This takes the open files from the project and returns them.
fn take_open_files(self, db: &mut dyn Db) -> FxHashSet<File> {
fn take_open_files(self, db: &mut dyn Db) -> FxIndexSet<File> {
tracing::debug!("Take open project files");

// Salsa will cancel any pending queries and remove its own reference to `open_files`
// so that the reference counter to `open_files` now drops to 1.
self.set_open_fileset(db).to(FxHashSet::default())
self.set_open_fileset(db).to(FxIndexSet::default())
}

/// Returns `true` if the file should be checked.
Expand Down Expand Up @@ -587,7 +586,7 @@ pub(crate) fn check_file_impl(db: &dyn Db, file: File) -> Result<Box<[Diagnostic

#[derive(Debug)]
enum ProjectFiles<'a> {
OpenFiles(&'a FxHashSet<File>),
OpenFiles(&'a FxIndexSet<File>),
Indexed(files::Indexed<'a>),
}

Expand Down Expand Up @@ -627,7 +626,7 @@ impl<'a> IntoIterator for &'a ProjectFiles<'a> {
}

enum ProjectFilesIter<'db> {
OpenFiles(hash_set::Iter<'db, File>),
OpenFiles(indexmap::set::Iter<'db, File>),
Indexed(files::IndexedIter<'db>),
}

Expand Down
2 changes: 1 addition & 1 deletion crates/ty_project/src/metadata/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ use ruff_db::vendored::VendoredFileSystem;
use ruff_macros::{Combine, OptionsMetadata, RustDoc};
use ruff_options_metadata::{OptionSet, OptionsMetadata, Visit};
use ruff_python_ast::PythonVersion;
use rustc_hash::FxHasher;
use serde::{Deserialize, Serialize};
use std::borrow::Cow;
use std::fmt::{self, Debug, Display};
Expand All @@ -28,6 +27,7 @@ use std::ops::Deref;
use std::sync::Arc;
use thiserror::Error;
use ty_combine::Combine;
use ty_python_semantic::FxHasher;
use ty_python_semantic::lint::{Level, LintSource, RuleSelection};
use ty_python_semantic::{
ProgramSettings, PythonEnvironment, PythonPlatform, PythonVersionFileSource,
Expand Down
4 changes: 2 additions & 2 deletions crates/ty_project/src/walk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ use ruff_db::files::{File, system_path_to_file};
use ruff_db::system::walk_directory::{ErrorKind, WalkDirectoryBuilder, WalkState};
use ruff_db::system::{SystemPath, SystemPathBuf};
use ruff_python_ast::PySourceType;
use rustc_hash::FxHashSet;
use std::path::PathBuf;
use thiserror::Error;
use ty_python_semantic::FxIndexSet;

/// Filter that decides which files are included in the project.
///
Expand Down Expand Up @@ -285,7 +285,7 @@ impl<'a> ProjectFilesWalker<'a> {
)
}

pub(crate) fn collect_set(self, db: &dyn Db) -> (FxHashSet<File>, Vec<IOErrorDiagnostic>) {
pub(crate) fn collect_set(self, db: &dyn Db) -> (FxIndexSet<File>, Vec<IOErrorDiagnostic>) {
let (files, diagnostics) = self.collect_vec(db);
(files.into_iter().collect(), diagnostics)
}
Expand Down
1 change: 1 addition & 0 deletions crates/ty_python_semantic/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ memchr = { workspace = true }
strum = { workspace = true }
strum_macros = { workspace = true }
strsim = "0.11.1"
url = { workspace = true }

[dev-dependencies]
ruff_db = { workspace = true, features = ["testing", "os"] }
Expand Down
Loading