8000 Remove queries from rustc_interface by Zoxc · Pull Request #59904 · rust-lang/rust · GitHub
[go: up one dir, main page]

Skip to content

Remove queries from rustc_interface #59904

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 10 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add a AtomicOnce type
  • Loading branch information
Zoxc committed Jul 8, 2019
commit d5fbdd22a91dbc3250a9643ad5bd17a8c46d2fa1
20 changes: 6 additions & 14 deletions src/librustc/ty/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,8 @@ use rustc_data_structures::stable_hasher::{HashStable, hash_stable_hashmap,
StableHasher, StableHasherResult,
StableVec};
use arena::SyncDroplessArena;
use rustc_data_structures::cold_path;
use rustc_data_structures::indexed_vec::{Idx, IndexVec};
use rustc_data_structures::sync::{Lrc, Lock, WorkerLocal, AtomicCell};
use rustc_data_structures::sync::{Lrc, Lock, WorkerLocal, AtomicOnce};
use std::any::Any;
use std::borrow::Borrow;
use std::cmp::Ordering;
Expand Down Expand Up @@ -1022,7 +1021,7 @@ pub struct GlobalCtxt<'tcx> {

pub hir_defs: hir::map::Definitions,

hir_map: AtomicCell<Option<&'tcx hir_map::Map<'tcx>>>,
hir_map: AtomicOnce<&'tcx hir_map::Map<'tcx>>,

/// A map from DefPathHash -> DefId. Includes DefIds from the local crate
/// as well as all upstream crates. Only populated in incremental mode.
Expand Down Expand Up @@ -1089,17 +1088,10 @@ impl<'tcx> TyCtxt<'tcx> {

#[inline(always)]
pub fn hir(self) -> &'tcx hir_map::Map<'tcx> {
let value = self.hir_map.load();
if unlikely!(value.is_none()) {
self.hir_map.get_or_init(|| {
// We can use `with_ignore` here because the hir map does its own tracking
cold_path(|| self.dep_graph.with_ignore(|| {
let map = self.hir_map(LOCAL_CRATE);
self.hir_map.store(Some(map));
map
}))
} else {
value.unwrap()
}
self.dep_graph.with_ignore(|| self.hir_map(LOCAL_CRATE))
})
}

pub fn alloc_steal_mir(self, mir: Body<'tcx>) -> &'tcx Steal<Body<'tcx>> {
Expand Down Expand Up @@ -1281,7 +1273,7 @@ impl<'tcx> TyCtxt<'tcx> {
extern_prelude: resolutions.extern_prelude,
hir_forest,
hir_defs,
hir_map: AtomicCell::new(None),
hir_map: AtomicOnce::new(),
def_path_hash_to_def_id,
queries: query::Queries::new(
providers,
Expand Down
29 changes: 29 additions & 0 deletions src/librustc_data_structures/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ use std::collections::HashMap;
use std::hash::{Hash, BuildHasher};
use std::marker::PhantomData;
use std::ops::{Deref, DerefMut};
use crate::cold_path;
use crate::owning_ref::{Erased, OwningRef};

pub fn serial_join<A, B, RA, RB>(oper_a: A, oper_b: B) -> (RA, RB)
Expand Down Expand Up @@ -573,6 +574,34 @@ impl<T> Once<T> {
}
}

/// A type whose inner value can be written once and then will stay read-only
pub struct AtomicOnce<T: Copy>(AtomicCell<Option<T>>);

impl<T: Copy> AtomicOnce<T> {
/// Creates an AtomicOnce value which is uninitialized
#[inline]
pub fn new() -> Self {
AtomicOnce(AtomicCell::new(None))
}

/// Gets the inner value. If the value is not already initalized.
/// It will be initalized by the closure. The closure may be called
/// concurrently by many threads.
#[inline(always)]
pub fn get_or_init(&self, f: impl FnOnce() -> T) -> T {
let value = self.0.load();
if unlikely!(value.is_none()) {
cold_path(|| {
let value = f();
self.0.store(Some(value));
value
})
} else {
value.unwrap()
}
}
}

#[derive(Debug)]
pub struct Lock<T>(InnerLock<T>);

Expand Down
0