Skip to content

Commit

Permalink
Use Arena for interning
Browse files Browse the repository at this point in the history
  • Loading branch information
Zoxc committed Jan 2, 2020
1 parent 8f52664 commit aadba95
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 28 deletions.
5 changes: 4 additions & 1 deletion src/librustc/arena.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,9 @@ macro_rules! arena_types {
[few] inferred_outlives_crate: rustc::ty::CratePredicatesMap<'tcx>,
[] upvars: rustc_data_structures::fx::FxIndexMap<rustc::hir::HirId, rustc::hir::Upvar>,

// Interned types
[] tys: rustc::ty::TyS<$tcx>,

// HIR types
[few] hir_forest: rustc::hir::map::Forest<$tcx>,
[] arm: rustc::hir::Arm<$tcx>,
Expand Down Expand Up @@ -176,7 +179,7 @@ macro_rules! declare_arena {
([], [$($a:tt $name:ident: $ty:ty,)*], $tcx:lifetime) => {
#[derive(Default)]
pub struct Arena<$tcx> {
dropless: DroplessArena,
pub dropless: DroplessArena,
drop: DropArena,
$($name: arena_for_type!($a[$ty]),)*
}
Expand Down
20 changes: 4 additions & 16 deletions src/librustc/ty/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ use crate::util::common::ErrorReported;
use crate::util::nodemap::{DefIdMap, DefIdSet, ItemLocalMap, ItemLocalSet, NodeMap};
use crate::util::nodemap::{FxHashMap, FxHashSet};

use arena::SyncDroplessArena;
use errors::DiagnosticBuilder;
use rustc_data_structures::profiling::SelfProfilerRef;
use rustc_data_structures::sharded::{IntoPointer, ShardedHashMap};
Expand Down Expand Up @@ -78,21 +77,11 @@ use syntax::expand::allocator::AllocatorKind;
use syntax::source_map::MultiSpan;
use syntax::symbol::{kw, sym, Symbol};

pub struct AllArenas {
pub interner: SyncDroplessArena,
}

impl AllArenas {
pub fn new() -> Self {
AllArenas { interner: SyncDroplessArena::default() }
}
}

type InternedSet<'tcx, T> = ShardedHashMap<Interned<'tcx, T>, ()>;

pub struct CtxtInterners<'tcx> {
/// The arena that types, regions, etc. are allocated from.
arena: &'tcx SyncDroplessArena,
arena: &'tcx WorkerLocal<Arena<'tcx>>,

/// Specifically use a speedy hash algorithm for these hash sets, since
/// they're accessed quite often.
Expand All @@ -112,7 +101,7 @@ pub struct CtxtInterners<'tcx> {
}

impl<'tcx> CtxtInterners<'tcx> {
fn new(arena: &'tcx SyncDroplessArena) -> CtxtInterners<'tcx> {
fn new(arena: &'tcx WorkerLocal<Arena<'tcx>>) -> CtxtInterners<'tcx> {
CtxtInterners {
arena,
type_: Default::default(),
Expand Down Expand Up @@ -1115,7 +1104,6 @@ impl<'tcx> TyCtxt<'tcx> {
lint_store: Lrc<lint::LintStore>,
local_providers: ty::query::Providers<'tcx>,
extern_providers: ty::query::Providers<'tcx>,
arenas: &'tcx AllArenas,
arena: &'tcx WorkerLocal<Arena<'tcx>>,
resolutions: ty::ResolverOutputs,
hir: hir_map::Map<'tcx>,
Expand All @@ -1126,7 +1114,7 @@ impl<'tcx> TyCtxt<'tcx> {
let data_layout = TargetDataLayout::parse(&s.target.target).unwrap_or_else(|err| {
s.fatal(&err);
});
let interners = CtxtInterners::new(&arenas.interner);
let interners = CtxtInterners::new(arena);
let common_types = CommonTypes::new(&interners);
let common_lifetimes = CommonLifetimes::new(&interners);
let common_consts = CommonConsts::new(&interners, &common_types);
Expand Down Expand Up @@ -2084,7 +2072,7 @@ macro_rules! slice_interners {
$(impl<'tcx> TyCtxt<'tcx> {
pub fn $method(self, v: &[$ty]) -> &'tcx List<$ty> {
self.interners.$field.intern_ref(v, || {
Interned(List::from_arena(&self.interners.arena, v))
Interned(List::from_arena(&*self.arena, v))
}).0
}
})+
Expand Down
10 changes: 6 additions & 4 deletions src/librustc/ty/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ pub use self::BorrowKind::*;
pub use self::IntVarValue::*;
pub use self::Variance::*;

use crate::arena::Arena;
use crate::hir::def::{CtorKind, CtorOf, DefKind, ExportMap, Res};
use crate::hir::def_id::{CrateNum, DefId, LocalDefId, CRATE_DEF_INDEX, LOCAL_CRATE};
use crate::hir::Node;
Expand All @@ -29,7 +30,6 @@ use crate::ty::util::{Discr, IntTypeExt};
use crate::ty::walk::TypeWalker;
use crate::util::captures::Captures;
use crate::util::nodemap::{DefIdMap, FxHashMap, NodeMap, NodeSet};
use arena::SyncDroplessArena;
use rustc_data_structures::svh::Svh;
use rustc_macros::HashStable;

Expand Down Expand Up @@ -76,7 +76,7 @@ pub use crate::ty::diagnostics::*;
pub use self::binding::BindingMode;
pub use self::binding::BindingMode::*;

pub use self::context::{keep_local, tls, AllArenas, FreeRegionInfo, TyCtxt};
pub use self::context::{keep_local, tls, FreeRegionInfo, TyCtxt};
pub use self::context::{
CanonicalUserType, CanonicalUserTypeAnnotation, CanonicalUserTypeAnnotations, ResolvedOpaqueTy,
UserType, UserTypeAnnotationIndex,
Expand Down Expand Up @@ -611,7 +611,7 @@ unsafe impl<T: Sync> Sync for List<T> {}

impl<T: Copy> List<T> {
#[inline]
fn from_arena<'tcx>(arena: &'tcx SyncDroplessArena, slice: &[T]) -> &'tcx List<T> {
fn from_arena<'tcx>(arena: &'tcx Arena<'tcx>, slice: &[T]) -> &'tcx List<T> {
assert!(!mem::needs_drop::<T>());
assert!(mem::size_of::<T>() != 0);
assert!(slice.len() != 0);
Expand All @@ -624,7 +624,9 @@ impl<T: Copy> List<T> {

let size = offset + slice.len() * mem::size_of::<T>();

let mem = arena.alloc_raw(size, cmp::max(mem::align_of::<T>(), mem::align_of::<usize>()));
let mem = arena
.dropless
.alloc_raw(size, cmp::max(mem::align_of::<T>(), mem::align_of::<usize>()));
unsafe {
let result = &mut *(mem.as_mut_ptr() as *mut List<T>);
// Write the length
Expand Down
4 changes: 1 addition & 3 deletions src/librustc_interface/passes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use rustc::session::search_paths::PathKind;
use rustc::session::Session;
use rustc::traits;
use rustc::ty::steal::Steal;
use rustc::ty::{self, AllArenas, GlobalCtxt, ResolverOutputs, TyCtxt};
use rustc::ty::{self, GlobalCtxt, ResolverOutputs, TyCtxt};
use rustc::util::common::{time, ErrorReported};
use rustc_builtin_macros;
use rustc_codegen_ssa::back::link::emit_metadata;
Expand Down Expand Up @@ -726,7 +726,6 @@ pub fn create_global_ctxt<'tcx>(
outputs: OutputFilenames,
crate_name: &str,
global_ctxt: &'tcx Once<GlobalCtxt<'tcx>>,
all_arenas: &'tcx AllArenas,
arena: &'tcx WorkerLocal<Arena<'tcx>>,
) -> QueryContext<'tcx> {
let sess = &compiler.session();
Expand Down Expand Up @@ -759,7 +758,6 @@ pub fn create_global_ctxt<'tcx>(
lint_store,
local_providers,
extern_providers,
&all_arenas,
arena,
resolver_outputs,
hir_map,
Expand Down
5 changes: 1 addition & 4 deletions src/librustc_interface/queries.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use rustc::lint::LintStore;
use rustc::session::config::{OutputFilenames, OutputType};
use rustc::session::Session;
use rustc::ty::steal::Steal;
use rustc::ty::{AllArenas, GlobalCtxt, ResolverOutputs};
use rustc::ty::{GlobalCtxt, ResolverOutputs};
use rustc::util::common::{time, ErrorReported};
use rustc_codegen_utils::codegen_backend::CodegenBackend;
use rustc_data_structures::sync::{Lrc, Once, WorkerLocal};
Expand Down Expand Up @@ -67,7 +67,6 @@ pub struct Queries<'tcx> {
compiler: &'tcx Compiler,
gcx: Once<GlobalCtxt<'tcx>>,

all_arenas: AllArenas,
arena: WorkerLocal<Arena<'tcx>>,

dep_graph_future: Query<Option<DepGraphFuture>>,
Expand All @@ -87,7 +86,6 @@ impl<'tcx> Queries<'tcx> {
Queries {
compiler,
gcx: Once::new(),
all_arenas: AllArenas::new(),
arena: WorkerLocal::new(|_| Arena::default()),
dep_graph_future: Default::default(),
parse: Default::default(),
Expand Down Expand Up @@ -264,7 +262,6 @@ impl<'tcx> Queries<'tcx> {
outputs,
&crate_name,
&self.gcx,
&self.all_arenas,
&self.arena,
))
})
Expand Down

0 comments on commit aadba95

Please sign in to comment.