Skip to content

Commit 1576a7c

Browse files
authoredNov 19, 2021
Rollup merge of #90990 - nnethercote:arenas-cleanup, r=oli-obk
Arenas cleanup I was looking closely at the arenas code and here are some small improvement to readability.
2 parents c74ff8b + 0a89598 commit 1576a7c

File tree

5 files changed

+80
-71
lines changed

5 files changed

+80
-71
lines changed
 

‎compiler/rustc_arena/src/lib.rs

+17-8
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ impl<T> Default for TypedArena<T> {
111111
// alloc() will trigger a grow().
112112
ptr: Cell::new(ptr::null_mut()),
113113
end: Cell::new(ptr::null_mut()),
114-
chunks: RefCell::new(vec![]),
114+
chunks: Default::default(),
115115
_own: PhantomData,
116116
}
117117
}
@@ -325,13 +325,17 @@ unsafe impl<#[may_dangle] T> Drop for TypedArena<T> {
325325

326326
unsafe impl<T: Send> Send for TypedArena<T> {}
327327

328+
/// An arena that can hold objects of multiple different types that impl `Copy`
329+
/// and/or satisfy `!mem::needs_drop`.
328330
pub struct DroplessArena {
329331
/// A pointer to the start of the free space.
330332
start: Cell<*mut u8>,
331333

332334
/// A pointer to the end of free space.
333335
///
334-
/// The allocation proceeds from the end of the chunk towards the start.
336+
/// The allocation proceeds downwards from the end of the chunk towards the
337+
/// start. (This is slightly simpler and faster than allocating upwards,
338+
/// see <https://fitzgeraldnick.com/2019/11/01/always-bump-downwards.html>.)
335339
/// When this pointer crosses the start pointer, a new chunk is allocated.
336340
end: Cell<*mut u8>,
337341

@@ -516,10 +520,14 @@ impl DroplessArena {
516520
}
517521
}
518522

