Skip to content

Commit 47570b9

Browse files
committed
use BTreeMap for ty_project::files::Indexed
1 parent cf451d8 commit 47570b9

File tree

9 files changed

+94
-58
lines changed

9 files changed

+94
-58
lines changed

crates/ruff_benchmark/benches/ty.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
use ruff_benchmark::criterion;
33
use ruff_benchmark::real_world_projects::{InstalledProject, RealWorldProject};
44

5+
use std::collections::BTreeSet;
56
use std::fmt::Write;
67
use std::ops::Range;
78

@@ -17,7 +18,7 @@ use ruff_python_ast::PythonVersion;
1718
use ty_project::metadata::options::{EnvironmentOptions, Options};
1819
use ty_project::metadata::value::{RangedValue, RelativePathBuf};
1920
use ty_project::watch::{ChangeEvent, ChangedKind};
20-
use ty_project::{CheckMode, Db, FxHashSet, ProjectDatabase, ProjectMetadata};
21+
use ty_project::{CheckMode, Db, ProjectDatabase, ProjectMetadata};
2122

2223
struct Case {
2324
db: ProjectDatabase,
@@ -88,7 +89,7 @@ fn setup_tomllib_case() -> Case {
8889
});
8990

9091
let mut db = ProjectDatabase::new(metadata, system).unwrap();
91-
let mut tomllib_files = FxHashSet::default();
92+
let mut tomllib_files = BTreeSet::default();
9293
let mut re: Option<File> = None;
9394

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

240241
db.set_check_mode(CheckMode::OpenFiles);
241242
db.project()
242-
.set_open_files(&mut db, FxHashSet::from_iter([file]));
243+
.set_open_files(&mut db, BTreeSet::from_iter([file]));
243244

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

crates/ty_project/src/db/changes.rs

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ impl ProjectDatabase {
5252
custom_stdlib_changed: false,
5353
};
5454
// Paths that were added
55-
let mut added_paths = FxHashSet::default();
55+
let mut added_paths = BTreeSet::default();
5656

5757
// Deduplicate the `sync` calls. Many file watchers emit multiple events for the same path.
5858
let mut synced_files = FxHashSet::default();
@@ -317,20 +317,19 @@ impl ProjectDatabase {
317317
}
318318
}
319319

320-
let diagnostics = if let Some(walker) =
321-
ProjectFilesWalker::incremental(self, added_paths.unstable_iter())
322-
{
323-
// Use directory walking to discover newly added files.
324-
let (files, diagnostics) = walker.collect_vec(self);
320+
let diagnostics =
321+
if let Some(walker) = ProjectFilesWalker::incremental(self, added_paths.into_iter()) {
322+
// Use directory walking to discover newly added files.
323+
let (files, diagnostics) = walker.collect_vec(self);
325324

326-
for file in files {
327-
project.add_file(self, file);
328-
}
325+
for file in files {
326+
project.add_file(self, file);
327+
}
329328

330-
diagnostics
331-
} else {
332-
Vec::new()
333-
};
329+
diagnostics
330+
} else {
331+
Vec::new()
332+
};
334333

335334
// Note: We simply replace all IO related diagnostics here. This isn't ideal, because
336335
// it removes IO errors that may still be relevant. However, tracking IO errors correctly

crates/ty_project/src/files.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1+
use std::collections::BTreeSet;
12
use std::marker::PhantomData;
23
use std::ops::Deref;
34
use std::sync::Arc;
45

56
use salsa::Setter;
6-
use ty_python_semantic::FxIndexSet;
77

88
use ruff_db::files::File;
99

@@ -127,7 +127,7 @@ impl<'db> LazyFiles<'db> {
127127
/// Sets the indexed files of a package to `files`.
128128
pub(super) fn set(
129129
mut self,
130-
files: FxIndexSet<File>,
130+
files: BTreeSet<File>,
131131
diagnostics: Vec<IOErrorDiagnostic>,
132132
) -> Indexed<'db> {
133133
let files = Indexed {
@@ -152,7 +152,7 @@ pub struct Indexed<'db> {
152152

153153
#[derive(Debug, get_size2::GetSize)]
154154
struct IndexedInner {
155-
files: FxIndexSet<File>,
155+
files: BTreeSet<File>,
156156
diagnostics: Vec<IOErrorDiagnostic>,
157157
}
158158

@@ -167,14 +167,14 @@ impl Indexed<'_> {
167167
}
168168

169169
impl Deref for Indexed<'_> {
170-
type Target = FxIndexSet<File>;
170+
type Target = BTreeSet<File>;
171171

172172
fn deref(&self) -> &Self::Target {
173173
&self.inner.files
174174
}
175175
}
176176

