Skip to content

Commit 7ee153e

Browse files
committed
Auto merge of #49645 - birkenfeld:unsized_rcs, r=<try>
Use Rc<str> and Rc<[T]> instead of Rc<String> and Rc<Vec<T>> to save a bit of memory and pointer chasing. For the `Vec<T>` case, there is one instance each of `get_mut` and `make_mut` that preclude the use of the slice type: * the `get_mut` one does `push`, so I left it as is * the `make_mut` one does a `reverse`, which would be fine but `Lrc<[T]>` does not impl `Clone` (should it?) - however, the reverse seems to be unnecessary - please if the solution makes sense Had some unrelated-looking local test failures, let's see if if CI gets them too.
2 parents 5758c2d + 639abc9 commit 7ee153e

File tree

21 files changed

+91
-91
lines changed

21 files changed

+91
-91
lines changed

src/librustc/middle/cstore.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -399,10 +399,9 @@ pub fn used_crates(tcx: TyCtxt, prefer: LinkagePreference)
399399
Some((cnum, path))
400400
})
401401
.collect::<Vec<_>>();
402-
let mut ordering = tcx.postorder_cnums(LOCAL_CRATE);
403-
Lrc::make_mut(&mut ordering).reverse();
402+
let ordering = tcx.postorder_cnums(LOCAL_CRATE);
404403
libs.sort_by_key(|&(a, _)| {
405-
ordering.iter().position(|x| *x == a)
404+
ordering.iter().rev().position(|x| *x == a)
406405
});
407406
libs
408407
}

src/librustc/middle/resolve_lifetime.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ pub struct ResolveLifetimes {
217217
defs: FxHashMap<LocalDefId, Lrc<FxHashMap<ItemLocalId, Region>>>,
218218
late_bound: FxHashMap<LocalDefId, Lrc<FxHashSet<ItemLocalId>>>,
219219
object_lifetime_defaults:
220-
FxHashMap<LocalDefId, Lrc<FxHashMap<ItemLocalId, Lrc<Vec<ObjectLifetimeDefault>>>>>,
220+
FxHashMap<LocalDefId, Lrc<FxHashMap<ItemLocalId, Lrc<[ObjectLifetimeDefault]>>>>,
221221
}
222222