523+
// Declare an `Arena` containing one dropless arena and many typed arenas (the
524+
// types of the typed arenas are specified by the arguments). The dropless
525+
// arena will be used for any types that impl `Copy`, and also for any of the
526+
// specified types that satisfy `!mem::needs_drop`.
519527
#[rustc_macro_transparency = "semitransparent"]
520-
pub macro declare_arena([$($a:tt $name:ident: $ty:ty,)*], $tcx:lifetime) {
528+
pub macro declare_arena([$($a:tt $name:ident: $ty:ty,)*]) {
521529
#[derive(Default)]
522-
pub struct Arena<$tcx> {
530+
pub struct Arena<'tcx> {
523531
pub dropless: $crate::DroplessArena,
524532
$($name: $crate::TypedArena<$ty>,)*
525533
}
@@ -532,6 +540,7 @@ pub macro declare_arena([$($a:tt $name:ident: $ty:ty,)*], $tcx:lifetime) {
532540
) -> &'a mut [Self];
533541
}
534542

543+
// Any type that impls `Copy` can be arena-allocated in the `DroplessArena`.
535544
impl<'tcx, T: Copy> ArenaAllocatable<'tcx, ()> for T {
536545
#[inline]
537546
fn allocate_on<'a>(self, arena: &'a Arena<'tcx>) -> &'a mut Self {
@@ -544,12 +553,11 @@ pub macro declare_arena([$($a:tt $name:ident: $ty:ty,)*], $tcx:lifetime) {
544553
) -> &'a mut [Self] {
545554
arena.dropless.alloc_from_iter(iter)
546555
}
547-
548556
}
549557
$(
550-
impl<$tcx> ArenaAllocatable<$tcx, $ty> for $ty {
558+
impl<'tcx> ArenaAllocatable<'tcx, $ty> for $ty {
551559
#[inline]
552-
fn allocate_on<'a>(self, arena: &'a Arena<$tcx>) -> &'a mut Self {
560+
fn allocate_on<'a>(self, arena: &'a Arena<'tcx>) -> &'a mut Self {
553561
if !::std::mem::needs_drop::<Self>() {
554562
arena.dropless.alloc(self)
555563
} else {
@@ -559,7 +567,7 @@ pub macro declare_arena([$($a:tt $name:ident: $ty:ty,)*], $tcx:lifetime) {
559567

560568
#[inline]
561569
fn allocate_from_iter<'a>(
562-
arena: &'a Arena<$tcx>,
570+
arena: &'a Arena<'tcx>,
563571
iter: impl ::std::iter::IntoIterator<Item = Self>,
564572
) -> &'a mut [Self] {
565573
if !::std::mem::needs_drop::<Self>() {
@@ -577,6 +585,7 @@ pub macro declare_arena([$($a:tt $name:ident: $ty:ty,)*], $tcx:lifetime) {
577585
value.allocate_on(self)
578586
}
579587

588+
// Any type that impls `Copy` can have slices be arena-allocated in the `DroplessArena`.
580589
#[inline]
581590
pub fn alloc_slice<T: ::std::marker::Copy>(&self, value: &[T]) -> &mut [T] {
582591
if value.is_empty() {

‎compiler/rustc_ast_lowering/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ mod item;
8484
mod pat;
8585
mod path;
8686

87-
rustc_hir::arena_types!(rustc_arena::declare_arena, 'tcx);
87+
rustc_hir::arena_types!(rustc_arena::declare_arena);
8888

8989
struct LoweringContext<'a, 'hir: 'a> {
9090
/// Used to assign IDs to HIR nodes that do not directly correspond to AST nodes.

‎compiler/rustc_hir/src/arena.rs

+38-38
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,52 @@
1-
/// This declares a list of types which can be allocated by `Arena`.
1+
/// This higher-order macro declares a list of types which can be allocated by `Arena`.
22
///
33
/// Specifying the `decode` modifier will add decode impls for `&T` and `&[T]`,
44
/// where `T` is the type listed. These impls will appear in the implement_ty_decoder! macro.
55
#[macro_export]
66
macro_rules! arena_types {
7-
($macro:path, $tcx:lifetime) => (
7+
($macro:path) => (
88
$macro!([
99
// HIR types
10-
[] hir_krate: rustc_hir::Crate<$tcx>,
11-
[] arm: rustc_hir::Arm<$tcx>,
12-
[] asm_operand: (rustc_hir::InlineAsmOperand<$tcx>, Span),
10+
[] hir_krate: rustc_hir::Crate<'tcx>,
11+
[] arm: rustc_hir::Arm<'tcx>,
12+
[] asm_operand: (rustc_hir::InlineAsmOperand<'tcx>, Span),
1313
[] asm_template: rustc_ast::InlineAsmTemplatePiece,
1414
[] attribute: rustc_ast::Attribute,
15-
[] block: rustc_hir::Block<$tcx>,
16-
[] bare_fn_ty: rustc_hir::BareFnTy<$tcx>,
17-
[] body: rustc_hir::Body<$tcx>,
18-
[] generic_arg: rustc_hir::GenericArg<$tcx>,
19-
[] generic_args: rustc_hir::GenericArgs<$tcx>,
20-
[] generic_bound: rustc_hir::GenericBound<$tcx>,
21-
[] generic_param: rustc_hir::GenericParam<$tcx>,
22-
[] expr: rustc_hir::Expr<$tcx>,
23-
[] expr_field: rustc_hir::ExprField<$tcx>,
24-
[] pat_field: rustc_hir::PatField<$tcx>,
25-
[] fn_decl: rustc_hir::FnDecl<$tcx>,
26-
[] foreign_item: rustc_hir::ForeignItem<$tcx>,
15+
[] block: rustc_hir::Block<'tcx>,
16+
[] bare_fn_ty: rustc_hir::BareFnTy<'tcx>,
17+
[] body: rustc_hir::Body<'tcx>,
18+
[] generic_arg: rustc_hir::GenericArg<'tcx>,
19+
[] generic_args: rustc_hir::GenericArgs<'tcx>,
20+
[] generic_bound: rustc_hir::GenericBound<'tcx>,
21+
[] generic_param: rustc_hir::GenericParam<'tcx>,
22+
[] expr: rustc_hir::Expr<'tcx>,
23+
[] expr_field: rustc_hir::ExprField<'tcx>,
24+
[] pat_field: rustc_hir::PatField<'tcx>,
25+
[] fn_decl: rustc_hir::FnDecl<'tcx>,
26+
[] foreign_item: rustc_hir::ForeignItem<'tcx>,
2727
[] foreign_item_ref: rustc_hir::ForeignItemRef,
28-
[] impl_item: rustc_hir::ImplItem<$tcx>,
28+
[] impl_item: rustc_hir::ImplItem<'tcx>,
2929
[] impl_item_ref: rustc_hir::ImplItemRef,
30-
[] item: rustc_hir::Item<$tcx>,
31-
[] inline_asm: rustc_hir::InlineAsm<$tcx>,
32-
[] llvm_inline_asm: rustc_hir::LlvmInlineAsm<$tcx>,
33-
[] local: rustc_hir::Local<$tcx>,
34-
[] mod_: rustc_hir::Mod<$tcx>,
35-
[] owner_info: rustc_hir::OwnerInfo<$tcx>,
36-
[] param: rustc_hir::Param<$tcx>,
37-
[] pat: rustc_hir::Pat<$tcx>,
38-
[] path: rustc_hir::Path<$tcx>,
39-
[] path_segment: rustc_hir::PathSegment<$tcx>,
40-
[] poly_trait_ref: rustc_hir::PolyTraitRef<$tcx>,
41-
[] qpath: rustc_hir::QPath<$tcx>,
42-
[] stmt: rustc_hir::Stmt<$tcx>,
43-
[] field_def: rustc_hir::FieldDef<$tcx>,
44-
[] trait_item: rustc_hir::TraitItem<$tcx>,
30+
[] item: rustc_hir::Item<'tcx>,
31+
[] inline_asm: rustc_hir::InlineAsm<'tcx>,
32+
[] llvm_inline_asm: rustc_hir::LlvmInlineAsm<'tcx>,
33+
[] local: rustc_hir::Local<'tcx>,
34+
[] mod_: rustc_hir::Mod<'tcx>,
35+
[] owner_info: rustc_hir::OwnerInfo<'tcx>,
36+
[] param: rustc_hir::Param<'tcx>,
37+
[] pat: rustc_hir::Pat<'tcx>,
38+
[] path: rustc_hir::Path<'tcx>,
39+
[] path_segment: rustc_hir::PathSegment<'tcx>,
40+
[] poly_trait_ref: rustc_hir::PolyTraitRef<'tcx>,
41+
[] qpath: rustc_hir::QPath<'tcx>,
42+
[] stmt: rustc_hir::Stmt<'tcx>,
43+
[] field_def: rustc_hir::FieldDef<'tcx>,
44+
[] trait_item: rustc_hir::TraitItem<'tcx>,
4545
[] trait_item_ref: rustc_hir::TraitItemRef,
46-
[] ty: rustc_hir::Ty<$tcx>,
47-
[] type_binding: rustc_hir::TypeBinding<$tcx>,
48-
[] variant: rustc_hir::Variant<$tcx>,
49-
[] where_predicate: rustc_hir::WherePredicate<$tcx>,
50-
], $tcx);
46+
[] ty: rustc_hir::Ty<'tcx>,
47+
[] type_binding: rustc_hir::TypeBinding<'tcx>,
48+
[] variant: rustc_hir::Variant<'tcx>,
49+
[] where_predicate: rustc_hir::WherePredicate<'tcx>,
50+
]);
5151
)
5252
}

‎compiler/rustc_middle/src/arena.rs

+15-15
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,33 @@
1-
/// This declares a list of types which can be allocated by `Arena`.
1+
/// This higher-order macro declares a list of types which can be allocated by `Arena`.
22
///
33
/// Specifying the `decode` modifier will add decode impls for `&T` and `&[T]` where `T` is the type
44
/// listed. These impls will appear in the implement_ty_decoder! macro.
55
#[macro_export]
66
macro_rules! arena_types {
7-
($macro:path, $tcx:lifetime) => (
7+
($macro:path) => (
88
$macro!([
99
[] layout: rustc_target::abi::Layout,
10-
[] fn_abi: rustc_target::abi::call::FnAbi<$tcx, rustc_middle::ty::Ty<$tcx>>,
10+
[] fn_abi: rustc_target::abi::call::FnAbi<'tcx, rustc_middle::ty::Ty<'tcx>>,
1111
// AdtDef are interned and compared by address
1212
[] adt_def: rustc_middle::ty::AdtDef,
13-
[] steal_thir: rustc_data_structures::steal::Steal<rustc_middle::thir::Thir<$tcx>>,
14-
[] steal_mir: rustc_data_structures::steal::Steal<rustc_middle::mir::Body<$tcx>>,
15-
[decode] mir: rustc_middle::mir::Body<$tcx>,
13+
[] steal_thir: rustc_data_structures::steal::Steal<rustc_middle::thir::Thir<'tcx>>,
14+
[] steal_mir: rustc_data_structures::steal::Steal<rustc_middle::mir::Body<'tcx>>,
15+
[decode] mir: rustc_middle::mir::Body<'tcx>,
1616
[] steal_promoted:
1717
rustc_data_structures::steal::Steal<
1818
rustc_index::vec::IndexVec<
1919
rustc_middle::mir::Promoted,
20-
rustc_middle::mir::Body<$tcx>
20+
rustc_middle::mir::Body<'tcx>
2121
>
2222
>,
2323
[decode] promoted:
2424
rustc_index::vec::IndexVec<
2525
rustc_middle::mir::Promoted,
26-
rustc_middle::mir::Body<$tcx>
26+
rustc_middle::mir::Body<'tcx>
2727
>,
28-
[decode] typeck_results: rustc_middle::ty::TypeckResults<$tcx>,
28+
[decode] typeck_results: rustc_middle::ty::TypeckResults<'tcx>,
2929
[decode] borrowck_result:
30-
rustc_middle::mir::BorrowCheckResult<$tcx>,
30+
rustc_middle::mir::BorrowCheckResult<'tcx>,
3131
[decode] unsafety_check_result: rustc_middle::mir::UnsafetyCheckResult,
3232
[decode] code_region: rustc_middle::mir::coverage::CodeRegion,
3333
[] const_allocs: rustc_middle::mir::interpret::Allocation,
@@ -78,14 +78,14 @@ macro_rules! arena_types {
7878
[] foreign_modules: Vec<rustc_session::cstore::ForeignModule>,
7979
[] upvars_mentioned: rustc_data_structures::fx::FxIndexMap<rustc_hir::HirId, rustc_hir::Upvar>,
8080
[] object_safety_violations: rustc_middle::traits::ObjectSafetyViolation,
81-
[] codegen_unit: rustc_middle::mir::mono::CodegenUnit<$tcx>,
81+
[] codegen_unit: rustc_middle::mir::mono::CodegenUnit<'tcx>,
8282
[] attribute: rustc_ast::Attribute,
8383
[] name_set: rustc_data_structures::fx::FxHashSet<rustc_span::symbol::Symbol>,
8484
[] hir_id_set: rustc_hir::HirIdSet,
8585

8686
// Interned types
87-
[] tys: rustc_middle::ty::TyS<$tcx>,
88-
[] predicates: rustc_middle::ty::PredicateInner<$tcx>,
87+
[] tys: rustc_middle::ty::TyS<'tcx>,
88+
[] predicates: rustc_middle::ty::PredicateInner<'tcx>,
8989

9090
// Note that this deliberately duplicates items in the `rustc_hir::arena`,
9191
// since we need to allocate this type on both the `rustc_hir` arena
@@ -97,8 +97,8 @@ macro_rules! arena_types {
9797
[decode] used_trait_imports: rustc_data_structures::fx::FxHashSet<rustc_hir::def_id::LocalDefId>,
9898

9999
[] dep_kind: rustc_middle::dep_graph::DepKindStruct,
100-
], $tcx);
100+
]);
101101
)
102102
}
103103

104-
arena_types!(rustc_arena::declare_arena, 'tcx);
104+
arena_types!(rustc_arena::declare_arena);

‎compiler/rustc_middle/src/ty/codec.rs

+9-9
Original file line numberDiff line numberDiff line change
@@ -417,17 +417,17 @@ macro_rules! __impl_decoder_methods {
417417
macro_rules! impl_arena_allocatable_decoder {
418418
([]$args:tt) => {};
419419
([decode $(, $attrs:ident)*]
420-
[[$name:ident: $ty:ty], $tcx:lifetime]) => {
421-
impl<$tcx, D: TyDecoder<$tcx>> RefDecodable<$tcx, D> for $ty {
420+
[$name:ident: $ty:ty]) => {
421+
impl<'tcx, D: TyDecoder<'tcx>> RefDecodable<'tcx, D> for $ty {
422422
#[inline]
423-
fn decode(decoder: &mut D) -> Result<&$tcx Self, D::Error> {
423+
fn decode(decoder: &mut D) -> Result<&'tcx Self, D::Error> {
424424
decode_arena_allocable(decoder)
425425
}
426426
}
427427

428-
impl<$tcx, D: TyDecoder<$tcx>> RefDecodable<$tcx, D> for [$ty] {
428+
impl<'tcx, D: TyDecoder<'tcx>> RefDecodable<'tcx, D> for [$ty] {
429429
#[inline]
430-
fn decode(decoder: &mut D) -> Result<&$tcx Self, D::Error> {
430+
fn decode(decoder: &mut D) -> Result<&'tcx Self, D::Error> {
431431
decode_arena_allocable_slice(decoder)
432432
}
433433
}
@@ -438,15 +438,15 @@ macro_rules! impl_arena_allocatable_decoder {
438438
}
439439

440440
macro_rules! impl_arena_allocatable_decoders {
441-
([$($a:tt $name:ident: $ty:ty,)*], $tcx:lifetime) => {
441+
([$($a:tt $name:ident: $ty:ty,)*]) => {
442442
$(
443-
impl_arena_allocatable_decoder!($a [[$name: $ty], $tcx]);
443+
impl_arena_allocatable_decoder!($a [$name: $ty]);
444444
)*
445445
}
446446
}
447447

448-
rustc_hir::arena_types!(impl_arena_allocatable_decoders, 'tcx);
449-
arena_types!(impl_arena_allocatable_decoders, 'tcx);
448+
rustc_hir::arena_types!(impl_arena_allocatable_decoders);
449+
arena_types!(impl_arena_allocatable_decoders);
450450

451451
#[macro_export]
452452
macro_rules! implement_ty_decoder {

0 commit comments

Comments
 (0)
Please sign in to comment.