177-
pub(super) type IndexedIter<'a> = std::iter::Copied<indexmap::set::Iter<'a, File>>;
177+
pub(super) type IndexedIter<'a> = std::iter::Copied<std::collections::btree_set::Iter<'a, File>>;
178178

179179
impl<'a> IntoIterator for &'a Indexed<'_> {
180180
type Item = File;
@@ -207,7 +207,7 @@ impl IndexedMut<'_> {
207207
}
208208

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

252252
#[cfg(test)]
253253
mod tests {
254-
use ty_python_semantic::FxIndexSet;
254+
use std::collections::BTreeSet;
255255

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

275275
let files = match project.file_set(&db).get() {
276-
Index::Lazy(lazy) => lazy.set(FxIndexSet::from_iter([file]), Vec::new()),
276+
Index::Lazy(lazy) => lazy.set(BTreeSet::from_iter([file]), Vec::new()),
277277
Index::Indexed(files) => files,
278278
};
279279

crates/ty_project/src/lib.rs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ use ruff_db::system::{SystemPath, SystemPathBuf};
2121
use salsa::Durability;
2222
use salsa::Setter;
2323
use std::backtrace::BacktraceStatus;
24+
use std::collections::BTreeSet;
2425
use std::iter::FusedIterator;
2526
use std::panic::{AssertUnwindSafe, UnwindSafe};
2627
use std::sync::Arc;
@@ -55,7 +56,7 @@ pub struct Project {
5556
/// The files that are open in the project, [`None`] if there are no open files.
5657
#[returns(ref)]
5758
#[default]
58-
open_fileset: FxHashSet<File>,
59+
open_fileset: BTreeSet<File>,
5960

6061
/// The first-party files of this project.
6162
#[default]
@@ -398,25 +399,25 @@ impl Project {
398399
}
399400

400401
/// Returns the open files in the project or `None` if there are no open files.
401-
pub fn open_files(self, db: &dyn Db) -> &FxHashSet<File> {
402+
pub fn open_files(self, db: &dyn Db) -> &BTreeSet<File> {
402403
self.open_fileset(db)
403404
}
404405

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

410411
self.set_open_fileset(db).to(open_files);
411412
}
412413

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

417418
// Salsa will cancel any pending queries and remove its own reference to `open_files`
418419
// so that the reference counter to `open_files` now drops to 1.
419-
self.set_open_fileset(db).to(FxHashSet::default())
420+
self.set_open_fileset(db).to(BTreeSet::default())
420421
}
421422

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

587588
#[derive(Debug)]
588589
enum ProjectFiles<'a> {
589-
OpenFiles(&'a FxHashSet<File>),
590+
OpenFiles(&'a BTreeSet<File>),
590591
Indexed(files::Indexed<'a>),
591592
}
592593

@@ -619,14 +620,14 @@ impl<'a> IntoIterator for &'a ProjectFiles<'a> {
619620

620621
fn into_iter(self) -> Self::IntoIter {
621622
match self {
622-
ProjectFiles::OpenFiles(files) => ProjectFilesIter::OpenFiles(files.unstable_iter()),
623+
ProjectFiles::OpenFiles(files) => ProjectFilesIter::OpenFiles(files.iter()),
623624
ProjectFiles::Indexed(files) => ProjectFilesIter::Indexed(files.into_iter()),
624625
}
625626
}
626627
}
627628

628629
enum ProjectFilesIter<'db> {
629-
OpenFiles(std::collections::hash_set::Iter<'db, File>),
630+
OpenFiles(std::collections::btree_set::Iter<'db, File>),
630631
Indexed(files::IndexedIter<'db>),
631632
}
632633

crates/ty_project/src/walk.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ use ruff_db::files::{File, system_path_to_file};
44
use ruff_db::system::walk_directory::{ErrorKind, WalkDirectoryBuilder, WalkState};
55
use ruff_db::system::{SystemPath, SystemPathBuf};
66
use ruff_python_ast::PySourceType;
7+
use std::collections::BTreeSet;
78
use std::path::PathBuf;
89
use thiserror::Error;
9-
use ty_python_semantic::FxIndexSet;
1010

1111
/// Filter that decides which files are included in the project.
1212
///
@@ -285,7 +285,7 @@ impl<'a> ProjectFilesWalker<'a> {
285285
)
286286
}
287287

288-
pub(crate) fn collect_set(self, db: &dyn Db) -> (FxIndexSet<File>, Vec<IOErrorDiagnostic>) {
288+
pub(crate) fn collect_set(self, db: &dyn Db) -> (BTreeSet<File>, Vec<IOErrorDiagnostic>) {
289289
let (files, diagnostics) = self.collect_vec(db);
290290
(files.into_iter().collect(), diagnostics)
291291
}

0 commit comments

Comments
 (0)