Skip to content

Commit 1f605d3

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

File tree

4 files changed

+74
-43
lines changed

4 files changed

+74
-43
lines changed

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/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
}

crates/ty_python_semantic/src/hash.rs

Lines changed: 52 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -68,33 +68,35 @@ impl<V: Eq + Hash> FxHashSet<V> {
6868
#[track_caller]
6969
#[allow(clippy::iter_without_into_iter)]
7070
#[deprecated(
71-
note = "FxHashSet does not guarantee stable iteration order; use FxIndexSet or unstable_iter() instead"
71+
note = "FxHashSet does not guarantee stable iteration order; use BTreeSet or unstable_iter() instead"
7272
)]
7373
pub fn iter(&self) -> std::collections::hash_set::Iter<'_, V> {
7474
panic!(
75-
"FxHashSet does not guarantee stable iteration order; use FxIndexSet or unstable_iter() instead"
75+
"FxHashSet does not guarantee stable iteration order; use BTreeSet or unstable_iter() instead"
7676
);
7777
}
7878

7979
#[track_caller]
8080
#[allow(clippy::should_implement_trait)]
8181
#[deprecated(
82-
note = "FxHashSet does not guarantee stable iteration order; use FxIndexSet or unstable_into_iter() instead"
82+
note = "FxHashSet does not guarantee stable iteration order; use BTreeSet or unstable_into_iter() instead"
8383
)]
8484
pub fn into_iter(self) -> std::collections::hash_set::IntoIter<V> {
8585
panic!(
86-
"FxHashSet does not guarantee stable iteration order; use FxIndexSet or unstable_into_iter() instead"
86+
"FxHashSet does not guarantee stable iteration order; use BTreeSet or unstable_into_iter() instead"
8787
);
8888
}
8989
}
9090

9191
impl<V: Ord> FxHashSet<V> {
92+
/// If you use this often, consider using `BTreeMap` instead of `FxHashMap`.
9293
pub fn sorted_ref_vec(&self) -> Vec<&V> {
9394
let mut vec: Vec<&V> = self.0.iter().collect();
9495
vec.sort();
9596
vec
9697
}
9798

99+
/// If you use this often, consider using `BTreeMap` instead of `FxHashMap`.
98100
pub fn into_sorted_vec(self) -> Vec<V> {
99101
let mut vec: Vec<V> = self.0.into_iter().collect();
100102
vec.sort();
@@ -199,83 +201,113 @@ impl<K: Eq + Hash, V> FxHashMap<K, V> {
199201
#[track_caller]
200202
#[allow(clippy::iter_without_into_iter)]
201203
#[deprecated(
202-
note = "FxHashMap does not guarantee stable iteration order; use FxIndexMap or unstable_iter() instead"
204+
note = "FxHashMap does not guarantee stable iteration order; use BTreeMap or unstable_iter() instead"
203205
)]
204206
pub fn iter(&self) -> std::collections::hash_map::Iter<'_, K, V> {
205207
panic!(
206-
"FxHashMap does not guarantee stable iteration order; use FxIndexMap or unstable_iter() instead"
208+
"FxHashMap does not guarantee stable iteration order; use BTreeMap or unstable_iter() instead"
207209
);
208210
}
209211

210212
#[track_caller]
211213
#[deprecated(
212-
note = "FxHashMap does not guarantee stable iteration order; use FxIndexMap or unstable_keys() instead"
214+
note = "FxHashMap does not guarantee stable iteration order; use BTreeMap or unstable_keys() instead"
213215
)]
214216
pub fn keys(&self) -> std::collections::hash_map::Keys<'_, K, V> {
215217
panic!(
216-
"FxHashMap does not guarantee stable iteration order; use FxIndexMap or unstable_keys() instead"
218+
"FxHashMap does not guarantee stable iteration order; use BTreeMap or unstable_keys() instead"
217219
);
218220
}
219221

220222
#[track_caller]
221223
#[deprecated(
222-
note = "FxHashMap does not guarantee stable iteration order; use FxIndexMap or unstable_values() instead"
224+
note = "FxHashMap does not guarantee stable iteration order; use BTreeMap or unstable_values() instead"
223225
)]
224226
pub fn values(&self) -> std::collections::hash_map::Values<'_, K, V> {
225227
panic!(
226-
"FxHashMap does not guarantee stable iteration order; use FxIndexMap or unstable_values() instead"
228+
"FxHashMap does not guarantee stable iteration order; use BTreeMap or unstable_values() instead"
227229
);
228230
}
229231

