Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Prev Previous commit
Next Next commit
rustc_metadata: Remove RwLock from CStore
  • Loading branch information
petrochenkov committed Oct 24, 2019
commit c5fee33e7aa71d7cfc5abd8848a81924cafd2b15
26 changes: 15 additions & 11 deletions src/librustc_metadata/creader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ impl<'a> CrateLoader<'a> {
}

fn register_crate(
&self,
&mut self,
host_lib: Option<Library>,
root: Option<&CratePaths>,
span: Span,
Expand Down Expand Up @@ -319,7 +319,7 @@ impl<'a> CrateLoader<'a> {
}

fn resolve_crate<'b>(
&'b self,
&'b mut self,
name: Symbol,
span: Span,
dep_kind: DepKind,
Expand All @@ -329,7 +329,7 @@ impl<'a> CrateLoader<'a> {
}

fn maybe_resolve_crate<'b>(
&'b self,
&'b mut self,
name: Symbol,
span: Span,
mut dep_kind: DepKind,
Expand Down Expand Up @@ -458,7 +458,7 @@ impl<'a> CrateLoader<'a> {
}

// Go through the crate metadata and load any crates that it references
fn resolve_crate_deps(&self,
fn resolve_crate_deps(&mut self,
root: &CratePaths,
crate_root: &CrateRoot<'_>,
metadata: &MetadataBlob,
Expand Down Expand Up @@ -519,7 +519,7 @@ impl<'a> CrateLoader<'a> {
decls
}

fn inject_panic_runtime(&self, krate: &ast::Crate) {
fn inject_panic_runtime(&mut self, krate: &ast::Crate) {
// If we're only compiling an rlib, then there's no need to select a
// panic runtime, so we just skip this section entirely.
let any_non_rlib = self.sess.crate_types.borrow().iter().any(|ct| {
Expand Down Expand Up @@ -600,7 +600,7 @@ impl<'a> CrateLoader<'a> {
&|data| data.root.needs_panic_runtime);
}

fn inject_sanitizer_runtime(&self) {
fn inject_sanitizer_runtime(&mut self) {
if let Some(ref sanitizer) = self.sess.opts.debugging_opts.sanitizer {
// Sanitizers can only be used on some tested platforms with
// executables linked to `std`
Expand Down Expand Up @@ -698,7 +698,7 @@ impl<'a> CrateLoader<'a> {
}
}

fn inject_profiler_runtime(&self) {
fn inject_profiler_runtime(&mut self) {
if self.sess.opts.debugging_opts.profile ||
self.sess.opts.cg.profile_generate.enabled()
{
Expand Down Expand Up @@ -852,7 +852,7 @@ impl<'a> CrateLoader<'a> {
});
}

pub fn postprocess(&self, krate: &ast::Crate) {
pub fn postprocess(&mut self, krate: &ast::Crate) {
self.inject_sanitizer_runtime();
self.inject_profiler_runtime();
self.inject_allocator_crate(krate);
Expand All @@ -863,7 +863,11 @@ impl<'a> CrateLoader<'a> {
}
}

pub fn process_extern_crate(&self, item: &ast::Item, definitions: &Definitions) -> CrateNum {
pub fn process_extern_crate(
&mut self,
item: &ast::Item,
definitions: &Definitions,
) -> CrateNum {
match item.kind {
ast::ItemKind::ExternCrate(orig_name) => {
debug!("resolving extern crate stmt. ident: {} orig_name: {:?}",
Expand Down Expand Up @@ -902,7 +906,7 @@ impl<'a> CrateLoader<'a> {
}
}

pub fn process_path_extern(&self, name: Symbol, span: Span) -> CrateNum {
pub fn process_path_extern(&mut self, name: Symbol, span: Span) -> CrateNum {
let cnum = self.resolve_crate(name, span, DepKind::Explicit, None).0;

self.update_extern_crate(
Expand All @@ -920,7 +924,7 @@ impl<'a> CrateLoader<'a> {
cnum
}

pub fn maybe_process_path_extern(&self, name: Symbol, span: Span) -> Option<CrateNum> {
pub fn maybe_process_path_extern(&mut self, name: Symbol, span: Span) -> Option<CrateNum> {
let cnum = self.maybe_resolve_crate(name, span, DepKind::Explicit, None).ok()?.0;

self.update_extern_crate(
Expand Down
25 changes: 11 additions & 14 deletions src/librustc_metadata/cstore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ crate struct CrateMetadata {

#[derive(Clone)]
pub struct CStore {
metas: RwLock<IndexVec<CrateNum, Option<Lrc<CrateMetadata>>>>,
metas: IndexVec<CrateNum, Option<Lrc<CrateMetadata>>>,
Copy link
Contributor Author

@petrochenkov petrochenkov Oct 20, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately we cannot remove this Lrc because CStore has to be cloneable.
EDIT: The reason - #65625 (comment).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wow, I didn't even realize that was a thing. Do you know where the clones are coming from?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess we could get rid of it by having the expansion query produce the CStore structure and redirect later uses to that instead. That would make the structure more like before this PR. It doesn't seem like a huge win right now though. It might be worth waiting on moving things into TyCtxt. Maybe we'd replace it by a crate_metadata(CrateNum) -> &'tcx CrateMetadata query or something.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, this certainly can be done later if "later" is a better time due to other changes (like TyCtxt).
The clone itself (the primary effect) is cheap - only a vector of Lrcs is cloned.
The locks around CrateMetadata::{dependencies,dep_kind,extern_crate} (the secondary effect) are also unlikely to be accessed often and cause performance issues.

}

pub enum LoadedMacro {
Expand All @@ -112,34 +112,31 @@ impl Default for CStore {
// order to make array indices in `metas` match with the
// corresponding `CrateNum`. This first entry will always remain
// `None`.
metas: RwLock::new(IndexVec::from_elem_n(None, 1)),
metas: IndexVec::from_elem_n(None, 1),
}
}
}

impl CStore {
crate fn alloc_new_crate_num(&self) -> CrateNum {
let mut metas = self.metas.borrow_mut();
let cnum = CrateNum::new(metas.len());
metas.push(None);
cnum
crate fn alloc_new_crate_num(&mut self) -> CrateNum {
self.metas.push(None);
CrateNum::new(self.metas.len() - 1)
}

crate fn get_crate_data(&self, cnum: CrateNum) -> Lrc<CrateMetadata> {
self.metas.borrow()[cnum].clone()
self.metas[cnum].clone()
.unwrap_or_else(|| panic!("Failed to get crate data for {:?}", cnum))
}

crate fn set_crate_data(&self, cnum: CrateNum, data: Lrc<CrateMetadata>) {
let mut metas = self.metas.borrow_mut();
assert!(metas[cnum].is_none(), "Overwriting crate metadata entry");
metas[cnum] = Some(data);
crate fn set_crate_data(&mut self, cnum: CrateNum, data: Lrc<CrateMetadata>) {
assert!(self.metas[cnum].is_none(), "Overwriting crate metadata entry");
self.metas[cnum] = Some(data);
}

crate fn iter_crate_data<I>(&self, mut i: I)
where I: FnMut(CrateNum, &Lrc<CrateMetadata>)
{
for (k, v) in self.metas.borrow().iter_enumerated() {
for (k, v) in self.metas.iter_enumerated() {
if let &Some(ref v) = v {
i(k, v);
}
Expand Down Expand Up @@ -170,7 +167,7 @@ impl CStore {

crate fn do_postorder_cnums_untracked(&self) -> Vec<CrateNum> {
let mut ordering = Vec::new();
for (num, v) in self.metas.borrow().iter_enumerated() {
for (num, v) in self.metas.iter_enumerated() {
if let &Some(_) = v {
self.push_dependencies_in_postorder(&mut ordering, num);
}
Expand Down