Skip to content

Commit e0100e6

Browse files
committed
Optimize
1 parent 43f09ad commit e0100e6

File tree

5 files changed

+28
-13
lines changed

5 files changed

+28
-13
lines changed

crates/ra_cfg/src/cfg_expr.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ pub enum CfgExpr {
1515

1616
impl CfgExpr {
1717
/// Fold the cfg by querying all basic `Atom` and `KeyValue` predicates.
18-
pub fn fold(&self, query: &impl Fn(&SmolStr, Option<&SmolStr>) -> bool) -> Option<bool> {
18+
pub fn fold(&self, query: &dyn Fn(&SmolStr, Option<&SmolStr>) -> bool) -> Option<bool> {
1919
match self {
2020
CfgExpr::Invalid => None,
2121
CfgExpr::Atom(name) => Some(query(name, None)),

crates/ra_hir/src/attr.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,15 @@ impl Attr {
4545

4646
pub(crate) fn from_attrs_owner(
4747
file_id: HirFileId,
48-
owner: &impl AttrsOwner,
48+
owner: &dyn AttrsOwner,
4949
db: &impl AstDatabase,
50-
) -> Arc<[Attr]> {
51-
owner.attrs().flat_map(|ast| Attr::from_src(Source { file_id, ast }, db)).collect()
50+
) -> Option<Arc<[Attr]>> {
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())
5257
}
5358

5459
pub(crate) fn is_simple_atom(&self, name: &str) -> bool {

crates/ra_hir/src/impl_block.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,9 @@ impl ModuleImplBlocks {
213213
match item {
214214
ast::ItemOrMacro::Item(ast::ModuleItem::ImplBlock(impl_block_ast)) => {
215215
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+
}) {
217219
continue;
218220
}
219221

@@ -228,7 +230,9 @@ impl ModuleImplBlocks {
228230
ast::ItemOrMacro::Item(_) => (),
229231
ast::ItemOrMacro::Macro(macro_call) => {
230232
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+
}) {
232236
continue;
233237
}
234238

crates/ra_hir/src/nameres/collector.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ use rustc_hash::FxHashMap;
77
use test_utils::tested_by;
88

99
use crate::{
10-
attr::Attr,
1110
db::DefDatabase,
1211
ids::{AstItemDef, LocationCtx, MacroCallId, MacroCallLoc, MacroDefId, MacroFileKind},
1312
name::MACRO_RULES,
@@ -715,8 +714,12 @@ where
715714
}
716715
}
717716

718-
fn is_cfg_enabled(&self, attrs: &[Attr]) -> bool {
719-
attrs.iter().all(|attr| attr.is_cfg_enabled(&self.def_collector.cfg_options) != Some(false))
717+
fn is_cfg_enabled(&self, attrs: &raw::Attrs) -> bool {
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+
})
720723
}
721724
}
722725

crates/ra_hir/src/nameres/raw.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -120,9 +120,12 @@ impl Index<Macro> for RawItems {
120120
}
121121
}
122122

123+
// Avoid heap allocation on items without attributes.
124+
pub(super) type Attrs = Option<Arc<[Attr]>>;
125+
123126
#[derive(Debug, PartialEq, Eq, Clone)]
124127
pub(super) struct RawItem {
125-
pub(super) attrs: Arc<[Attr]>,
128+
pub(super) attrs: Attrs,
126129
pub(super) kind: RawItemKind,
127130
}
128131

@@ -390,7 +393,7 @@ impl<DB: AstDatabase> RawItemsCollector<&DB> {
390393
fn push_import(
391394
&mut self,
392395
current_module: Option<Module>,
393-
attrs: Arc<[Attr]>,
396+
attrs: Attrs,
394397
data: ImportData,
395398
source: ImportSourcePtr,
396399
) {
@@ -399,7 +402,7 @@ impl<DB: AstDatabase> RawItemsCollector<&DB> {
399402
self.push_item(current_module, attrs, RawItemKind::Import(import))
400403
}
401404

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) {
403406
match current_module {
404407
Some(module) => match &mut self.raw_items.modules[module] {
405408
ModuleData::Definition { items, .. } => items,
@@ -410,7 +413,7 @@ impl<DB: AstDatabase> RawItemsCollector<&DB> {
410413
.push(RawItem { attrs, kind })
411414
}
412415

413-
fn parse_attrs(&self, item: &impl ast::AttrsOwner) -> Arc<[Attr]> {
416+
fn parse_attrs(&self, item: &impl ast::AttrsOwner) -> Attrs {
414417
Attr::from_attrs_owner(self.file_id, item, self.db)
415418
}
416419
}

0 commit comments

Comments
 (0)