Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit fd7707c

Browse files
committed
Auto merge of rust-lang#138916 - nnethercote:rm-ItemInner, r=<try>
rustdoc: remove `ItemInner`. The `Item` struct is 48 bytes and contains a `Box<ItemInner>`; `ItemInner` is 104 bytes. This is an odd arrangement. Normally you'd have one of the following. - A single large struct, which avoids the allocation for the `Box`, but can result in lots of wasted space in unused parts of a container like `Vec<Item>`, `HashSet<Item>`, etc. - Or, something like struct `Item(Box<ItemInner>)`, which requires the `Box` allocation but gives a very small `Item` size, which is good for containers like `Vec<Item>`. (`Vec<Box<Item>>` would also work.) `Item`/`ItemInner` currently gets the worst of both worlds: it always requires a `Box`, but `Item` is also pretty big and so wastes space in containers. It would make sense to push it in one direction or the other. This commit does the first option, a single large struct. r? `@ghost`
2 parents 1df5aff + 0c21f21 commit fd7707c

File tree

11 files changed

+89
-110
lines changed

11 files changed

+89
-110
lines changed

src/librustdoc/clean/auto_trait.rs

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -115,19 +115,17 @@ fn synthesize_auto_trait_impl<'tcx>(
115115

116116
Some(clean::Item {
117117
name: None,
118-
inner: Box::new(clean::ItemInner {
119-
attrs: Default::default(),
120-
stability: None,
121-
kind: clean::ImplItem(Box::new(clean::Impl {
122-
safety: hir::Safety::Safe,
123-
generics,
124-
trait_: Some(clean_trait_ref_with_constraints(cx, trait_ref, ThinVec::new())),
125-
for_: clean_middle_ty(ty::Binder::dummy(ty), cx, None, None),
126-
items: Vec::new(),
127-
polarity,
128-
kind: clean::ImplKind::Auto,
129-
})),
130-
}),
118+
attrs: Default::default(),
119+
stability: None,
120+
kind: clean::ImplItem(Box::new(clean::Impl {
121+
safety: hir::Safety::Safe,
122+
generics,
123+
trait_: Some(clean_trait_ref_with_constraints(cx, trait_ref, ThinVec::new())),
124+
for_: clean_middle_ty(ty::Binder::dummy(ty), cx, None, None),
125+
items: Vec::new(),
126+
polarity,
127+
kind: clean::ImplKind::Auto,
128+
})),
131129
item_id: clean::ItemId::Auto { trait_: trait_def_id, for_: item_def_id },
132130
cfg: None,
133131
inline_stmt_id: None,

src/librustdoc/clean/blanket_impl.rs

Lines changed: 36 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -85,44 +85,42 @@ pub(crate) fn synthesize_blanket_impls(
8585
blanket_impls.push(clean::Item {
8686
name: None,
8787
item_id: clean::ItemId::Blanket { impl_id: impl_def_id, for_: item_def_id },
88-
inner: Box::new(clean::ItemInner {
89-
attrs: Default::default(),
90-
stability: None,
91-
kind: clean::ImplItem(Box::new(clean::Impl {
92-
safety: hir::Safety::Safe,
93-
generics: clean_ty_generics(
94-
cx,
95-
tcx.generics_of(impl_def_id),
96-
tcx.explicit_predicates_of(impl_def_id),
97-
),
98-
// FIXME(eddyb) compute both `trait_` and `for_` from
99-
// the post-inference `trait_ref`, as it's more accurate.
100-
trait_: Some(clean_trait_ref_with_constraints(
101-
cx,
102-
ty::Binder::dummy(trait_ref.instantiate_identity()),
103-
ThinVec::new(),
104-
)),
105-
for_: clean_middle_ty(
106-
ty::Binder::dummy(ty.instantiate_identity()),
107-
cx,
108-
None,
109-
None,
110-
),
111-
items: tcx
112-
.associated_items(impl_def_id)
113-
.in_definition_order()
114-
.filter(|item| !item.is_impl_trait_in_trait())
115-
.map(|item| clean_middle_assoc_item(item, cx))
116-
.collect(),
117-
polarity: ty::ImplPolarity::Positive,
118-
kind: clean::ImplKind::Blanket(Box::new(clean_middle_ty(
119-
ty::Binder::dummy(trait_ref.instantiate_identity().self_ty()),
120-
cx,
121-
None,
122-
None,
123-
))),
124-
})),
125-
}),
88+
attrs: Default::default(),
89+
stability: None,
90+
kind: clean::ImplItem(Box::new(clean::Impl {
91+
safety: hir::Safety::Safe,
92+
generics: clean_ty_generics(
93+
cx,
94+
tcx.generics_of(impl_def_id),
95+
tcx.explicit_predicates_of(impl_def_id),
96+
),
97+
// FIXME(eddyb) compute both `trait_` and `for_` from
98+
// the post-inference `trait_ref`, as it's more accurate.
99+
trait_: Some(clean_trait_ref_with_constraints(
100+
cx,
101+
ty::Binder::dummy(trait_ref.instantiate_identity()),
102+
ThinVec::new(),
103+
)),
104+
for_: clean_middle_ty(
105+
ty::Binder::dummy(ty.instantiate_identity()),
106+
cx,
107+
None,
108+
None,
109+
),
110+
items: tcx
111+
.associated_items(impl_def_id)
112+
.in_definition_order()
113+
.filter(|item| !item.is_impl_trait_in_trait())
114+
.map(|item| clean_middle_assoc_item(item, cx))
115+
.collect(),
116+
polarity: ty::ImplPolarity::Positive,
117+
kind: clean::ImplKind::Blanket(Box::new(clean_middle_ty(
118+
ty::Binder::dummy(trait_ref.instantiate_identity().self_ty()),
119+
cx,
120+
None,
121+
None,
122+
))),
123+
})),
126124
cfg: None,
127125
inline_stmt_id: None,
128126
});

src/librustdoc/clean/inline.rs

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -669,27 +669,25 @@ fn build_module_items(
669669
// We can use the item's `DefId` directly since the only information ever used
670670
// from it is `DefId.krate`.
671671
item_id: ItemId::DefId(did),
672-
inner: Box::new(clean::ItemInner {
673-
attrs: Default::default(),
674-
stability: None,
675-
kind: clean::ImportItem(clean::Import::new_simple(
676-
item.ident.name,
677-
clean::ImportSource {
678-
path: clean::Path {
679-
res,
680-
segments: thin_vec![clean::PathSegment {
681-
name: prim_ty.as_sym(),
682-
args: clean::GenericArgs::AngleBracketed {
683-
args: Default::default(),
684-
constraints: ThinVec::new(),
685-
},
686-
}],
687-
},
688-
did: None,
672+
attrs: Default::default(),
673+
stability: None,
674+
kind: clean::ImportItem(clean::Import::new_simple(
675+
item.ident.name,
676+
clean::ImportSource {
677+
path: clean::Path {
678+
res,
679+
segments: thin_vec![clean::PathSegment {
680+
name: prim_ty.as_sym(),
681+
args: clean::GenericArgs::AngleBracketed {
682+
args: Default::default(),
683+
constraints: ThinVec::new(),
684+
},
685+
}],
689686
},
690-
true,
691-
)),
692-
}),
687+
did: None,
688+
},
689+
true,
690+
)),
693691
cfg: None,
694692
inline_stmt_id: None,
695693
});

src/librustdoc/clean/types.rs

Lines changed: 13 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -314,30 +314,18 @@ pub(crate) struct Item {
314314
/// The name of this item.
315315
/// Optional because not every item has a name, e.g. impls.
316316
pub(crate) name: Option<Symbol>,
317-
pub(crate) inner: Box<ItemInner>,
318-
pub(crate) item_id: ItemId,
319-
/// This is the `LocalDefId` of the `use` statement if the item was inlined.
320-
/// The crate metadata doesn't hold this information, so the `use` statement
321-
/// always belongs to the current crate.
322-
pub(crate) inline_stmt_id: Option<LocalDefId>,
323-
pub(crate) cfg: Option<Arc<Cfg>>,
324-
}
325-
326-
#[derive(Clone)]
327-
pub(crate) struct ItemInner {
328317
/// Information about this item that is specific to what kind of item it is.
329318
/// E.g., struct vs enum vs function.
330319
pub(crate) kind: ItemKind,
331320
pub(crate) attrs: Attributes,
332321
/// The effective stability, filled out by the `propagate-stability` pass.
333322
pub(crate) stability: Option<Stability>,
334-
}
335-
336-
impl std::ops::Deref for Item {
337-
type Target = ItemInner;
338-
fn deref(&self) -> &ItemInner {
339-
&self.inner
340-
}
323+
pub(crate) item_id: ItemId,
324+
/// This is the `LocalDefId` of the `use` statement if the item was inlined.
325+
/// The crate metadata doesn't hold this information, so the `use` statement
326+
/// always belongs to the current crate.
327+
pub(crate) inline_stmt_id: Option<LocalDefId>,
328+
pub(crate) cfg: Option<Arc<Cfg>>,
341329
}
342330

343331
/// NOTE: this does NOT unconditionally print every item, to avoid thousands of lines of logs.
@@ -383,13 +371,12 @@ impl Item {
383371
///
384372
/// This method should only be called after the `propagate-stability` pass has been run.
385373
pub(crate) fn stability(&self, tcx: TyCtxt<'_>) -> Option<Stability> {
386-
let stability = self.inner.stability;
387374
debug_assert!(
388-
stability.is_some()
375+
self.stability.is_some()
389376
|| self.def_id().is_none_or(|did| tcx.lookup_stability(did).is_none()),
390377
"missing stability for cleaned item: {self:?}",
391378
);
392-
stability
379+
self.stability
393380
}
394381

395382
pub(crate) fn const_stability(&self, tcx: TyCtxt<'_>) -> Option<ConstStability> {
@@ -489,7 +476,9 @@ impl Item {
489476

490477
Item {
491478
item_id: def_id.into(),
492-
inner: Box::new(ItemInner { kind, attrs, stability: None }),
479+
kind,
480+
attrs,
481+
stability: None,
493482
name,
494483
cfg,
495484
inline_stmt_id: None,
@@ -2639,13 +2628,13 @@ mod size_asserts {
26392628

26402629
use super::*;
26412630
// tidy-alphabetical-start
2642-
static_assert_size!(Crate, 56); // frequently moved by-value
2631+
static_assert_size!(Crate, 144); // frequently moved by-value
26432632
static_assert_size!(DocFragment, 32);
26442633
static_assert_size!(GenericArg, 32);
26452634
static_assert_size!(GenericArgs, 24);
26462635
static_assert_size!(GenericParamDef, 40);
26472636
static_assert_size!(Generics, 16);
2648-
static_assert_size!(Item, 48);
2637+
static_assert_size!(Item, 136);
26492638
static_assert_size!(ItemKind, 48);
26502639
static_assert_size!(PathSegment, 32);
26512640
static_assert_size!(Type, 32);

src/librustdoc/clean/utils.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ pub(crate) fn krate(cx: &mut DocContext<'_>) -> Crate {
6060
let primitives = local_crate.primitives(cx.tcx);
6161
let keywords = local_crate.keywords(cx.tcx);
6262
{
63-
let ItemKind::ModuleItem(m) = &mut module.inner.kind else { unreachable!() };
63+
let ItemKind::ModuleItem(m) = &mut module.kind else { unreachable!() };
6464
m.items.extend(primitives.iter().map(|&(def_id, prim)| {
6565
Item::from_def_id_and_parts(
6666
def_id,

src/librustdoc/fold.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ use std::mem;
33
use crate::clean::*;
44

55
pub(crate) fn strip_item(mut item: Item) -> Item {
6-
if !matches!(item.inner.kind, StrippedItem(..)) {
7-
item.inner.kind = StrippedItem(Box::new(item.inner.kind));
6+
if !matches!(item.kind, StrippedItem(..)) {
7+
item.kind = StrippedItem(Box::new(item.kind));
88
}
99
item
1010
}
@@ -102,9 +102,9 @@ pub(crate) trait DocFolder: Sized {
102102

103103
/// don't override!
104104
fn fold_item_recur(&mut self, mut item: Item) -> Item {
105-
item.inner.kind = match item.inner.kind {
105+
item.kind = match item.kind {
106106
StrippedItem(box i) => StrippedItem(Box::new(self.fold_inner_recur(i))),
107-
_ => self.fold_inner_recur(item.inner.kind),
107+
_ => self.fold_inner_recur(item.kind),
108108
};
109109
item
110110
}

src/librustdoc/formats/cache.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -383,11 +383,7 @@ impl DocFolder for CacheBuilder<'_, '_> {
383383

384384
// Once we've recursively found all the generics, hoard off all the
385385
// implementations elsewhere.
386-
let ret = if let clean::Item {
387-
inner: box clean::ItemInner { kind: clean::ImplItem(ref i), .. },
388-
..
389-
} = item
390-
{
386+
let ret = if let clean::Item { kind: clean::ImplItem(ref i), .. } = item {
391387
// Figure out the id of this impl. This may map to a
392388
// primitive rather than always to a struct/enum.
393389
// Note: matching twice to restrict the lifetime of the `i` borrow.

src/librustdoc/formats/renderer.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ fn run_format_inner<'tcx, T: FormatRenderer<'tcx>>(
8585

8686
cx.mod_item_in(&item)?;
8787
let (clean::StrippedItem(box clean::ModuleItem(module)) | clean::ModuleItem(module)) =
88-
item.inner.kind
88+
item.kind
8989
else {
9090
unreachable!()
9191
};

src/librustdoc/json/conversions.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ fn from_clean_item(item: clean::Item, renderer: &JsonRenderer<'_>) -> ItemEnum {
241241
let is_crate = item.is_crate();
242242
let header = item.fn_header(renderer.tcx);
243243

244-
match item.inner.kind {
244+
match item.kind {
245245
ModuleItem(m) => {
246246
ItemEnum::Module(Module { is_crate, items: renderer.ids(m.items), is_stripped: false })
247247
}

src/librustdoc/passes/collect_trait_impls.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ pub(crate) fn collect_trait_impls(mut krate: Crate, cx: &mut DocContext<'_>) ->
210210
}
211211
});
212212

213-
if let ModuleItem(Module { items, .. }) = &mut krate.module.inner.kind {
213+
if let ModuleItem(Module { items, .. }) = &mut krate.module.kind {
214214
items.extend(synth_impls);
215215
items.extend(new_items_external);
216216
items.extend(new_items_local);
@@ -259,7 +259,7 @@ impl DocVisitor<'_> for ItemAndAliasCollector<'_> {
259259
fn visit_item(&mut self, i: &Item) {
260260
self.items.insert(i.item_id);
261261

262-
if let TypeAliasItem(alias) = &i.inner.kind
262+
if let TypeAliasItem(alias) = &i.kind
263263
&& let Some(did) = alias.type_.def_id(self.cache)
264264
{
265265
self.items.insert(ItemId::DefId(did));

src/librustdoc/passes/propagate_stability.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ impl DocFolder for StabilityPropagator<'_, '_> {
117117
}
118118
};
119119

120-
item.inner.stability = stability;
120+
item.stability = stability;
121121
self.parent_stability = stability;
122122
let item = self.fold_item_recur(item);
123123
self.parent_stability = parent_stability;

0 commit comments

Comments
 (0)