Skip to content

Commit 3ae3029

Browse files
committed
Optimization: less calls to solve a module's parent
1 parent 32ec9a9 commit 3ae3029

4 files changed

Lines changed: 19 additions & 8 deletions

File tree

compiler/noirc_frontend/src/elaborator/mod.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ use crate::{
2828
},
2929
lexer::Lexer,
3030
macros_api::{
31-
BlockExpression, Ident, NodeInterner, NoirFunction, NoirStruct, Pattern,
31+
BlockExpression, Ident, NodeInterner, NoirFunction, NoirStruct, PathKind, Pattern,
3232
SecondaryAttribute, StructId,
3333
},
3434
node_interner::{
@@ -545,7 +545,11 @@ impl<'context> Elaborator<'context> {
545545
}
546546

547547
fn resolve_trait_by_path(&mut self, path: Path) -> Option<TraitId> {
548-
let path_resolver = StandardPathResolver::new(self.module_id(), self.parent_module_id());
548+
// Optimization: no need to pass a parent module ID if the path is not a super path
549+
let parent_module_id =
550+
if let PathKind::Super = path.kind { self.parent_module_id() } else { None };
551+
552+
let path_resolver = StandardPathResolver::new(self.module_id(), parent_module_id);
549553

550554
let error = match path_resolver.resolve(self.def_maps, path.clone(), &mut None) {
551555
Ok(PathResolution { module_def_id: ModuleDefId::TraitId(trait_id), error }) => {

compiler/noirc_frontend/src/elaborator/scope.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use crate::hir::def_map::{LocalModuleId, ModuleId};
55
use crate::hir::resolution::path_resolver::{PathResolver, StandardPathResolver};
66
use crate::hir::resolution::resolver::SELF_TYPE_NAME;
77
use crate::hir::scope::{Scope as GenericScope, ScopeTree as GenericScopeTree};
8-
use crate::macros_api::Ident;
8+
use crate::macros_api::{Ident, PathKind};
99
use crate::{
1010
hir::{
1111
def_map::{ModuleDefId, TryFromModuleDefId},
@@ -48,7 +48,11 @@ impl<'context> Elaborator<'context> {
4848
}
4949

5050
pub(super) fn resolve_path(&mut self, path: Path) -> Result<ModuleDefId, ResolverError> {
51-
let resolver = StandardPathResolver::new(self.module_id(), self.parent_module_id());
51+
// Optimization: no need to pass a parent module ID if the path is not a super path
52+
let parent_module_id =
53+
if let PathKind::Super = path.kind { self.parent_module_id() } else { None };
54+
55+
let resolver = StandardPathResolver::new(self.module_id(), parent_module_id);
5256
let path_resolution;
5357

5458
if self.interner.track_references {

compiler/noirc_frontend/src/hir/def_collector/dc_crate.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,7 @@ impl DefCollector {
336336
root_file_id,
337337
crate_root,
338338
crate_id,
339+
None,
339340
context,
340341
macro_processors,
341342
));

compiler/noirc_frontend/src/hir/def_collector/dc_mod.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ pub fn collect_defs(
5050
file_id: FileId,
5151
module_id: LocalModuleId,
5252
crate_id: CrateId,
53+
parent_module_id: Option<LocalModuleId>,
5354
context: &mut Context,
5455
macro_processors: &[&dyn MacroProcessor],
5556
) -> Vec<(CompilationError, FileId)> {
@@ -74,10 +75,6 @@ pub fn collect_defs(
7475
macro_processors,
7576
));
7677

77-
let parent_module_id = context
78-
.def_interner
79-
.try_module_parent(&ModuleId { krate: crate_id, local_id: collector.module_id });
80-
8178
// Then add the imports to defCollector to resolve once all modules in the hierarchy have been resolved
8279
for import in ast.imports {
8380
collector.def_collector.imports.push(ImportDirective {
@@ -563,6 +560,7 @@ impl<'a> ModCollector<'a> {
563560
macro_processors: &[&dyn MacroProcessor],
564561
) -> Vec<(CompilationError, FileId)> {
565562
let mut errors: Vec<(CompilationError, FileId)> = vec![];
563+
let parent_module_id = Some(self.module_id);
566564
for submodule in submodules {
567565
match self.push_child_module(
568566
context,
@@ -578,6 +576,7 @@ impl<'a> ModCollector<'a> {
578576
file_id,
579577
child.local_id,
580578
crate_id,
579+
parent_module_id,
581580
context,
582581
macro_processors,
583582
));
@@ -654,6 +653,8 @@ impl<'a> ModCollector<'a> {
654653
parsing_errors.iter().map(|e| (e.clone().into(), child_file_id)).collect::<Vec<_>>(),
655654
);
656655

656+
let parent_module_id = Some(self.module_id);
657+
657658
// Add module into def collector and get a ModuleId
658659
match self.push_child_module(
659660
context,
@@ -672,6 +673,7 @@ impl<'a> ModCollector<'a> {
672673
child_file_id,
673674
child_mod_id.local_id,
674675
crate_id,
676+
parent_module_id,
675677
context,
676678
macro_processors,
677679
));

0 commit comments

Comments
 (0)