Skip to content

Commit 64267bf

Browse files
committed
revert using FxIndexSet for unstable iteration parts
1 parent 65c04e4 commit 64267bf

File tree

13 files changed

+73
-55
lines changed

13 files changed

+73
-55
lines changed

crates/ty_project/src/db/changes.rs

Lines changed: 13 additions & 12 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 = BTreeSet::default();
55+
let mut added_paths = FxHashSet::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,19 +317,20 @@ impl ProjectDatabase {
317317
}
318318
}
319319

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);
320+
let diagnostics = if let Some(walker) =
321+
ProjectFilesWalker::incremental(self, added_paths.unstable_into_iter())
322+
{
323+
// Use directory walking to discover newly added files.
324+
let (files, diagnostics) = walker.collect_vec(self);
324325

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

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

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

crates/ty_project/src/files.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ pub(super) type IndexedIter<'a> = std::iter::Copied<std::collections::hash_set::
182182

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

crates/ty_project/src/lib.rs

Lines changed: 2 additions & 1 deletion
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::hash_set;
2425
use std::iter::FusedIterator;
2526
use std::panic::{AssertUnwindSafe, UnwindSafe};
2627
use std::sync::Arc;
@@ -621,7 +622,7 @@ impl<'a> ProjectFiles<'a> {
621622
}
622623

623624
enum ProjectFilesIter<'db> {
624-
OpenFiles(std::collections::hash_set::Iter<'db, File>),
625+
OpenFiles(hash_set::Iter<'db, File>),
625626
Indexed(files::IndexedIter<'db>),
626627
}
627628

crates/ty_python_semantic/src/hash.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@ use rustc_hash::FxBuildHasher;
22
use std::borrow::Borrow;
33
use std::hash::Hash;
44

5+
pub(crate) type Union<'a, V> = std::collections::hash_set::Union<'a, V, FxBuildHasher>;
6+
pub(crate) type Intersection<'a, V> =
7+
std::collections::hash_set::Intersection<'a, V, FxBuildHasher>;
8+
59
/// Always use this instead of [`rustc_hash::FxHashSet`].
610
/// This struct intentionally does not implement `(Into)Iterator` because the iterator's output order will be unstable if the set depends on salsa's non-deterministic IDs or execution order.
711
/// Only use `unstable_iter()`, etc. if you are sure the iterator is safe to use despite that.
@@ -99,6 +103,14 @@ impl<V: Eq + Hash> FxHashSet<V> {
99103
{
100104
self.0.contains(value)
101105
}
106+
107+
pub fn union<'a>(&'a self, other: &'a FxHashSet<V>) -> Union<'a, V> {
108+
self.0.union(&other.0)
109+
}
110+
111+
pub fn intersection<'a>(&'a self, other: &'a FxHashSet<V>) -> Intersection<'a, V> {
112+
self.0.intersection(&other.0)
113+
}
102114
}
103115

104116
impl<V: Ord> FxHashSet<V> {

crates/ty_python_semantic/src/lib.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,10 @@ mod diagnostic;
6060
#[cfg(feature = "testing")]
6161
pub mod pull_types;
6262

63-
pub type FxOrderMap<K, V> = ordermap::map::OrderMap<K, V, BuildHasherDefault<FxHasher>>;
64-
pub type FxOrderSet<V> = ordermap::set::OrderSet<V, BuildHasherDefault<FxHasher>>;
65-
pub type FxIndexMap<K, V> = indexmap::IndexMap<K, V, BuildHasherDefault<FxHasher>>;
66-
pub type FxIndexSet<V> = indexmap::IndexSet<V, BuildHasherDefault<FxHasher>>;
63+
type FxOrderMap<K, V> = ordermap::map::OrderMap<K, V, BuildHasherDefault<FxHasher>>;
64+
type FxOrderSet<V> = ordermap::set::OrderSet<V, BuildHasherDefault<FxHasher>>;
65+
type FxIndexMap<K, V> = indexmap::IndexMap<K, V, BuildHasherDefault<FxHasher>>;
66+
type FxIndexSet<V> = indexmap::IndexSet<V, BuildHasherDefault<FxHasher>>;
6767

6868
/// Returns the default registry with all known semantic lints.
6969
pub fn default_lint_registry() -> &'static LintRegistry {

crates/ty_python_semantic/src/semantic_model.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ impl<'db> SemanticModel<'db> {
163163
let builtin = module.is_known(self.db, KnownModule::Builtins);
164164

165165
let mut completions = vec![];
166-
for Member { name, ty } in all_members(self.db, ty) {
166+
for Member { name, ty } in all_members(self.db, ty).unstable_into_iter() {
167167
completions.push(Completion {
168168
name,
169169
ty: Some(ty),
@@ -197,7 +197,7 @@ impl<'db> SemanticModel<'db> {
197197
pub fn attribute_completions(&self, node: &ast::ExprAttribute) -> Vec<Completion<'db>> {
198198
let ty = node.value.inferred_type(self);
199199
all_members(self.db, ty)
200-
.into_iter()
200+
.unstable_into_iter()
201201
.map(|member| Completion {
202202
name: member.name,
203203
ty: Some(member.ty),

crates/ty_python_semantic/src/types/call/bind.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ use crate::types::{
3939
TrackedConstraintSet, TypeAliasType, TypeContext, TypeVarVariance, UnionBuilder, UnionType,
4040
WrapperDescriptorKind, enums, ide_support, todo_type,
4141
};
42-
use crate::{FxHashMap, FxIndexSet, Program};
42+
use crate::{FxHashMap, FxHashSet, Program};
4343
use ruff_db::diagnostic::{Annotation, Diagnostic, SubDiagnostic, SubDiagnosticSeverity};
4444
use ruff_python_ast::{self as ast, ArgOrKeyword, PythonVersion};
4545

@@ -887,7 +887,7 @@ impl<'db> Bindings<'db> {
887887
overload.set_return_type(Type::heterogeneous_tuple(
888888
db,
889889
ide_support::all_members(db, *ty)
890-
.into_iter()
890+
.unstable_into_iter()
891891
.sorted()
892892
.map(|member| Type::string_literal(db, &member.name)),
893893
));
@@ -1247,7 +1247,7 @@ impl<'db> Bindings<'db> {
12471247
let extract_inferable = |instance: &NominalInstanceType<'db>| {
12481248
if instance.has_known_class(db, KnownClass::NoneType) {
12491249
// Caller explicitly passed None, so no typevars are inferable.
1250-
return Some(FxIndexSet::default());
1250+
return Some(FxHashSet::default());
12511251
}
12521252
instance
12531253
.tuple_spec(db)?
@@ -1261,7 +1261,7 @@ impl<'db> Bindings<'db> {
12611261

12621262
let inferable = match overload.parameter_types() {
12631263
// Caller did not provide argument, so no typevars are inferable.
1264-
[None] => FxIndexSet::default(),
1264+
[None] => FxHashSet::default(),
12651265
[Some(Type::NominalInstance(instance))] => {
12661266
match extract_inferable(instance) {
12671267
Some(inferable) => inferable,

crates/ty_python_semantic/src/types/constraints.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ use crate::types::{
6767
BoundTypeVarIdentity, BoundTypeVarInstance, IntersectionType, Type, TypeRelation,
6868
TypeVarBoundOrConstraints, UnionType, walk_bound_type_var_type,
6969
};
70-
use crate::{Db, FxHashMap, FxHashSet, FxIndexSet, FxOrderSet};
70+
use crate::{Db, FxHashMap, FxHashSet, FxOrderSet};
7171

7272
/// An extension trait for building constraint sets from [`Option`] values.
7373
pub(crate) trait OptionConstraintsExtension<T> {
@@ -1036,8 +1036,7 @@ impl<'db> Node<'db> {
10361036
Node::Interior(_) => {}
10371037
}
10381038

1039-
// We should use `FxIndexSet` here since `BoundTypeVarInstance::{valid, required}_specializations` is query-dependent.
1040-
let mut typevars = FxIndexSet::default();
1039+
let mut typevars = FxHashSet::default();
10411040
self.for_each_constraint(db, &mut |constraint| {
10421041
typevars.insert(constraint.typevar(db));
10431042
});
@@ -1056,7 +1055,7 @@ impl<'db> Node<'db> {
10561055
.is_always_satisfied(db)
10571056
};
10581057

1059-
for typevar in typevars {
1058+
for typevar in typevars.unstable_iter() {
10601059
if typevar.is_inferable(db, inferable) {
10611060
// If the typevar is in inferable position, we need to verify that some valid
10621061
// specialization satisfies the constraint set.
@@ -1848,11 +1847,11 @@ impl<'db> InteriorNode<'db> {
18481847
// Seed the seen set with all of the constraints that are present in the input BDD, and the
18491848
// visit queue with all pairs of those constraints. (We use "combinations" because we don't
18501849
// need to compare a constraint against itself, and because ordering doesn't matter.)
1851-
let mut seen_constraints = FxIndexSet::default();
1850+
let mut seen_constraints = FxHashSet::default();
18521851
Node::Interior(self).for_each_constraint(db, &mut |constraint| {
18531852
seen_constraints.insert(constraint);
18541853
});
1855-
let mut to_visit: Vec<(_, _)> = (seen_constraints.iter().copied())
1854+
let mut to_visit: Vec<(_, _)> = (seen_constraints.unstable_iter().copied())
18561855
.tuple_combinations()
18571856
.collect();
18581857

@@ -2011,7 +2010,7 @@ impl<'db> InteriorNode<'db> {
20112010
// seen set and (if we haven't already seen it) to the to-visit queue.
20122011
if seen_constraints.insert(intersection_constraint) {
20132012
to_visit.extend(
2014-
(seen_constraints.iter().copied())
2013+
(seen_constraints.unstable_iter().copied())
20152014
.filter(|seen| *seen != intersection_constraint)
20162015
.map(|seen| (seen, intersection_constraint)),
20172016
);

crates/ty_python_semantic/src/types/diagnostic.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2236,7 +2236,7 @@ impl TypeCheckDiagnostics {
22362236
pub(super) fn extend(&mut self, other: &TypeCheckDiagnostics) {
22372237
self.diagnostics.extend_from_slice(&other.diagnostics);
22382238
self.used_suppressions
2239-
.extend(other.used_suppressions.unstable_iter().copied());
2239+
.extend(other.used_suppressions.unstable_iter());
22402240
}
22412241

22422242
pub(super) fn extend_diagnostics(&mut self, diagnostics: impl IntoIterator<Item = Diagnostic>) {

crates/ty_python_semantic/src/types/function.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1463,7 +1463,9 @@ impl KnownFunction {
14631463
};
14641464
let ty_members = all_members(db, *ty);
14651465
overload.set_return_type(Type::BooleanLiteral(
1466-
ty_members.iter().any(|m| m.name == member.value(db)),
1466+
ty_members
1467+
.unstable_iter()
1468+
.any(|m| m.name == member.value(db)),
14671469
));
14681470
}
14691471

0 commit comments

Comments
 (0)