mirror of
https://github.com/rust-lang/rust-analyzer
synced 2025-01-13 13:48:50 +00:00
Merge #1369
1369: don't cache parses twice r=matklad a=matklad Co-authored-by: Aleksey Kladov <aleksey.kladov@gmail.com>
This commit is contained in:
commit
6aa8d8b99d
5 changed files with 61 additions and 33 deletions
|
@ -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<SyntaxNode>;
|
||||
#[salsa::transparent]
|
||||
#[salsa::invoke(crate::ids::HirFileId::parse_or_expand_query)]
|
||||
fn parse_or_expand(&self, file_id: HirFileId) -> Option<TreeArc<SyntaxNode>>;
|
||||
#[salsa::invoke(crate::ids::HirFileId::parse_macro_query)]
|
||||
fn parse_macro(&self, macro_file: ids::MacroFile) -> Option<TreeArc<SyntaxNode>>;
|
||||
|
||||
#[salsa::invoke(crate::ids::macro_def_query)]
|
||||
fn macro_def(&self, macro_id: MacroDefId) -> Option<Arc<mbe::MacroRules>>;
|
||||
|
|
|
@ -62,32 +62,35 @@ impl HirFileId {
|
|||
file_id: HirFileId,
|
||||
) -> Option<TreeArc<SyntaxNode>> {
|
||||
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<TreeArc<SyntaxNode>> {
|
||||
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,
|
||||
}
|
||||
|
|
|
@ -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},
|
||||
|
|
|
@ -226,7 +226,7 @@ impl RootDatabase {
|
|||
|
||||
self.query(ra_db::ParseQuery).sweep(sweep);
|
||||
|
||||
self.query(hir::db::ParseOrExpandQuery).sweep(sweep);
|
||||
self.query(hir::db::ParseMacroQuery).sweep(sweep);
|
||||
self.query(hir::db::MacroDefQuery).sweep(sweep);
|
||||
self.query(hir::db::MacroArgQuery).sweep(sweep);
|
||||
self.query(hir::db::MacroExpandQuery).sweep(sweep);
|
||||
|
|
|
@ -4,12 +4,12 @@ use std::{
|
|||
sync::Arc,
|
||||
};
|
||||
|
||||
use ra_syntax::{TreeArc, SyntaxNode};
|
||||
use ra_syntax::{TreeArc, SyntaxNode, Parse, AstNode};
|
||||
use ra_db::{
|
||||
FileTextQuery, SourceRootId,
|
||||
salsa::{Database, debug::{DebugQueryTable, TableEntry}},
|
||||
};
|
||||
use hir::HirFileId;
|
||||
use hir::MacroFile;
|
||||
|
||||
use crate::{
|
||||
FileId, db::RootDatabase,
|
||||
|
@ -17,18 +17,23 @@ use crate::{
|
|||
};
|
||||
|
||||
pub(crate) fn syntax_tree_stats(db: &RootDatabase) -> SyntaxTreeStats {
|
||||
db.query(hir::db::ParseOrExpandQuery).entries::<SyntaxTreeStats>()
|
||||
db.query(ra_db::ParseQuery).entries::<SyntaxTreeStats>()
|
||||
}
|
||||
pub(crate) fn macro_syntax_tree_stats(db: &RootDatabase) -> SyntaxTreeStats {
|
||||
db.query(hir::db::ParseMacroQuery).entries::<SyntaxTreeStats>()
|
||||
}
|
||||
|
||||
pub(crate) fn status(db: &RootDatabase) -> String {
|
||||
let files_stats = db.query(FileTextQuery).entries::<FilesStats>();
|
||||
let syntax_tree_stats = syntax_tree_stats(db);
|
||||
let macro_syntax_tree_stats = macro_syntax_tree_stats(db);
|
||||
let symbols_stats = db.query(LibrarySymbolsQuery).entries::<LibrarySymbolsStats>();
|
||||
format!(
|
||||
"{}\n{}\n{}\n\n\nmemory:\n{}\ngc {:?} seconds ago",
|
||||
"{}\n{}\n{}\n{} (macros)\n\n\nmemory:\n{}\ngc {:?} seconds ago",
|
||||
files_stats,
|
||||
symbols_stats,
|
||||
syntax_tree_stats,
|
||||
macro_syntax_tree_stats,
|
||||
MemoryStats::current(),
|
||||
db.last_gc.elapsed().as_secs(),
|
||||
)
|
||||
|
@ -73,10 +78,27 @@ impl fmt::Display for SyntaxTreeStats {
|
|||
}
|
||||
}
|
||||
|
||||
impl FromIterator<TableEntry<HirFileId, Option<TreeArc<SyntaxNode>>>> for SyntaxTreeStats {
|
||||
impl FromIterator<TableEntry<FileId, Parse>> for SyntaxTreeStats {
|
||||
fn from_iter<T>(iter: T) -> SyntaxTreeStats
|
||||
where
|
||||
T: IntoIterator<Item = TableEntry<HirFileId, Option<TreeArc<SyntaxNode>>>>,
|
||||
T: IntoIterator<Item = TableEntry<FileId, Parse>>,
|
||||
{
|
||||
let mut res = SyntaxTreeStats::default();
|
||||
for entry in iter {
|
||||
res.total += 1;
|
||||
if let Some(tree) = entry.value.as_ref().map(|it| &it.tree) {
|
||||
res.retained += 1;
|
||||
res.retained_size += tree.syntax().memory_size_of_subtree();
|
||||
}
|
||||
}
|
||||
res
|
||||
}
|
||||
}
|
||||
|
||||
impl FromIterator<TableEntry<MacroFile, Option<TreeArc<SyntaxNode>>>> for SyntaxTreeStats {
|
||||
fn from_iter<T>(iter: T) -> SyntaxTreeStats
|
||||
where
|
||||
T: IntoIterator<Item = TableEntry<MacroFile, Option<TreeArc<SyntaxNode>>>>,
|
||||
{
|
||||
let mut res = SyntaxTreeStats::default();
|
||||
for entry in iter {
|
||||
|
|
Loading…
Reference in a new issue