From 8b7f58976b32ccc768e35151b77aa9ea004b7495 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sun, 2 Jun 2019 20:15:10 +0300 Subject: don't cache parses twice Before this commit, `Parse`s for original file ended up two times in salsa's db: first, when we parse original file, and second, when we parse macro or a file. Given that parse trees are the worst ofenders in terms of memory, it makes sense to make sure we store them only once. --- crates/ra_hir/src/db.rs | 3 +++ crates/ra_hir/src/ids.rs | 53 +++++++++++++++++++++++++----------------------- crates/ra_hir/src/lib.rs | 2 +- 3 files changed, 32 insertions(+), 26 deletions(-) (limited to 'crates/ra_hir') diff --git a/crates/ra_hir/src/db.rs b/crates/ra_hir/src/db.rs index 872103219..3afd0994c 100644 --- a/crates/ra_hir/src/db.rs +++ b/crates/ra_hir/src/db.rs @@ -33,8 +33,11 @@ pub trait AstDatabase: SourceDatabase { #[salsa::transparent] #[salsa::invoke(crate::source_id::AstIdMap::file_item_query)] fn ast_id_to_node(&self, file_id: HirFileId, ast_id: ErasedFileAstId) -> TreeArc; + #[salsa::transparent] #[salsa::invoke(crate::ids::HirFileId::parse_or_expand_query)] fn parse_or_expand(&self, file_id: HirFileId) -> Option>; + #[salsa::invoke(crate::ids::HirFileId::parse_macro_query)] + fn parse_macro(&self, macro_file: ids::MacroFile) -> Option>; #[salsa::invoke(crate::ids::macro_def_query)] fn macro_def(&self, macro_id: MacroDefId) -> Option>; diff --git a/crates/ra_hir/src/ids.rs b/crates/ra_hir/src/ids.rs index 79e32e579..a95561812 100644 --- a/crates/ra_hir/src/ids.rs +++ b/crates/ra_hir/src/ids.rs @@ -62,32 +62,35 @@ impl HirFileId { file_id: HirFileId, ) -> Option> { db.check_canceled(); - let _p = profile("parse_or_expand_query"); match file_id.0 { HirFileIdRepr::File(file_id) => Some(db.parse(file_id).tree.syntax().to_owned()), - HirFileIdRepr::Macro(macro_file) => { - let macro_call_id = macro_file.macro_call_id; - let tt = db - .macro_expand(macro_call_id) - .map_err(|err| { - // Note: - // The final goal we would like to make all parse_macro success, - // such that the following log will not call anyway. - log::warn!( - "fail on macro_parse: (reason: {}) {}", - err, - macro_call_id.debug_dump(db) - ); - }) - .ok()?; - match macro_file.macro_file_kind { - MacroFileKind::Items => { - Some(mbe::token_tree_to_ast_item_list(&tt).syntax().to_owned()) - } - MacroFileKind::Expr => { - mbe::token_tree_to_expr(&tt).ok().map(|it| it.syntax().to_owned()) - } - } + HirFileIdRepr::Macro(macro_file) => db.parse_macro(macro_file), + } + } + + pub(crate) fn parse_macro_query( + db: &impl AstDatabase, + macro_file: MacroFile, + ) -> Option> { + let _p = profile("parse_macro_query"); + let macro_call_id = macro_file.macro_call_id; + let tt = db + .macro_expand(macro_call_id) + .map_err(|err| { + // Note: + // The final goal we would like to make all parse_macro success, + // such that the following log will not call anyway. + log::warn!( + "fail on macro_parse: (reason: {}) {}", + err, + macro_call_id.debug_dump(db) + ); + }) + .ok()?; + match macro_file.macro_file_kind { + MacroFileKind::Items => Some(mbe::token_tree_to_ast_item_list(&tt).syntax().to_owned()), + MacroFileKind::Expr => { + mbe::token_tree_to_expr(&tt).ok().map(|it| it.syntax().to_owned()) } } } @@ -100,7 +103,7 @@ enum HirFileIdRepr { } #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] -struct MacroFile { +pub struct MacroFile { macro_call_id: MacroCallId, macro_file_kind: MacroFileKind, } diff --git a/crates/ra_hir/src/lib.rs b/crates/ra_hir/src/lib.rs index f2a31795d..18dea5f17 100644 --- a/crates/ra_hir/src/lib.rs +++ b/crates/ra_hir/src/lib.rs @@ -60,7 +60,7 @@ pub use self::{ path::{Path, PathKind}, name::Name, source_id::{AstIdMap, ErasedFileAstId}, - ids::{HirFileId, MacroDefId, MacroCallId, MacroCallLoc}, + ids::{HirFileId, MacroDefId, MacroCallId, MacroCallLoc, MacroFile}, nameres::{PerNs, Namespace, ImportId}, ty::{Ty, ApplicationTy, TypeCtor, TraitRef, Substs, display::HirDisplay, CallableDef}, impl_block::{ImplBlock, ImplItem}, -- cgit v1.2.3