Skip to content
This repository has been archived by the owner on Aug 31, 2023. It is now read-only.

perf(rome_js_semantic): Use FX Hash function #3565

Merged
merged 1 commit into from
Nov 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/rome_js_semantic/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand Down
5 changes: 3 additions & 2 deletions crates/rome_js_semantic/src/events.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//! 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::{
Expand Down Expand Up @@ -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)]
Expand Down Expand Up @@ -233,7 +234,7 @@ impl SemanticEventExtractor {
stash: VecDeque::new(),
scopes: vec![],
next_scope_id: 0,
bindings: HashMap::new(),
bindings: FxHashMap::default(),
}
}

Expand Down
59 changes: 30 additions & 29 deletions crates/rome_js_semantic/src/semantic_model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -335,7 +336,7 @@ impl Scope {
}

/// Return the [Closure] associated with this scope if
/// it has one, otherwise returns None.
/// it has one, otherwise returns None.
/// See [HasClosureAstNode] for nodes that have closure.
pub fn closure(&self) -> Option<Closure> {
Closure::from_scope(self.data.clone(), self.id, self.range())
Expand Down Expand Up @@ -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)>,
}
Expand All @@ -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(),
}
Expand Down Expand Up @@ -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![],
});
Expand Down