From cc04cfc982b0321b55d54937709c01ca242eb5ff Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Sun, 30 Jan 2022 22:18:32 +0100 Subject: [PATCH] Reduce allocations in attribute collection --- crates/hir/src/lib.rs | 2 +- crates/hir_def/src/attr.rs | 78 ++++++++++++--------------- crates/hir_def/src/child_by_source.rs | 2 +- crates/hir_expand/src/db.rs | 4 +- crates/hir_expand/src/hygiene.rs | 2 +- crates/hir_expand/src/lib.rs | 8 +-- crates/syntax/src/ast.rs | 4 +- crates/syntax/src/ast/node_ext.rs | 11 ++-- crates/syntax/src/ast/traits.rs | 20 ++++--- 9 files changed, 61 insertions(+), 70 deletions(-) diff --git a/crates/hir/src/lib.rs b/crates/hir/src/lib.rs index d357755236..fe518c8642 100644 --- a/crates/hir/src/lib.rs +++ b/crates/hir/src/lib.rs @@ -664,7 +664,7 @@ fn emit_def_diagnostic(db: &dyn HirDatabase, acc: &mut Vec, diag: let attr = node .doc_comments_and_attrs() .nth((*invoc_attr_index) as usize) - .and_then(Either::right) + .and_then(Either::left) .unwrap_or_else(|| panic!("cannot find attribute #{}", invoc_attr_index)); ( ast_id.with_value(SyntaxNodePtr::from(AstPtr::new(&attr))), diff --git a/crates/hir_def/src/attr.rs b/crates/hir_def/src/attr.rs index 4aeb714189..f4d8d0d3bd 100644 --- a/crates/hir_def/src/attr.rs +++ b/crates/hir_def/src/attr.rs @@ -525,38 +525,36 @@ impl AttrsWithOwner { fn inner_attributes( syntax: &SyntaxNode, -) -> Option<(impl Iterator, impl Iterator)> { - let (attrs, docs) = match_ast! { +) -> Option>> { + let node = match_ast! { match syntax { - ast::SourceFile(it) => (it.attrs(), ast::DocCommentIter::from_syntax_node(it.syntax())), - ast::ExternBlock(it) => { - let extern_item_list = it.extern_item_list()?; - (extern_item_list.attrs(), ast::DocCommentIter::from_syntax_node(extern_item_list.syntax())) + ast::SourceFile(_) => syntax.clone(), + ast::ExternBlock(it) => it.extern_item_list()?.syntax().clone(), + ast::Fn(it) => it.body()?.stmt_list()?.syntax().clone(), + ast::Impl(it) => it.assoc_item_list()?.syntax().clone(), + ast::Module(it) => it.item_list()?.syntax().clone(), + ast::BlockExpr(it) => { + use syntax::SyntaxKind::{BLOCK_EXPR , EXPR_STMT}; + // Block expressions accept outer and inner attributes, but only when they are the outer + // expression of an expression statement or the final expression of another block expression. + let may_carry_attributes = matches!( + it.syntax().parent().map(|it| it.kind()), + Some(BLOCK_EXPR | EXPR_STMT) + ); + if !may_carry_attributes { + return None + } + syntax.clone() }, - ast::Fn(it) => { - let body = it.body()?; - let stmt_list = body.stmt_list()?; - (stmt_list.attrs(), ast::DocCommentIter::from_syntax_node(body.syntax())) - }, - ast::Impl(it) => { - let assoc_item_list = it.assoc_item_list()?; - (assoc_item_list.attrs(), ast::DocCommentIter::from_syntax_node(assoc_item_list.syntax())) - }, - ast::Module(it) => { - let item_list = it.item_list()?; - (item_list.attrs(), ast::DocCommentIter::from_syntax_node(item_list.syntax())) - }, - // FIXME: BlockExpr's only accept inner attributes in specific cases - // Excerpt from the reference: - // Block expressions accept outer and inner attributes, but only when they are the outer - // expression of an expression statement or the final expression of another block expression. - ast::BlockExpr(_it) => return None, _ => return None, } }; - let attrs = attrs.filter(|attr| attr.kind().is_inner()); - let docs = docs.filter(|doc| doc.is_inner()); - Some((attrs, docs)) + + let attrs = ast::AttrDocCommentIter::from_syntax_node(&node).filter(|el| match el { + Either::Left(attr) => attr.kind().is_inner(), + Either::Right(comment) => comment.is_inner(), + }); + Some(attrs) } #[derive(Debug)] @@ -833,24 +831,16 @@ fn attrs_from_item_tree(id: ItemTreeId, db: &dyn DefDatabase fn collect_attrs( owner: &dyn ast::HasAttrs, ) -> impl Iterator)> { - let (inner_attrs, inner_docs) = inner_attributes(owner.syntax()) - .map_or((None, None), |(attrs, docs)| (Some(attrs), Some(docs))); - - let outer_attrs = owner.attrs().filter(|attr| attr.kind().is_outer()); - let attrs = outer_attrs - .chain(inner_attrs.into_iter().flatten()) - .map(|attr| (attr.syntax().text_range().start(), Either::Left(attr))); - - let outer_docs = - ast::DocCommentIter::from_syntax_node(owner.syntax()).filter(ast::Comment::is_outer); - let docs = outer_docs - .chain(inner_docs.into_iter().flatten()) - .map(|docs_text| (docs_text.syntax().text_range().start(), Either::Right(docs_text))); - // sort here by syntax node offset because the source can have doc attributes and doc strings be interleaved - docs.chain(attrs) - .sorted_by_key(|&(offset, _)| offset) + let inner_attrs = inner_attributes(owner.syntax()).into_iter().flatten(); + let outer_attrs = + ast::AttrDocCommentIter::from_syntax_node(owner.syntax()).filter(|el| match el { + Either::Left(attr) => attr.kind().is_outer(), + Either::Right(comment) => comment.is_outer(), + }); + outer_attrs + .chain(inner_attrs) .enumerate() - .map(|(id, (_, attr))| (AttrId { ast_index: id as u32 }, attr)) + .map(|(id, attr)| (AttrId { ast_index: id as u32 }, attr)) } pub(crate) fn variants_attrs_source_map( diff --git a/crates/hir_def/src/child_by_source.rs b/crates/hir_def/src/child_by_source.rs index 6b4abd8c44..5c32a31e44 100644 --- a/crates/hir_def/src/child_by_source.rs +++ b/crates/hir_def/src/child_by_source.rs @@ -117,7 +117,7 @@ impl ChildBySource for ItemScope { |(ast_id, calls)| { let adt = ast_id.to_node(db.upcast()); calls.for_each(|(attr_id, calls)| { - if let Some(Either::Right(attr)) = + if let Some(Either::Left(attr)) = adt.doc_comments_and_attrs().nth(attr_id.ast_index as usize) { res[keys::DERIVE_MACRO_CALL].insert(attr, (attr_id, calls.into())); diff --git a/crates/hir_expand/src/db.rs b/crates/hir_expand/src/db.rs index 69749decd5..b46f3fc955 100644 --- a/crates/hir_expand/src/db.rs +++ b/crates/hir_expand/src/db.rs @@ -157,7 +157,7 @@ pub fn expand_speculative( let attr = item .doc_comments_and_attrs() .nth(invoc_attr_index as usize) - .and_then(Either::right)?; + .and_then(Either::left)?; match attr.token_tree() { Some(token_tree) => { let (mut tree, map) = syntax_node_to_token_tree(attr.token_tree()?.syntax()); @@ -323,7 +323,7 @@ fn censor_for_macro_input(loc: &MacroCallLoc, node: &SyntaxNode) -> FxHashSet diff --git a/crates/syntax/src/ast.rs b/crates/syntax/src/ast.rs index 5c1aed3cd4..91b46cf8e9 100644 --- a/crates/syntax/src/ast.rs +++ b/crates/syntax/src/ast.rs @@ -27,8 +27,8 @@ pub use self::{ operators::{ArithOp, BinaryOp, CmpOp, LogicOp, Ordering, RangeOp, UnaryOp}, token_ext::{CommentKind, CommentPlacement, CommentShape, IsString, QuoteOffsets, Radix}, traits::{ - DocCommentIter, HasArgList, HasAttrs, HasDocComments, HasGenericParams, HasLoopBody, - HasModuleItem, HasName, HasTypeBounds, HasVisibility, + AttrDocCommentIter, DocCommentIter, HasArgList, HasAttrs, HasDocComments, HasGenericParams, + HasLoopBody, HasModuleItem, HasName, HasTypeBounds, HasVisibility, }, }; diff --git a/crates/syntax/src/ast/node_ext.rs b/crates/syntax/src/ast/node_ext.rs index 705aa5edac..7211c77e88 100644 --- a/crates/syntax/src/ast/node_ext.rs +++ b/crates/syntax/src/ast/node_ext.rs @@ -160,14 +160,9 @@ impl ast::Attr { } pub fn kind(&self) -> AttrKind { - let first_token = self.syntax().first_token(); - let first_token_kind = first_token.as_ref().map(SyntaxToken::kind); - let second_token_kind = - first_token.and_then(|token| token.next_token()).as_ref().map(SyntaxToken::kind); - - match (first_token_kind, second_token_kind) { - (Some(T![#]), Some(T![!])) => AttrKind::Inner, - _ => AttrKind::Outer, + match self.excl_token() { + Some(_) => AttrKind::Inner, + None => AttrKind::Outer, } } diff --git a/crates/syntax/src/ast/traits.rs b/crates/syntax/src/ast/traits.rs index 98b1087e64..aa2b7ed5c8 100644 --- a/crates/syntax/src/ast/traits.rs +++ b/crates/syntax/src/ast/traits.rs @@ -76,8 +76,8 @@ pub trait HasDocComments: HasAttrs { fn doc_comments(&self) -> DocCommentIter { DocCommentIter { iter: self.syntax().children_with_tokens() } } - fn doc_comments_and_attrs(&self) -> AttrCommentIter { - AttrCommentIter { iter: self.syntax().children_with_tokens() } + fn doc_comments_and_attrs(&self) -> AttrDocCommentIter { + AttrDocCommentIter { iter: self.syntax().children_with_tokens() } } } @@ -113,17 +113,23 @@ impl Iterator for DocCommentIter { } } -pub struct AttrCommentIter { +pub struct AttrDocCommentIter { iter: SyntaxElementChildren, } -impl Iterator for AttrCommentIter { - type Item = Either; +impl AttrDocCommentIter { + pub fn from_syntax_node(syntax_node: &ast::SyntaxNode) -> AttrDocCommentIter { + AttrDocCommentIter { iter: syntax_node.children_with_tokens() } + } +} + +impl Iterator for AttrDocCommentIter { + type Item = Either; fn next(&mut self) -> Option { self.iter.by_ref().find_map(|el| match el { - SyntaxElement::Node(node) => ast::Attr::cast(node).map(Either::Right), + SyntaxElement::Node(node) => ast::Attr::cast(node).map(Either::Left), SyntaxElement::Token(tok) => { - ast::Comment::cast(tok).filter(ast::Comment::is_doc).map(Either::Left) + ast::Comment::cast(tok).filter(ast::Comment::is_doc).map(Either::Right) } }) }