From c63329ba384158cc5ebdb182fc338baf26404da1 Mon Sep 17 00:00:00 2001 From: kevaundray Date: Sun, 17 Dec 2023 21:34:38 +0000 Subject: [PATCH 1/4] add method to return all file_ids --- compiler/fm/src/file_map.rs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/compiler/fm/src/file_map.rs b/compiler/fm/src/file_map.rs index 0cbdc535e40..cc68890e57b 100644 --- a/compiler/fm/src/file_map.rs +++ b/compiler/fm/src/file_map.rs @@ -75,6 +75,10 @@ impl FileMap { pub fn get_file_id(&self, file_name: &PathString) -> Option { self.name_to_id.get(file_name).cloned() } + + pub fn all_file_ids(&self) -> impl Iterator { + self.name_to_id.values() + } } impl Default for FileMap { fn default() -> Self { From aaaaeef5e24b7ade21ac5350eb79aa9fc4f0cd3f Mon Sep 17 00:00:00 2001 From: kevaundray Date: Sun, 17 Dec 2023 21:37:05 +0000 Subject: [PATCH 2/4] add method to cache ASTs --- compiler/noirc_frontend/src/hir/mod.rs | 38 ++++++++++++++++++++++++-- 1 file changed, 35 insertions(+), 3 deletions(-) diff --git a/compiler/noirc_frontend/src/hir/mod.rs b/compiler/noirc_frontend/src/hir/mod.rs index 3a435f302af..e1698a390c1 100644 --- a/compiler/noirc_frontend/src/hir/mod.rs +++ b/compiler/noirc_frontend/src/hir/mod.rs @@ -7,12 +7,14 @@ pub mod type_check; use crate::graph::{CrateGraph, CrateId}; use crate::hir_def::function::FuncMeta; use crate::node_interner::{FuncId, NodeInterner, StructId}; +use crate::parser::ParserError; +use crate::ParsedModule; use def_map::{Contract, CrateDefMap}; use fm::FileManager; use noirc_errors::Location; -use std::collections::BTreeMap; +use std::collections::{BTreeMap, HashMap}; -use self::def_map::TestFunction; +use self::def_map::{parse_file, TestFunction}; /// Helper object which groups together several useful context objects used /// during name resolution. Once name resolution is finished, only the @@ -26,6 +28,8 @@ pub struct Context { /// A map of each file that already has been visited from a prior `mod foo;` declaration. /// This is used to issue an error if a second `mod foo;` is declared to the same file. pub visited_files: BTreeMap, + + file_to_ast_cache: HashMap)>, } #[derive(Debug, Copy, Clone)] @@ -37,15 +41,43 @@ pub enum FunctionNameMatch<'a> { impl Context { pub fn new(file_manager: FileManager, crate_graph: CrateGraph) -> Context { - Context { + let mut context = Context { def_interner: NodeInterner::default(), def_maps: BTreeMap::new(), visited_files: BTreeMap::new(), crate_graph, file_manager, + file_to_ast_cache: HashMap::default(), + }; + + context.populate_cache_with_file_manager(); + + context + } + + // TODO: This method should live at a higher level + fn populate_cache_with_file_manager(&mut self) { + for file_id in self.file_manager.as_file_map().all_file_ids() { + let file_path = self.file_manager.path(*file_id); + let file_extension = + file_path.extension().expect("expected all file paths to have an extension"); + // TODO: Another reason we may not want to have this method here + // TODO: is the fact that we want to not have the compiler worry + // TODO about the nr file extension + if file_extension != "nr" { + continue; + } + self.file_to_ast_cache.insert(*file_id, parse_file(&self.file_manager, *file_id)); } } + pub fn parsed_file_results(&self, file_id: fm::FileId) -> (ParsedModule, Vec) { + // TODO: we could make it parse the file if it is not in the cache + // + // TODO: Check if we can return a reference here instead of cloning. + self.file_to_ast_cache.get(&file_id).expect("noir file not found in cache").clone() + } + /// Returns the CrateDefMap for a given CrateId. /// It is perfectly valid for the compiler to look /// up a CrateDefMap and it is not available. From fcf6d5f68de759818e5579b1ca7a2ec2356f54be Mon Sep 17 00:00:00 2001 From: kevaundray Date: Sun, 17 Dec 2023 21:37:16 +0000 Subject: [PATCH 3/4] modify rest of code --- compiler/noirc_frontend/src/hir/def_collector/dc_mod.rs | 4 ++-- compiler/noirc_frontend/src/hir/def_map/mod.rs | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/compiler/noirc_frontend/src/hir/def_collector/dc_mod.rs b/compiler/noirc_frontend/src/hir/def_collector/dc_mod.rs index 04791b11b2a..37eac1a8600 100644 --- a/compiler/noirc_frontend/src/hir/def_collector/dc_mod.rs +++ b/compiler/noirc_frontend/src/hir/def_collector/dc_mod.rs @@ -20,7 +20,7 @@ use super::{ }, errors::{DefCollectorErrorKind, DuplicateType}, }; -use crate::hir::def_map::{parse_file, LocalModuleId, ModuleData, ModuleId}; +use crate::hir::def_map::{LocalModuleId, ModuleData, ModuleId}; use crate::hir::resolution::import::ImportDirective; use crate::hir::Context; @@ -555,7 +555,7 @@ impl<'a> ModCollector<'a> { context.visited_files.insert(child_file_id, location); // Parse the AST for the module we just found and then recursively look for it's defs - let (ast, parsing_errors) = parse_file(&context.file_manager, child_file_id); + let (ast, parsing_errors) = context.parsed_file_results(child_file_id); let ast = ast.into_sorted(); errors.extend( diff --git a/compiler/noirc_frontend/src/hir/def_map/mod.rs b/compiler/noirc_frontend/src/hir/def_map/mod.rs index 026f407981d..0fc79006cf5 100644 --- a/compiler/noirc_frontend/src/hir/def_map/mod.rs +++ b/compiler/noirc_frontend/src/hir/def_map/mod.rs @@ -86,7 +86,7 @@ impl CrateDefMap { // First parse the root file. let root_file_id = context.crate_graph[crate_id].root_file_id; - let (ast, parsing_errors) = parse_file(&context.file_manager, root_file_id); + let (ast, parsing_errors) = context.parsed_file_results(root_file_id); let mut ast = ast.into_sorted(); for macro_processor in ¯o_processors { From d30c342c14698779f3491df7132f2193cdf263b8 Mon Sep 17 00:00:00 2001 From: kevaundray Date: Sun, 17 Dec 2023 22:04:36 +0000 Subject: [PATCH 4/4] add method to repopulate cache --- compiler/noirc_driver/src/lib.rs | 2 ++ compiler/noirc_frontend/src/hir/mod.rs | 16 ++++++++++++++++ 2 files changed, 18 insertions(+) diff --git a/compiler/noirc_driver/src/lib.rs b/compiler/noirc_driver/src/lib.rs index 24b159568f2..2eae7ccb7dc 100644 --- a/compiler/noirc_driver/src/lib.rs +++ b/compiler/noirc_driver/src/lib.rs @@ -92,6 +92,8 @@ pub fn prepare_crate(context: &mut Context, file_name: &Path) -> CrateId { context.file_manager.add_file_with_source_canonical_path(Path::new(&path), source); } + context.repopulate_cache(); + let path_to_std_lib_file = Path::new(STD_CRATE_NAME).join("lib.nr"); let std_file_id = context .file_manager diff --git a/compiler/noirc_frontend/src/hir/mod.rs b/compiler/noirc_frontend/src/hir/mod.rs index e1698a390c1..f50d38769c0 100644 --- a/compiler/noirc_frontend/src/hir/mod.rs +++ b/compiler/noirc_frontend/src/hir/mod.rs @@ -71,6 +71,22 @@ impl Context { } } + // TODO: Do not merge with this method! + pub fn repopulate_cache(&mut self) { + for file_id in self.file_manager.as_file_map().all_file_ids() { + let file_path = self.file_manager.path(*file_id); + let file_extension = + file_path.extension().expect("expected all file paths to have an extension"); + // TODO: Another reason we may not want to have this method here + // TODO: is the fact that we want to not have the compiler worry + // TODO about the nr file extension + if file_extension != "nr" { + continue; + } + self.file_to_ast_cache.insert(*file_id, parse_file(&self.file_manager, *file_id)); + } + } + pub fn parsed_file_results(&self, file_id: fm::FileId) -> (ParsedModule, Vec) { // TODO: we could make it parse the file if it is not in the cache //