Skip to content

Commit b7fc040

Browse files
authored
Merge pull request #20169 from Veykril/push-quvvsupnqqwv
Skip unnecessary `eq` work in `BodySourceMap`
2 parents da1888a + 638e49b commit b7fc040

File tree

2 files changed

+49
-11
lines changed

2 files changed

+49
-11
lines changed

crates/hir-def/src/expr_store.rs

Lines changed: 48 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ pub type TypeSource = InFile<TypePtr>;
9393
pub type LifetimePtr = AstPtr<ast::Lifetime>;
9494
pub type LifetimeSource = InFile<LifetimePtr>;
9595

96-
#[derive(Debug, Eq, PartialEq)]
96+
#[derive(Debug, PartialEq, Eq)]
9797
pub struct ExpressionStore {
9898
pub exprs: Arena<Expr>,
9999
pub pats: Arena<Pat>,
@@ -114,7 +114,7 @@ pub struct ExpressionStore {
114114
ident_hygiene: FxHashMap<ExprOrPatId, HygieneId>,
115115
}
116116

117-
#[derive(Debug, Eq, PartialEq, Default)]
117+
#[derive(Debug, Eq, Default)]
118118
pub struct ExpressionStoreSourceMap {
119119
// AST expressions can create patterns in destructuring assignments. Therefore, `ExprSource` can also map
120120
// to `PatId`, and `PatId` can also map to `ExprSource` (the other way around is unaffected).
@@ -127,19 +127,20 @@ pub struct ExpressionStoreSourceMap {
127127
label_map: FxHashMap<LabelSource, LabelId>,
128128
label_map_back: ArenaMap<LabelId, LabelSource>,
129129

130-
binding_definitions: FxHashMap<BindingId, SmallVec<[PatId; 4]>>,
131-
132-
/// We don't create explicit nodes for record fields (`S { record_field: 92 }`).
133-
/// Instead, we use id of expression (`92`) to identify the field.
134-
field_map_back: FxHashMap<ExprId, FieldSource>,
135-
pat_field_map_back: FxHashMap<PatId, PatFieldSource>,
136-
137130
types_map_back: ArenaMap<TypeRefId, TypeSource>,
138131
types_map: FxHashMap<TypeSource, TypeRefId>,
139132

140133
lifetime_map_back: ArenaMap<LifetimeRefId, LifetimeSource>,
141134
lifetime_map: FxHashMap<LifetimeSource, LifetimeRefId>,
142135

136+
binding_definitions:
137+
ArenaMap<BindingId, SmallVec<[PatId; 2 * size_of::<usize>() / size_of::<PatId>()]>>,
138+
139+
/// We don't create explicit nodes for record fields (`S { record_field: 92 }`).
140+
/// Instead, we use id of expression (`92`) to identify the field.
141+
field_map_back: FxHashMap<ExprId, FieldSource>,
142+
pat_field_map_back: FxHashMap<PatId, PatFieldSource>,
143+
143144
template_map: Option<Box<FormatTemplate>>,
144145

145146
pub expansions: FxHashMap<InFile<MacroCallPtr>, MacroCallId>,
@@ -149,6 +150,43 @@ pub struct ExpressionStoreSourceMap {
149150
pub diagnostics: Vec<ExpressionStoreDiagnostics>,
150151
}
151152

153+
impl PartialEq for ExpressionStoreSourceMap {
154+
fn eq(&self, other: &Self) -> bool {
155+
// we only need to compare one of the two mappings
156+
// as the other is a reverse mapping and thus will compare
157+
// the same as normal mapping
158+
let Self {
159+
expr_map: _,
160+
expr_map_back,
161+
pat_map: _,
162+
pat_map_back,
163+
label_map: _,
164+
label_map_back,
165+
types_map_back,
166+
types_map: _,
167+
lifetime_map_back,
168+
lifetime_map: _,
169+
// If this changed, our pattern data must have changed
170+
binding_definitions: _,
171+
// If this changed, our expression data must have changed
172+
field_map_back: _,
173+
// If this changed, our pattern data must have changed
174+
pat_field_map_back: _,
175+
template_map,
176+
expansions,
177+
diagnostics,
178+
} = self;
179+
*expr_map_back == other.expr_map_back
180+
&& *pat_map_back == other.pat_map_back
181+
&& *label_map_back == other.label_map_back
182+
&& *types_map_back == other.types_map_back
183+
&& *lifetime_map_back == other.lifetime_map_back
184+
&& *template_map == other.template_map
185+
&& *expansions == other.expansions
186+
&& *diagnostics == other.diagnostics
187+
}
188+
}
189+
152190
/// The body of an item (function, const etc.).
153191
#[derive(Debug, Eq, PartialEq, Default)]
154192
pub struct ExpressionStoreBuilder {
@@ -698,7 +736,7 @@ impl ExpressionStoreSourceMap {
698736
}
699737

700738
pub fn patterns_for_binding(&self, binding: BindingId) -> &[PatId] {
701-
self.binding_definitions.get(&binding).map_or(&[], Deref::deref)
739+
self.binding_definitions.get(binding).map_or(&[], Deref::deref)
702740
}
703741

704742
pub fn node_label(&self, node: InFile<&ast::Label>) -> Option<LabelId> {

crates/hir-def/src/expr_store/scope.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -535,7 +535,7 @@ fn foo() {
535535

536536
let resolved = scopes.resolve_name_in_scope(expr_scope, &name_ref.as_name()).unwrap();
537537
let pat_src = source_map
538-
.pat_syntax(*source_map.binding_definitions[&resolved.binding()].first().unwrap())
538+
.pat_syntax(*source_map.binding_definitions[resolved.binding()].first().unwrap())
539539
.unwrap();
540540

541541
let local_name = pat_src.value.syntax_node_ptr().to_node(file.syntax());

0 commit comments

Comments
 (0)