Skip to content

Commit d914f17

Browse files
committed
Auto merge of #90919 - nnethercote:rm-DropArena, r=Mark-Simulacrum
Remove `DropArena`. Most arena-allocate types that impl `Drop` get their own `TypedArena`, but a few infrequently used ones share a `DropArena`. This sharing adds complexity but doesn't help performance or memory usage. Perhaps it was more effective in the past prior to some other improvements to arenas. This commit removes `DropArena` and the sharing of arenas via the `few` attribute of the `arena_types` macro. This change removes over 100 lines of code and nine uses of `unsafe` (one of which affects the parallel compiler) and makes the remaining code easier to read.
2 parents 934624f + fb80c73 commit d914f17

File tree

3 files changed

+17
-154
lines changed

3 files changed

+17
-154
lines changed

compiler/rustc_arena/src/lib.rs

+7-134
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
#![feature(rustc_attrs)]
2020
#![cfg_attr(test, feature(test))]
2121

22-
use rustc_data_structures::sync;
2322
use smallvec::SmallVec;
2423

2524
use std::alloc::Layout;
@@ -517,130 +516,12 @@ impl DroplessArena {
517516
}
518517
}
519518

520-
/// Calls the destructor for an object when dropped.
521-
struct DropType {
522-
drop_fn: unsafe fn(*mut u8),
523-
obj: *mut u8,
524-
}
525-
526-
// SAFETY: we require `T: Send` before type-erasing into `DropType`.
527-
#[cfg(parallel_compiler)]
528-
unsafe impl sync::Send for DropType {}
529-
530-
impl DropType {
531-
#[inline]
532-
unsafe fn new<T: sync::Send>(obj: *mut T) -> Self {
533-
unsafe fn drop_for_type<T>(to_drop: *mut u8) {
534-
std::ptr::drop_in_place(to_drop as *mut T)
535-
}
536-
537-
DropType { drop_fn: drop_for_type::<T>, obj: obj as *mut u8 }
538-
}
539-
}
540-
541-
impl Drop for DropType {
542-
fn drop(&mut self) {
543-
unsafe { (self.drop_fn)(self.obj) }
544-
}
545-
}
546-
547-
/// An arena which can be used to allocate any type.
548-
///
549-
/// # Safety
550-
///
551-
/// Allocating in this arena is unsafe since the type system
552-
/// doesn't know which types it contains. In order to
553-
/// allocate safely, you must store a `PhantomData<T>`
554-
/// alongside this arena for each type `T` you allocate.
555-
#[derive(Default)]
556-
pub struct DropArena {
557-
/// A list of destructors to run when the arena drops.
558-
/// Ordered so `destructors` gets dropped before the arena
559-
/// since its destructor can reference memory in the arena.
560-
destructors: RefCell<Vec<DropType>>,
561-
arena: DroplessArena,
562-
}
563-
564-
impl DropArena {
565-
#[inline]
566-
pub unsafe fn alloc<T>(&self, object: T) -> &mut T
567-
where
568-
T: sync::Send,
569-
{
570-
let mem = self.arena.alloc_raw(Layout::new::<T>()) as *mut T;
571-
// Write into uninitialized memory.
572-
ptr::write(mem, object);
573-
let result = &mut *mem;
574-
// Record the destructor after doing the allocation as that may panic
575-
// and would cause `object`'s destructor to run twice if it was recorded before.
576-
self.destructors.borrow_mut().push(DropType::new(result));
577-
result
578-
}
579-
580-
#[inline]
581-
pub unsafe fn alloc_from_iter<T, I>(&self, iter: I) -> &mut [T]
582-
where
583-
T: sync::Send,
584-
I: IntoIterator<Item = T>,
585-
{
586-
let mut vec: SmallVec<[_; 8]> = iter.into_iter().collect();
587-
if vec.is_empty() {
588-
return &mut [];
589-
}
590-
let len = vec.len();
591-
592-
let start_ptr = self.arena.alloc_raw(Layout::array::<T>(len).unwrap()) as *mut T;
593-
594-
let mut destructors = self.destructors.borrow_mut();
595-
// Reserve space for the destructors so we can't panic while adding them.
596-
destructors.reserve(len);
597-
598-
// Move the content to the arena by copying it and then forgetting
599-
// the content of the SmallVec.
600-
vec.as_ptr().copy_to_nonoverlapping(start_ptr, len);
601-
mem::forget(vec.drain(..));
602-
603-
// Record the destructors after doing the allocation as that may panic
604-
// and would cause `object`'s destructor to run twice if it was recorded before.
605-
for i in 0..len {
606-
destructors.push(DropType::new(start_ptr.add(i)));
607-
}
608-
609-
slice::from_raw_parts_mut(start_ptr, len)
610-
}
611-
}
612-
613-
pub macro arena_for_type {
614-
([][$ty:ty]) => {
615-
$crate::TypedArena<$ty>
616-
},
617-
([few $(, $attrs:ident)*][$ty:ty]) => {
618-
::std::marker::PhantomData<$ty>
619-
},
620-
([$ignore:ident $(, $attrs:ident)*]$args:tt) => {
621-
$crate::arena_for_type!([$($attrs),*]$args)
622-
},
623-
}
624-
625-
pub macro which_arena_for_type {
626-
([][$arena:expr]) => {
627-
::std::option::Option::Some($arena)
628-
},
629-
([few$(, $attrs:ident)*][$arena:expr]) => {
630-
::std::option::Option::None
631-
},
632-
([$ignore:ident$(, $attrs:ident)*]$args:tt) => {
633-
$crate::which_arena_for_type!([$($attrs),*]$args)
634-
},
635-
}
636-
637519
#[rustc_macro_transparency = "semitransparent"]
638520
pub macro declare_arena([$($a:tt $name:ident: $ty:ty,)*], $tcx:lifetime) {
639521
#[derive(Default)]
640522
pub struct Arena<$tcx> {
641523
pub dropless: $crate::DroplessArena,
642-
drop: $crate::DropArena,
643-
$($name: $crate::arena_for_type!($a[$ty]),)*
524+
$($name: $crate::TypedArena<$ty>,)*
644525
}
645526

646527
pub trait ArenaAllocatable<'tcx, T = Self>: Sized {
@@ -670,13 +551,9 @@ pub macro declare_arena([$($a:tt $name:ident: $ty:ty,)*], $tcx:lifetime) {
670551
#[inline]
671552
fn allocate_on<'a>(self, arena: &'a Arena<$tcx>) -> &'a mut Self {
672553
if !::std::mem::needs_drop::<Self>() {
673-
return arena.dropless.alloc(self);
674-
}
675-
match $crate::which_arena_for_type!($a[&arena.$name]) {
676-
::std::option::Option::<&$crate::TypedArena<Self>>::Some(ty_arena) => {
677-
ty_arena.alloc(self)
678-
}
679-
::std::option::Option::None => unsafe { arena.drop.alloc(self) },
554+
arena.dropless.alloc(self)
555+
} else {
556+
arena.$name.alloc(self)
680557
}
681558
}
682559

