Skip to content

Commit

Permalink
Optimization: less calls to solve a module's parent
Browse files Browse the repository at this point in the history
  • Loading branch information
asterite committed Jul 12, 2024
1 parent 32ec9a9 commit 3ae3029
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 8 deletions.
8 changes: 6 additions & 2 deletions compiler/noirc_frontend/src/elaborator/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ use crate::{
},
lexer::Lexer,
macros_api::{
BlockExpression, Ident, NodeInterner, NoirFunction, NoirStruct, Pattern,
BlockExpression, Ident, NodeInterner, NoirFunction, NoirStruct, PathKind, Pattern,
SecondaryAttribute, StructId,
},
node_interner::{
Expand Down Expand Up @@ -545,7 +545,11 @@ impl<'context> Elaborator<'context> {
}

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

let path_resolver = StandardPathResolver::new(self.module_id(), parent_module_id);

let error = match path_resolver.resolve(self.def_maps, path.clone(), &mut None) {
Ok(PathResolution { module_def_id: ModuleDefId::TraitId(trait_id), error }) => {
Expand Down
8 changes: 6 additions & 2 deletions compiler/noirc_frontend/src/elaborator/scope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::hir::def_map::{LocalModuleId, ModuleId};
use crate::hir::resolution::path_resolver::{PathResolver, StandardPathResolver};
use crate::hir::resolution::resolver::SELF_TYPE_NAME;
use crate::hir::scope::{Scope as GenericScope, ScopeTree as GenericScopeTree};
use crate::macros_api::Ident;
use crate::macros_api::{Ident, PathKind};
use crate::{
hir::{
def_map::{ModuleDefId, TryFromModuleDefId},
Expand Down Expand Up @@ -48,7 +48,11 @@ impl<'context> Elaborator<'context> {
}

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

let resolver = StandardPathResolver::new(self.module_id(), parent_module_id);
let path_resolution;

if self.interner.track_references {
Expand Down
1 change: 1 addition & 0 deletions compiler/noirc_frontend/src/hir/def_collector/dc_crate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,7 @@ impl DefCollector {
root_file_id,
crate_root,
crate_id,
None,
context,
macro_processors,
));
Expand Down
10 changes: 6 additions & 4 deletions compiler/noirc_frontend/src/hir/def_collector/dc_mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ pub fn collect_defs(
file_id: FileId,
module_id: LocalModuleId,
crate_id: CrateId,
parent_module_id: Option<LocalModuleId>,
context: &mut Context,
macro_processors: &[&dyn MacroProcessor],
) -> Vec<(CompilationError, FileId)> {
Expand All @@ -74,10 +75,6 @@ pub fn collect_defs(
macro_processors,
));

let parent_module_id = context
.def_interner
.try_module_parent(&ModuleId { krate: crate_id, local_id: collector.module_id });

// Then add the imports to defCollector to resolve once all modules in the hierarchy have been resolved
for import in ast.imports {
collector.def_collector.imports.push(ImportDirective {
Expand Down Expand Up @@ -563,6 +560,7 @@ impl<'a> ModCollector<'a> {
macro_processors: &[&dyn MacroProcessor],
) -> Vec<(CompilationError, FileId)> {
let mut errors: Vec<(CompilationError, FileId)> = vec![];
let parent_module_id = Some(self.module_id);
for submodule in submodules {
match self.push_child_module(
context,
Expand All @@ -578,6 +576,7 @@ impl<'a> ModCollector<'a> {
file_id,
child.local_id,
crate_id,
parent_module_id,
context,
macro_processors,
));
Expand Down Expand Up @@ -654,6 +653,8 @@ impl<'a> ModCollector<'a> {
parsing_errors.iter().map(|e| (e.clone().into(), child_file_id)).collect::<Vec<_>>(),
);

let parent_module_id = Some(self.module_id);

// Add module into def collector and get a ModuleId
match self.push_child_module(
context,
Expand All @@ -672,6 +673,7 @@ impl<'a> ModCollector<'a> {
child_file_id,
child_mod_id.local_id,
crate_id,
parent_module_id,
context,
macro_processors,
));
Expand Down

0 comments on commit 3ae3029

Please sign in to comment.