Skip to content

Commit 8a49f93

Browse files
bors[bot]lnicola
andauthored
Merge #5497
5497: Store macro invocation parameters as text instead of tt r=jonas-schievink a=lnicola We don't want to expand macros on every source change because it can be arbitrarily slow, but the token trees can be rather large. So instead we can cache the invocation parameters (as text). Co-authored-by: Laurențiu Nicola <[email protected]>
2 parents 085891d + cb958cf commit 8a49f93

File tree

5 files changed

+20
-13
lines changed

5 files changed

+20
-13
lines changed

crates/ra_hir/src/db.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ pub use hir_def::db::{
1111
};
1212
pub use hir_expand::db::{
1313
AstDatabase, AstDatabaseStorage, AstIdMapQuery, InternEagerExpansionQuery, InternMacroQuery,
14-
MacroArgQuery, MacroDefQuery, MacroExpandQuery, ParseMacroQuery,
14+
MacroArgTextQuery, MacroDefQuery, MacroExpandQuery, ParseMacroQuery,
1515
};
1616
pub use hir_ty::db::{
1717
AssociatedTyDataQuery, AssociatedTyValueQuery, CallableItemSignatureQuery, FieldTypesQuery,

crates/ra_hir_expand/src/db.rs

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use mbe::{ExpandResult, MacroRules};
66
use ra_db::{salsa, SourceDatabase};
77
use ra_parser::FragmentKind;
88
use ra_prof::profile;
9-
use ra_syntax::{algo::diff, AstNode, Parse, SyntaxKind::*, SyntaxNode};
9+
use ra_syntax::{algo::diff, AstNode, GreenNode, Parse, SyntaxKind::*, SyntaxNode};
1010

1111
use crate::{
1212
ast_id_map::AstIdMap, BuiltinDeriveExpander, BuiltinFnLikeExpander, EagerCallLoc, EagerMacroId,
@@ -72,6 +72,8 @@ pub trait AstDatabase: SourceDatabase {
7272

7373
#[salsa::interned]
7474
fn intern_macro(&self, macro_call: MacroCallLoc) -> LazyMacroId;
75+
fn macro_arg_text(&self, id: MacroCallId) -> Option<GreenNode>;
76+
#[salsa::transparent]
7577
fn macro_arg(&self, id: MacroCallId) -> Option<Arc<(tt::Subtree, mbe::TokenMap)>>;
7678
fn macro_def(&self, id: MacroDefId) -> Option<Arc<(TokenExpander, mbe::TokenMap)>>;
7779
fn parse_macro(&self, macro_file: MacroFile)
@@ -148,10 +150,7 @@ pub(crate) fn macro_def(
148150
}
149151
}
150152

151-
pub(crate) fn macro_arg(
152-
db: &dyn AstDatabase,
153-
id: MacroCallId,
154-
) -> Option<Arc<(tt::Subtree, mbe::TokenMap)>> {
153+
pub(crate) fn macro_arg_text(db: &dyn AstDatabase, id: MacroCallId) -> Option<GreenNode> {
155154
let id = match id {
156155
MacroCallId::LazyMacro(id) => id,
157156
MacroCallId::EagerMacro(_id) => {
@@ -161,7 +160,15 @@ pub(crate) fn macro_arg(
161160
};
162161
let loc = db.lookup_intern_macro(id);
163162
let arg = loc.kind.arg(db)?;
164-
let (tt, tmap) = mbe::syntax_node_to_token_tree(&arg)?;
163+
Some(arg.green().clone())
164+
}
165+
166+
pub(crate) fn macro_arg(
167+
db: &dyn AstDatabase,
168+
id: MacroCallId,
169+
) -> Option<Arc<(tt::Subtree, mbe::TokenMap)>> {
170+
let arg = db.macro_arg_text(id)?;
171+
let (tt, tmap) = mbe::syntax_node_to_token_tree(&SyntaxNode::new_root(arg))?;
165172
Some(Arc::new((tt, tmap)))
166173
}
167174

crates/ra_ide_db/src/change.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ impl RootDatabase {
151151

152152
// Macros do take significant space, but less then the syntax trees
153153
// self.query(hir::db::MacroDefQuery).sweep(sweep);
154-
// self.query(hir::db::MacroArgQuery).sweep(sweep);
154+
// self.query(hir::db::MacroArgTextQuery).sweep(sweep);
155155
// self.query(hir::db::MacroExpandQuery).sweep(sweep);
156156

157157
hir::db::AstIdMapQuery.in_db(self).sweep(sweep);
@@ -199,7 +199,7 @@ impl RootDatabase {
199199

200200
// AstDatabase
201201
hir::db::AstIdMapQuery
202-
hir::db::MacroArgQuery
202+
hir::db::MacroArgTextQuery
203203
hir::db::MacroDefQuery
204204
hir::db::ParseMacroQuery
205205
hir::db::MacroExpandQuery

crates/ra_syntax/src/lib.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,16 +42,14 @@ use std::{marker::PhantomData, sync::Arc};
4242
use ra_text_edit::Indel;
4343
use stdx::format_to;
4444

45-
use crate::syntax_node::GreenNode;
46-
4745
pub use crate::{
4846
algo::InsertPosition,
4947
ast::{AstNode, AstToken},
5048
parsing::{lex_single_syntax_kind, lex_single_valid_syntax_kind, tokenize, Token},
5149
ptr::{AstPtr, SyntaxNodePtr},
5250
syntax_error::SyntaxError,
5351
syntax_node::{
54-
Direction, NodeOrToken, SyntaxElement, SyntaxElementChildren, SyntaxNode,
52+
Direction, GreenNode, NodeOrToken, SyntaxElement, SyntaxElementChildren, SyntaxNode,
5553
SyntaxNodeChildren, SyntaxToken, SyntaxTreeBuilder,
5654
},
5755
};

crates/ra_syntax/src/syntax_node.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ use rowan::{GreenNodeBuilder, Language};
1010

1111
use crate::{Parse, SmolStr, SyntaxError, SyntaxKind, TextSize};
1212

13-
pub(crate) use rowan::{GreenNode, GreenToken};
13+
pub use rowan::GreenNode;
14+
15+
pub(crate) use rowan::GreenToken;
1416

1517
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
1618
pub enum RustLanguage {}

0 commit comments

Comments
 (0)