Skip to content

Commit e82734e

Browse files
committed
Auto merge of rust-lang#70161 - cjgillot:query-arena, r=nikomatsakis
Allocate some query results on an arena This avoids a cloning few `Lrc` and `Vec`s in the queries.
2 parents 7688266 + 802c0be commit e82734e

File tree

17 files changed

+78
-98
lines changed

17 files changed

+78
-98
lines changed

src/librustc_codegen_llvm/context.rs

+4-5
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ use rustc_target::spec::{HasTargetSpec, Target};
2626
use std::cell::{Cell, RefCell};
2727
use std::ffi::CStr;
2828
use std::str;
29-
use std::sync::Arc;
3029

3130
/// There is one `CodegenCx` per compilation unit. Each one has its own LLVM
3231
/// `llvm::Context` so that several compilation units may be optimized in parallel.
@@ -39,7 +38,7 @@ pub struct CodegenCx<'ll, 'tcx> {
3938

4039
pub llmod: &'ll llvm::Module,
4140
pub llcx: &'ll llvm::Context,
42-
pub codegen_unit: Arc<CodegenUnit<'tcx>>,
41+
pub codegen_unit: &'tcx CodegenUnit<'tcx>,
4342

4443
/// Cache instances of monomorphic and polymorphic items
4544
pub instances: RefCell<FxHashMap<Instance<'tcx>, &'ll Value>>,
@@ -232,7 +231,7 @@ pub unsafe fn create_module(
232231
impl<'ll, 'tcx> CodegenCx<'ll, 'tcx> {
233232
crate fn new(
234233
tcx: TyCtxt<'tcx>,
235-
codegen_unit: Arc<CodegenUnit<'tcx>>,
234+
codegen_unit: &'tcx CodegenUnit<'tcx>,
236235
llvm_module: &'ll crate::ModuleLlvm,
237236
) -> Self {
238237
// An interesting part of Windows which MSVC forces our hand on (and
@@ -402,8 +401,8 @@ impl MiscMethods<'tcx> for CodegenCx<'ll, 'tcx> {
402401
self.check_overflow
403402
}
404403

405-
fn codegen_unit(&self) -> &Arc<CodegenUnit<'tcx>> {
406-
&self.codegen_unit
404+
fn codegen_unit(&self) -> &'tcx CodegenUnit<'tcx> {
405+
self.codegen_unit
407406
}
408407

409408
fn used_statics(&self) -> &RefCell<Vec<&'ll Value>> {

src/librustc_codegen_ssa/back/symbol_export.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
use std::collections::hash_map::Entry::*;
2-
use std::sync::Arc;
32

43
use rustc_ast::expand::allocator::ALLOCATOR_METHODS;
54
use rustc_data_structures::fingerprint::Fingerprint;
@@ -164,11 +163,11 @@ fn is_reachable_non_generic_provider_extern(tcx: TyCtxt<'_>, def_id: DefId) -> b
164163
fn exported_symbols_provider_local(
165164
tcx: TyCtxt<'_>,
166165
cnum: CrateNum,
167-
) -> Arc<Vec<(ExportedSymbol<'_>, SymbolExportLevel)>> {
166+
) -> &'tcx [(ExportedSymbol<'_>, SymbolExportLevel)] {
168167
assert_eq!(cnum, LOCAL_CRATE);
169168

170169
if !tcx.sess.opts.output_types.should_codegen() {
171-
return Arc::new(vec![]);
170+
return &[];
172171
}
173172

174173
let mut symbols: Vec<_> = tcx
@@ -274,7 +273,7 @@ fn exported_symbols_provider_local(
274273
// Sort so we get a stable incr. comp. hash.
275274
symbols.sort_by_cached_key(|s| s.0.symbol_name_for_local_instance(tcx));
276275

277-
Arc::new(symbols)
276+
tcx.arena.alloc_from_iter(symbols)
278277
}
279278

280279
fn upstream_monomorphizations_provider(

src/librustc_codegen_ssa/base.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -533,15 +533,14 @@ pub fn codegen_crate<B: ExtraBackendMethods>(
533533
// Run the monomorphization collector and partition the collected items into
534534
// codegen units.
535535
let codegen_units = tcx.collect_and_partition_mono_items(LOCAL_CRATE).1;
536-
let codegen_units = (*codegen_units).clone();
537536

538537
// Force all codegen_unit queries so they are already either red or green
539538
// when compile_codegen_unit accesses them. We are not able to re-execute
540539
// the codegen_unit query from just the DepNode, so an unknown color would
541540
// lead to having to re-execute compile_codegen_unit, possibly
542541
// unnecessarily.
543542
if tcx.dep_graph.is_fully_enabled() {
544-
for cgu in &codegen_units {
543+
for cgu in codegen_units {
545544
tcx.codegen_unit(cgu.name());
546545
}
547546
}
@@ -603,7 +602,7 @@ pub fn codegen_crate<B: ExtraBackendMethods>(
603602
// We sort the codegen units by size. This way we can schedule work for LLVM
604603
// a bit more efficiently.
605604
let codegen_units = {
606-
let mut codegen_units = codegen_units;
605+
let mut codegen_units = codegen_units.iter().collect::<Vec<_>>();
607606
codegen_units.sort_by_cached_key(|cgu| cmp::Reverse(cgu.size_estimate()));
608607
codegen_units
609608
};

src/librustc_codegen_ssa/traits/misc.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ use rustc_middle::mir::mono::CodegenUnit;
44
use rustc_middle::ty::{self, Instance, Ty};
55
use rustc_session::Session;
66
use std::cell::RefCell;
7-
use std::sync::Arc;
87

98
pub trait MiscMethods<'tcx>: BackendTypes {
109
fn vtables(
@@ -15,7 +14,7 @@ pub trait MiscMethods<'tcx>: BackendTypes {
1514
fn get_fn_addr(&self, instance: Instance<'tcx>) -> Self::Value;
1615
fn eh_personality(&self) -> Self::Value;
1716
fn sess(&self) -> &Session;
18-
fn codegen_unit(&self) -> &Arc<CodegenUnit<'tcx>>;
17+
fn codegen_unit(&self) -> &'tcx CodegenUnit<'tcx>;
1918
fn used_statics(&self) -> &RefCell<Vec<Self::Value>>;
2019
fn set_frame_pointer_elimination(&self, llfn: Self::Function);
2120
fn apply_target_cpu_attr(&self, llfn: Self::Function);

src/librustc_infer/traits/error_reporting/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ pub fn report_object_safety_error(
3939
tcx: TyCtxt<'tcx>,
4040
span: Span,
4141
trait_def_id: DefId,
42-
violations: Vec<ObjectSafetyViolation>,
42+
violations: &[ObjectSafetyViolation],
4343
) -> DiagnosticBuilder<'tcx> {
4444
let trait_str = tcx.def_path_str(trait_def_id);
4545
let trait_span = tcx.hir().get_if_local(trait_def_id).and_then(|node| match node {

src/librustc_metadata/rmeta/decoder.rs

+13-15
Original file line numberDiff line numberDiff line change
@@ -1196,7 +1196,7 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
11961196
}
11971197
}
11981198

1199-
fn get_item_attrs(&self, node_id: DefIndex, sess: &Session) -> Lrc<[ast::Attribute]> {
1199+
fn get_item_attrs(&self, node_id: DefIndex, sess: &Session) -> Vec<ast::Attribute> {
12001200
// The attributes for a tuple struct/variant are attached to the definition, not the ctor;
12011201
// we assume that someone passing in a tuple struct ctor is actually wanting to
12021202
// look at the definition
@@ -1207,15 +1207,13 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
12071207
node_id
12081208
};
12091209

1210-
Lrc::from(
1211-
self.root
1212-
.tables
1213-
.attributes
1214-
.get(self, item_id)
1215-
.unwrap_or(Lazy::empty())
1216-
.decode((self, sess))
1217-
.collect::<Vec<_>>(),
1218-
)
1210+
self.root
1211+
.tables
1212+
.attributes
1213+
.get(self, item_id)
1214+
.unwrap_or(Lazy::empty())
1215+
.decode((self, sess))
1216+
.collect::<Vec<_>>()
12191217
}
12201218

12211219
fn get_struct_field_names(&self, id: DefIndex, sess: &Session) -> Vec<Spanned<ast::Name>> {
@@ -1330,25 +1328,25 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
13301328
}
13311329
}
13321330

1333-
fn get_fn_param_names(&self, id: DefIndex) -> Vec<ast::Name> {
1331+
fn get_fn_param_names(&self, tcx: TyCtxt<'tcx>, id: DefIndex) -> &'tcx [ast::Name] {
13341332
let param_names = match self.kind(id) {
13351333
EntryKind::Fn(data) | EntryKind::ForeignFn(data) => data.decode(self).param_names,
13361334
EntryKind::AssocFn(data) => data.decode(self).fn_data.param_names,
13371335
_ => Lazy::empty(),
13381336
};
1339-
param_names.decode(self).collect()
1337+
tcx.arena.alloc_from_iter(param_names.decode(self))
13401338
}
13411339

13421340
fn exported_symbols(
13431341
&self,
13441342
tcx: TyCtxt<'tcx>,
1345-
) -> Vec<(ExportedSymbol<'tcx>, SymbolExportLevel)> {
1343+
) -> &'tcx [(ExportedSymbol<'tcx>, SymbolExportLevel)] {
13461344
if self.root.is_proc_macro_crate() {
13471345
// If this crate is a custom derive crate, then we're not even going to
13481346
// link those in so we skip those crates.
1349-
vec![]
1347+
&[]
13501348
} else {
1351-
self.root.exported_symbols.decode((self, tcx)).collect()
1349+
tcx.arena.alloc_from_iter(self.root.exported_symbols.decode((self, tcx)))
13521350
}
13531351
}
13541352

src/librustc_metadata/rmeta/decoder/cstore_impl.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ use rustc_span::symbol::Symbol;
2626
use rustc_data_structures::sync::Lrc;
2727
use smallvec::SmallVec;
2828
use std::any::Any;
29-
use std::sync::Arc;
3029

3130
macro_rules! provide {
3231
(<$lt:tt> $tcx:ident, $def_id:ident, $other:ident, $cdata:ident,
@@ -139,12 +138,14 @@ provide! { <'tcx> tcx, def_id, other, cdata,
139138
lookup_deprecation_entry => {
140139
cdata.get_deprecation(def_id.index).map(DeprecationEntry::external)
141140
}
142-
item_attrs => { cdata.get_item_attrs(def_id.index, tcx.sess) }
141+
item_attrs => { tcx.arena.alloc_from_iter(
142+
cdata.get_item_attrs(def_id.index, tcx.sess).into_iter()
143+
) }
143144
// FIXME(#38501) We've skipped a `read` on the `hir_owner_nodes` of
144145
// a `fn` when encoding, so the dep-tracking wouldn't work.
145146
// This is only used by rustdoc anyway, which shouldn't have
146147
// incremental recompilation ever enabled.
147-
fn_arg_names => { cdata.get_fn_param_names(def_id.index) }
148+
fn_arg_names => { cdata.get_fn_param_names(tcx, def_id.index) }
148149
rendered_const => { cdata.get_rendered_const(def_id.index) }
149150
impl_parent => { cdata.get_parent_impl(def_id.index) }
150151
trait_of_item => { cdata.get_trait_of_item(def_id.index) }
@@ -239,7 +240,7 @@ provide! { <'tcx> tcx, def_id, other, cdata,
239240
// to block export of generics from dylibs, but we must fix
240241
// rust-lang/rust#65890 before we can do that robustly.
241242

242-
Arc::new(syms)
243+
syms
243244
}
244245
}
245246

src/librustc_middle/arena.rs

+5
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,11 @@ macro_rules! arena_types {
116116
[few] crate_variances: rustc_middle::ty::CrateVariancesMap<'tcx>,
117117
[few] inferred_outlives_crate: rustc_middle::ty::CratePredicatesMap<'tcx>,
118118
[] upvars: rustc_data_structures::fx::FxIndexMap<rustc_hir::HirId, rustc_hir::Upvar>,
119+
[] object_safety_violations: rustc_middle::traits::ObjectSafetyViolation,
120+
[] codegen_unit: rustc_middle::mir::mono::CodegenUnit<$tcx>,
121+
[] attribute: rustc_ast::ast::Attribute,
122+
[] name_set: rustc_data_structures::fx::FxHashSet<rustc_ast::ast::Name>,
123+
[] hir_id_set: rustc_hir::HirIdSet,
119124

120125
// Interned types
121126
[] tys: rustc_middle::ty::TyS<$tcx>,

src/librustc_middle/query/mod.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -610,7 +610,7 @@ rustc_queries! {
610610
}
611611

612612
Other {
613-
query reachable_set(_: CrateNum) -> Lrc<HirIdSet> {
613+
query reachable_set(_: CrateNum) -> &'tcx HirIdSet {
614614
desc { "reachability" }
615615
}
616616

@@ -642,7 +642,7 @@ rustc_queries! {
642642
query lookup_stability(_: DefId) -> Option<&'tcx attr::Stability> {}
643643
query lookup_const_stability(_: DefId) -> Option<&'tcx attr::ConstStability> {}
644644
query lookup_deprecation_entry(_: DefId) -> Option<DeprecationEntry> {}
645-
query item_attrs(_: DefId) -> Lrc<[ast::Attribute]> {}
645+
query item_attrs(_: DefId) -> &'tcx [ast::Attribute] {}
646646
}
647647

648648
Codegen {
@@ -652,7 +652,7 @@ rustc_queries! {
652652
}
653653

654654
Other {
655-
query fn_arg_names(_: DefId) -> Vec<ast::Name> {}
655+
query fn_arg_names(_: DefId) -> &'tcx [ast::Name] {}
656656
/// Gets the rendered value of the specified constant or associated constant.
657657
/// Used by rustdoc.
658658
query rendered_const(_: DefId) -> String {}
@@ -699,7 +699,7 @@ rustc_queries! {
699699
desc { |tcx| "building specialization graph of trait `{}`", tcx.def_path_str(key) }
700700
cache_on_disk_if { true }
701701
}
702-
query object_safety_violations(key: DefId) -> Vec<traits::ObjectSafetyViolation> {
702+
query object_safety_violations(key: DefId) -> &'tcx [traits::ObjectSafetyViolation] {
703703
desc { |tcx| "determine object safety of trait `{}`", tcx.def_path_str(key) }
704704
}
705705

@@ -1047,7 +1047,7 @@ rustc_queries! {
10471047
desc { "looking up all possibly unused extern crates" }
10481048
}
10491049
query names_imported_by_glob_use(_: DefId)
1050-
-> Lrc<FxHashSet<ast::Name>> {
1050+
-> &'tcx FxHashSet<ast::Name> {
10511051
eval_always
10521052
}
10531053

@@ -1075,19 +1075,19 @@ rustc_queries! {
10751075
/// correspond to a publicly visible symbol in `cnum` machine code.
10761076
/// - The `exported_symbols` sets of different crates do not intersect.
10771077
query exported_symbols(_: CrateNum)
1078-
-> Arc<Vec<(ExportedSymbol<'tcx>, SymbolExportLevel)>> {
1078+
-> &'tcx [(ExportedSymbol<'tcx>, SymbolExportLevel)] {
10791079
desc { "exported_symbols" }
10801080
}
10811081
}
10821082

10831083
Codegen {
10841084
query collect_and_partition_mono_items(_: CrateNum)
1085-
-> (Arc<DefIdSet>, Arc<Vec<Arc<CodegenUnit<'tcx>>>>) {
1085+
-> (&'tcx DefIdSet, &'tcx [CodegenUnit<'tcx>]) {
10861086
eval_always
10871087
desc { "collect_and_partition_mono_items" }
10881088
}
10891089
query is_codegened_item(_: DefId) -> bool {}
1090-
query codegen_unit(_: Symbol) -> Arc<CodegenUnit<'tcx>> {
1090+
query codegen_unit(_: Symbol) -> &'tcx CodegenUnit<'tcx> {
10911091
desc { "codegen_unit" }
10921092
}
10931093
query backend_optimization_level(_: CrateNum) -> OptLevel {

src/librustc_middle/ty/context.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2723,7 +2723,7 @@ pub fn provide(providers: &mut ty::query::Providers<'_>) {
27232723
};
27242724
providers.names_imported_by_glob_use = |tcx, id| {
27252725
assert_eq!(id.krate, LOCAL_CRATE);
2726-
Lrc::new(tcx.glob_map.get(&id).cloned().unwrap_or_default())
2726+
tcx.arena.alloc(tcx.glob_map.get(&id).cloned().unwrap_or_default())
27272727
};
27282728

27292729
providers.lookup_stability = |tcx, id| {

src/librustc_middle/ty/mod.rs

+4-19
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ use rustc_data_structures::fx::FxHashMap;
2828
use rustc_data_structures::fx::FxIndexMap;
2929
use rustc_data_structures::sorted_map::SortedIndexMultiMap;
3030
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
31-
use rustc_data_structures::sync::{self, par_iter, Lrc, ParallelIterator};
31+
use rustc_data_structures::sync::{self, par_iter, ParallelIterator};
3232
use rustc_hir as hir;
3333
use rustc_hir::def::{CtorKind, CtorOf, DefKind, Namespace, Res};
3434
use rustc_hir::def_id::{CrateNum, DefId, DefIdMap, LocalDefId, CRATE_DEF_INDEX};
@@ -2596,22 +2596,7 @@ impl BorrowKind {
25962596
}
25972597
}
25982598

2599-
#[derive(Debug, Clone)]
2600-
pub enum Attributes<'tcx> {
2601-
Owned(Lrc<[ast::Attribute]>),
2602-
Borrowed(&'tcx [ast::Attribute]),
2603-
}
2604-
2605-
impl<'tcx> ::std::ops::Deref for Attributes<'tcx> {
2606-
type Target = [ast::Attribute];
2607-
2608-
fn deref(&self) -> &[ast::Attribute] {
2609-
match self {
2610-
&Attributes::Owned(ref data) => &data,
2611-
&Attributes::Borrowed(data) => data,
2612-
}
2613-
}
2614-
}
2599+
pub type Attributes<'tcx> = &'tcx [ast::Attribute];
26152600

26162601
#[derive(Debug, PartialEq, Eq)]
26172602
pub enum ImplOverlapKind {
@@ -2847,9 +2832,9 @@ impl<'tcx> TyCtxt<'tcx> {
28472832
/// Gets the attributes of a definition.
28482833
pub fn get_attrs(self, did: DefId) -> Attributes<'tcx> {
28492834
if let Some(id) = self.hir().as_local_hir_id(did) {
2850-
Attributes::Borrowed(self.hir().attrs(id))
2835+
self.hir().attrs(id)
28512836
} else {
2852-
Attributes::Owned(self.item_attrs(did))
2837+
self.item_attrs(did)
28532838
}
28542839
}
28552840

0 commit comments

Comments
 (0)