Skip to content

Commit c3733a7

Browse files
committed
Auto merge of #50528 - whitfin:issue-50508, r=michaelwoerister
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 531e4ab + d95ba30 commit c3733a7

File tree

8 files changed

+86
-153
lines changed

8 files changed

+86
-153
lines changed

src/librustc_incremental/persist/dirty_clean.rs

+13-9
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

+1-1
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

+24-27
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,9 @@ pub struct CrateLoader<'a> {
5858
fn dump_crates(cstore: &CStore) {
5959
info!("resolved crates:");
6060
cstore.iter_crate_data(|_, data| {
61-
info!(" name: {}", data.name());
61+
info!(" name: {}", data.root.name);
6262
info!(" cnum: {}", data.cnum);
63-
info!(" hash: {}", data.hash());
63+
info!(" hash: {}", data.root.hash);
6464
info!(" reqd: {:?}", *data.dep_kind.lock());
6565
let CrateSource { dylib, rlib, rmeta } = data.source.clone();
6666
dylib.map(|dl| info!(" dylib: {}", dl.0.display()));
@@ -113,7 +113,7 @@ impl<'a> CrateLoader<'a> {
113113
if data.name != name { return }
114114

115115
match hash {
116-
Some(hash) if *hash == data.hash() => { ret = Some(cnum); return }
116+
Some(hash) if *hash == data.root.hash => { ret = Some(cnum); return }
117117
Some(..) => return,
118118
None => {}
119119
}
@@ -172,9 +172,9 @@ impl<'a> CrateLoader<'a> {
172172

173173
// Check for conflicts with any crate loaded so far
174174
self.cstore.iter_crate_data(|_, other| {
175-
if other.name() == root.name && // same crate-name
176-
other.disambiguator() == root.disambiguator && // same crate-disambiguator
177-
other.hash() != root.hash { // but different SVH
175+
if other.root.name == root.name && // same crate-name
176+
other.root.disambiguator == root.disambiguator && // same crate-disambiguator
177+
other.root.hash != root.hash { // but different SVH
178178
span_fatal!(self.sess, span, E0523,
179179
"found two different crates with name `{}` that are \
180180
not distinguished by differing `-C metadata`. This \
@@ -214,7 +214,6 @@ impl<'a> CrateLoader<'a> {
214214
let root = if root.is_some() { root } else { &crate_paths };
215215

216216
let Library { dylib, rlib, rmeta, metadata } = lib;
217-
218217
let cnum_map = self.resolve_crate_deps(root, &crate_root, &metadata, cnum, span, dep_kind);
219218

220219
let dependencies: Vec<CrateNum> = cnum_map.iter().cloned().collect();
@@ -243,13 +242,12 @@ impl<'a> CrateLoader<'a> {
243242
cnum,
244243
dependencies: Lock::new(dependencies),
245244
codemap_import_info: RwLock::new(vec![]),
246-
attribute_cache: Lock::new([Vec::new(), Vec::new()]),
247245
dep_kind: Lock::new(dep_kind),
248246
source: cstore::CrateSource {
249247
dylib,
250248
rlib,
251249
rmeta,
252-
},
250+
}
253251
};
254252

255253
let cmeta = Lrc::new(cmeta);
@@ -345,7 +343,7 @@ impl<'a> CrateLoader<'a> {
345343
if locate_ctxt.triple == &self.sess.opts.target_triple {
346344
let mut result = LoadResult::Loaded(library);
347345
self.cstore.iter_crate_data(|cnum, data| {
348-
if data.name() == root.name && root.hash == data.hash() {
346+
if data.root.name == root.name && root.hash == data.root.hash {
349347
assert!(locate_ctxt.hash.is_none());
350348
info!("load success, going to previous cnum: {}", cnum);
351349
result = LoadResult::Previous(cnum);
@@ -642,15 +640,14 @@ impl<'a> CrateLoader<'a> {
642640
let mut needs_panic_runtime = attr::contains_name(&krate.attrs,
643641
"needs_panic_runtime");
644642

645-
let sess = self.sess;
646643
self.cstore.iter_crate_data(|cnum, data| {
647644
needs_panic_runtime = needs_panic_runtime ||
648-
data.needs_panic_runtime(sess);
649-
if data.is_panic_runtime(sess) {
645+
data.root.needs_panic_runtime;
646+
if data.root.panic_runtime {
650647
// Inject a dependency from all #![needs_panic_runtime] to this
651648
// #![panic_runtime] crate.
652649
self.inject_dependency_if(cnum, "a panic runtime",
653-
&|data| data.needs_panic_runtime(sess));
650+
&|data| data.root.needs_panic_runtime);
654651
runtime_found = runtime_found || *data.dep_kind.lock() == DepKind::Explicit;
655652
}
656653
});
@@ -687,19 +684,19 @@ impl<'a> CrateLoader<'a> {
687684

688685
// Sanity check the loaded crate to ensure it is indeed a panic runtime
689686
// and the panic strategy is indeed what we thought it was.
690-
if !data.is_panic_runtime(self.sess) {
687+
if !data.root.panic_runtime {
691688
self.sess.err(&format!("the crate `{}` is not a panic runtime",
692689
name));
693690
}
694-
if data.panic_strategy() != desired_strategy {
691+
if data.root.panic_strategy != desired_strategy {
695692
self.sess.err(&format!("the crate `{}` does not have the panic \
696693
strategy `{}`",
697694
name, desired_strategy.desc()));
698695
}
699696

700697
self.sess.injected_panic_runtime.set(Some(cnum));
701698
self.inject_dependency_if(cnum, "a panic runtime",
702-
&|data| data.needs_panic_runtime(self.sess));
699+
&|data| data.root.needs_panic_runtime);
703700
}
704701

705702
fn inject_sanitizer_runtime(&mut self) {
@@ -794,7 +791,7 @@ impl<'a> CrateLoader<'a> {
794791
PathKind::Crate, dep_kind);
795792

796793
// Sanity check the loaded crate to ensure it is indeed a sanitizer runtime
797-
if !data.is_sanitizer_runtime(self.sess) {
794+
if !data.root.sanitizer_runtime {
798795
self.sess.err(&format!("the crate `{}` is not a sanitizer runtime",
799796
name));
800797
}
@@ -817,7 +814,7 @@ impl<'a> CrateLoader<'a> {
817814
PathKind::Crate, dep_kind);
818815

819816
// Sanity check the loaded crate to ensure it is indeed a profiler runtime
820-
if !data.is_profiler_runtime(self.sess) {
817+
if !data.root.profiler_runtime {
821818
self.sess.err(&format!("the crate `profiler_builtins` is not \
822819
a profiler runtime"));
823820
}
@@ -834,7 +831,7 @@ impl<'a> CrateLoader<'a> {
834831
let mut needs_allocator = attr::contains_name(&krate.attrs,
835832
"needs_allocator");
836833
self.cstore.iter_crate_data(|_, data| {
837-
needs_allocator = needs_allocator || data.needs_allocator(self.sess);
834+
needs_allocator = needs_allocator || data.root.needs_allocator;
838835
});
839836
if !needs_allocator {
840837
self.sess.injected_allocator.set(None);
@@ -876,7 +873,7 @@ impl<'a> CrateLoader<'a> {
876873
None
877874
};
878875
self.cstore.iter_crate_data(|_, data| {
879-
if !data.has_global_allocator() {
876+
if !data.root.has_global_allocator {
880877
return
881878
}
882879
match global_allocator {
@@ -885,14 +882,14 @@ impl<'a> CrateLoader<'a> {
885882
conflicts with this global \
886883
allocator in: {}",
887884
other_crate,
888-
data.name()));
885+
data.root.name));
889886
}
890887
Some(None) => {
891888
self.sess.err(&format!("the #[global_allocator] in this \
892889
crate conflicts with global \
893-
allocator in: {}", data.name()));
890+
allocator in: {}", data.root.name));
894891
}
895-
None => global_allocator = Some(Some(data.name())),
892+
None => global_allocator = Some(Some(data.root.name)),
896893
}
897894
});
898895
if global_allocator.is_some() {
@@ -954,7 +951,7 @@ impl<'a> CrateLoader<'a> {
954951
// error.
955952
let mut allocator = None;
956953
self.cstore.iter_crate_data(|_, data| {
957-
if allocator.is_none() && data.has_default_lib_allocator() {
954+
if allocator.is_none() && data.root.has_default_lib_allocator {
958955
allocator = Some(data.clone());
959956
}
960957
});
@@ -1030,9 +1027,9 @@ impl<'a> CrateLoader<'a> {
10301027
self.sess.err(&format!("the crate `{}` cannot depend \
10311028
on a crate that needs {}, but \
10321029
it depends on `{}`",
1033-
self.cstore.get_crate_data(krate).name(),
1030+
self.cstore.get_crate_data(krate).root.name,
10341031
what,
1035-
data.name()));
1032+
data.root.name));
10361033
}
10371034
}
10381035

src/librustc_metadata/cstore.rs

+2-70
Original file line numberDiff line numberDiff line change
@@ -13,18 +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};
27-
use syntax::edition::Edition;
23+
use syntax::ast;
2824
use syntax::ext::base::SyntaxExtension;
2925
use syntax::symbol::Symbol;
3026
use syntax_pos;
@@ -69,7 +65,6 @@ pub struct CrateMetadata {
6965
pub cnum: CrateNum,
7066
pub dependencies: Lock<Vec<CrateNum>>,
7167
pub codemap_import_info: RwLock<Vec<ImportedFileMap>>,
72-
pub attribute_cache: Lock<[Vec<Option<Lrc<[ast::Attribute]>>>; 2]>,
7368

7469
pub root: schema::CrateRoot,
7570

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

src/librustc_metadata/cstore_impl.rs

+14-14
Original file line numberDiff line numberDiff line change
@@ -170,17 +170,17 @@ provide! { <'tcx> tcx, def_id, other, cdata,
170170
is_mir_available => { cdata.is_item_mir_available(def_id.index) }
171171

172172
dylib_dependency_formats => { Lrc::new(cdata.get_dylib_dependency_formats()) }
173-
is_panic_runtime => { cdata.is_panic_runtime(tcx.sess) }
174-
is_compiler_builtins => { cdata.is_compiler_builtins(tcx.sess) }
175-
has_global_allocator => { cdata.has_global_allocator() }
176-
is_sanitizer_runtime => { cdata.is_sanitizer_runtime(tcx.sess) }
177-
is_profiler_runtime => { cdata.is_profiler_runtime(tcx.sess) }
178-
panic_strategy => { cdata.panic_strategy() }
173+
is_panic_runtime => { cdata.root.panic_runtime }
174+
is_compiler_builtins => { cdata.root.compiler_builtins }
175+
has_global_allocator => { cdata.root.has_global_allocator }
176+
is_sanitizer_runtime => { cdata.root.sanitizer_runtime }
177+
is_profiler_runtime => { cdata.root.profiler_runtime }
178+
panic_strategy => { cdata.root.panic_strategy }
179179
extern_crate => {
180180
let r = Lrc::new(*cdata.extern_crate.lock());
181181
r
182182
}
183-
is_no_builtins => { cdata.is_no_builtins(tcx.sess) }
183+
is_no_builtins => { cdata.root.no_builtins }
184184
impl_defaultness => { cdata.get_impl_defaultness(def_id.index) }
185185
reachable_non_generics => {
186186
let reachable_non_generics = tcx
@@ -209,9 +209,9 @@ provide! { <'tcx> tcx, def_id, other, cdata,
209209
DefId { krate: def_id.krate, index }
210210
})
211211
}
212-
crate_disambiguator => { cdata.disambiguator() }
213-
crate_hash => { cdata.hash() }
214-
original_crate_name => { cdata.name() }
212+
crate_disambiguator => { cdata.root.disambiguator }
213+
crate_hash => { cdata.root.hash }
214+
original_crate_name => { cdata.root.name }
215215

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

@@ -457,17 +457,17 @@ impl CrateStore for cstore::CStore {
457457

458458
fn crate_disambiguator_untracked(&self, cnum: CrateNum) -> CrateDisambiguator
459459
{
460-
self.get_crate_data(cnum).disambiguator()
460+
self.get_crate_data(cnum).root.disambiguator
461461
}
462462

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

468468
fn crate_edition_untracked(&self, cnum: CrateNum) -> Edition
469469
{
470-
self.get_crate_data(cnum).edition()
470+
self.get_crate_data(cnum).root.edition
471471
}
472472

473473
/// Returns the `DefKey` for a given `DefId`. This indicates the
@@ -519,7 +519,7 @@ impl CrateStore for cstore::CStore {
519519
} else if data.name == "proc_macro" &&
520520
self.get_crate_data(id.krate).item_name(id.index) == "quote" {
521521
let ext = SyntaxExtension::ProcMacro(Box::new(::proc_macro::__internal::Quoter),
522-
data.edition());
522+
data.root.edition);
523523
return LoadedMacro::ProcMacro(Lrc::new(ext));
524524
}
525525

0 commit comments

Comments
 (0)