Skip to content

Commit e0e9c86

Browse files
authored
Rollup merge of rust-lang#59545 - Zoxc:the-arena-3, r=eddyb,michaelwoerister
Use arenas to avoid Lrc in queries rust-lang#2 The `Remove subtle Default impl for Value` makes the compilation stop due earlier due to cycle errors, since there's no longer a default value to continue the compilation with. Based on rust-lang#59540.
2 parents 27cc0db + d46e732 commit e0e9c86

File tree

41 files changed

+355
-352
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+355
-352
lines changed

Cargo.lock

+1
Original file line numberDiff line numberDiff line change
@@ -2877,6 +2877,7 @@ dependencies = [
28772877
"rustc_errors 0.0.0",
28782878
"rustc_target 0.0.0",
28792879
"serialize 0.0.0",
2880+
"smallvec 0.6.7 (registry+https://github.com/rust-lang/crates.io-index)",
28802881
"stable_deref_trait 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
28812882
"syntax 0.0.0",
28822883
"syntax_ext 0.0.0",

src/libarena/lib.rs

+24-5
Original file line numberDiff line numberDiff line change
@@ -486,9 +486,31 @@ impl DroplessArena {
486486
}
487487
}
488488

489+
#[inline]
490+
unsafe fn write_from_iter<T, I: Iterator<Item = T>>(
491+
&self,
492+
mut iter: I,
493+
len: usize,
494+
mem: *mut T,
495+
) -> &mut [T] {
496+
let mut i = 0;
497+
// Use a manual loop since LLVM manages to optimize it better for
498+
// slice iterators
499+
loop {
500+
let value = iter.next();
501+
if i >= len || value.is_none() {
502+
// We only return as many items as the iterator gave us, even
503+
// though it was supposed to give us `len`
504+
return slice::from_raw_parts_mut(mem, i);
505+
}
506+
ptr::write(mem.offset(i as isize), value.unwrap());
507+
i += 1;
508+
}
509+
}
510+
489511
#[inline]
490512
pub fn alloc_from_iter<T, I: IntoIterator<Item = T>>(&self, iter: I) -> &mut [T] {
491-
let mut iter = iter.into_iter();
513+
let iter = iter.into_iter();
492514
assert!(mem::size_of::<T>() != 0);
493515
assert!(!mem::needs_drop::<T>());
494516

@@ -505,10 +527,7 @@ impl DroplessArena {
505527
let size = len.checked_mul(mem::size_of::<T>()).unwrap();
506528
let mem = self.alloc_raw(size, mem::align_of::<T>()) as *mut _ as *mut T;
507529
unsafe {
508-
for i in 0..len {
509-
ptr::write(mem.offset(i as isize), iter.next().unwrap())
510-
}
511-
slice::from_raw_parts_mut(mem, len)
530+
self.write_from_iter(iter, len, mem)
512531
}
513532
}
514533
(_, _) => {

src/librustc/arena.rs

+45-2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,15 @@ use std::cell::RefCell;
66
use std::marker::PhantomData;
77
use smallvec::SmallVec;
88

9+
/// This declares a list of types which can be allocated by `Arena`.
10+
///
11+
/// The `few` modifier will cause allocation to use the shared arena and recording the destructor.
12+
/// This is faster and more memory efficient if there's only a few allocations of the type.
13+
/// Leaving `few` out will cause the type to get its own dedicated `TypedArena` which is
14+
/// faster and more memory efficient if there is lots of allocations.
15+
///
16+
/// Specifying the `decode` modifier will add decode impls for &T and &[T] where T is the type
17+
/// listed. These impls will appear in the implement_ty_decoder! macro.
918
#[macro_export]
1019
macro_rules! arena_types {
1120
($macro:path, $args:tt, $tcx:lifetime) => (
@@ -14,7 +23,7 @@ macro_rules! arena_types {
1423
rustc::hir::def_id::DefId,
1524
rustc::ty::subst::SubstsRef<$tcx>
1625
)>,
17-
[few] mir_keys: rustc::util::nodemap::DefIdSet,
26+
[few, decode] mir_keys: rustc::util::nodemap::DefIdSet,
1827
[decode] specialization_graph: rustc::traits::specialization_graph::Graph,
1928
[] region_scope_tree: rustc::middle::region::ScopeTree,
2029
[] item_local_set: rustc::util::nodemap::ItemLocalSet,
@@ -58,6 +67,40 @@ macro_rules! arena_types {
5867
rustc::infer::canonical::Canonical<'tcx,
5968
rustc::infer::canonical::QueryResponse<'tcx, rustc::ty::Ty<'tcx>>
6069
>,
70+
[few] crate_inherent_impls: rustc::ty::CrateInherentImpls,
71+
[decode] borrowck: rustc::middle::borrowck::BorrowCheckResult,
72+
[few] upstream_monomorphizations:
73+
rustc::util::nodemap::DefIdMap<
74+
rustc_data_structures::fx::FxHashMap<
75+
rustc::ty::subst::SubstsRef<'tcx>,
76+
rustc::hir::def_id::CrateNum
77+
>
78+
>,
79+
[few] resolve_lifetimes: rustc::middle::resolve_lifetime::ResolveLifetimes,
80+
[decode] generic_predicates: rustc::ty::GenericPredicates<'tcx>,
81+
[few] lint_levels: rustc::lint::LintLevelMap,
82+
[few] stability_index: rustc::middle::stability::Index<'tcx>,
83+
[few] features: syntax::feature_gate::Features,
84+
[few] all_traits: Vec<rustc::hir::def_id::DefId>,
85+
[few] privacy_access_levels: rustc::middle::privacy::AccessLevels,
86+
[few] target_features_whitelist: rustc_data_structures::fx::FxHashMap<
87+
String,
88+
Option<syntax::symbol::Symbol>
89+
>,
90+
[few] wasm_import_module_map: rustc_data_structures::fx::FxHashMap<
91+
rustc::hir::def_id::DefId,
92+
String
93+
>,
94+
[few] get_lib_features: rustc::middle::lib_features::LibFeatures,
95+
[few] defined_lib_features: rustc::middle::lang_items::LanguageItems,
96+
[few] visible_parent_map: rustc::util::nodemap::DefIdMap<rustc::hir::def_id::DefId>,
97+
[few] foreign_module: rustc::middle::cstore::ForeignModule,
98+
[few] foreign_modules: Vec<rustc::middle::cstore::ForeignModule>,
99+
[few] reachable_non_generics: rustc::util::nodemap::DefIdMap<
100+
rustc::middle::exported_symbols::SymbolExportLevel
101+
>,
102+
[few] crate_variances: rustc::ty::CrateVariancesMap<'tcx>,
103+
[few] inferred_outlives_crate: rustc::ty::CratePredicatesMap<'tcx>,
61104
], $tcx);
62105
)
63106
}
@@ -119,7 +162,7 @@ pub trait ArenaAllocatable {}
119162

120163
impl<T: Copy> ArenaAllocatable for T {}
121164

122-
pub unsafe trait ArenaField<'tcx>: Sized {
165+
unsafe trait ArenaField<'tcx>: Sized {
123166
/// Returns a specific arena to allocate from.
124167
/// If None is returned, the DropArena will be used.
125168
fn arena<'a>(arena: &'a Arena<'tcx>) -> Option<&'a TypedArena<Self>>;

src/librustc/lint/mod.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
pub use self::Level::*;
2222
pub use self::LintSource::*;
2323

24-
use rustc_data_structures::sync::{self, Lrc};
24+
use rustc_data_structures::sync;
2525

2626
use crate::hir::def_id::{CrateNum, LOCAL_CRATE};
2727
use crate::hir::intravisit;
@@ -767,7 +767,7 @@ pub fn maybe_lint_level_root(tcx: TyCtxt<'_, '_, '_>, id: hir::HirId) -> bool {
767767
}
768768

769769
fn lint_levels<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, cnum: CrateNum)
770-
-> Lrc<LintLevelMap>
770+
-> &'tcx LintLevelMap
771771
{
772772
assert_eq!(cnum, LOCAL_CRATE);
773773
let mut builder = LintLevelMapBuilder {
@@ -784,7 +784,7 @@ fn lint_levels<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, cnum: CrateNum)
784784
intravisit::walk_crate(&mut builder, krate);
785785
builder.levels.pop(push);
786786

787-
Lrc::new(builder.levels.build_map())
787+
tcx.arena.alloc(builder.levels.build_map())
788788
}
789789

