perf(rome_js_semantic): Use FX Hash function
MODIFY
Cargo.lock
Cargo.lock
@@ -1735,6 +1735,7 @@ dependencies = [
"rome_markup",
"rome_rowan",
"rust-lapper",
+ "rustc-hash",
]
[[package]]

MODIFY
crates/rome_js_semantic/Cargo.toml
crates/rome_js_semantic/Cargo.toml
@@ -11,6 +11,7 @@ repository = "https://github.com/rome/tools"
rome_rowan = { path = "../rome_rowan" }
rome_js_syntax = { path = "../rome_js_syntax" }
rust-lapper = "1.0.1"
+rustc-hash = { workspace = true }
[dev-dependencies]
rome_markup = { path = "../rome_markup" }

MODIFY
crates/rome_js_semantic/src/events.rs
crates/rome_js_semantic/src/events.rs
//! Events emitted by the [SemanticEventExtractor] which are then constructed into the Semantic Model
+use rustc_hash::FxHashMap;
use std::collections::{HashMap, VecDeque};
use rome_js_syntax::{
@@ -158,7 +159,7 @@ pub struct SemanticEventExtractor {
stash: VecDeque<SemanticEvent>,
scopes: Vec<Scope>,
next_scope_id: usize,
- bindings: HashMap<SyntaxTokenText, TextRange>,
+ bindings: FxHashMap<SyntaxTokenText, TextRange>,
}
#[derive(Debug)]
@@ -233,7 +234,7 @@ impl SemanticEventExtractor {
stash: VecDeque::new(),
scopes: vec![],
next_scope_id: 0,
- bindings: HashMap::new(),
+ bindings: FxHashMap::default(),
}
}

