From dc2b30e9b6084048e441765b91ef830a836d3dfc Mon Sep 17 00:00:00 2001 From: Muhammad Mominul Huque Date: Fri, 12 Oct 2018 00:07:44 +0600 Subject: Replace HashMap, HashSet with FxHashMap and FxHashSet --- crates/ra_analysis/Cargo.toml | 1 + crates/ra_analysis/src/db/imp.rs | 9 +++++---- crates/ra_analysis/src/imp.rs | 7 ++++--- crates/ra_analysis/src/lib.rs | 5 +++-- crates/ra_analysis/src/roots.rs | 6 +++--- crates/ra_analysis/tests/tests.rs | 5 +++-- crates/ra_editor/Cargo.toml | 1 + crates/ra_editor/src/completion.rs | 8 ++++---- crates/ra_editor/src/folding_ranges.rs | 6 +++--- crates/ra_editor/src/lib.rs | 1 + crates/ra_editor/src/scope/fn_scope.rs | 14 ++++++-------- crates/ra_lsp_server/Cargo.toml | 1 + crates/ra_lsp_server/src/lib.rs | 1 + crates/ra_lsp_server/src/main_loop/handlers.rs | 4 ++-- crates/ra_lsp_server/src/main_loop/mod.rs | 12 ++++++------ crates/ra_lsp_server/src/main_loop/subscriptions.rs | 6 +++--- crates/ra_lsp_server/src/project_model.rs | 6 +++--- crates/ra_lsp_server/src/req.rs | 4 ++-- crates/ra_lsp_server/src/server_world.rs | 8 ++++---- 19 files changed, 56 insertions(+), 49 deletions(-) (limited to 'crates') diff --git a/crates/ra_analysis/Cargo.toml b/crates/ra_analysis/Cargo.toml index a30cdfc9c..a82e1761c 100644 --- a/crates/ra_analysis/Cargo.toml +++ b/crates/ra_analysis/Cargo.toml @@ -15,6 +15,7 @@ im = "12.0.0" ra_syntax = { path = "../ra_syntax" } ra_editor = { path = "../ra_editor" } salsa = { path = "../salsa" } +rustc-hash = "1.0" [dev-dependencies] test_utils = { path = "../test_utils" } diff --git a/crates/ra_analysis/src/db/imp.rs b/crates/ra_analysis/src/db/imp.rs index f26be1046..36f6cf290 100644 --- a/crates/ra_analysis/src/db/imp.rs +++ b/crates/ra_analysis/src/db/imp.rs @@ -2,9 +2,10 @@ use std::{ sync::Arc, any::Any, hash::{Hash, Hasher}, - collections::hash_map::{DefaultHasher, HashMap}, + collections::hash_map::{DefaultHasher}, iter, }; +use rustc_hash::FxHashMap; use salsa; use {FileId, imp::FileResolverImp}; use super::{State, Query, QueryCtx}; @@ -13,7 +14,7 @@ pub(super) type Data = Arc; #[derive(Debug)] pub(super) struct Db { - names: Arc>, + names: Arc>, pub(super) imp: salsa::Db, } @@ -85,7 +86,7 @@ where pub(super) struct QueryRegistry { config: Option>, - names: HashMap, + names: FxHashMap, } impl QueryRegistry { @@ -109,7 +110,7 @@ impl QueryRegistry { (Arc::new(res), fingerprint) }) ); - let mut names = HashMap::new(); + let mut names = FxHashMap::default(); names.insert(FILE_TEXT, "FILE_TEXT"); names.insert(FILE_SET, "FILE_SET"); QueryRegistry { config: Some(config), names } diff --git a/crates/ra_analysis/src/imp.rs b/crates/ra_analysis/src/imp.rs index 517867e86..47bc0032b 100644 --- a/crates/ra_analysis/src/imp.rs +++ b/crates/ra_analysis/src/imp.rs @@ -4,11 +4,12 @@ use std::{ atomic::{AtomicBool, Ordering::SeqCst}, }, fmt, - collections::{HashSet, VecDeque}, + collections::VecDeque, iter, }; use relative_path::RelativePath; +use rustc_hash::FxHashSet; use ra_editor::{self, FileSymbol, LineIndex, find_node_at_offset, LocalEdit, resolve_local_name}; use ra_syntax::{ TextUnit, TextRange, SmolStr, File, AstNode, @@ -84,7 +85,7 @@ impl AnalysisHostImpl { data.root = Arc::new(data.root.apply_changes(&mut iter::empty(), Some(resolver))); } pub fn set_crate_graph(&mut self, graph: CrateGraph) { - let mut visited = HashSet::new(); + let mut visited = FxHashSet::default(); for &file_id in graph.crate_roots.values() { if !visited.insert(file_id) { panic!("duplicate crate root: {:?}", file_id); @@ -168,7 +169,7 @@ impl AnalysisImpl { let mut res = Vec::new(); let mut work = VecDeque::new(); work.push_back(file_id); - let mut visited = HashSet::new(); + let mut visited = FxHashSet::default(); while let Some(id) = work.pop_front() { if let Some(crate_id) = crate_graph.crate_id_for_crate_root(id) { res.push(crate_id); diff --git a/crates/ra_analysis/src/lib.rs b/crates/ra_analysis/src/lib.rs index f6ceb7eb2..849fd93e4 100644 --- a/crates/ra_analysis/src/lib.rs +++ b/crates/ra_analysis/src/lib.rs @@ -11,6 +11,7 @@ extern crate relative_path; extern crate crossbeam_channel; extern crate im; extern crate salsa; +extern crate rustc_hash; mod symbol_index; mod module_map; @@ -23,13 +24,13 @@ mod descriptors; use std::{ sync::Arc, - collections::HashMap, fmt::Debug, }; use relative_path::{RelativePath, RelativePathBuf}; use ra_syntax::{File, TextRange, TextUnit, AtomEdit}; use imp::{AnalysisImpl, AnalysisHostImpl, FileResolverImp}; +use rustc_hash::FxHashMap; pub use ra_editor::{ StructureNode, LineIndex, FileSymbol, @@ -46,7 +47,7 @@ pub struct CrateId(pub u32); #[derive(Debug, Clone, Default)] pub struct CrateGraph { - pub crate_roots: HashMap, + pub crate_roots: FxHashMap, } pub trait FileResolver: Debug + Send + Sync + 'static { diff --git a/crates/ra_analysis/src/roots.rs b/crates/ra_analysis/src/roots.rs index 1835a9b25..32a8c5bd0 100644 --- a/crates/ra_analysis/src/roots.rs +++ b/crates/ra_analysis/src/roots.rs @@ -1,11 +1,11 @@ use std::{ - collections::HashMap, sync::Arc, panic, }; use once_cell::sync::OnceCell; use rayon::prelude::*; +use rustc_hash::FxHashMap; use ra_editor::LineIndex; use ra_syntax::File; @@ -118,7 +118,7 @@ impl FileData { #[derive(Debug)] pub(crate) struct ReadonlySourceRoot { symbol_index: Arc, - file_map: HashMap, + file_map: FxHashMap, module_tree: Arc, } @@ -139,7 +139,7 @@ impl ReadonlySourceRoot { let symbol_index = SymbolIndex::for_files( modules.par_iter().map(|it| (it.0, it.1.clone())) ); - let file_map: HashMap = files + let file_map: FxHashMap = files .into_iter() .map(|(id, text)| (id, FileData::new(text))) .collect(); diff --git a/crates/ra_analysis/tests/tests.rs b/crates/ra_analysis/tests/tests.rs index 334dc5e48..a886cd0ff 100644 --- a/crates/ra_analysis/tests/tests.rs +++ b/crates/ra_analysis/tests/tests.rs @@ -1,12 +1,13 @@ extern crate relative_path; extern crate ra_analysis; +extern crate rustc_hash; extern crate test_utils; use std::{ sync::Arc, - collections::HashMap, }; +use rustc_hash::FxHashMap; use relative_path::{RelativePath, RelativePathBuf}; use ra_analysis::{Analysis, AnalysisHost, FileId, FileResolver, JobHandle, CrateGraph, CrateId}; use test_utils::assert_eq_dbg; @@ -131,7 +132,7 @@ fn test_resolve_crate_root() { let crate_graph = CrateGraph { crate_roots: { - let mut m = HashMap::new(); + let mut m = FxHashMap::default(); m.insert(CrateId(1), FileId(1)); m }, diff --git a/crates/ra_editor/Cargo.toml b/crates/ra_editor/Cargo.toml index 40e3254ff..91cefc8d7 100644 --- a/crates/ra_editor/Cargo.toml +++ b/crates/ra_editor/Cargo.toml @@ -8,6 +8,7 @@ publish = false itertools = "0.7.8" superslice = "0.1.0" join_to_string = "0.1.1" +rustc-hash = "1.0" ra_syntax = { path = "../ra_syntax" } diff --git a/crates/ra_editor/src/completion.rs b/crates/ra_editor/src/completion.rs index 570d72d66..20b8484b3 100644 --- a/crates/ra_editor/src/completion.rs +++ b/crates/ra_editor/src/completion.rs @@ -1,4 +1,4 @@ -use std::collections::{HashSet, HashMap}; +use rustc_hash::{FxHashMap, FxHashSet}; use ra_syntax::{ File, TextUnit, AstNode, SyntaxNodeRef, SyntaxKind::*, @@ -96,7 +96,7 @@ fn complete_name_ref(file: &File, name_ref: ast::NameRef, acc: &mut Vec) { - let mut params = HashMap::new(); + let mut params = FxHashMap::default(); for node in ctx.ancestors() { let _ = visitor_ctx(&mut params) .visit::(process) @@ -114,7 +114,7 @@ fn param_completions(ctx: SyntaxNodeRef, acc: &mut Vec) { }) }); - fn process<'a, N: ast::FnDefOwner<'a>>(node: N, params: &mut HashMap)>) { + fn process<'a, N: ast::FnDefOwner<'a>>(node: N, params: &mut FxHashMap)>) { node.functions() .filter_map(|it| it.param_list()) .flat_map(|it| it.params()) @@ -232,7 +232,7 @@ fn complete_mod_item_snippets(acc: &mut Vec) { } fn complete_fn(name_ref: ast::NameRef, scopes: &FnScopes, acc: &mut Vec) { - let mut shadowed = HashSet::new(); + let mut shadowed = FxHashSet::default(); acc.extend( scopes.scope_chain(name_ref.syntax()) .flat_map(|scope| scopes.entries(scope).iter()) diff --git a/crates/ra_editor/src/folding_ranges.rs b/crates/ra_editor/src/folding_ranges.rs index 733512368..3aabd54ae 100644 --- a/crates/ra_editor/src/folding_ranges.rs +++ b/crates/ra_editor/src/folding_ranges.rs @@ -1,4 +1,4 @@ -use std::collections::HashSet; +use rustc_hash::FxHashSet; use ra_syntax::{ File, TextRange, SyntaxNodeRef, @@ -20,7 +20,7 @@ pub struct Fold { pub fn folding_ranges(file: &File) -> Vec { let mut res = vec![]; - let mut visited = HashSet::new(); + let mut visited = FxHashSet::default(); for node in file.syntax().descendants() { if visited.contains(&node) { @@ -56,7 +56,7 @@ pub fn folding_ranges(file: &File) -> Vec { fn contiguous_range_for<'a>( kind: SyntaxKind, node: SyntaxNodeRef<'a>, - visited: &mut HashSet>, + visited: &mut FxHashSet>, ) -> Option { visited.insert(node); diff --git a/crates/ra_editor/src/lib.rs b/crates/ra_editor/src/lib.rs index 906ee11fe..710afc65d 100644 --- a/crates/ra_editor/src/lib.rs +++ b/crates/ra_editor/src/lib.rs @@ -2,6 +2,7 @@ extern crate ra_syntax; extern crate superslice; extern crate itertools; extern crate join_to_string; +extern crate rustc_hash; #[cfg(test)] #[macro_use] extern crate test_utils as _test_utils; diff --git a/crates/ra_editor/src/scope/fn_scope.rs b/crates/ra_editor/src/scope/fn_scope.rs index 65d85279f..9a48bda02 100644 --- a/crates/ra_editor/src/scope/fn_scope.rs +++ b/crates/ra_editor/src/scope/fn_scope.rs @@ -1,7 +1,5 @@ -use std::{ - fmt, - collections::HashMap, -}; +use std::fmt; +use rustc_hash::FxHashMap; use ra_syntax::{ SyntaxNodeRef, SyntaxNode, SmolStr, AstNode, @@ -15,7 +13,7 @@ type ScopeId = usize; pub struct FnScopes { pub self_param: Option, scopes: Vec, - scope_for: HashMap, + scope_for: FxHashMap, } impl FnScopes { @@ -25,7 +23,7 @@ impl FnScopes { .and_then(|it| it.self_param()) .map(|it| it.syntax().owned()), scopes: Vec::new(), - scope_for: HashMap::new() + scope_for: FxHashMap::default() }; let root = scopes.root_scope(); scopes.add_params_bindings(root, fn_def.param_list()); @@ -242,9 +240,9 @@ struct ScopeData { } pub fn resolve_local_name<'a>(name_ref: ast::NameRef, scopes: &'a FnScopes) -> Option<&'a ScopeEntry> { - use std::collections::HashSet; + use rustc_hash::FxHashSet; - let mut shadowed = HashSet::new(); + let mut shadowed = FxHashSet::default(); let ret = scopes.scope_chain(name_ref.syntax()) .flat_map(|scope| scopes.entries(scope).iter()) .filter(|entry| shadowed.insert(entry.name())) diff --git a/crates/ra_lsp_server/Cargo.toml b/crates/ra_lsp_server/Cargo.toml index 32463e499..1fe6b2ebe 100644 --- a/crates/ra_lsp_server/Cargo.toml +++ b/crates/ra_lsp_server/Cargo.toml @@ -21,6 +21,7 @@ im = "12.0.0" cargo_metadata = "0.6.0" text_unit = { version = "0.1.2", features = ["serde"] } smol_str = { version = "0.1.5", features = ["serde"] } +rustc-hash = "1.0" ra_syntax = { path = "../ra_syntax" } ra_editor = { path = "../ra_editor" } diff --git a/crates/ra_lsp_server/src/lib.rs b/crates/ra_lsp_server/src/lib.rs index d2f76972f..60652d55e 100644 --- a/crates/ra_lsp_server/src/lib.rs +++ b/crates/ra_lsp_server/src/lib.rs @@ -16,6 +16,7 @@ extern crate walkdir; extern crate im; extern crate relative_path; extern crate cargo_metadata; +extern crate rustc_hash; extern crate gen_lsp_server; extern crate ra_editor; diff --git a/crates/ra_lsp_server/src/main_loop/handlers.rs b/crates/ra_lsp_server/src/main_loop/handlers.rs index 725036cc7..ab8be15e9 100644 --- a/crates/ra_lsp_server/src/main_loop/handlers.rs +++ b/crates/ra_lsp_server/src/main_loop/handlers.rs @@ -1,4 +1,4 @@ -use std::collections::{HashMap}; +use rustc_hash::FxHashMap; use languageserver_types::{ Diagnostic, DiagnosticSeverity, DocumentSymbol, @@ -267,7 +267,7 @@ pub fn handle_runnables( bin: "cargo".to_string(), args, env: { - let mut m = HashMap::new(); + let mut m = FxHashMap::default(); m.insert( "RUST_BACKTRACE".to_string(), "short".to_string(), diff --git a/crates/ra_lsp_server/src/main_loop/mod.rs b/crates/ra_lsp_server/src/main_loop/mod.rs index 53c6f1dff..402615e42 100644 --- a/crates/ra_lsp_server/src/main_loop/mod.rs +++ b/crates/ra_lsp_server/src/main_loop/mod.rs @@ -3,7 +3,6 @@ mod subscriptions; use std::{ path::PathBuf, - collections::{HashMap}, }; use serde::{Serialize, de::DeserializeOwned}; @@ -15,6 +14,7 @@ use gen_lsp_server::{ RawRequest, RawNotification, RawMessage, RawResponse, ErrorCode, handle_shutdown, }; +use rustc_hash::FxHashMap; use { req, @@ -50,7 +50,7 @@ pub fn main_loop( info!("server initialized, serving requests"); let mut state = ServerWorldState::new(); - let mut pending_requests = HashMap::new(); + let mut pending_requests = FxHashMap::default(); let mut subs = Subscriptions::new(); let main_res = main_loop_inner( internal_mode, @@ -95,7 +95,7 @@ fn main_loop_inner( fs_worker: Worker)>, ws_worker: Worker>, state: &mut ServerWorldState, - pending_requests: &mut HashMap, + pending_requests: &mut FxHashMap, subs: &mut Subscriptions, ) -> Result<()> { let (libdata_sender, libdata_receiver) = unbounded(); @@ -213,7 +213,7 @@ fn main_loop_inner( fn on_task( task: Task, msg_sender: &Sender, - pending_requests: &mut HashMap, + pending_requests: &mut FxHashMap, ) { match task { Task::Respond(response) => { @@ -229,7 +229,7 @@ fn on_task( fn on_request( world: &mut ServerWorldState, - pending_requests: &mut HashMap, + pending_requests: &mut FxHashMap, pool: &ThreadPool, sender: &Sender, req: RawRequest, @@ -269,7 +269,7 @@ fn on_request( fn on_notification( msg_sender: &Sender, state: &mut ServerWorldState, - pending_requests: &mut HashMap, + pending_requests: &mut FxHashMap, subs: &mut Subscriptions, not: RawNotification, ) -> Result<()> { diff --git a/crates/ra_lsp_server/src/main_loop/subscriptions.rs b/crates/ra_lsp_server/src/main_loop/subscriptions.rs index 27f92cc9a..310153382 100644 --- a/crates/ra_lsp_server/src/main_loop/subscriptions.rs +++ b/crates/ra_lsp_server/src/main_loop/subscriptions.rs @@ -1,13 +1,13 @@ -use std::collections::HashSet; +use rustc_hash::FxHashSet; use ra_analysis::FileId; pub struct Subscriptions { - subs: HashSet, + subs: FxHashSet, } impl Subscriptions { pub fn new() -> Subscriptions { - Subscriptions { subs: HashSet::new() } + Subscriptions { subs: FxHashSet::default() } } pub fn add_sub(&mut self, file_id: FileId) { self.subs.insert(file_id); diff --git a/crates/ra_lsp_server/src/project_model.rs b/crates/ra_lsp_server/src/project_model.rs index 5db34e3e5..43e4fd654 100644 --- a/crates/ra_lsp_server/src/project_model.rs +++ b/crates/ra_lsp_server/src/project_model.rs @@ -1,7 +1,7 @@ use std::{ - collections::{HashMap, HashSet}, path::{Path, PathBuf}, }; +use rustc_hash::{FxHashMap, FxHashSet}; use cargo_metadata::{metadata_run, CargoOpt}; use ra_syntax::SmolStr; @@ -80,11 +80,11 @@ impl CargoWorkspace { true, Some(CargoOpt::AllFeatures) ).map_err(|e| format_err!("cargo metadata failed: {}", e))?; - let mut pkg_by_id = HashMap::new(); + let mut pkg_by_id = FxHashMap::default(); let mut packages = Vec::new(); let mut targets = Vec::new(); - let ws_members: HashSet = meta.workspace_members + let ws_members: FxHashSet = meta.workspace_members .into_iter() .map(|it| it.raw) .collect(); diff --git a/crates/ra_lsp_server/src/req.rs b/crates/ra_lsp_server/src/req.rs index 458c79ea9..f80957589 100644 --- a/crates/ra_lsp_server/src/req.rs +++ b/crates/ra_lsp_server/src/req.rs @@ -1,4 +1,4 @@ -use std::collections::HashMap; +use rustc_hash::FxHashMap; use languageserver_types::{TextDocumentIdentifier, Range, Url, Position, Location}; use url_serde; @@ -149,7 +149,7 @@ pub struct Runnable { pub label: String, pub bin: String, pub args: Vec, - pub env: HashMap, + pub env: FxHashMap, } #[derive(Serialize, Debug)] diff --git a/crates/ra_lsp_server/src/server_world.rs b/crates/ra_lsp_server/src/server_world.rs index 865f7c491..c4cdf83d4 100644 --- a/crates/ra_lsp_server/src/server_world.rs +++ b/crates/ra_lsp_server/src/server_world.rs @@ -1,10 +1,10 @@ use std::{ fs, path::{PathBuf, Path}, - collections::HashMap, sync::Arc, }; +use rustc_hash::FxHashMap; use languageserver_types::Url; use ra_analysis::{FileId, AnalysisHost, Analysis, CrateGraph, CrateId, LibraryData, FileResolver}; @@ -20,7 +20,7 @@ pub struct ServerWorldState { pub workspaces: Arc>, pub analysis_host: AnalysisHost, pub path_map: PathMap, - pub mem_map: HashMap>, + pub mem_map: FxHashMap>, } #[derive(Clone)] @@ -36,7 +36,7 @@ impl ServerWorldState { workspaces: Arc::new(Vec::new()), analysis_host: AnalysisHost::new(), path_map: PathMap::new(), - mem_map: HashMap::new(), + mem_map: FxHashMap::default(), } } pub fn apply_fs_changes(&mut self, events: Vec) { @@ -121,7 +121,7 @@ impl ServerWorldState { Ok(file_id) } pub fn set_workspaces(&mut self, ws: Vec) { - let mut crate_roots = HashMap::new(); + let mut crate_roots = FxHashMap::default(); ws.iter() .flat_map(|ws| { ws.packages() -- cgit v1.2.3