Skip to content

Commit 158f8d0

Browse files
committed
Auto merge of #80014 - jyn514:box-item-kind, r=nnethercote
[rustdoc] Box ItemKind to reduce the size of `Item` This brings the size of `Item` from ``` [src/librustdoc/lib.rs:103] std::mem::size_of::<Item>() = 536 ``` to ``` [src/librustdoc/lib.rs:103] std::mem::size_of::<Item>() = 136 ``` This is an alternative to #79967; I don't think it makes sense to make both changes. Helps with #79103.
2 parents e2a2592 + 5fce0dd commit 158f8d0

20 files changed

+93
-93
lines changed

src/librustdoc/clean/auto_trait.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ impl<'a, 'tcx> AutoTraitFinder<'a, 'tcx> {
123123
attrs: Default::default(),
124124
visibility: Inherited,
125125
def_id: self.cx.next_def_id(param_env_def_id.krate),
126-
kind: ImplItem(Impl {
126+
kind: box ImplItem(Impl {
127127
unsafety: hir::Unsafety::Normal,
128128
generics: new_generics,
129129
provided_trait_methods: Default::default(),

src/librustdoc/clean/blanket_impl.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ impl<'a, 'tcx> BlanketImplFinder<'a, 'tcx> {
112112
attrs: Default::default(),
113113
visibility: Inherited,
114114
def_id: self.cx.next_def_id(impl_def_id.krate),
115-
kind: ImplItem(Impl {
115+
kind: box ImplItem(Impl {
116116
unsafety: hir::Unsafety::Normal,
117117
generics: (
118118
self.cx.tcx.generics_of(impl_def_id),

src/librustdoc/clean/inline.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -482,7 +482,7 @@ fn build_module(cx: &DocContext<'_>, did: DefId, visited: &mut FxHashSet<DefId>)
482482
source: clean::Span::dummy(),
483483
def_id: DefId::local(CRATE_DEF_INDEX),
484484
visibility: clean::Public,
485-
kind: clean::ImportItem(clean::Import::new_simple(
485+
kind: box clean::ImportItem(clean::Import::new_simple(
486486
item.ident.name,
487487
clean::ImportSource {
488488
path: clean::Path {

src/librustdoc/clean/mod.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -2160,7 +2160,7 @@ fn clean_extern_crate(
21602160
source: krate.span.clean(cx),
21612161
def_id: crate_def_id,
21622162
visibility: krate.vis.clean(cx),
2163-
kind: ExternCrateItem(name, orig_name),
2163+
kind: box ExternCrateItem(name, orig_name),
21642164
}]
21652165
}
21662166

@@ -2228,7 +2228,7 @@ impl Clean<Vec<Item>> for doctree::Import<'_> {
22282228
source: self.span.clean(cx),
22292229
def_id: cx.tcx.hir().local_def_id(self.id).to_def_id(),
22302230
visibility: self.vis.clean(cx),
2231-
kind: ImportItem(Import::new_simple(
2231+
kind: box ImportItem(Import::new_simple(
22322232
self.name,
22332233
resolve_use_source(cx, path),
22342234
false,
@@ -2246,7 +2246,7 @@ impl Clean<Vec<Item>> for doctree::Import<'_> {
22462246
source: self.span.clean(cx),
22472247
def_id: cx.tcx.hir().local_def_id(self.id).to_def_id(),
22482248
visibility: self.vis.clean(cx),
2249-
kind: ImportItem(inner),
2249+
kind: box ImportItem(inner),
22502250
}]
22512251
}
22522252
}

src/librustdoc/clean/types.rs

+10-6
Original file line numberDiff line numberDiff line change
@@ -84,10 +84,14 @@ crate struct Item {
8484
crate name: Option<Symbol>,
8585
crate attrs: Attributes,
8686
crate visibility: Visibility,
87-
crate kind: ItemKind,
87+
crate kind: Box<ItemKind>,
8888
crate def_id: DefId,
8989
}
9090

91+
// `Item` is used a lot. Make sure it doesn't unintentionally get bigger.
92+
#[cfg(target_arch = "x86_64")]
93+
rustc_data_structures::static_assert_size!(Item, 136);
94+
9195
impl fmt::Debug for Item {
9296
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
9397
let def_id: &dyn fmt::Debug = if self.is_fake() { &"**FAKE**" } else { &self.def_id };
@@ -152,7 +156,7 @@ impl Item {
152156

153157
Item {
154158
def_id,
155-
kind,
159+
kind: box kind,
156160
name,
157161
source: source.clean(cx),
158162
attrs: cx.tcx.get_attrs(def_id).clean(cx),
@@ -171,7 +175,7 @@ impl Item {
171175
}
172176

173177
crate fn is_crate(&self) -> bool {
174-
match self.kind {
178+
match *self.kind {
175179
StrippedItem(box ModuleItem(Module { is_crate: true, .. }))
176180
| ModuleItem(Module { is_crate: true, .. }) => true,
177181
_ => false,
@@ -223,14 +227,14 @@ impl Item {
223227
self.type_() == ItemType::Keyword
224228
}
225229
crate fn is_stripped(&self) -> bool {
226-
match self.kind {
230+
match *self.kind {
227231
StrippedItem(..) => true,
228232
ImportItem(ref i) => !i.should_be_displayed,
229233
_ => false,
230234
}
231235
}
232236
crate fn has_stripped_fields(&self) -> Option<bool> {
233-
match self.kind {
237+
match *self.kind {
234238
StructItem(ref _struct) => Some(_struct.fields_stripped),
235239
UnionItem(ref union) => Some(union.fields_stripped),
236240
VariantItem(Variant { kind: VariantKind::Struct(ref vstruct) }) => {
@@ -281,7 +285,7 @@ impl Item {
281285
}
282286

283287
crate fn is_default(&self) -> bool {
284-
match self.kind {
288+
match *self.kind {
285289
ItemKind::MethodItem(_, Some(defaultness)) => {
286290
defaultness.has_value() && !defaultness.is_final()
287291
}

src/librustdoc/clean/utils.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ crate fn krate(mut cx: &mut DocContext<'_>) -> Crate {
4343
let mut module = module.clean(cx);
4444
let mut masked_crates = FxHashSet::default();
4545

46-
match module.kind {
46+
match *module.kind {
4747
ItemKind::ModuleItem(ref module) => {
4848
for it in &module.items {
4949
// `compiler_builtins` should be masked too, but we can't apply
@@ -61,7 +61,7 @@ crate fn krate(mut cx: &mut DocContext<'_>) -> Crate {
6161

6262
let ExternalCrate { name, src, primitives, keywords, .. } = LOCAL_CRATE.clean(cx);
6363
{
64-
let m = match module.kind {
64+
let m = match *module.kind {
6565
ItemKind::ModuleItem(ref mut m) => m,
6666
_ => unreachable!(),
6767
};
@@ -338,7 +338,7 @@ crate fn build_deref_target_impls(cx: &DocContext<'_>, items: &[Item], ret: &mut
338338
let tcx = cx.tcx;
339339

340340
for item in items {
341-
let target = match item.kind {
341+
let target = match *item.kind {
342342
ItemKind::TypedefItem(ref t, true) => &t.type_,
343343
_ => continue,
344344
};

src/librustdoc/fold.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ crate struct StripItem(pub Item);
55
impl StripItem {
66
crate fn strip(self) -> Option<Item> {
77
match self.0 {
8-
Item { kind: StrippedItem(..), .. } => Some(self.0),
8+
Item { kind: box StrippedItem(..), .. } => Some(self.0),
99
mut i => {
10-
i.kind = StrippedItem(box i.kind);
10+
i.kind = box StrippedItem(i.kind);
1111
Some(i)
1212
}
1313
}
@@ -72,9 +72,9 @@ crate trait DocFolder: Sized {
7272

7373
/// don't override!
7474
fn fold_item_recur(&mut self, mut item: Item) -> Item {
75-
item.kind = match item.kind {
75+
item.kind = box match *item.kind {
7676
StrippedItem(box i) => StrippedItem(box self.fold_inner_recur(i)),
77-
_ => self.fold_inner_recur(item.kind),
77+
_ => self.fold_inner_recur(*item.kind),
7878
};
7979
item
8080
}

src/librustdoc/formats/cache.rs

+24-29
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ impl DocFolder for Cache {
219219

220220
// If this is a stripped module,
221221
// we don't want it or its children in the search index.
222-
let orig_stripped_mod = match item.kind {
222+
let orig_stripped_mod = match *item.kind {
223223
clean::StrippedItem(box clean::ModuleItem(..)) => {
224224
mem::replace(&mut self.stripped_mod, true)
225225
}
@@ -228,7 +228,7 @@ impl DocFolder for Cache {
228228

229229
// If the impl is from a masked crate or references something from a
230230
// masked crate then remove it completely.
231-
if let clean::ImplItem(ref i) = item.kind {
231+
if let clean::ImplItem(ref i) = *item.kind {
232232
if self.masked_crates.contains(&item.def_id.krate)
233233
|| i.trait_.def_id().map_or(false, |d| self.masked_crates.contains(&d.krate))
234234
|| i.for_.def_id().map_or(false, |d| self.masked_crates.contains(&d.krate))
@@ -239,12 +239,12 @@ impl DocFolder for Cache {
239239

240240
// Propagate a trait method's documentation to all implementors of the
241241
// trait.
242-
if let clean::TraitItem(ref t) = item.kind {
242+
if let clean::TraitItem(ref t) = *item.kind {
243243
self.traits.entry(item.def_id).or_insert_with(|| t.clone());
244244
}
245245

246246
// Collect all the implementors of traits.
247-
if let clean::ImplItem(ref i) = item.kind {
247+
if let clean::ImplItem(ref i) = *item.kind {
248248
if let Some(did) = i.trait_.def_id() {
249249
if i.blanket_impl.is_none() {
250250
self.implementors
@@ -257,7 +257,7 @@ impl DocFolder for Cache {
257257

258258
// Index this method for searching later on.
259259
if let Some(ref s) = item.name {
260-
let (parent, is_inherent_impl_item) = match item.kind {
260+
let (parent, is_inherent_impl_item) = match *item.kind {
261261
clean::StrippedItem(..) => ((None, None), false),
262262
clean::AssocConstItem(..) | clean::TypedefItem(_, true)
263263
if self.parent_is_trait_impl =>
@@ -348,7 +348,7 @@ impl DocFolder for Cache {
348348
_ => false,
349349
};
350350

351-
match item.kind {
351+
match *item.kind {
352352
clean::StructItem(..)
353353
| clean::EnumItem(..)
354354
| clean::TypedefItem(..)
@@ -387,7 +387,7 @@ impl DocFolder for Cache {
387387

388388
// Maintain the parent stack
389389
let orig_parent_is_trait_impl = self.parent_is_trait_impl;
390-
let parent_pushed = match item.kind {
390+
let parent_pushed = match *item.kind {
391391
clean::TraitItem(..)
392392
| clean::EnumItem(..)
393393
| clean::ForeignTypeItem
@@ -425,38 +425,33 @@ impl DocFolder for Cache {
425425
// Once we've recursively found all the generics, hoard off all the
426426
// implementations elsewhere.
427427
let item = self.fold_item_recur(item);
428-
let ret = if let clean::Item { kind: clean::ImplItem(_), .. } = item {
428+
let ret = if let clean::Item { kind: box clean::ImplItem(ref i), .. } = item {
429429
// Figure out the id of this impl. This may map to a
430430
// primitive rather than always to a struct/enum.
431431
// Note: matching twice to restrict the lifetime of the `i` borrow.
432432
let mut dids = FxHashSet::default();
433-
if let clean::Item { kind: clean::ImplItem(ref i), .. } = item {
434-
match i.for_ {
435-
clean::ResolvedPath { did, .. }
436-
| clean::BorrowedRef { type_: box clean::ResolvedPath { did, .. }, .. } => {
437-
dids.insert(did);
438-
}
439-
ref t => {
440-
let did = t
441-
.primitive_type()
442-
.and_then(|t| self.primitive_locations.get(&t).cloned());
433+
match i.for_ {
434+
clean::ResolvedPath { did, .. }
435+
| clean::BorrowedRef { type_: box clean::ResolvedPath { did, .. }, .. } => {
436+
dids.insert(did);
437+
}
438+
ref t => {
439+
let did =
440+
t.primitive_type().and_then(|t| self.primitive_locations.get(&t).cloned());
443441

444-
if let Some(did) = did {
445-
dids.insert(did);
446-
}
442+
if let Some(did) = did {
443+
dids.insert(did);
447444
}
448445
}
446+
}
449447

450-
if let Some(generics) = i.trait_.as_ref().and_then(|t| t.generics()) {
451-
for bound in generics {
452-
if let Some(did) = bound.def_id() {
453-
dids.insert(did);
454-
}
448+
if let Some(generics) = i.trait_.as_ref().and_then(|t| t.generics()) {
449+
for bound in generics {
450+
if let Some(did) = bound.def_id() {
451+
dids.insert(did);
455452
}
456453
}
457-
} else {
458-
unreachable!()
459-
};
454+
}
460455
let impl_item = Impl { impl_item: item };
461456
if impl_item.trait_did().map_or(true, |d| self.traits.contains_key(&d)) {
462457
for did in dids {

src/librustdoc/formats/item_type.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ impl Serialize for ItemType {
6060

6161
impl<'a> From<&'a clean::Item> for ItemType {
6262
fn from(item: &'a clean::Item) -> ItemType {
63-
let kind = match item.kind {
63+
let kind = match *item.kind {
6464
clean::StrippedItem(box ref item) => item,
6565
ref kind => kind,
6666
};

src/librustdoc/formats/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ crate struct Impl {
3232

3333
impl Impl {
3434
crate fn inner_impl(&self) -> &clean::Impl {
35-
match self.impl_item.kind {
35+
match *self.impl_item.kind {
3636
clean::ImplItem(ref impl_) => impl_,
3737
_ => panic!("non-impl item found in impl"),
3838
}

src/librustdoc/formats/renderer.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ crate fn run_format<'tcx, T: FormatRenderer<'tcx>>(
8989
}
9090

9191
cx.mod_item_in(&item, &name, &cache)?;
92-
let module = match item.kind {
92+
let module = match *item.kind {
9393
clean::StrippedItem(box clean::ModuleItem(m)) | clean::ModuleItem(m) => m,
9494
_ => unreachable!(),
9595
};

src/librustdoc/html/render/cache.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ crate fn build_index(krate: &clean::Crate, cache: &mut Cache) -> String {
165165
}
166166

167167
crate fn get_index_search_type(item: &clean::Item) -> Option<IndexItemFunctionType> {
168-
let (all_types, ret_types) = match item.kind {
168+
let (all_types, ret_types) = match *item.kind {
169169
clean::FunctionItem(ref f) => (&f.all_types, &f.ret_types),
170170
clean::MethodItem(ref m, _) => (&m.all_types, &m.ret_types),
171171
clean::TyMethodItem(ref m) => (&m.all_types, &m.ret_types),

0 commit comments

Comments
 (0)