From e0100e63ae2e873f119b905ac77c3355ffb351b0 Mon Sep 17 00:00:00 2001 From: uHOOCCOOHu Date: Thu, 3 Oct 2019 01:38:56 +0800 Subject: Optimize --- crates/ra_hir/src/attr.rs | 11 ++++++++--- crates/ra_hir/src/impl_block.rs | 8 ++++++-- crates/ra_hir/src/nameres/collector.rs | 9 ++++++--- crates/ra_hir/src/nameres/raw.rs | 11 +++++++---- 4 files changed, 27 insertions(+), 12 deletions(-) (limited to 'crates/ra_hir/src') diff --git a/crates/ra_hir/src/attr.rs b/crates/ra_hir/src/attr.rs index 84c36b8da..a8a7e9006 100644 --- a/crates/ra_hir/src/attr.rs +++ b/crates/ra_hir/src/attr.rs @@ -45,10 +45,15 @@ impl Attr { pub(crate) fn from_attrs_owner( file_id: HirFileId, - owner: &impl AttrsOwner, + owner: &dyn AttrsOwner, db: &impl AstDatabase, - ) -> Arc<[Attr]> { - owner.attrs().flat_map(|ast| Attr::from_src(Source { file_id, ast }, db)).collect() + ) -> Option> { + let mut attrs = owner.attrs().peekable(); + if attrs.peek().is_none() { + // Avoid heap allocation + return None; + } + Some(attrs.flat_map(|ast| Attr::from_src(Source { file_id, ast }, db)).collect()) } pub(crate) fn is_simple_atom(&self, name: &str) -> bool { diff --git a/crates/ra_hir/src/impl_block.rs b/crates/ra_hir/src/impl_block.rs index 7877c3171..55dfc393b 100644 --- a/crates/ra_hir/src/impl_block.rs +++ b/crates/ra_hir/src/impl_block.rs @@ -213,7 +213,9 @@ impl ModuleImplBlocks { match item { ast::ItemOrMacro::Item(ast::ModuleItem::ImplBlock(impl_block_ast)) => { let attrs = Attr::from_attrs_owner(file_id, &impl_block_ast, db); - if attrs.iter().any(|attr| attr.is_cfg_enabled(cfg_options) == Some(false)) { + if attrs.map_or(false, |attrs| { + attrs.iter().any(|attr| attr.is_cfg_enabled(cfg_options) == Some(false)) + }) { continue; } @@ -228,7 +230,9 @@ impl ModuleImplBlocks { ast::ItemOrMacro::Item(_) => (), ast::ItemOrMacro::Macro(macro_call) => { let attrs = Attr::from_attrs_owner(file_id, ¯o_call, db); - if attrs.iter().any(|attr| attr.is_cfg_enabled(cfg_options) == Some(false)) { + if attrs.map_or(false, |attrs| { + attrs.iter().any(|attr| attr.is_cfg_enabled(cfg_options) == Some(false)) + }) { continue; } diff --git a/crates/ra_hir/src/nameres/collector.rs b/crates/ra_hir/src/nameres/collector.rs index 1d79cbd8c..cef2dc9d2 100644 --- a/crates/ra_hir/src/nameres/collector.rs +++ b/crates/ra_hir/src/nameres/collector.rs @@ -7,7 +7,6 @@ use rustc_hash::FxHashMap; use test_utils::tested_by; use crate::{ - attr::Attr, db::DefDatabase, ids::{AstItemDef, LocationCtx, MacroCallId, MacroCallLoc, MacroDefId, MacroFileKind}, name::MACRO_RULES, @@ -715,8 +714,12 @@ where } } - fn is_cfg_enabled(&self, attrs: &[Attr]) -> bool { - attrs.iter().all(|attr| attr.is_cfg_enabled(&self.def_collector.cfg_options) != Some(false)) + fn is_cfg_enabled(&self, attrs: &raw::Attrs) -> bool { + attrs.as_ref().map_or(true, |attrs| { + attrs + .iter() + .all(|attr| attr.is_cfg_enabled(&self.def_collector.cfg_options) != Some(false)) + }) } } diff --git a/crates/ra_hir/src/nameres/raw.rs b/crates/ra_hir/src/nameres/raw.rs index f02d4eb7a..623b343c4 100644 --- a/crates/ra_hir/src/nameres/raw.rs +++ b/crates/ra_hir/src/nameres/raw.rs @@ -120,9 +120,12 @@ impl Index for RawItems { } } +// Avoid heap allocation on items without attributes. +pub(super) type Attrs = Option>; + #[derive(Debug, PartialEq, Eq, Clone)] pub(super) struct RawItem { - pub(super) attrs: Arc<[Attr]>, + pub(super) attrs: Attrs, pub(super) kind: RawItemKind, } @@ -390,7 +393,7 @@ impl RawItemsCollector<&DB> { fn push_import( &mut self, current_module: Option, - attrs: Arc<[Attr]>, + attrs: Attrs, data: ImportData, source: ImportSourcePtr, ) { @@ -399,7 +402,7 @@ impl RawItemsCollector<&DB> { self.push_item(current_module, attrs, RawItemKind::Import(import)) } - fn push_item(&mut self, current_module: Option, attrs: Arc<[Attr]>, kind: RawItemKind) { + fn push_item(&mut self, current_module: Option, attrs: Attrs, kind: RawItemKind) { match current_module { Some(module) => match &mut self.raw_items.modules[module] { ModuleData::Definition { items, .. } => items, @@ -410,7 +413,7 @@ impl RawItemsCollector<&DB> { .push(RawItem { attrs, kind }) } - fn parse_attrs(&self, item: &impl ast::AttrsOwner) -> Arc<[Attr]> { + fn parse_attrs(&self, item: &impl ast::AttrsOwner) -> Attrs { Attr::from_attrs_owner(self.file_id, item, self.db) } } -- cgit v1.2.3