Skip to content

Commit 95d67ec

Browse files
committed
Use more of FxHash*
1 parent 82ce579 commit 95d67ec

File tree

6 files changed

+25
-15
lines changed

6 files changed

+25
-15
lines changed

crates/ra_assists/src/handlers/reorder_fields.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
use std::collections::HashMap;
1+
use itertools::Itertools;
2+
use rustc_hash::FxHashMap;
23

34
use hir::{Adt, ModuleDef, PathResolution, Semantics, Struct};
4-
use itertools::Itertools;
55
use ra_ide_db::RootDatabase;
66
use ra_syntax::{algo, ast, match_ast, AstNode, SyntaxKind, SyntaxKind::*, SyntaxNode};
77

@@ -87,7 +87,7 @@ fn struct_definition(path: &ast::Path, sema: &Semantics<RootDatabase>) -> Option
8787
}
8888
}
8989

90-
fn compute_fields_ranks(path: &ast::Path, ctx: &AssistContext) -> Option<HashMap<String, usize>> {
90+
fn compute_fields_ranks(path: &ast::Path, ctx: &AssistContext) -> Option<FxHashMap<String, usize>> {
9191
Some(
9292
struct_definition(path, &ctx.sema)?
9393
.fields(ctx.db)

crates/ra_ssr/src/parsing.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ fn parse_pattern(pattern_str: &str) -> Result<Vec<PatternElement>, SsrError> {
165165
/// Checks for errors in a rule. e.g. the replace pattern referencing placeholders that the search
166166
/// pattern didn't define.
167167
fn validate_rule(rule: &SsrRule) -> Result<(), SsrError> {
168-
let mut defined_placeholders = std::collections::HashSet::new();
168+
let mut defined_placeholders = FxHashSet::default();
169169
for p in &rule.pattern.raw.tokens {
170170
if let PatternElement::Placeholder(placeholder) = p {
171171
defined_placeholders.insert(&placeholder.ident);

crates/rust-analyzer/src/cli/analysis_stats.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
11
//! Fully type-check project and print various stats, like the number of type
22
//! errors.
33
4-
use std::{collections::HashSet, path::Path, time::Instant};
4+
use std::{path::Path, time::Instant};
5+
6+
use itertools::Itertools;
7+
use rand::{seq::SliceRandom, thread_rng};
8+
use rustc_hash::FxHashSet;
59

610
use hir::{
711
db::{AstDatabase, DefDatabase, HirDatabase},
812
original_range, AssocItem, Crate, HasSource, HirDisplay, ModuleDef,
913
};
1014
use hir_def::FunctionId;
1115
use hir_ty::{Ty, TypeWalk};
12-
use itertools::Itertools;
1316
use ra_db::SourceDatabaseExt;
1417
use ra_syntax::AstNode;
15-
use rand::{seq::SliceRandom, thread_rng};
1618
use stdx::format_to;
1719

1820
use crate::cli::{load_cargo::load_cargo, progress_report::ProgressReport, Result, Verbosity};
@@ -33,7 +35,7 @@ pub fn analysis_stats(
3335
println!("Database loaded {:?}", db_load_time.elapsed());
3436
let analysis_time = Instant::now();
3537
let mut num_crates = 0;
36-
let mut visited_modules = HashSet::new();
38+
let mut visited_modules = FxHashSet::default();
3739
let mut visit_queue = Vec::new();
3840

3941
let mut krates = Crate::all(db);

crates/rust-analyzer/src/cli/diagnostics.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
//! Analyze all modules in a project for diagnostics. Exits with a non-zero status
22
//! code if any errors are found.
33
4+
use std::path::Path;
5+
46
use anyhow::anyhow;
7+
use rustc_hash::FxHashSet;
8+
59
use hir::Crate;
610
use ra_db::SourceDatabaseExt;
711
use ra_ide::Severity;
8-
use std::{collections::HashSet, path::Path};
912

1013
use crate::cli::{load_cargo::load_cargo, Result};
1114

@@ -20,7 +23,7 @@ pub fn diagnostics(
2023
let analysis = host.analysis();
2124

2225
let mut found_error = false;
23-
let mut visited_files = HashSet::new();
26+
let mut visited_files = FxHashSet::default();
2427

2528
let mut work = Vec::new();
2629
let krates = Crate::all(db);

crates/rust-analyzer/src/diagnostics.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
//! Book keeping for keeping diagnostics easily in sync with the client.
22
pub(crate) mod to_proto;
33

4-
use std::{collections::HashMap, mem, sync::Arc};
4+
use std::{mem, sync::Arc};
55

66
use ra_ide::FileId;
7-
use rustc_hash::FxHashSet;
7+
use rustc_hash::{FxHashMap, FxHashSet};
88

99
use crate::lsp_ext;
1010

11-
pub(crate) type CheckFixes = Arc<HashMap<FileId, Vec<Fix>>>;
11+
pub(crate) type CheckFixes = Arc<FxHashMap<FileId, Vec<Fix>>>;
1212

1313
#[derive(Debug, Default, Clone)]
1414
pub struct DiagnosticsConfig {
@@ -18,8 +18,8 @@ pub struct DiagnosticsConfig {
1818

1919
#[derive(Debug, Default, Clone)]
2020
pub(crate) struct DiagnosticCollection {
21-
pub(crate) native: HashMap<FileId, Vec<lsp_types::Diagnostic>>,
22-
pub(crate) check: HashMap<FileId, Vec<lsp_types::Diagnostic>>,
21+
pub(crate) native: FxHashMap<FileId, Vec<lsp_types::Diagnostic>>,
22+
pub(crate) check: FxHashMap<FileId, Vec<lsp_types::Diagnostic>>,
2323
pub(crate) check_fixes: CheckFixes,
2424
changes: FxHashSet<FileId>,
2525
}

docs/dev/README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,11 @@ The default name is a lowercased name of the type: `global_state: GlobalState`.
254254
Avoid ad-hoc acronyms and contractions, but use the ones that exist consistently (`db`, `ctx`, `acc`).
255255
The default name for "result of the function" local variable is `res`.
256256

257+
## Collection types
258+
259+
We prefer `rustc_hash::FxHashMap` and `rustc_hash::FxHashSet` instead of the ones in `std::collections`.
260+
They use a hasher that's slightly faster and using them consistently will reduce code size by some small amount.
261+
257262
## Preconditions
258263

259264
Function preconditions should generally be expressed in types and provided by the caller (rather than checked by callee):

0 commit comments

Comments
 (0)