223223
impl_stable_hash_for!(struct ::middle::resolve_lifetime::ResolveLifetimes {
@@ -406,7 +406,7 @@ fn resolve_lifetimes<'tcx>(
406406
.or_insert_with(|| Lrc::new(FxHashMap()));
407407
Lrc::get_mut(map)
408408
.unwrap()
409-
.insert(hir_id.local_id, Lrc::new(v));
409+
.insert(hir_id.local_id, Lrc::from(v));
410410
}
411411

412412
Lrc::new(ResolveLifetimes {

src/librustc/traits/mod.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -803,11 +803,11 @@ fn substitute_normalize_and_test_predicates<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx
803803
fn vtable_methods<'a, 'tcx>(
804804
tcx: TyCtxt<'a, 'tcx, 'tcx>,
805805
trait_ref: ty::PolyTraitRef<'tcx>)
806-
-> Lrc<Vec<Option<(DefId, &'tcx Substs<'tcx>)>>>
806+
-> Lrc<[Option<(DefId, &'tcx Substs<'tcx>)>]>
807807
{
808808
debug!("vtable_methods({:?})", trait_ref);
809809

810-
Lrc::new(
810+
Lrc::from(
811811
supertraits(tcx, trait_ref).flat_map(move |trait_ref| {
812812
let trait_methods = tcx.associated_items(trait_ref.def_id())
813813
.filter(|item| item.kind == ty::AssociatedKind::Method);
@@ -850,7 +850,7 @@ fn vtable_methods<'a, 'tcx>(
850850

851851
Some((def_id, substs))
852852
})
853-
}).collect()
853+
}).collect::<Vec<_>>()
854854
)
855855
}
856856

src/librustc/ty/context.rs

+9-9
Original file line numberDiff line numberDiff line change
@@ -846,7 +846,7 @@ pub struct GlobalCtxt<'tcx> {
846846
Lrc<StableVec<TraitCandidate>>>>>,
847847

848848
/// Export map produced by name resolution.
849-
export_map: FxHashMap<DefId, Lrc<Vec<Export>>>,
849+
export_map: FxHashMap<DefId, Lrc<[Export]>>,
850850

851851
pub hir: hir_map::Map<'tcx>,
852852

@@ -859,7 +859,7 @@ pub struct GlobalCtxt<'tcx> {
859859
// Records the free variables refrenced by every closure
860860
// expression. Do not track deps for this, just recompute it from
861861
// scratch every time.
862-
freevars: FxHashMap<DefId, Lrc<Vec<hir::Freevar>>>,
862+
freevars: FxHashMap<DefId, Lrc<[hir::Freevar]>>,
863863

864864
maybe_unused_trait_imports: FxHashSet<DefId>,
865865

@@ -1254,10 +1254,10 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
12541254
types: common_types,
12551255
trait_map,
12561256
export_map: resolutions.export_map.into_iter().map(|(k, v)| {
1257-
(k, Lrc::new(v))
1257+
(k, Lrc::from(v))
12581258
}).collect(),
12591259
freevars: resolutions.freevars.into_iter().map(|(k, v)| {
1260-
(hir.local_def_id(k), Lrc::new(v))
1260+
(hir.local_def_id(k), Lrc::from(v))
12611261
}).collect(),
12621262
maybe_unused_trait_imports:
12631263
resolutions.maybe_unused_trait_imports
@@ -1335,7 +1335,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
13351335
self.stability_index(LOCAL_CRATE)
13361336
}
13371337

1338-
pub fn crates(self) -> Lrc<Vec<CrateNum>> {
1338+
pub fn crates(self) -> Lrc<[CrateNum]> {
13391339
self.all_crate_nums(LOCAL_CRATE)
13401340
}
13411341

@@ -2450,7 +2450,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
24502450
}
24512451

24522452
pub fn object_lifetime_defaults(self, id: HirId)
2453-
-> Option<Lrc<Vec<ObjectLifetimeDefault>>>
2453+
-> Option<Lrc<[ObjectLifetimeDefault]>>
24542454
{
24552455
self.object_lifetime_defaults_map(id.owner)
24562456
.and_then(|map| map.get(&id.local_id).cloned())
@@ -2529,7 +2529,7 @@ pub fn provide(providers: &mut ty::maps::Providers) {
25292529
};
25302530
providers.maybe_unused_extern_crates = |tcx, cnum| {
25312531
assert_eq!(cnum, LOCAL_CRATE);
2532-
Lrc::new(tcx.maybe_unused_extern_crates.clone())
2532+
Lrc::from(tcx.maybe_unused_extern_crates.clone())
25332533
};
25342534

25352535
providers.stability_index = |tcx, cnum| {
@@ -2552,11 +2552,11 @@ pub fn provide(providers: &mut ty::maps::Providers) {
25522552
};
25532553
providers.all_crate_nums = |tcx, cnum| {
25542554
assert_eq!(cnum, LOCAL_CRATE);
2555-
Lrc::new(tcx.cstore.crates_untracked())
2555+
Lrc::from(tcx.cstore.crates_untracked())
25562556
};
25572557
providers.postorder_cnums = |tcx, cnum| {
25582558
assert_eq!(cnum, LOCAL_CRATE);
2559-
Lrc::new(tcx.cstore.postorder_cnums_untracked())
2559+
Lrc::from(tcx.cstore.postorder_cnums_untracked())
25602560
};
25612561
providers.output_filenames = |tcx, cnum| {
25622562
assert_eq!(cnum, LOCAL_CRATE);

src/librustc/ty/maps/mod.rs

+20-20
Original file line numberDiff line numberDiff line change
@@ -132,13 +132,13 @@ define_maps! { <'tcx>
132132

133133
/// Maps from def-id of a type or region parameter to its
134134
/// (inferred) variance.
135-
[] fn variances_of: ItemVariances(DefId) -> Lrc<Vec<ty::Variance>>,
135+
[] fn variances_of: ItemVariances(DefId) -> Lrc<[ty::Variance]>,
136136

137137
/// Maps from def-id of a type to its (inferred) outlives.
138138
[] fn inferred_outlives_of: InferredOutlivesOf(DefId) -> Vec<ty::Predicate<'tcx>>,
139139

140140
/// Maps from an impl/trait def-id to a list of the def-ids of its items
141-
[] fn associated_item_def_ids: AssociatedItemDefIds(DefId) -> Lrc<Vec<DefId>>,
141+
[] fn associated_item_def_ids: AssociatedItemDefIds(DefId) -> Lrc<[DefId]>,
142142

143143
/// Maps from a trait item to the trait item "descriptor"
144144
[] fn associated_item: AssociatedItems(DefId) -> ty::AssociatedItem,
@@ -252,7 +252,7 @@ define_maps! { <'tcx>
252252
[] fn rvalue_promotable_map: RvaluePromotableMap(DefId) -> Lrc<ItemLocalSet>,
253253
[] fn is_mir_available: IsMirAvailable(DefId) -> bool,
254254
[] fn vtable_methods: vtable_methods_node(ty::PolyTraitRef<'tcx>)
255-
-> Lrc<Vec<Option<(DefId, &'tcx Substs<'tcx>)>>>,
255+
-> Lrc<[Option<(DefId, &'tcx Substs<'tcx>)>]>,
256256

257257
[] fn trans_fulfill_obligation: fulfill_obligation_dep_node(
258258
(ty::ParamEnv<'tcx>, ty::PolyTraitRef<'tcx>)) -> Vtable<'tcx, ()>,
@@ -279,7 +279,7 @@ define_maps! { <'tcx>
279279
ty::layout::LayoutError<'tcx>>,
280280

281281
[] fn dylib_dependency_formats: DylibDepFormats(CrateNum)
282-
-> Lrc<Vec<(CrateNum, LinkagePreference)>>,
282+
-> Lrc<[(CrateNum, LinkagePreference)]>,
283283

284284
[fatal_cycle] fn is_panic_runtime: IsPanicRuntime(CrateNum) -> bool,
285285
[fatal_cycle] fn is_compiler_builtins: IsCompilerBuiltins(CrateNum) -> bool,
@@ -294,7 +294,7 @@ define_maps! { <'tcx>
294294
[] fn specializes: specializes_node((DefId, DefId)) -> bool,
295295
[] fn in_scope_traits_map: InScopeTraits(DefIndex)
296296
-> Option<Lrc<FxHashMap<ItemLocalId, Lrc<StableVec<TraitCandidate>>>>>,
297-
[] fn module_exports: ModuleExports(DefId) -> Option<Lrc<Vec<Export>>>,
297+
[] fn module_exports: ModuleExports(DefId) -> Option<Lrc<[Export]>>,
298298
[] fn lint_levels: lint_levels_node(CrateNum) -> Lrc<lint::LintLevelMap>,
299299

300300
[] fn impl_defaultness: ImplDefaultness(DefId) -> hir::Defaultness,
@@ -319,9 +319,9 @@ define_maps! { <'tcx>
319319
[] fn is_reachable_non_generic: IsReachableNonGeneric(DefId) -> bool,
320320

321321

322-
[] fn native_libraries: NativeLibraries(CrateNum) -> Lrc<Vec<NativeLibrary>>,
322+
[] fn native_libraries: NativeLibraries(CrateNum) -> Lrc<[NativeLibrary]>,
323323

324-
[] fn foreign_modules: ForeignModules(CrateNum) -> Lrc<Vec<ForeignModule>>,
324+
[] fn foreign_modules: ForeignModules(CrateNum) -> Lrc<[ForeignModule]>,
325325

326326
[] fn plugin_registrar_fn: PluginRegistrarFn(CrateNum) -> Option<DefId>,
327327
[] fn derive_registrar_fn: DeriveRegistrarFn(CrateNum) -> Option<DefId>,
@@ -330,17 +330,17 @@ define_maps! { <'tcx>
330330
[] fn original_crate_name: OriginalCrateName(CrateNum) -> Symbol,
331331

332332
[] fn implementations_of_trait: implementations_of_trait_node((CrateNum, DefId))
333-
-> Lrc<Vec<DefId>>,
333+
-> Lrc<[DefId]>,
334334
[] fn all_trait_implementations: AllTraitImplementations(CrateNum)
335-
-> Lrc<Vec<DefId>>,
335+
-> Lrc<[DefId]>,
336336

337337
[] fn dllimport_foreign_items: DllimportForeignItems(CrateNum)
338338
-> Lrc<FxHashSet<DefId>>,
339339
[] fn is_dllimport_foreign_item: IsDllimportForeignItem(DefId) -> bool,
340340
[] fn is_statically_included_foreign_item: IsStaticallyIncludedForeignItem(DefId) -> bool,
341341
[] fn native_library_kind: NativeLibraryKind(DefId)
342342
-> Option<NativeLibraryKind>,
343-
[] fn link_args: link_args_node(CrateNum) -> Lrc<Vec<String>>,
343+
[] fn link_args: link_args_node(CrateNum) -> Lrc<[String]>,
344344

345345
// Lifetime resolution. See `middle::resolve_lifetimes`.
346346
[] fn resolve_lifetimes: ResolveLifetimes(CrateNum) -> Lrc<ResolveLifetimes>,
@@ -349,31 +349,31 @@ define_maps! { <'tcx>
349349
[] fn is_late_bound_map: IsLateBound(DefIndex) ->
350350
Option<Lrc<FxHashSet<ItemLocalId>>>,
351351
[] fn object_lifetime_defaults_map: ObjectLifetimeDefaults(DefIndex)
352-
-> Option<Lrc<FxHashMap<ItemLocalId, Lrc<Vec<ObjectLifetimeDefault>>>>>,
352+
-> Option<Lrc<FxHashMap<ItemLocalId, Lrc<[ObjectLifetimeDefault]>>>>,
353353

354354
[] fn visibility: Visibility(DefId) -> ty::Visibility,
355355
[] fn dep_kind: DepKind(CrateNum) -> DepKind,
356356
[] fn crate_name: CrateName(CrateNum) -> Symbol,
357-
[] fn item_children: ItemChildren(DefId) -> Lrc<Vec<Export>>,
357+
[] fn item_children: ItemChildren(DefId) -> Lrc<[Export]>,
358358
[] fn extern_mod_stmt_cnum: ExternModStmtCnum(DefId) -> Option<CrateNum>,
359359

360360
[] fn get_lang_items: get_lang_items_node(CrateNum) -> Lrc<LanguageItems>,
361-
[] fn defined_lang_items: DefinedLangItems(CrateNum) -> Lrc<Vec<(DefId, usize)>>,
362-
[] fn missing_lang_items: MissingLangItems(CrateNum) -> Lrc<Vec<LangItem>>,
361+
[] fn defined_lang_items: DefinedLangItems(CrateNum) -> Lrc<[(DefId, usize)]>,
362+
[] fn missing_lang_items: MissingLangItems(CrateNum) -> Lrc<[LangItem]>,
363363
[] fn extern_const_body: ExternConstBody(DefId) -> ExternConstBody<'tcx>,
364364
[] fn visible_parent_map: visible_parent_map_node(CrateNum)
365365
-> Lrc<DefIdMap<DefId>>,
366366
[] fn missing_extern_crate_item: MissingExternCrateItem(CrateNum) -> bool,
367367
[] fn used_crate_source: UsedCrateSource(CrateNum) -> Lrc<CrateSource>,
368-
[] fn postorder_cnums: postorder_cnums_node(CrateNum) -> Lrc<Vec<CrateNum>>,
368+
[] fn postorder_cnums: postorder_cnums_node(CrateNum) -> Lrc<[CrateNum]>,
369369

370-
[] fn freevars: Freevars(DefId) -> Option<Lrc<Vec<hir::Freevar>>>,
370+
[] fn freevars: Freevars(DefId) -> Option<Lrc<[hir::Freevar]>>,
371371
[] fn maybe_unused_trait_import: MaybeUnusedTraitImport(DefId) -> bool,
372372
[] fn maybe_unused_extern_crates: maybe_unused_extern_crates_node(CrateNum)
373-
-> Lrc<Vec<(DefId, Span)>>,
373+
-> Lrc<[(DefId, Span)]>,
374374

375375
[] fn stability_index: stability_index_node(CrateNum) -> Lrc<stability::Index<'tcx>>,
376-
[] fn all_crate_nums: all_crate_nums_node(CrateNum) -> Lrc<Vec<CrateNum>>,
376+
[] fn all_crate_nums: all_crate_nums_node(CrateNum) -> Lrc<[CrateNum]>,
377377

378378
[] fn exported_symbols: ExportedSymbols(CrateNum)
379379
-> Arc<Vec<(ExportedSymbol, SymbolExportLevel)>>,
@@ -425,9 +425,9 @@ define_maps! { <'tcx>
425425

426426
[] fn features_query: features_node(CrateNum) -> Lrc<feature_gate::Features>,
427427

428-
[] fn program_clauses_for: ProgramClausesFor(DefId) -> Lrc<Vec<Clause<'tcx>>>,
428+
[] fn program_clauses_for: ProgramClausesFor(DefId) -> Lrc<[Clause<'tcx>]>,
429429

430-
[] fn wasm_custom_sections: WasmCustomSections(CrateNum) -> Lrc<Vec<DefId>>,
430+
[] fn wasm_custom_sections: WasmCustomSections(CrateNum) -> Lrc<[DefId]>,
431431
[] fn wasm_import_module_map: WasmImportModuleMap(CrateNum)
432432
-> Lrc<FxHashMap<DefId, String>>,
433433
}

src/librustc/ty/mod.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -337,10 +337,10 @@ pub struct CrateVariancesMap {
337337
/// For each item with generics, maps to a vector of the variance
338338
/// of its generics. If an item has no generics, it will have no
339339
/// entry.
340-
pub variances: FxHashMap<DefId, Lrc<Vec<ty::Variance>>>,
340+
pub variances: FxHashMap<DefId, Lrc<[ty::Variance]>>,
341341

342342
/// An empty vector, useful for cloning.
343-
pub empty_variance: Lrc<Vec<ty::Variance>>,
343+
pub empty_variance: Lrc<[ty::Variance]>,
344344
}
345345

346346
impl Variance {
@@ -2641,7 +2641,7 @@ fn adt_sized_constraint<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
26412641

26422642
fn associated_item_def_ids<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
26432643
def_id: DefId)
2644-
-> Lrc<Vec<DefId>> {
2644+
-> Lrc<[DefId]> {
26452645
let id = tcx.hir.as_local_node_id(def_id).unwrap();
26462646
let item = tcx.hir.expect_item(id);
26472647
let vec: Vec<_> = match item.node {
@@ -2660,7 +2660,7 @@ fn associated_item_def_ids<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
26602660
hir::ItemTraitAlias(..) => vec![],
26612661
_ => span_bug!(item.span, "associated_item_def_ids: not impl or trait")
26622662
};
2663-
Lrc::new(vec)
2663+
Lrc::from(vec)
26642664
}
26652665

26662666
fn def_span<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, def_id: DefId) -> Span {
@@ -2772,6 +2772,7 @@ pub fn provide(providers: &mut ty::maps::Providers) {
27722772
/// (constructing this map requires touching the entire crate).
27732773
#[derive(Clone, Debug)]
27742774
pub struct CrateInherentImpls {
2775+
// Note: needs to be a Lrc<Vec<DefId>> since get_mut().push() is used
27752776
pub inherent_impls: DefIdMap<Lrc<Vec<DefId>>>,
27762777
}
27772778

src/librustc/ty/relate.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ impl<'tcx> Relate<'tcx> for ty::TypeAndMut<'tcx> {
131131
}
132132

133133
pub fn relate_substs<'a, 'gcx, 'tcx, R>(relation: &mut R,
134-
variances: Option<&Vec<ty::Variance>>,
134+
variances: Option<&[ty::Variance]>,
135135
a_subst: &'tcx Substs<'tcx>,
136136
b_subst: &'tcx Substs<'tcx>)
137137
-> RelateResult<'tcx, &'tcx Substs<'tcx>>

0 commit comments

Comments
 (0)