MODIFY
crates/rome_js_semantic/src/semantic_model.rs
crates/rome_js_semantic/src/semantic_model.rs
@@ -10,8 +10,9 @@ use rome_js_syntax::{
};
use rome_rowan::{AstNode, SyntaxTokenText};
use rust_lapper::{Interval, Lapper};
+use rustc_hash::{FxHashMap, FxHashSet};
use std::{
- collections::{BTreeSet, HashMap, HashSet, VecDeque},
+ collections::{BTreeSet, HashSet, VecDeque},
iter::FusedIterator,
sync::Arc,
};
@@ -112,7 +113,7 @@ struct SemanticModelScopeData {
// All bindings of this scope
bindings: Vec<TextRange>,
// Map pointing to the [bindings] vec of each bindings by its name
- bindings_by_name: HashMap<SyntaxTokenText, usize>,
+ bindings_by_name: FxHashMap<SyntaxTokenText, usize>,
// All read references of a scope
read_references: Vec<ScopeReference>,
// All write references of a scope
@@ -130,19 +131,19 @@ struct SemanticModelData {
scopes: Vec<SemanticModelScopeData>,
scope_by_range: rust_lapper::Lapper<usize, usize>,
// Maps the start of a node range to a scope id
- scope_hoisted_to_by_range: HashMap<TextSize, usize>,
+ scope_hoisted_to_by_range: FxHashMap<TextSize, usize>,
// Map to each by its range
- node_by_range: HashMap<TextRange, JsSyntaxNode>,
+ node_by_range: FxHashMap<TextRange, JsSyntaxNode>,
// Maps any range in the code to its declaration
- declared_at_by_range: HashMap<TextRange, TextRange>,
+ declared_at_by_range: FxHashMap<TextRange, TextRange>,
// Maps a declaration range to the range of its references
- declaration_all_references: HashMap<TextRange, Vec<(ReferenceType, TextRange)>>,
+ declaration_all_references: FxHashMap<TextRange, Vec<(ReferenceType, TextRange)>>,
// Maps a declaration range to the range of its "reads"
- declaration_all_reads: HashMap<TextRange, Vec<(ReferenceType, TextRange)>>,
+ declaration_all_reads: FxHashMap<TextRange, Vec<(ReferenceType, TextRange)>>,
// Maps a declaration range to the range of its "writes"
- declaration_all_writes: HashMap<TextRange, Vec<(ReferenceType, TextRange)>>,
+ declaration_all_writes: FxHashMap<TextRange, Vec<(ReferenceType, TextRange)>>,
// All bindings that were exported
- exported: HashSet<TextRange>,
+ exported: FxHashSet<TextRange>,
/// All references that could not be resolved
unresolved_references: Vec<(ReferenceType, TextRange)>,
/// All references that are resolved to globals
@@ -882,16 +883,16 @@ impl<T: HasClosureAstNode> ClosureExtensions for T {}
/// and stored inside the [SemanticModel].
pub struct SemanticModelBuilder {
root: JsAnyRoot,
- node_by_range: HashMap<TextRange, JsSyntaxNode>,
- globals: HashSet<String>,
+ node_by_range: FxHashMap<TextRange, JsSyntaxNode>,
+ globals: FxHashSet<String>,
scopes: Vec<SemanticModelScopeData>,
- scope_range_by_start: HashMap<TextSize, BTreeSet<Interval<usize, usize>>>,
- scope_hoisted_to_by_range: HashMap<TextSize, usize>,
- declarations_by_range: HashMap<TextRange, TextRange>,
- declaration_all_references: HashMap<TextRange, Vec<(ReferenceType, TextRange)>>,
- declaration_all_reads: HashMap<TextRange, Vec<(ReferenceType, TextRange)>>,
- declaration_all_writes: HashMap<TextRange, Vec<(ReferenceType, TextRange)>>,
- exported: HashSet<TextRange>,
+ scope_range_by_start: FxHashMap<TextSize, BTreeSet<Interval<usize, usize>>>,
+ scope_hoisted_to_by_range: FxHashMap<TextSize, usize>,
+ declarations_by_range: FxHashMap<TextRange, TextRange>,
+ declaration_all_references: FxHashMap<TextRange, Vec<(ReferenceType, TextRange)>>,
+ declaration_all_reads: FxHashMap<TextRange, Vec<(ReferenceType, TextRange)>>,
+ declaration_all_writes: FxHashMap<TextRange, Vec<(ReferenceType, TextRange)>>,
+ exported: FxHashSet<TextRange>,
unresolved_references: Vec<(ReferenceType, TextRange)>,
global_references: Vec<(ReferenceType, TextRange)>,
}
@@ -900,16 +901,16 @@ impl SemanticModelBuilder {
pub fn new(root: JsAnyRoot) -> Self {
Self {
root,
- node_by_range: HashMap::new(),
- globals: HashSet::new(),
+ node_by_range: FxHashMap::default(),
+ globals: FxHashSet::default(),
scopes: vec![],
- scope_range_by_start: HashMap::new(),
- scope_hoisted_to_by_range: HashMap::new(),
- declarations_by_range: HashMap::new(),
- declaration_all_references: HashMap::new(),
- declaration_all_reads: HashMap::new(),
- declaration_all_writes: HashMap::new(),
- exported: HashSet::new(),
+ scope_range_by_start: FxHashMap::default(),
+ scope_hoisted_to_by_range: FxHashMap::default(),
+ declarations_by_range: FxHashMap::default(),
+ declaration_all_references: FxHashMap::default(),
+ declaration_all_reads: FxHashMap::default(),
+ declaration_all_writes: FxHashMap::default(),
+ exported: FxHashSet::default(),
unresolved_references: Vec::new(),
global_references: Vec::new(),
}
@@ -942,7 +943,7 @@ impl SemanticModelBuilder {
parent: parent_scope_id,
children: vec![],
bindings: vec![],
- bindings_by_name: HashMap::new(),
+ bindings_by_name: FxHashMap::default(),
read_references: vec![],
write_references: vec![],
});

perf(rome_js_semantic): Use FX Hash function (#3565)