aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir/src
diff options
context:
space:
mode:
authoruHOOCCOOHu <[email protected]>2019-10-02 18:38:56 +0100
committeruHOOCCOOHu <[email protected]>2019-10-02 19:28:03 +0100
commite0100e63ae2e873f119b905ac77c3355ffb351b0 (patch)
tree25d799c1e52a32af80f603f4e6103e0969c5b6a8 /crates/ra_hir/src
parent43f09ad36ccc1c53c78a66274693e53161c9b2fa (diff)
Optimize
Diffstat (limited to 'crates/ra_hir/src')
-rw-r--r--crates/ra_hir/src/attr.rs11
-rw-r--r--crates/ra_hir/src/impl_block.rs8
-rw-r--r--crates/ra_hir/src/nameres/collector.rs9
-rw-r--r--crates/ra_hir/src/nameres/raw.rs11
4 files changed, 27 insertions, 12 deletions
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 {
45 45
46 pub(crate) fn from_attrs_owner( 46 pub(crate) fn from_attrs_owner(
47 file_id: HirFileId, 47 file_id: HirFileId,
48 owner: &impl AttrsOwner, 48 owner: &dyn AttrsOwner,
49 db: &impl AstDatabase, 49 db: &impl AstDatabase,
50 ) -> Arc<[Attr]> { 50 ) -> Option<Arc<[Attr]>> {
51 owner.attrs().flat_map(|ast| Attr::from_src(Source { file_id, ast }, db)).collect() 51 let mut attrs = owner.attrs().peekable();
52 if attrs.peek().is_none() {
53 // Avoid heap allocation
54 return None;
55 }
56 Some(attrs.flat_map(|ast| Attr::from_src(Source { file_id, ast }, db)).collect())
52 } 57 }
53 58
54 pub(crate) fn is_simple_atom(&self, name: &str) -> bool { 59 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 {
213 match item { 213 match item {
214 ast::ItemOrMacro::Item(ast::ModuleItem::ImplBlock(impl_block_ast)) => { 214 ast::ItemOrMacro::Item(ast::ModuleItem::ImplBlock(impl_block_ast)) => {
215 let attrs = Attr::from_attrs_owner(file_id, &impl_block_ast, db); 215 let attrs = Attr::from_attrs_owner(file_id, &impl_block_ast, db);
216 if attrs.iter().any(|attr| attr.is_cfg_enabled(cfg_options) == Some(false)) { 216 if attrs.map_or(false, |attrs| {
217 attrs.iter().any(|attr| attr.is_cfg_enabled(cfg_options) == Some(false))
218 }) {
217 continue; 219 continue;
218 } 220 }
219 221
@@ -228,7 +230,9 @@ impl ModuleImplBlocks {
228 ast::ItemOrMacro::Item(_) => (), 230 ast::ItemOrMacro::Item(_) => (),
229 ast::ItemOrMacro::Macro(macro_call) => { 231 ast::ItemOrMacro::Macro(macro_call) => {
230 let attrs = Attr::from_attrs_owner(file_id, &macro_call, db); 232 let attrs = Attr::from_attrs_owner(file_id, &macro_call, db);
231 if attrs.iter().any(|attr| attr.is_cfg_enabled(cfg_options) == Some(false)) { 233 if attrs.map_or(false, |attrs| {
234 attrs.iter().any(|attr| attr.is_cfg_enabled(cfg_options) == Some(false))
235 }) {
232 continue; 236 continue;
233 } 237 }
234 238
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;
7use test_utils::tested_by; 7use test_utils::tested_by;
8 8
9use crate::{ 9use crate::{
10 attr::Attr,
11 db::DefDatabase, 10 db::DefDatabase,
12 ids::{AstItemDef, LocationCtx, MacroCallId, MacroCallLoc, MacroDefId, MacroFileKind}, 11 ids::{AstItemDef, LocationCtx, MacroCallId, MacroCallLoc, MacroDefId, MacroFileKind},
13 name::MACRO_RULES, 12 name::MACRO_RULES,
@@ -715,8 +714,12 @@ where
715 } 714 }
716 } 715 }
717 716
718 fn is_cfg_enabled(&self, attrs: &[Attr]) -> bool { 717 fn is_cfg_enabled(&self, attrs: &raw::Attrs) -> bool {
719 attrs.iter().all(|attr| attr.is_cfg_enabled(&self.def_collector.cfg_options) != Some(false)) 718 attrs.as_ref().map_or(true, |attrs| {
719 attrs
720 .iter()
721 .all(|attr| attr.is_cfg_enabled(&self.def_collector.cfg_options) != Some(false))
722 })
720 } 723 }
721} 724}
722 725
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<Macro> for RawItems {
120 } 120 }
121} 121}
122 122
123// Avoid heap allocation on items without attributes.
124pub(super) type Attrs = Option<Arc<[Attr]>>;
125
123#[derive(Debug, PartialEq, Eq, Clone)] 126#[derive(Debug, PartialEq, Eq, Clone)]
124pub(super) struct RawItem { 127pub(super) struct RawItem {
125 pub(super) attrs: Arc<[Attr]>, 128 pub(super) attrs: Attrs,
126 pub(super) kind: RawItemKind, 129 pub(super) kind: RawItemKind,
127} 130}
128 131
@@ -390,7 +393,7 @@ impl<DB: AstDatabase> RawItemsCollector<&DB> {
390 fn push_import( 393 fn push_import(
391 &mut self, 394 &mut self,
392 current_module: Option<Module>, 395 current_module: Option<Module>,
393 attrs: Arc<[Attr]>, 396 attrs: Attrs,
394 data: ImportData, 397 data: ImportData,
395 source: ImportSourcePtr, 398 source: ImportSourcePtr,
396 ) { 399 ) {
@@ -399,7 +402,7 @@ impl<DB: AstDatabase> RawItemsCollector<&DB> {
399 self.push_item(current_module, attrs, RawItemKind::Import(import)) 402 self.push_item(current_module, attrs, RawItemKind::Import(import))
400 } 403 }
401 404
402 fn push_item(&mut self, current_module: Option<Module>, attrs: Arc<[Attr]>, kind: RawItemKind) { 405 fn push_item(&mut self, current_module: Option<Module>, attrs: Attrs, kind: RawItemKind) {
403 match current_module { 406 match current_module {
404 Some(module) => match &mut self.raw_items.modules[module] { 407 Some(module) => match &mut self.raw_items.modules[module] {
405 ModuleData::Definition { items, .. } => items, 408 ModuleData::Definition { items, .. } => items,
@@ -410,7 +413,7 @@ impl<DB: AstDatabase> RawItemsCollector<&DB> {
410 .push(RawItem { attrs, kind }) 413 .push(RawItem { attrs, kind })
411 } 414 }
412 415
413 fn parse_attrs(&self, item: &impl ast::AttrsOwner) -> Arc<[Attr]> { 416 fn parse_attrs(&self, item: &impl ast::AttrsOwner) -> Attrs {
414 Attr::from_attrs_owner(self.file_id, item, self.db) 417 Attr::from_attrs_owner(self.file_id, item, self.db)
415 } 418 }
416} 419}