Skip to content

Commit ca8093e

Browse files
committed
Auto merge of #13116 - Veykril:nohash, r=Veykril
Make use of NoHash hashing for FileId and CrateId Both of these are mere integers so there is nothing to hash here. Ideally we would use this for `la_arena::Idx` too, but that doesn't work due to the orphan rule, and `la_arena` is unfortunately a public library so we can't really do much here... Unless we remove the trait restriction but I'd like not to
2 parents 6eb7689 + 5b6aefe commit ca8093e

File tree

23 files changed

+539
-429
lines changed

23 files changed

+539
-429
lines changed

Cargo.lock

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/base-db/src/input.rs

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,11 @@
99
use std::{fmt, ops, panic::RefUnwindSafe, str::FromStr, sync::Arc};
1010

1111
use cfg::CfgOptions;
12-
use rustc_hash::{FxHashMap, FxHashSet};
12+
use rustc_hash::FxHashMap;
13+
use stdx::hash::{NoHashHashMap, NoHashHashSet};
1314
use syntax::SmolStr;
1415
use tt::Subtree;
15-
use vfs::{file_set::FileSet, FileId, VfsPath};
16+
use vfs::{file_set::FileSet, AnchoredPath, FileId, VfsPath};
1617

1718
/// Files are grouped into source roots. A source root is a directory on the
1819
/// file systems which is watched for changes. Typically it corresponds to a
@@ -31,22 +32,30 @@ pub struct SourceRoot {
3132
/// Libraries are considered mostly immutable, this assumption is used to
3233
/// optimize salsa's query structure
3334
pub is_library: bool,
34-
pub(crate) file_set: FileSet,
35+
file_set: FileSet,
3536
}
3637

3738
impl SourceRoot {
3839
pub fn new_local(file_set: FileSet) -> SourceRoot {
3940
SourceRoot { is_library: false, file_set }
4041
}
42+
4143
pub fn new_library(file_set: FileSet) -> SourceRoot {
4244
SourceRoot { is_library: true, file_set }
4345
}
46+
4447
pub fn path_for_file(&self, file: &FileId) -> Option<&VfsPath> {
4548
self.file_set.path_for_file(file)
4649
}
50+
4751
pub fn file_for_path(&self, path: &VfsPath) -> Option<&FileId> {
4852
self.file_set.file_for_path(path)
4953
}
54+
55+
pub fn resolve_path(&self, path: AnchoredPath<'_>) -> Option<FileId> {
56+
self.file_set.resolve_path(path)
57+
}
58+
5059
pub fn iter(&self) -> impl Iterator<Item = FileId> + '_ {
5160
self.file_set.iter()
5261
}
@@ -72,12 +81,19 @@ impl SourceRoot {
7281
/// <https://github.com/rust-lang/rust-analyzer/blob/master/docs/dev/architecture.md#serialization>
7382
#[derive(Debug, Clone, Default /* Serialize, Deserialize */)]
7483
pub struct CrateGraph {
75-
arena: FxHashMap<CrateId, CrateData>,
84+
arena: NoHashHashMap<CrateId, CrateData>,
7685
}
7786

78-
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
87+
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
7988
pub struct CrateId(pub u32);
8089

90+
impl stdx::hash::NoHashHashable for CrateId {}
91+
impl std::hash::Hash for CrateId {
92+
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
93+
self.0.hash(state);
94+
}
95+
}
96+
8197
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
8298
pub struct CrateName(SmolStr);
8399