@@ -686,13 +563,9 @@ pub macro declare_arena([$($a:tt $name:ident: $ty:ty,)*], $tcx:lifetime) {
686563
iter: impl ::std::iter::IntoIterator<Item = Self>,
687564
) -> &'a mut [Self] {
688565
if !::std::mem::needs_drop::<Self>() {
689-
return arena.dropless.alloc_from_iter(iter);
690-
}
691-
match $crate::which_arena_for_type!($a[&arena.$name]) {
692-
::std::option::Option::<&$crate::TypedArena<Self>>::Some(ty_arena) => {
693-
ty_arena.alloc_from_iter(iter)
694-
}
695-
::std::option::Option::None => unsafe { arena.drop.alloc_from_iter(iter) },
566+
arena.dropless.alloc_from_iter(iter)
567+
} else {
568+
arena.$name.alloc_from_iter(iter)
696569
}
697570
}
698571
}

compiler/rustc_hir/src/arena.rs

+5-10
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,13 @@
11
/// This declares a list of types which can be allocated by `Arena`.
22
///
3-
/// The `few` modifier will cause allocation to use the shared arena and recording the destructor.
4-
/// This is faster and more memory efficient if there's only a few allocations of the type.
5-
/// Leaving `few` out will cause the type to get its own dedicated `TypedArena` which is
6-
/// faster and more memory efficient if there is lots of allocations.
7-
///
83
/// Specifying the `decode` modifier will add decode impls for `&T` and `&[T]`,
94
/// where `T` is the type listed. These impls will appear in the implement_ty_decoder! macro.
105
#[macro_export]
116
macro_rules! arena_types {
127
($macro:path, $tcx:lifetime) => (
138
$macro!([
149
// HIR types
15-
[few] hir_krate: rustc_hir::Crate<$tcx>,
10+
[] hir_krate: rustc_hir::Crate<$tcx>,
1611
[] arm: rustc_hir::Arm<$tcx>,
1712
[] asm_operand: (rustc_hir::InlineAsmOperand<$tcx>, Span),
1813
[] asm_template: rustc_ast::InlineAsmTemplatePiece,
@@ -29,14 +24,14 @@ macro_rules! arena_types {
2924
[] pat_field: rustc_hir::PatField<$tcx>,
3025
[] fn_decl: rustc_hir::FnDecl<$tcx>,
3126
[] foreign_item: rustc_hir::ForeignItem<$tcx>,
32-
[few] foreign_item_ref: rustc_hir::ForeignItemRef,
27+
[] foreign_item_ref: rustc_hir::ForeignItemRef,
3328
[] impl_item: rustc_hir::ImplItem<$tcx>,
3429
[] impl_item_ref: rustc_hir::ImplItemRef,
3530
[] item: rustc_hir::Item<$tcx>,
36-
[few] inline_asm: rustc_hir::InlineAsm<$tcx>,
37-
[few] llvm_inline_asm: rustc_hir::LlvmInlineAsm<$tcx>,
31+
[] inline_asm: rustc_hir::InlineAsm<$tcx>,
32+
[] llvm_inline_asm: rustc_hir::LlvmInlineAsm<$tcx>,
3833
[] local: rustc_hir::Local<$tcx>,
39-
[few] mod_: rustc_hir::Mod<$tcx>,
34+
[] mod_: rustc_hir::Mod<$tcx>,
4035
[] owner_info: rustc_hir::OwnerInfo<$tcx>,
4136
[] param: rustc_hir::Param<$tcx>,
4237
[] pat: rustc_hir::Pat<$tcx>,

compiler/rustc_middle/src/arena.rs

+5-10
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
11
/// This declares a list of types which can be allocated by `Arena`.
22
///
3-
/// The `few` modifier will cause allocation to use the shared arena and recording the destructor.
4-
/// This is faster and more memory efficient if there's only a few allocations of the type.
5-
/// Leaving `few` out will cause the type to get its own dedicated `TypedArena` which is
6-
/// faster and more memory efficient if there is lots of allocations.
7-
///
83
/// Specifying the `decode` modifier will add decode impls for `&T` and `&[T]` where `T` is the type
94
/// listed. These impls will appear in the implement_ty_decoder! macro.
105
#[macro_export]
@@ -37,7 +32,7 @@ macro_rules! arena_types {
3732
[decode] code_region: rustc_middle::mir::coverage::CodeRegion,
3833
[] const_allocs: rustc_middle::mir::interpret::Allocation,
3934
// Required for the incremental on-disk cache
40-
[few] mir_keys: rustc_hir::def_id::DefIdSet,
35+
[] mir_keys: rustc_hir::def_id::DefIdSet,
4136
[] region_scope_tree: rustc_middle::middle::region::ScopeTree,
4237
[] dropck_outlives:
4338
rustc_middle::infer::canonical::Canonical<'tcx,
@@ -77,10 +72,10 @@ macro_rules! arena_types {
7772
rustc_middle::infer::canonical::Canonical<'tcx,
7873
rustc_middle::infer::canonical::QueryResponse<'tcx, rustc_middle::ty::Ty<'tcx>>
7974
>,
80-
[few] all_traits: Vec<rustc_hir::def_id::DefId>,
81-
[few] privacy_access_levels: rustc_middle::middle::privacy::AccessLevels,
82-
[few] foreign_module: rustc_session::cstore::ForeignModule,
83-
[few] foreign_modules: Vec<rustc_session::cstore::ForeignModule>,
75+
[] all_traits: Vec<rustc_hir::def_id::DefId>,
76+
[] privacy_access_levels: rustc_middle::middle::privacy::AccessLevels,
77+
[] foreign_module: rustc_session::cstore::ForeignModule,
78+
[] foreign_modules: Vec<rustc_session::cstore::ForeignModule>,
8479
[] upvars_mentioned: rustc_data_structures::fx::FxIndexMap<rustc_hir::HirId, rustc_hir::Upvar>,
8580
[] object_safety_violations: rustc_middle::traits::ObjectSafetyViolation,
8681
[] codegen_unit: rustc_middle::mir::mono::CodegenUnit<$tcx>,

0 commit comments

Comments
 (0)