Skip to content

Commit f3f0922

Browse files
committed
Auto merge of #50528 - whitfin:issue-50508, r=<try>
Remove attribute_cache from CrateMetadata This PR will fix #50508 by removing the `attribute_cache` from the `CrateMetadata` struct. Seeing as performance was referenced in the original issue, I also cleaned up a `self.entry(node_id);` call which might have occasionally happened redundantly. r? @michaelwoerister
2 parents 3ec2058 + 8eaef65 commit f3f0922

File tree

8 files changed

+85
-148
lines changed

8 files changed

+85
-148
lines changed

src/librustc_incremental/persist/dirty_clean.rs

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -453,16 +453,20 @@ impl<'a, 'tcx> DirtyCleanVisitor<'a, 'tcx> {
453453
out
454454
}
455455

456-
fn dep_nodes(&self, labels: &Labels, def_id: DefId) -> Vec<DepNode> {
457-
let mut out = Vec::with_capacity(labels.len());
456+
fn dep_nodes<'l>(
457+
&self,
458+
labels: &'l Labels,
459+
def_id: DefId
460+
) -> impl Iterator<Item = DepNode> + 'l {
458461
let def_path_hash = self.tcx.def_path_hash(def_id);
459-
for label in labels.iter() {
460-
match DepNode::from_label_string(label, def_path_hash) {
461-
Ok(dep_node) => out.push(dep_node),
462-
Err(()) => unreachable!(),
463-
}
464-
}
465-
out
462+
labels
463+
.iter()
464+
.map(move |label| {
465+
match DepNode::from_label_string(label, def_path_hash) {
466+
Ok(dep_node) => dep_node,
467+
Err(()) => unreachable!(),
468+
}
469+
})
466470
}
467471