@@ -342,7 +358,7 @@ impl CrateGraph {
342358
// Check if adding a dep from `from` to `to` creates a cycle. To figure
343359
// that out, look for a path in the *opposite* direction, from `to` to
344360
// `from`.
345-
if let Some(path) = self.find_path(&mut FxHashSet::default(), dep.crate_id, from) {
361+
if let Some(path) = self.find_path(&mut NoHashHashSet::default(), dep.crate_id, from) {
346362
let path = path.into_iter().map(|it| (it, self[it].display_name.clone())).collect();
347363
let err = CyclicDependenciesError { path };
348364
assert!(err.from().0 == from && err.to().0 == dep.crate_id);
@@ -365,7 +381,7 @@ impl CrateGraph {
365381
/// including the crate itself.
366382
pub fn transitive_deps(&self, of: CrateId) -> impl Iterator<Item = CrateId> {
367383
let mut worklist = vec![of];
368-
let mut deps = FxHashSet::default();
384+
let mut deps = NoHashHashSet::default();
369385

370386
while let Some(krate) = worklist.pop() {
371387
if !deps.insert(krate) {
@@ -382,10 +398,10 @@ impl CrateGraph {
382398
/// including the crate itself.
383399
pub fn transitive_rev_deps(&self, of: CrateId) -> impl Iterator<Item = CrateId> {
384400
let mut worklist = vec![of];
385-
let mut rev_deps = FxHashSet::default();
401+
let mut rev_deps = NoHashHashSet::default();
386402
rev_deps.insert(of);
387403

388-
let mut inverted_graph = FxHashMap::<_, Vec<_>>::default();
404+
let mut inverted_graph = NoHashHashMap::<_, Vec<_>>::default();
389405
self.arena.iter().for_each(|(&krate, data)| {
390406
data.dependencies
391407
.iter()
@@ -409,7 +425,7 @@ impl CrateGraph {
409425
/// come before the crate itself).
410426
pub fn crates_in_topological_order(&self) -> Vec<CrateId> {
411427
let mut res = Vec::new();
412-
let mut visited = FxHashSet::default();
428+
let mut visited = NoHashHashSet::default();
413429

414430
for krate in self.arena.keys().copied() {
415431
go(self, &mut visited, &mut res, krate);
@@ -419,7 +435,7 @@ impl CrateGraph {
419435

420436
fn go(
421437
graph: &CrateGraph,
422-
visited: &mut FxHashSet<CrateId>,
438+
visited: &mut NoHashHashSet<CrateId>,
423439
res: &mut Vec<CrateId>,
424440
source: CrateId,
425441
) {
@@ -459,7 +475,7 @@ impl CrateGraph {
459475

460476
fn find_path(
461477
&self,
462-
visited: &mut FxHashSet<CrateId>,
478+
visited: &mut NoHashHashSet<CrateId>,
463479
from: CrateId,
464480
to: CrateId,
465481
) -> Option<Vec<CrateId>> {

crates/base-db/src/lib.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ pub mod fixture;
88

99
use std::{panic, sync::Arc};
1010

11-
use rustc_hash::FxHashSet;
11+
use stdx::hash::NoHashHashSet;
1212
use syntax::{ast, Parse, SourceFile, TextRange, TextSize};
1313

1414
pub use crate::{
@@ -58,7 +58,7 @@ pub trait FileLoader {
5858
/// Text of the file.
5959
fn file_text(&self, file_id: FileId) -> Arc<String>;
6060
fn resolve_path(&self, path: AnchoredPath<'_>) -> Option<FileId>;
61-
fn relevant_crates(&self, file_id: FileId) -> Arc<FxHashSet<CrateId>>;
61+
fn relevant_crates(&self, file_id: FileId) -> Arc<NoHashHashSet<CrateId>>;
6262
}
6363

6464
/// Database which stores all significant input facts: source code and project
@@ -94,10 +94,10 @@ pub trait SourceDatabaseExt: SourceDatabase {
9494
#[salsa::input]
9595
fn source_root(&self, id: SourceRootId) -> Arc<SourceRoot>;
9696

97-
fn source_root_crates(&self, id: SourceRootId) -> Arc<FxHashSet<CrateId>>;
97+
fn source_root_crates(&self, id: SourceRootId) -> Arc<NoHashHashSet<CrateId>>;
9898
}
9999

100-
fn source_root_crates(db: &dyn SourceDatabaseExt, id: SourceRootId) -> Arc<FxHashSet<CrateId>> {
100+
fn source_root_crates(db: &dyn SourceDatabaseExt, id: SourceRootId) -> Arc<NoHashHashSet<CrateId>> {
101101
let graph = db.crate_graph();
102102
let res = graph
103103
.iter()
@@ -120,10 +120,10 @@ impl<T: SourceDatabaseExt> FileLoader for FileLoaderDelegate<&'_ T> {
120120
// FIXME: this *somehow* should be platform agnostic...
121121
let source_root = self.0.file_source_root(path.anchor);
122122
let source_root = self.0.source_root(source_root);
123-
source_root.file_set.resolve_path(path)
123+
source_root.resolve_path(path)
124124
}
125125

126-
fn relevant_crates(&self, file_id: FileId) -> Arc<FxHashSet<CrateId>> {
126+
fn relevant_crates(&self, file_id: FileId) -> Arc<NoHashHashSet<CrateId>> {
127127
let _p = profile::span("relevant_crates");
128128
let source_root = self.0.file_source_root(file_id);
129129
self.0.source_root_crates(source_root)

crates/hir-def/src/test_db.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use base_db::{
1010
SourceDatabase, Upcast,
1111
};
1212
use hir_expand::{db::AstDatabase, InFile};
13-
use rustc_hash::FxHashSet;
13+
use stdx::hash::NoHashHashSet;
1414
use syntax::{algo, ast, AstNode};
1515

1616
use crate::{
@@ -76,7 +76,7 @@ impl FileLoader for TestDB {
7676
fn resolve_path(&self, path: AnchoredPath<'_>) -> Option<FileId> {
7777
FileLoaderDelegate(self).resolve_path(path)
7878
}
79-
fn relevant_crates(&self, file_id: FileId) -> Arc<FxHashSet<CrateId>> {
79+
fn relevant_crates(&self, file_id: FileId) -> Arc<NoHashHashSet<CrateId>> {
8080
FileLoaderDelegate(self).relevant_crates(file_id)
8181
}
8282
}

crates/hir-ty/src/test_db.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use base_db::{
1010
};
1111
use hir_def::{db::DefDatabase, ModuleId};
1212
use hir_expand::db::AstDatabase;
13-
use rustc_hash::{FxHashMap, FxHashSet};
13+
use stdx::hash::{NoHashHashMap, NoHashHashSet};
1414
use syntax::TextRange;
1515
use test_utils::extract_annotations;
1616

@@ -80,7 +80,7 @@ impl FileLoader for TestDB {
8080
fn resolve_path(&self, path: AnchoredPath<'_>) -> Option<FileId> {
8181
FileLoaderDelegate(self).resolve_path(path)
8282
}
83-
fn relevant_crates(&self, file_id: FileId) -> Arc<FxHashSet<CrateId>> {
83+
fn relevant_crates(&self, file_id: FileId) -> Arc<NoHashHashSet<CrateId>> {
8484
FileLoaderDelegate(self).relevant_crates(file_id)
8585
}
8686
}
@@ -102,7 +102,7 @@ impl TestDB {
102102
self.module_for_file_opt(file_id).unwrap()
103103
}
104104

105-
pub(crate) fn extract_annotations(&self) -> FxHashMap<FileId, Vec<(TextRange, String)>> {
105+
pub(crate) fn extract_annotations(&self) -> NoHashHashMap<FileId, Vec<(TextRange, String)>> {
106106
let mut files = Vec::new();
107107
let crate_graph = self.crate_graph();
108108
for krate in crate_graph.iter() {

crates/ide-db/src/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ use hir::{
5252
db::{AstDatabase, DefDatabase, HirDatabase},
5353
symbols::FileSymbolKind,
5454
};
55+
use stdx::hash::NoHashHashSet;
5556

5657
use crate::{line_index::LineIndex, symbol_index::SymbolsDatabase};
5758
pub use rustc_hash::{FxHashMap, FxHashSet, FxHasher};
@@ -118,7 +119,7 @@ impl FileLoader for RootDatabase {
118119
fn resolve_path(&self, path: AnchoredPath<'_>) -> Option<FileId> {
119120
FileLoaderDelegate(self).resolve_path(path)
120121
}
121-
fn relevant_crates(&self, file_id: FileId) -> Arc<FxHashSet<CrateId>> {
122+
fn relevant_crates(&self, file_id: FileId) -> Arc<NoHashHashSet<CrateId>> {
122123
FileLoaderDelegate(self).relevant_crates(file_id)
123124
}
124125
}

crates/ide-db/src/line_index.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22
//! representation.
33
use std::{iter, mem};
44

5-
use rustc_hash::FxHashMap;
5+
use stdx::hash::NoHashHashMap;
66
use syntax::{TextRange, TextSize};
77

88
#[derive(Clone, Debug, PartialEq, Eq)]
99
pub struct LineIndex {
1010
/// Offset the the beginning of each line, zero-based
1111
pub(crate) newlines: Vec<TextSize>,
1212
/// List of non-ASCII characters on each line
13-
pub(crate) utf16_lines: FxHashMap<u32, Vec<Utf16Char>>,
13+
pub(crate) utf16_lines: NoHashHashMap<u32, Vec<Utf16Char>>,
1414
}
1515

1616
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
@@ -55,7 +55,7 @@ impl Utf16Char {
5555

5656
impl LineIndex {
5757
pub fn new(text: &str) -> LineIndex {
58-
let mut utf16_lines = FxHashMap::default();
58+
let mut utf16_lines = NoHashHashMap::default();
5959
let mut utf16_chars = Vec::new();
6060

6161
let mut newlines = vec![0.into()];

crates/ide-db/src/search.rs

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use std::{mem, sync::Arc};
99
use base_db::{FileId, FileRange, SourceDatabase, SourceDatabaseExt};
1010
use hir::{DefWithBody, HasAttrs, HasSource, InFile, ModuleSource, Semantics, Visibility};
1111
use once_cell::unsync::Lazy;
12-
use rustc_hash::FxHashMap;
12+
use stdx::hash::NoHashHashMap;
1313
use syntax::{ast, match_ast, AstNode, TextRange, TextSize};
1414

1515
use crate::{
@@ -20,7 +20,7 @@ use crate::{
2020

2121
#[derive(Debug, Default, Clone)]
2222
pub struct UsageSearchResult {
23-
pub references: FxHashMap<FileId, Vec<FileReference>>,
23+
pub references: NoHashHashMap<FileId, Vec<FileReference>>,
2424
}
2525

2626
impl UsageSearchResult {
@@ -45,7 +45,7 @@ impl UsageSearchResult {
4545

4646
impl IntoIterator for UsageSearchResult {
4747
type Item = (FileId, Vec<FileReference>);
48-
type IntoIter = <FxHashMap<FileId, Vec<FileReference>> as IntoIterator>::IntoIter;
48+
type IntoIter = <NoHashHashMap<FileId, Vec<FileReference>> as IntoIterator>::IntoIter;
4949

5050
fn into_iter(self) -> Self::IntoIter {
5151
self.references.into_iter()
@@ -78,17 +78,17 @@ pub enum ReferenceCategory {
7878
/// e.g. for things like local variables.
7979
#[derive(Clone, Debug)]
8080
pub struct SearchScope {
81-
entries: FxHashMap<FileId, Option<TextRange>>,
81+
entries: NoHashHashMap<FileId, Option<TextRange>>,
8282
}
8383

8484
impl SearchScope {
85-
fn new(entries: FxHashMap<FileId, Option<TextRange>>) -> SearchScope {
85+
fn new(entries: NoHashHashMap<FileId, Option<TextRange>>) -> SearchScope {
8686
SearchScope { entries }
8787
}
8888

8989
/// Build a search scope spanning the entire crate graph of files.
9090
fn crate_graph(db: &RootDatabase) -> SearchScope {
91-
let mut entries = FxHashMap::default();
91+
let mut entries = NoHashHashMap::default();
9292

9393
let graph = db.crate_graph();
9494
for krate in graph.iter() {
@@ -102,7 +102,7 @@ impl SearchScope {
102102

103103
/// Build a search scope spanning all the reverse dependencies of the given crate.
104104
fn reverse_dependencies(db: &RootDatabase, of: hir::Crate) -> SearchScope {
105-
let mut entries = FxHashMap::default();
105+
let mut entries = NoHashHashMap::default();
106106
for rev_dep in of.transitive_reverse_dependencies(db) {
107107
let root_file = rev_dep.root_file(db);
108108
let source_root_id = db.file_source_root(root_file);
@@ -117,14 +117,12 @@ impl SearchScope {
117117
let root_file = of.root_file(db);
118118
let source_root_id = db.file_source_root(root_file);
119119
let source_root = db.source_root(source_root_id);
120-
SearchScope {
121-
entries: source_root.iter().map(|id| (id, None)).collect::<FxHashMap<_, _>>(),
122-
}
120+
SearchScope { entries: source_root.iter().map(|id| (id, None)).collect() }
123121
}
124122

125123
/// Build a search scope spanning the given module and all its submodules.
126124
fn module_and_children(db: &RootDatabase, module: hir::Module) -> SearchScope {
127-
let mut entries = FxHashMap::default();
125+
let mut entries = NoHashHashMap::default();
128126

129127
let (file_id, range) = {
130128
let InFile { file_id, value } = module.definition_source(db);
@@ -157,7 +155,7 @@ impl SearchScope {
157155

158156
/// Build an empty search scope.
159157
pub fn empty() -> SearchScope {
160-
SearchScope::new(FxHashMap::default())
158+
SearchScope::new(NoHashHashMap::default())
161159
}
162160

163161
/// Build a empty search scope spanning the given file.

crates/ide-db/src/source_change.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,15 @@
66
use std::{collections::hash_map::Entry, iter, mem};
77

88
use base_db::{AnchoredPathBuf, FileId};
9-
use rustc_hash::FxHashMap;
10-
use stdx::never;
9+
use stdx::{hash::NoHashHashMap, never};
1110
use syntax::{algo, AstNode, SyntaxNode, SyntaxNodePtr, TextRange, TextSize};
1211
use text_edit::{TextEdit, TextEditBuilder};
1312

1413
use crate::SnippetCap;
1514

1615
#[derive(Default, Debug, Clone)]
1716
pub struct SourceChange {
18-
pub source_file_edits: FxHashMap<FileId, TextEdit>,
17+
pub source_file_edits: NoHashHashMap<FileId, TextEdit>,
1918
pub file_system_edits: Vec<FileSystemEdit>,
2019
pub is_snippet: bool,
2120
}
@@ -24,7 +23,7 @@ impl SourceChange {
2423
/// Creates a new SourceChange with the given label
2524
/// from the edits.
2625
pub fn from_edits(
27-
source_file_edits: FxHashMap<FileId, TextEdit>,
26+
source_file_edits: NoHashHashMap<FileId, TextEdit>,
2827
file_system_edits: Vec<FileSystemEdit>,
2928
) -> Self {
3029
SourceChange { source_file_edits, file_system_edits, is_snippet: false }
@@ -78,8 +77,8 @@ impl Extend<FileSystemEdit> for SourceChange {
7877
}
7978
}
8079

81-
impl From<FxHashMap<FileId, TextEdit>> for SourceChange {
82-
fn from(source_file_edits: FxHashMap<FileId, TextEdit>) -> SourceChange {
80+
impl From<NoHashHashMap<FileId, TextEdit>> for SourceChange {
81+
fn from(source_file_edits: NoHashHashMap<FileId, TextEdit>) -> SourceChange {
8382
SourceChange { source_file_edits, file_system_edits: Vec::new(), is_snippet: false }
8483
}
8584
}

0 commit comments

Comments
 (0)