790790
struct LintLevelMapBuilder<'a, 'tcx: 'a> {

src/librustc/middle/cstore.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -256,8 +256,8 @@ pub fn used_crates(tcx: TyCtxt<'_, '_, '_>, prefer: LinkagePreference)
256256
Some((cnum, path))
257257
})
258258
.collect::<Vec<_>>();
259-
let mut ordering = tcx.postorder_cnums(LOCAL_CRATE);
260-
Lrc::make_mut(&mut ordering).reverse();
259+
let mut ordering = tcx.postorder_cnums(LOCAL_CRATE).to_owned();
260+
ordering.reverse();
261261
libs.sort_by_cached_key(|&(a, _)| {
262262
ordering.iter().position(|x| *x == a)
263263
});

src/librustc/middle/resolve_lifetime.rs

+9-14
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ use crate::rustc::lint;
1515
use crate::session::Session;
1616
use crate::util::nodemap::{DefIdMap, FxHashMap, FxHashSet, HirIdMap, HirIdSet};
1717
use errors::{Applicability, DiagnosticBuilder};
18-
use rustc_data_structures::sync::Lrc;
1918
use rustc_macros::HashStable;
2019
use std::borrow::Cow;
2120
use std::cell::Cell;
@@ -211,10 +210,10 @@ struct NamedRegionMap {
211210
/// See [`NamedRegionMap`].
212211
#[derive(Default)]
213212
pub struct ResolveLifetimes {
214-
defs: FxHashMap<LocalDefId, Lrc<FxHashMap<ItemLocalId, Region>>>,
215-
late_bound: FxHashMap<LocalDefId, Lrc<FxHashSet<ItemLocalId>>>,
213+
defs: FxHashMap<LocalDefId, FxHashMap<ItemLocalId, Region>>,
214+
late_bound: FxHashMap<LocalDefId, FxHashSet<ItemLocalId>>,
216215
object_lifetime_defaults:
217-
FxHashMap<LocalDefId, Lrc<FxHashMap<ItemLocalId, Lrc<Vec<ObjectLifetimeDefault>>>>>,
216+
FxHashMap<LocalDefId, FxHashMap<ItemLocalId, Vec<ObjectLifetimeDefault>>>,
218217
}
219218

220219
impl_stable_hash_for!(struct crate::middle::resolve_lifetime::ResolveLifetimes {
@@ -347,23 +346,21 @@ pub fn provide(providers: &mut ty::query::Providers<'_>) {
347346

348347
named_region_map: |tcx, id| {
349348
let id = LocalDefId::from_def_id(DefId::local(id)); // (*)
350-
tcx.resolve_lifetimes(LOCAL_CRATE).defs.get(&id).cloned()
349+
tcx.resolve_lifetimes(LOCAL_CRATE).defs.get(&id)
351350
},
352351

353352
is_late_bound_map: |tcx, id| {
354353
let id = LocalDefId::from_def_id(DefId::local(id)); // (*)
355354
tcx.resolve_lifetimes(LOCAL_CRATE)
356355
.late_bound
357356
.get(&id)
358-
.cloned()
359357
},
360358

361359
object_lifetime_defaults_map: |tcx, id| {
362360
let id = LocalDefId::from_def_id(DefId::local(id)); // (*)
363361
tcx.resolve_lifetimes(LOCAL_CRATE)
364362
.object_lifetime_defaults
365363
.get(&id)
366-
.cloned()
367364
},
368365

369366
..*providers
@@ -379,7 +376,7 @@ pub fn provide(providers: &mut ty::query::Providers<'_>) {
379376
fn resolve_lifetimes<'tcx>(
380377
tcx: TyCtxt<'_, 'tcx, 'tcx>,
381378
for_krate: CrateNum,
382-
) -> Lrc<ResolveLifetimes> {
379+
) -> &'tcx ResolveLifetimes {
383380
assert_eq!(for_krate, LOCAL_CRATE);
384381

385382
let named_region_map = krate(tcx);
@@ -388,24 +385,22 @@ fn resolve_lifetimes<'tcx>(
388385

389386
for (hir_id, v) in named_region_map.defs {
390387
let map = rl.defs.entry(hir_id.owner_local_def_id()).or_default();
391-
Lrc::get_mut(map).unwrap().insert(hir_id.local_id, v);
388+
map.insert(hir_id.local_id, v);
392389
}
393390
for hir_id in named_region_map.late_bound {
394391
let map = rl.late_bound
395392
.entry(hir_id.owner_local_def_id())
396393
.or_default();
397-
Lrc::get_mut(map).unwrap().insert(hir_id.local_id);
394+
map.insert(hir_id.local_id);
398395
}
399396
for (hir_id, v) in named_region_map.object_lifetime_defaults {
400397
let map = rl.object_lifetime_defaults
401398
.entry(hir_id.owner_local_def_id())
402399
.or_default();
403-
Lrc::get_mut(map)
404-
.unwrap()
405-
.insert(hir_id.local_id, Lrc::new(v));
400+
map.insert(hir_id.local_id, v);
406401
}
407402

408-
Lrc::new(rl)
403+
tcx.arena.alloc(rl)
409404
}
410405

411406
fn krate<'tcx>(tcx: TyCtxt<'_, 'tcx, 'tcx>) -> NamedRegionMap {

src/librustc/middle/stability.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -883,7 +883,7 @@ pub fn check_unused_or_stable_features<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>) {
883883
remaining_lib_features.remove(&Symbol::intern("test"));
884884

885885
let check_features =
886-
|remaining_lib_features: &mut FxHashMap<_, _>, defined_features: &Vec<_>| {
886+
|remaining_lib_features: &mut FxHashMap<_, _>, defined_features: &[_]| {
887887
for &(feature, since) in defined_features {
888888
if let Some(since) = since {
889889
if let Some(span) = remaining_lib_features.get(&feature) {
@@ -908,7 +908,7 @@ pub fn check_unused_or_stable_features<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>) {
908908
if remaining_lib_features.is_empty() {
909909
break;
910910
}
911-
check_features(&mut remaining_lib_features, &tcx.defined_lib_features(cnum));
911+
check_features(&mut remaining_lib_features, tcx.defined_lib_features(cnum));
912912
}
913913
}
914914

0 commit comments

Comments
 (0)