468472
fn dep_node_str(&self, dep_node: &DepNode) -> String {

src/librustc_incremental/persist/work_product.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ pub fn copy_cgu_workproducts_to_incr_comp_cache_dir(
2828
if sess.opts.incremental.is_none() {
2929
return None
3030
}
31-
let work_product_id = WorkProductId::from_cgu_name(cgu_name);
3231

3332
let saved_files: Option<Vec<_>> =
3433
files.iter()
@@ -63,6 +62,7 @@ pub fn copy_cgu_workproducts_to_incr_comp_cache_dir(
6362
saved_files,
6463
};
6564

65+
let work_product_id = WorkProductId::from_cgu_name(cgu_name);
6666
Some((work_product_id, work_product))
6767
}
6868

src/librustc_metadata/creader.rs

Lines changed: 25 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,7 @@ use std::ops::Deref;
3333
use std::path::PathBuf;
3434
use std::{cmp, fs};
3535

36-
use syntax::ast;
37-
use syntax::attr;
36+
use syntax::{ast, attr};
3837
use syntax::ext::base::SyntaxExtension;
3938
use syntax::symbol::Symbol;
4039
use syntax::visit;
@@ -57,9 +56,9 @@ pub struct CrateLoader<'a> {
5756
fn dump_crates(cstore: &CStore) {
5857
info!("resolved crates:");
5958
cstore.iter_crate_data(|_, data| {
60-
info!(" name: {}", data.name());
59+
info!(" name: {}", data.root.name);
6160
info!(" cnum: {}", data.cnum);
62-
info!(" hash: {}", data.hash());
61+
info!(" hash: {}", data.root.hash);
6362
info!(" reqd: {:?}", *data.dep_kind.lock());
6463
let CrateSource { dylib, rlib, rmeta } = data.source.clone();
6564
dylib.map(|dl| info!(" dylib: {}", dl.0.display()));
@@ -112,7 +111,7 @@ impl<'a> CrateLoader<'a> {
112111
if data.name != name { return }
113112

114113
match hash {
115-
Some(hash) if *hash == data.hash() => { ret = Some(cnum); return }
114+
Some(hash) if *hash == data.root.hash => { ret = Some(cnum); return }
116115
Some(..) => return,
117116
None => {}
118117
}
@@ -171,9 +170,9 @@ impl<'a> CrateLoader<'a> {
171170

172171
// Check for conflicts with any crate loaded so far
173172
self.cstore.iter_crate_data(|_, other| {
174-
if other.name() == root.name && // same crate-name
175-
other.disambiguator() == root.disambiguator && // same crate-disambiguator
176-
other.hash() != root.hash { // but different SVH
173+
if other.root.name == root.name && // same crate-name
174+
other.root.disambiguator == root.disambiguator && // same crate-disambiguator
175+
other.root.hash != root.hash { // but different SVH
177176
span_fatal!(self.sess, span, E0523,
178177
"found two different crates with name `{}` that are \
179178
not distinguished by differing `-C metadata`. This \
@@ -213,7 +212,6 @@ impl<'a> CrateLoader<'a> {
213212
let root = if root.is_some() { root } else { &crate_paths };
214213

215214
let Library { dylib, rlib, rmeta, metadata } = lib;
216-
217215
let cnum_map = self.resolve_crate_deps(root, &crate_root, &metadata, cnum, span, dep_kind);
218216

219217
let dependencies: Vec<CrateNum> = cnum_map.iter().cloned().collect();
@@ -242,13 +240,12 @@ impl<'a> CrateLoader<'a> {
242240
cnum,
243241
dependencies: Lock::new(dependencies),
244242
codemap_import_info: RwLock::new(vec![]),
245-
attribute_cache: Lock::new([Vec::new(), Vec::new()]),
246243
dep_kind: Lock::new(dep_kind),
247244
source: cstore::CrateSource {
248245
dylib,
249246
rlib,
250247
rmeta,
251-
},
248+
}
252249
};
253250

254251
let cmeta = Lrc::new(cmeta);
@@ -344,7 +341,7 @@ impl<'a> CrateLoader<'a> {
344341
if locate_ctxt.triple == &self.sess.opts.target_triple {
345342
let mut result = LoadResult::Loaded(library);
346343
self.cstore.iter_crate_data(|cnum, data| {
347-
if data.name() == root.name && root.hash == data.hash() {
344+
if data.root.name == root.name && root.hash == data.root.hash {
348345
assert!(locate_ctxt.hash.is_none());
349346
info!("load success, going to previous cnum: {}", cnum);
350347
result = LoadResult::Previous(cnum);
@@ -636,15 +633,14 @@ impl<'a> CrateLoader<'a> {
636633
let mut needs_panic_runtime = attr::contains_name(&krate.attrs,
637634
"needs_panic_runtime");
638635

639-
let sess = self.sess;
640636
self.cstore.iter_crate_data(|cnum, data| {
641637
needs_panic_runtime = needs_panic_runtime ||
642-
data.needs_panic_runtime(sess);
643-
if data.is_panic_runtime(sess) {
638+
data.root.needs_panic_runtime;
639+
if data.root.panic_runtime {
644640
// Inject a dependency from all #![needs_panic_runtime] to this
645641
// #![panic_runtime] crate.
646642
self.inject_dependency_if(cnum, "a panic runtime",
647-
&|data| data.needs_panic_runtime(sess));
643+
&|data| data.root.needs_panic_runtime);
648644
runtime_found = runtime_found || *data.dep_kind.lock() == DepKind::Explicit;
649645
}
650646
});
@@ -681,19 +677,19 @@ impl<'a> CrateLoader<'a> {
681677

682678
// Sanity check the loaded crate to ensure it is indeed a panic runtime
683679
// and the panic strategy is indeed what we thought it was.
684-
if !data.is_panic_runtime(self.sess) {
680+
if !data.root.panic_runtime {
685681
self.sess.err(&format!("the crate `{}` is not a panic runtime",
686682
name));
687683
}
688-
if data.panic_strategy() != desired_strategy {
684+
if data.root.panic_strategy != desired_strategy {
689685
self.sess.err(&format!("the crate `{}` does not have the panic \
690686
strategy `{}`",
691687
name, desired_strategy.desc()));
692688
}
693689

694690
self.sess.injected_panic_runtime.set(Some(cnum));
695691
self.inject_dependency_if(cnum, "a panic runtime",
696-
&|data| data.needs_panic_runtime(self.sess));
692+
&|data| data.root.needs_panic_runtime);
697693
}
698694

699695
fn inject_sanitizer_runtime(&mut self) {
@@ -788,7 +784,7 @@ impl<'a> CrateLoader<'a> {
788784
PathKind::Crate, dep_kind);
789785

790786
// Sanity check the loaded crate to ensure it is indeed a sanitizer runtime
791-
if !data.is_sanitizer_runtime(self.sess) {
787+
if !data.root.sanitizer_runtime {
792788
self.sess.err(&format!("the crate `{}` is not a sanitizer runtime",
793789
name));
794790
}
@@ -811,7 +807,7 @@ impl<'a> CrateLoader<'a> {
811807
PathKind::Crate, dep_kind);
812808

813809
// Sanity check the loaded crate to ensure it is indeed a profiler runtime
814-
if !data.is_profiler_runtime(self.sess) {
810+
if !data.root.profiler_runtime {
815811
self.sess.err(&format!("the crate `profiler_builtins` is not \
816812
a profiler runtime"));
817813
}
@@ -828,7 +824,7 @@ impl<'a> CrateLoader<'a> {
828824
let mut needs_allocator = attr::contains_name(&krate.attrs,
829825
"needs_allocator");
830826
self.cstore.iter_crate_data(|_, data| {
831-
needs_allocator = needs_allocator || data.needs_allocator(self.sess);
827+
needs_allocator = needs_allocator || data.root.needs_allocator;
832828
});
833829
if !needs_allocator {
834830
self.sess.injected_allocator.set(None);
@@ -870,7 +866,7 @@ impl<'a> CrateLoader<'a> {
870866
None
871867
};
872868
self.cstore.iter_crate_data(|_, data| {
873-
if !data.has_global_allocator() {
869+
if !data.root.has_global_allocator {
874870
return
875871
}
876872
match global_allocator {
@@ -879,14 +875,14 @@ impl<'a> CrateLoader<'a> {
879875
conflicts with this global \
880876
allocator in: {}",
881877
other_crate,
882-
data.name()));
878+
data.root.name));
883879
}
884880
Some(None) => {
885881
self.sess.err(&format!("the #[global_allocator] in this \
886882
crate conflicts with global \
887-
allocator in: {}", data.name()));
883+
allocator in: {}", data.root.name));
888884
}
889-
None => global_allocator = Some(Some(data.name())),
885+
None => global_allocator = Some(Some(data.root.name)),
890886
}
891887
});
892888
if global_allocator.is_some() {
@@ -948,7 +944,7 @@ impl<'a> CrateLoader<'a> {
948944
// error.
949945
let mut allocator = None;
950946
self.cstore.iter_crate_data(|_, data| {
951-
if allocator.is_none() && data.has_default_lib_allocator() {
947+
if allocator.is_none() && data.root.has_default_lib_allocator {
952948
allocator = Some(data.clone());
953949
}
954950
});
@@ -1024,9 +1020,9 @@ impl<'a> CrateLoader<'a> {
10241020
self.sess.err(&format!("the crate `{}` cannot depend \
10251021
on a crate that needs {}, but \
10261022
it depends on `{}`",
1027-
self.cstore.get_crate_data(krate).name(),
1023+
self.cstore.get_crate_data(krate).root.name,
10281024
what,
1029-
data.name()));
1025+
data.root.name));
10301026
}
10311027
}
10321028

src/librustc_metadata/cstore.rs

Lines changed: 2 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,14 @@
1313

1414
use schema;
1515

16-
use rustc::hir::def_id::{CRATE_DEF_INDEX, CrateNum, DefIndex};
16+
use rustc::hir::def_id::{CrateNum, DefIndex};
1717
use rustc::hir::map::definitions::DefPathTable;
18-
use rustc::hir::svh::Svh;
1918
use rustc::middle::cstore::{DepKind, ExternCrate, MetadataLoader};
20-
use rustc::session::{Session, CrateDisambiguator};
21-
use rustc_target::spec::PanicStrategy;
2219
use rustc_data_structures::indexed_vec::IndexVec;
2320
use rustc::util::nodemap::{FxHashMap, NodeMap};
2421

2522
use rustc_data_structures::sync::{Lrc, RwLock, Lock};
26-
use syntax::{ast, attr};
23+
use syntax::ast;
2724
use syntax::ext::base::SyntaxExtension;
2825
use syntax::symbol::Symbol;
2926
use syntax_pos;
@@ -68,7 +65,6 @@ pub struct CrateMetadata {
6865
pub cnum: CrateNum,
6966
pub dependencies: Lock<Vec<CrateNum>>,
7067
pub codemap_import_info: RwLock<Vec<ImportedFileMap>>,
71-
pub attribute_cache: Lock<[Vec<Option<Lrc<[ast::Attribute]>>>; 2]>,
7268

7369
pub root: schema::CrateRoot,
7470

@@ -176,62 +172,3 @@ impl CStore {
176172
self.extern_mod_crate_map.borrow().get(&emod_id).cloned()
177173
}
178174
}
179-
180-
impl CrateMetadata {
181-
pub fn name(&self) -> Symbol {
182-
self.root.name
183-
}
184-
pub fn hash(&self) -> Svh {
185-
self.root.hash
186-
}
187-
pub fn disambiguator(&self) -> CrateDisambiguator {
188-
self.root.disambiguator
189-
}
190-
191-
pub fn needs_allocator(&self, sess: &Session) -> bool {
192-
let attrs = self.get_item_attrs(CRATE_DEF_INDEX, sess);
193-
attr::contains_name(&attrs, "needs_allocator")
194-
}
195-
196-
pub fn has_global_allocator(&self) -> bool {
197-
self.root.has_global_allocator.clone()
198-
}
199-
200-
pub fn has_default_lib_allocator(&self) -> bool {
201-
self.root.has_default_lib_allocator.clone()
202-
}
203-
204-
pub fn is_panic_runtime(&self, sess: &Session) -> bool {
205-
let attrs = self.get_item_attrs(CRATE_DEF_INDEX, sess);
206-
attr::contains_name(&attrs, "panic_runtime")
207-
}
208-
209-
pub fn needs_panic_runtime(&self, sess: &Session) -> bool {
210-
let attrs = self.get_item_attrs(CRATE_DEF_INDEX, sess);
211-
attr::contains_name(&attrs, "needs_panic_runtime")
212-
}
213-
214-
pub fn is_compiler_builtins(&self, sess: &Session) -> bool {
215-
let attrs = self.get_item_attrs(CRATE_DEF_INDEX, sess);
216-
attr::contains_name(&attrs, "compiler_builtins")
217-
}
218-
219-
pub fn is_sanitizer_runtime(&self, sess: &Session) -> bool {
220-
let attrs = self.get_item_attrs(CRATE_DEF_INDEX, sess);
221-
attr::contains_name(&attrs, "sanitizer_runtime")
222-
}
223-
224-
pub fn is_profiler_runtime(&self, sess: &Session) -> bool {
225-
let attrs = self.get_item_attrs(CRATE_DEF_INDEX, sess);
226-
attr::contains_name(&attrs, "profiler_runtime")
227-
}
228-
229-
pub fn is_no_builtins(&self, sess: &Session) -> bool {
230-
let attrs = self.get_item_attrs(CRATE_DEF_INDEX, sess);
231-
attr::contains_name(&attrs, "no_builtins")
232-
}
233-
234-
pub fn panic_strategy(&self) -> PanicStrategy {
235-
self.root.panic_strategy.clone()
236-
}
237-
}

src/librustc_metadata/cstore_impl.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -169,17 +169,17 @@ provide! { <'tcx> tcx, def_id, other, cdata,
169169
is_mir_available => { cdata.is_item_mir_available(def_id.index) }
170170

171171
dylib_dependency_formats => { Lrc::new(cdata.get_dylib_dependency_formats()) }
172-
is_panic_runtime => { cdata.is_panic_runtime(tcx.sess) }
173-
is_compiler_builtins => { cdata.is_compiler_builtins(tcx.sess) }
174-
has_global_allocator => { cdata.has_global_allocator() }
175-
is_sanitizer_runtime => { cdata.is_sanitizer_runtime(tcx.sess) }
176-
is_profiler_runtime => { cdata.is_profiler_runtime(tcx.sess) }
177-
panic_strategy => { cdata.panic_strategy() }
172+
is_panic_runtime => { cdata.root.panic_runtime }
173+
is_compiler_builtins => { cdata.root.compiler_builtins }
174+
has_global_allocator => { cdata.root.has_global_allocator }
175+
is_sanitizer_runtime => { cdata.root.sanitizer_runtime }
176+
is_profiler_runtime => { cdata.root.profiler_runtime }
177+
panic_strategy => { cdata.root.panic_strategy }
178178
extern_crate => {
179179
let r = Lrc::new(*cdata.extern_crate.lock());
180180
r
181181
}
182-
is_no_builtins => { cdata.is_no_builtins(tcx.sess) }
182+
is_no_builtins => { cdata.root.no_builtins }
183183
impl_defaultness => { cdata.get_impl_defaultness(def_id.index) }
184184
reachable_non_generics => {
185185
let reachable_non_generics = tcx
@@ -208,9 +208,9 @@ provide! { <'tcx> tcx, def_id, other, cdata,
208208
DefId { krate: def_id.krate, index }
209209
})
210210
}
211-
crate_disambiguator => { cdata.disambiguator() }
212-
crate_hash => { cdata.hash() }
213-
original_crate_name => { cdata.name() }
211+
crate_disambiguator => { cdata.root.disambiguator }
212+
crate_hash => { cdata.root.hash }
213+
original_crate_name => { cdata.root.name }
214214

215215
extra_filename => { cdata.root.extra_filename.clone() }
216216

@@ -456,12 +456,12 @@ impl CrateStore for cstore::CStore {
456456

457457
fn crate_disambiguator_untracked(&self, cnum: CrateNum) -> CrateDisambiguator
458458
{
459-
self.get_crate_data(cnum).disambiguator()
459+
self.get_crate_data(cnum).root.disambiguator
460460
}
461461

462462
fn crate_hash_untracked(&self, cnum: CrateNum) -> hir::svh::Svh
463463
{
464-
self.get_crate_data(cnum).hash()
464+
self.get_crate_data(cnum).root.hash
465465
}
466466

467467
/// Returns the `DefKey` for a given `DefId`. This indicates the

0 commit comments

Comments
 (0)