Skip to content

Commit

Permalink
Box ItemKind to reduce the size of Item
Browse files Browse the repository at this point in the history
This brings the size of `Item` from

```
[src/librustdoc/lib.rs:103] std::mem::size_of::<Item>() = 680
```

to

```
[src/librustdoc/lib.rs:103] std::mem::size_of::<Item>() = 280
```
  • Loading branch information
jyn514 committed Dec 23, 2020
1 parent 3ffea60 commit 4a44263
Show file tree
Hide file tree
Showing 20 changed files with 89 additions and 93 deletions.
2 changes: 1 addition & 1 deletion src/librustdoc/clean/auto_trait.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ impl<'a, 'tcx> AutoTraitFinder<'a, 'tcx> {
attrs: Default::default(),
visibility: Inherited,
def_id: self.cx.next_def_id(param_env_def_id.krate),
kind: ImplItem(Impl {
kind: box ImplItem(Impl {
unsafety: hir::Unsafety::Normal,
generics: new_generics,
provided_trait_methods: Default::default(),
Expand Down
2 changes: 1 addition & 1 deletion src/librustdoc/clean/blanket_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ impl<'a, 'tcx> BlanketImplFinder<'a, 'tcx> {
attrs: Default::default(),
visibility: Inherited,
def_id: self.cx.next_def_id(impl_def_id.krate),
kind: ImplItem(Impl {
kind: box ImplItem(Impl {
unsafety: hir::Unsafety::Normal,
generics: (
self.cx.tcx.generics_of(impl_def_id),
Expand Down
2 changes: 1 addition & 1 deletion src/librustdoc/clean/inline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -482,7 +482,7 @@ fn build_module(cx: &DocContext<'_>, did: DefId, visited: &mut FxHashSet<DefId>)
source: clean::Span::dummy(),
def_id: DefId::local(CRATE_DEF_INDEX),
visibility: clean::Public,
kind: clean::ImportItem(clean::Import::new_simple(
kind: box clean::ImportItem(clean::Import::new_simple(
item.ident.name,
clean::ImportSource {
path: clean::Path {
Expand Down
6 changes: 3 additions & 3 deletions src/librustdoc/clean/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2144,7 +2144,7 @@ fn clean_extern_crate(
source: krate.span.clean(cx),
def_id: crate_def_id,
visibility: krate.vis.clean(cx),
kind: ExternCrateItem(name, orig_name),
kind: box ExternCrateItem(name, orig_name),
}]
}

Expand Down Expand Up @@ -2212,7 +2212,7 @@ impl Clean<Vec<Item>> for doctree::Import<'_> {
source: self.span.clean(cx),
def_id: cx.tcx.hir().local_def_id(self.id).to_def_id(),
visibility: self.vis.clean(cx),
kind: ImportItem(Import::new_simple(
kind: box ImportItem(Import::new_simple(
self.name,
resolve_use_source(cx, path),
false,
Expand All @@ -2230,7 +2230,7 @@ impl Clean<Vec<Item>> for doctree::Import<'_> {
source: self.span.clean(cx),
def_id: cx.tcx.hir().local_def_id(self.id).to_def_id(),
visibility: self.vis.clean(cx),
kind: ImportItem(inner),
kind: box ImportItem(inner),
}]
}
}
Expand Down
12 changes: 6 additions & 6 deletions src/librustdoc/clean/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ crate struct Item {
crate name: Option<Symbol>,
crate attrs: Attributes,
crate visibility: Visibility,
crate kind: ItemKind,
crate kind: Box<ItemKind>,
crate def_id: DefId,
}

Expand Down Expand Up @@ -152,7 +152,7 @@ impl Item {

Item {
def_id,
kind,
kind: box kind,
name,
source: source.clean(cx),
attrs: cx.tcx.get_attrs(def_id).clean(cx),
Expand All @@ -171,7 +171,7 @@ impl Item {
}

crate fn is_crate(&self) -> bool {
match self.kind {
match *self.kind {
StrippedItem(box ModuleItem(Module { is_crate: true, .. }))
| ModuleItem(Module { is_crate: true, .. }) => true,
_ => false,
Expand Down Expand Up @@ -223,14 +223,14 @@ impl Item {
self.type_() == ItemType::Keyword
}
crate fn is_stripped(&self) -> bool {
match self.kind {
match *self.kind {
StrippedItem(..) => true,
ImportItem(ref i) => !i.should_be_displayed,
_ => false,
}
}
crate fn has_stripped_fields(&self) -> Option<bool> {
match self.kind {
match *self.kind {
StructItem(ref _struct) => Some(_struct.fields_stripped),
UnionItem(ref union) => Some(union.fields_stripped),
VariantItem(Variant { kind: VariantKind::Struct(ref vstruct) }) => {
Expand Down Expand Up @@ -281,7 +281,7 @@ impl Item {
}

crate fn is_default(&self) -> bool {
match self.kind {
match *self.kind {
ItemKind::MethodItem(_, Some(defaultness)) => {
defaultness.has_value() && !defaultness.is_final()
}
Expand Down
6 changes: 3 additions & 3 deletions src/librustdoc/clean/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ crate fn krate(mut cx: &mut DocContext<'_>) -> Crate {
let mut module = module.clean(cx);
let mut masked_crates = FxHashSet::default();

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

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

for item in items {
let target = match item.kind {
let target = match *item.kind {
ItemKind::TypedefItem(ref t, true) => &t.type_,
_ => continue,
};
Expand Down
8 changes: 4 additions & 4 deletions src/librustdoc/fold.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ crate struct StripItem(pub Item);
impl StripItem {
crate fn strip(self) -> Option<Item> {
match self.0 {
Item { kind: StrippedItem(..), .. } => Some(self.0),
Item { kind: box StrippedItem(..), .. } => Some(self.0),
mut i => {
i.kind = StrippedItem(box i.kind);
i.kind = box StrippedItem(i.kind);
Some(i)
}
}
Expand Down Expand Up @@ -72,9 +72,9 @@ crate trait DocFolder: Sized {

/// don't override!
fn fold_item_recur(&mut self, mut item: Item) -> Item {
item.kind = match item.kind {
item.kind = box match *item.kind {
StrippedItem(box i) => StrippedItem(box self.fold_inner_recur(i)),
_ => self.fold_inner_recur(item.kind),
_ => self.fold_inner_recur(*item.kind),
};
item
}
Expand Down
53 changes: 24 additions & 29 deletions src/librustdoc/formats/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ impl DocFolder for Cache {

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

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

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

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

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

match item.kind {
match *item.kind {
clean::StructItem(..)
| clean::EnumItem(..)
| clean::TypedefItem(..)
Expand Down Expand Up @@ -387,7 +387,7 @@ impl DocFolder for Cache {

// Maintain the parent stack
let orig_parent_is_trait_impl = self.parent_is_trait_impl;
let parent_pushed = match item.kind {
let parent_pushed = match *item.kind {
clean::TraitItem(..)
| clean::EnumItem(..)
| clean::ForeignTypeItem
Expand Down Expand Up @@ -425,38 +425,33 @@ impl DocFolder for Cache {
// Once we've recursively found all the generics, hoard off all the
// implementations elsewhere.
let item = self.fold_item_recur(item);
let ret = if let clean::Item { kind: clean::ImplItem(_), .. } = item {
let ret = if let clean::Item { kind: box clean::ImplItem(ref i), .. } = item {
// Figure out the id of this impl. This may map to a
// primitive rather than always to a struct/enum.
// Note: matching twice to restrict the lifetime of the `i` borrow.
let mut dids = FxHashSet::default();
if let clean::Item { kind: clean::ImplItem(ref i), .. } = item {
match i.for_ {
clean::ResolvedPath { did, .. }
| clean::BorrowedRef { type_: box clean::ResolvedPath { did, .. }, .. } => {
dids.insert(did);
}
ref t => {
let did = t
.primitive_type()
.and_then(|t| self.primitive_locations.get(&t).cloned());
match i.for_ {
clean::ResolvedPath { did, .. }
| clean::BorrowedRef { type_: box clean::ResolvedPath { did, .. }, .. } => {
dids.insert(did);
}
ref t => {
let did =
t.primitive_type().and_then(|t| self.primitive_locations.get(&t).cloned());

if let Some(did) = did {
dids.insert(did);
}
if let Some(did) = did {
dids.insert(did);
}
}
}

if let Some(generics) = i.trait_.as_ref().and_then(|t| t.generics()) {
for bound in generics {
if let Some(did) = bound.def_id() {
dids.insert(did);
}
if let Some(generics) = i.trait_.as_ref().and_then(|t| t.generics()) {
for bound in generics {
if let Some(did) = bound.def_id() {
dids.insert(did);
}
}
} else {
unreachable!()
};
}
let impl_item = Impl { impl_item: item };
if impl_item.trait_did().map_or(true, |d| self.traits.contains_key(&d)) {
for did in dids {
Expand Down
2 changes: 1 addition & 1 deletion src/librustdoc/formats/item_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ impl Serialize for ItemType {

impl<'a> From<&'a clean::Item> for ItemType {
fn from(item: &'a clean::Item) -> ItemType {
let kind = match item.kind {
let kind = match *item.kind {
clean::StrippedItem(box ref item) => item,
ref kind => kind,
};
Expand Down
2 changes: 1 addition & 1 deletion src/librustdoc/formats/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ crate struct Impl {

impl Impl {
crate fn inner_impl(&self) -> &clean::Impl {
match self.impl_item.kind {
match *self.impl_item.kind {
clean::ImplItem(ref impl_) => impl_,
_ => panic!("non-impl item found in impl"),
}
Expand Down
2 changes: 1 addition & 1 deletion src/librustdoc/formats/renderer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ crate fn run_format<'tcx, T: FormatRenderer<'tcx>>(
}

cx.mod_item_in(&item, &name, &cache)?;
let module = match item.kind {
let module = match *item.kind {
clean::StrippedItem(box clean::ModuleItem(m)) | clean::ModuleItem(m) => m,
_ => unreachable!(),
};
Expand Down
2 changes: 1 addition & 1 deletion src/librustdoc/html/render/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ crate fn build_index(krate: &clean::Crate, cache: &mut Cache) -> String {
}

crate fn get_index_search_type(item: &clean::Item) -> Option<IndexItemFunctionType> {
let (all_types, ret_types) = match item.kind {
let (all_types, ret_types) = match *item.kind {
clean::FunctionItem(ref f) => (&f.all_types, &f.ret_types),
clean::MethodItem(ref m, _) => (&m.all_types, &m.ret_types),
clean::TyMethodItem(ref m) => (&m.all_types, &m.ret_types),
Expand Down
Loading

0 comments on commit 4a44263

Please sign in to comment.