230232
#[track_caller]
231233
#[allow(clippy::iter_without_into_iter)]
232234
#[deprecated(
233-
note = "FxHashMap does not guarantee stable iteration order; use FxIndexMap or unstable_iter_mut() instead"
235+
note = "FxHashMap does not guarantee stable iteration order; use BTreeMap or unstable_iter_mut() instead"
234236
)]
235237
pub fn iter_mut(&mut self) -> std::collections::hash_map::IterMut<'_, K, V> {
236238
panic!(
237-
"FxHashMap does not guarantee stable iteration order; use FxIndexMap or unstable_iter_mut() instead"
239+
"FxHashMap does not guarantee stable iteration order; use FxIndexMap/BTreeMap or unstable_iter_mut() instead"
238240
);
239241
}
240242

241243
#[track_caller]
242244
#[deprecated(
243-
note = "FxHashMap does not guarantee stable iteration order; use FxIndexMap or unstable_values_mut() instead"
245+
note = "FxHashMap does not guarantee stable iteration order; use BTreeMap or unstable_values_mut() instead"
244246
)]
245247
pub fn values_mut(&mut self) -> std::collections::hash_map::ValuesMut<'_, K, V> {
246248
panic!(
247-
"FxHashMap does not guarantee stable iteration order; use FxIndexMap or unstable_values_mut() instead"
249+
"FxHashMap does not guarantee stable iteration order; use BTreeMap or unstable_values_mut() instead"
248250
);
249251
}
250252

251253
#[track_caller]
252254
#[allow(clippy::should_implement_trait)]
253255
#[deprecated(
254-
note = "FxHashMap does not guarantee stable iteration order; use FxIndexMap or unstable_into_iter() instead"
256+
note = "FxHashMap does not guarantee stable iteration order; use BTreeMap or unstable_into_iter() instead"
255257
)]
256258
pub fn into_iter(self) -> std::collections::hash_map::IntoIter<K, V> {
257259
panic!(
258-
"FxHashMap does not guarantee stable iteration order; use FxIndexMap or unstable_into_iter() instead"
260+
"FxHashMap does not guarantee stable iteration order; use BTreeMap or unstable_into_iter() instead"
259261
);
260262
}
261263

262264
#[track_caller]
263265
#[deprecated(
264-
note = "FxHashMap does not guarantee stable iteration order; use FxIndexMap or unstable_into_keys() instead"
266+
note = "FxHashMap does not guarantee stable iteration order; use BTreeMap or unstable_into_keys() instead"
265267
)]
266268
pub fn into_keys(self) -> std::collections::hash_map::IntoKeys<K, V> {
267269
panic!(
268-
"FxHashMap does not guarantee stable iteration order; use FxIndexMap or unstable_into_keys() instead"
270+
"FxHashMap does not guarantee stable iteration order; use BTreeMap or unstable_into_keys() instead"
269271
);
270272
}
271273

272274
#[track_caller]
273275
#[deprecated(
274-
note = "FxHashMap does not guarantee stable iteration order; use FxIndexMap or unstable_into_values() instead"
276+
note = "FxHashMap does not guarantee stable iteration order; use BTreeMap or unstable_into_values() instead"
275277
)]
276278
pub fn into_values(self) -> std::collections::hash_map::IntoValues<K, V> {
277279
panic!(
278-
"FxHashMap does not guarantee stable iteration order; use FxIndexMap or unstable_into_values() instead"
280+
"FxHashMap does not guarantee stable iteration order; use BTreeMap or unstable_into_values() instead"
279281
);
280282
}
281283
}
284+
285+
impl<K: Ord, V> FxHashMap<K, V> {
286+
/// If you use this often, consider using `BTreeMap` instead of `FxHashMap`.
287+
pub fn sorted_key_ref_vec(&self) -> Vec<&K> {
288+
let mut vec: Vec<&K> = self.0.keys().collect();
289+
vec.sort();
290+
vec
291+
}
292+
293+
/// If you use this often, consider using `BTreeMap` instead of `FxHashMap`.
294+
pub fn into_sorted_key_vec(self) -> Vec<K> {
295+
let mut vec: Vec<K> = self.0.into_keys().collect();
296+
vec.sort();
297+
vec
298+
}
299+
300+
/// If you use this often, consider using `BTreeMap` instead of `FxHashMap`.
301+
pub fn sorted_ref_vec(&self) -> Vec<(&K, &V)> {
302+
let mut vec: Vec<(&K, &V)> = self.0.iter().collect();
303+
vec.sort_by(|(k1, _), (k2, _)| k1.cmp(k2));
304+
vec
305+
}
306+
307+
/// If you use this often, consider using `BTreeMap` instead of `FxHashMap`.
308+
pub fn into_sorted_vec(self) -> Vec<(K, V)> {
309+
let mut vec: Vec<(K, V)> = self.0.into_iter().collect();
310+
vec.sort_by(|(k1, _), (k2, _)| k1.cmp(k2));
311+
vec
312+
}
313+
}

0 commit comments

Comments
 (0)