Skip to content

Commit 45d3dae

Browse files
committed
rustdoc: Store DefId's in ItemId on heap for decreasing Item's size
1 parent acd4dc2 commit 45d3dae

16 files changed

+115
-109
lines changed

src/librustdoc/clean/auto_trait.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ impl<'a, 'tcx> AutoTraitFinder<'a, 'tcx> {
113113
name: None,
114114
attrs: Default::default(),
115115
visibility: Inherited,
116-
def_id: ItemId::Auto { trait_: trait_def_id, for_: item_def_id },
116+
def_id: ItemId::Auto(box ImplId { trait_: trait_def_id, for_: item_def_id }),
117117
kind: box ImplItem(Impl {
118118
span: Span::dummy(),
119119
unsafety: hir::Unsafety::Normal,

src/librustdoc/clean/blanket_impl.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ impl<'a, 'tcx> BlanketImplFinder<'a, 'tcx> {
9696
name: None,
9797
attrs: Default::default(),
9898
visibility: Inherited,
99-
def_id: ItemId::Blanket { trait_: trait_def_id, for_: item_def_id },
99+
def_id: ItemId::Blanket(box ImplId { trait_: trait_def_id, for_: item_def_id }),
100100
kind: box ImplItem(Impl {
101101
span: self.cx.tcx.def_span(impl_def_id).clean(self.cx),
102102
unsafety: hir::Unsafety::Normal,

src/librustdoc/clean/types.rs

Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use rustc_data_structures::fx::{FxHashMap, FxHashSet};
1818
use rustc_data_structures::thin_vec::ThinVec;
1919
use rustc_hir as hir;
2020
use rustc_hir::def::{CtorKind, DefKind, Res};
21-
use rustc_hir::def_id::{CrateNum, DefId, DefIndex, CRATE_DEF_INDEX};
21+
use rustc_hir::def_id::{CrateNum, DefId, CRATE_DEF_INDEX, LOCAL_CRATE};
2222
use rustc_hir::lang_items::LangItem;
2323
use rustc_hir::{BodyId, Mutability};
2424
use rustc_index::vec::IndexVec;
@@ -50,61 +50,59 @@ use self::Type::*;
5050

5151
crate type ItemIdSet = FxHashSet<ItemId>;
5252

53-
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Copy)]
53+
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
54+
crate struct ImplId {
55+
crate trait_: DefId,
56+
crate for_: DefId,
57+
}
58+
59+
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
5460
crate enum ItemId {
5561
/// A "normal" item that uses a [`DefId`] for identification.
5662
DefId(DefId),
5763
/// Identifier that is used for auto traits.
58-
Auto { trait_: DefId, for_: DefId },
64+
Auto(Box<ImplId>),
5965
/// Identifier that is used for blanket implementations.
60-
Blanket { trait_: DefId, for_: DefId },
66+
Blanket(Box<ImplId>),
6167
/// Identifier for primitive types.
6268
Primitive(CrateNum),
6369
}
6470

6571
impl ItemId {
6672
#[inline]
67-
crate fn is_local(self) -> bool {
73+
crate fn is_local(&self) -> bool {
6874
match self {
69-
ItemId::Auto { for_: id, .. }
70-
| ItemId::Blanket { for_: id, .. }
75+
ItemId::Auto(box ImplId { for_: id, .. })
76+
| ItemId::Blanket(box ImplId { for_: id, .. })
7177
| ItemId::DefId(id) => id.is_local(),
72-
ItemId::Primitive(krate) => krate == LOCAL_CRATE,
78+
ItemId::Primitive(krate) => *krate == LOCAL_CRATE,
7379
}
7480
}
7581

7682
#[inline]
7783
#[track_caller]
78-
crate fn expect_def_id(self) -> DefId {
84+
crate fn expect_def_id(&self) -> DefId {
7985
self.as_def_id()
8086
.unwrap_or_else(|| panic!("ItemId::expect_def_id: `{:?}` isn't a DefId", self))
8187
}
8288

8389
#[inline]
84-
crate fn as_def_id(self) -> Option<DefId> {
90+
crate fn as_def_id(&self) -> Option<DefId> {
8591
match self {
86-
ItemId::DefId(id) => Some(id),
92+
ItemId::DefId(id) => Some(*id),
8793
_ => None,
8894
}
8995
}
9096

9197
#[inline]
92-
crate fn krate(self) -> CrateNum {
93-
match self {
94-
ItemId::Auto { for_: id, .. }
95-
| ItemId::Blanket { for_: id, .. }
98+
crate fn krate(&self) -> CrateNum {
99+
match *self {
100+
ItemId::Auto(box ImplId { for_: id, .. })
101+
| ItemId::Blanket(box ImplId { for_: id, .. })
96102
| ItemId::DefId(id) => id.krate,
97103
ItemId::Primitive(krate) => krate,
98104
}
99105
}
100-
101-
#[inline]
102-
crate fn index(self) -> Option<DefIndex> {
103-
match self {
104-
ItemId::DefId(id) => Some(id.index),
105-
_ => None,
106-
}
107-
}
108106
}
109107

110108
impl From<DefId> for ItemId {
@@ -379,7 +377,7 @@ impl Item {
379377
{
380378
*span
381379
} else {
382-
self.def_id.as_def_id().map(|did| rustc_span(did, tcx)).unwrap_or_else(|| Span::dummy())
380+
self.def_id.as_def_id().map(|did| rustc_span(did, tcx)).unwrap_or_else(Span::dummy)
383381
}
384382
}
385383

src/librustdoc/core.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -128,8 +128,8 @@ impl<'tcx> DocContext<'tcx> {
128128

129129
/// Like `hir().local_def_id_to_hir_id()`, but skips calling it on fake DefIds.
130130
/// (This avoids a slice-index-out-of-bounds panic.)
131-
crate fn as_local_hir_id(tcx: TyCtxt<'_>, def_id: ItemId) -> Option<HirId> {
132-
match def_id {
131+
crate fn as_local_hir_id(tcx: TyCtxt<'_>, def_id: &ItemId) -> Option<HirId> {
132+
match *def_id {
133133
ItemId::DefId(real_id) => {
134134
real_id.as_local().map(|def_id| tcx.hir().local_def_id_to_hir_id(def_id))
135135
}
@@ -432,7 +432,7 @@ crate fn run_global_ctxt(
432432
);
433433
tcx.struct_lint_node(
434434
crate::lint::MISSING_CRATE_LEVEL_DOCS,
435-
DocContext::as_local_hir_id(tcx, krate.module.def_id).unwrap(),
435+
DocContext::as_local_hir_id(tcx, &krate.module.def_id).unwrap(),
436436
|lint| {
437437
let mut diag =
438438
lint.build("no documentation found for this crate's top-level module");

src/librustdoc/formats/cache.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ impl<'a, 'tcx> DocFolder for CacheBuilder<'a, 'tcx> {
290290
// A crate has a module at its root, containing all items,
291291
// which should not be indexed. The crate-item itself is
292292
// inserted later on when serializing the search-index.
293-
if item.def_id.index().map_or(false, |idx| idx != CRATE_DEF_INDEX) {
293+
if item.def_id.as_def_id().map_or(false, |did| did.index != CRATE_DEF_INDEX) {
294294
let desc = item.doc_value().map_or_else(String::new, |x| {
295295
short_markdown_summary(&x.as_str(), &item.link_names(&self.cache))
296296
});

src/librustdoc/html/render/mod.rs

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -753,7 +753,7 @@ fn assoc_const(
753753
w,
754754
"{}{}const <a href=\"{}\" class=\"constant\"><b>{}</b></a>: {}",
755755
extra,
756-
it.visibility.print_with_space(it.def_id, cx),
756+
it.visibility.print_with_space(it.def_id.clone(), cx),
757757
naive_assoc_href(it, link, cx),
758758
it.name.as_ref().unwrap(),
759759
ty.print(cx)
@@ -872,7 +872,7 @@ fn render_assoc_item(
872872
.unwrap_or_else(|| format!("#{}.{}", ty, name))
873873
}
874874
};
875-
let vis = meth.visibility.print_with_space(meth.def_id, cx).to_string();
875+
let vis = meth.visibility.print_with_space(meth.def_id.clone(), cx).to_string();
876876
let constness =
877877
print_constness_with_space(&header.constness, meth.const_stability(cx.tcx()));
878878
let asyncness = header.asyncness.print_with_space();
@@ -984,7 +984,7 @@ fn render_attributes_in_code(w: &mut Buffer, it: &clean::Item) {
984984
}
985985
}
986986

987-
#[derive(Copy, Clone)]
987+
#[derive(Clone)]
988988
enum AssocItemLink<'a> {
989989
Anchor(Option<&'a str>),
990990
GotoSource(ItemId, &'a FxHashSet<Symbol>),
@@ -994,7 +994,7 @@ impl<'a> AssocItemLink<'a> {
994994
fn anchor(&self, id: &'a str) -> Self {
995995
match *self {
996996
AssocItemLink::Anchor(_) => AssocItemLink::Anchor(Some(&id)),
997-
ref other => *other,
997+
ref other => other.clone(),
998998
}
999999
}
10001000
}
@@ -1306,7 +1306,14 @@ fn render_impl(
13061306
} else {
13071307
// In case the item isn't documented,
13081308
// provide short documentation from the trait.
1309-
document_short(&mut doc_buffer, it, cx, link, parent, show_def_docs);
1309+
document_short(
1310+
&mut doc_buffer,
1311+
it,
1312+
cx,
1313+
link.clone(),
1314+
parent,
1315+
show_def_docs,
1316+
);
13101317
}
13111318
}
13121319
} else {
@@ -1317,7 +1324,7 @@ fn render_impl(
13171324
}
13181325
}
13191326
} else {
1320-
document_short(&mut doc_buffer, item, cx, link, parent, show_def_docs);
1327+
document_short(&mut doc_buffer, item, cx, link.clone(), parent, show_def_docs);
13211328
}
13221329
}
13231330
let w = if short_documented && trait_.is_some() { interesting } else { boring };
@@ -1445,7 +1452,7 @@ fn render_impl(
14451452
trait_item,
14461453
if trait_.is_some() { &i.impl_item } else { parent },
14471454
parent,
1448-
link,
1455+
link.clone(),
14491456
render_mode,
14501457
false,
14511458
trait_.map(|t| &t.trait_),

src/librustdoc/html/render/print_item.rs

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ fn item_module(w: &mut Buffer, cx: &Context<'_>, item: &clean::Item, items: &[cl
245245
// (which is the position in the vector).
246246
indices.dedup_by_key(|i| {
247247
(
248-
items[*i].def_id,
248+
items[*i].def_id.clone(),
249249
if items[*i].name.as_ref().is_some() { Some(full_path(cx, &items[*i])) } else { None },
250250
items[*i].type_(),
251251
if items[*i].is_import() { *i } else { 0 },
@@ -288,14 +288,14 @@ fn item_module(w: &mut Buffer, cx: &Context<'_>, item: &clean::Item, items: &[cl
288288
Some(ref src) => write!(
289289
w,
290290
"<div class=\"item-left\"><code>{}extern crate {} as {};",
291-
myitem.visibility.print_with_space(myitem.def_id, cx),
291+
myitem.visibility.print_with_space(myitem.def_id.clone(), cx),
292292
anchor(myitem.def_id.expect_def_id(), &*src.as_str(), cx),
293293
myitem.name.as_ref().unwrap(),
294294
),
295295
None => write!(
296296
w,
297297
"<div class=\"item-left\"><code>{}extern crate {};",
298-
myitem.visibility.print_with_space(myitem.def_id, cx),
298+
myitem.visibility.print_with_space(myitem.def_id.clone(), cx),
299299
anchor(
300300
myitem.def_id.expect_def_id(),
301301
&*myitem.name.as_ref().unwrap().as_str(),
@@ -336,7 +336,7 @@ fn item_module(w: &mut Buffer, cx: &Context<'_>, item: &clean::Item, items: &[cl
336336
<div class=\"item-right docblock-short\">{stab_tags}</div>",
337337
stab = stab.unwrap_or_default(),
338338
add = add,
339-
vis = myitem.visibility.print_with_space(myitem.def_id, cx),
339+
vis = myitem.visibility.print_with_space(myitem.def_id.clone(), cx),
340340
imp = import.print(cx),
341341
stab_tags = stab_tags.unwrap_or_default(),
342342
);
@@ -437,7 +437,7 @@ fn extra_info_tags(item: &clean::Item, parent: &clean::Item, tcx: TyCtxt<'_>) ->
437437
}
438438

439439
fn item_function(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, f: &clean::Function) {
440-
let vis = it.visibility.print_with_space(it.def_id, cx).to_string();
440+
let vis = it.visibility.print_with_space(it.def_id.clone(), cx).to_string();
441441
let constness = print_constness_with_space(&f.header.constness, it.const_stability(cx.tcx()));
442442
let asyncness = f.header.asyncness.print_with_space();
443443
let unsafety = f.header.unsafety.print_with_space();
@@ -489,7 +489,7 @@ fn item_trait(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, t: &clean::Tra
489489
write!(
490490
w,
491491
"{}{}{}trait {}{}{}",
492-
it.visibility.print_with_space(it.def_id, cx),
492+
it.visibility.print_with_space(it.def_id.clone(), cx),
493493
t.unsafety.print_with_space(),
494494
if t.is_auto { "auto " } else { "" },
495495
it.name.as_ref().unwrap(),
@@ -710,8 +710,10 @@ fn item_trait(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, t: &clean::Tra
710710

711711
for implementor in foreign {
712712
let provided_methods = implementor.inner_impl().provided_trait_methods(cx.tcx());
713-
let assoc_link =
714-
AssocItemLink::GotoSource(implementor.impl_item.def_id, &provided_methods);
713+
let assoc_link = AssocItemLink::GotoSource(
714+
implementor.impl_item.def_id.clone(),
715+
&provided_methods,
716+
);
715717
render_impl(
716718
w,
717719
cx,
@@ -915,7 +917,7 @@ fn item_enum(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, e: &clean::Enum
915917
write!(
916918
w,
917919
"{}enum {}{}{}",
918-
it.visibility.print_with_space(it.def_id, cx),
920+
it.visibility.print_with_space(it.def_id.clone(), cx),
919921
it.name.as_ref().unwrap(),
920922
e.generics.print(cx),
921923
print_where_clause(&e.generics, cx, 0, true),
@@ -1103,7 +1105,7 @@ fn item_constant(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, c: &clean::
11031105
write!(
11041106
w,
11051107
"{vis}const {name}: {typ}",
1106-
vis = it.visibility.print_with_space(it.def_id, cx),
1108+
vis = it.visibility.print_with_space(it.def_id.clone(), cx),
11071109
name = it.name.as_ref().unwrap(),
11081110
typ = c.type_.print(cx),
11091111
);
@@ -1193,7 +1195,7 @@ fn item_static(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, s: &clean::St
11931195
write!(
11941196
w,
11951197
"{vis}static {mutability}{name}: {typ}</pre>",
1196-
vis = it.visibility.print_with_space(it.def_id, cx),
1198+
vis = it.visibility.print_with_space(it.def_id.clone(), cx),
11971199
mutability = s.mutability.print_with_space(),
11981200
name = it.name.as_ref().unwrap(),
11991201
typ = s.type_.print(cx)
@@ -1207,7 +1209,7 @@ fn item_foreign_type(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item) {
12071209
write!(
12081210
w,
12091211
" {}type {};\n}}</pre>",
1210-
it.visibility.print_with_space(it.def_id, cx),
1212+
it.visibility.print_with_space(it.def_id.clone(), cx),
12111213
it.name.as_ref().unwrap(),
12121214
);
12131215

@@ -1362,7 +1364,7 @@ fn render_union(
13621364
write!(
13631365
w,
13641366
"{}{}{}",
1365-
it.visibility.print_with_space(it.def_id, cx),
1367+
it.visibility.print_with_space(it.def_id.clone(), cx),
13661368
if structhead { "union " } else { "" },
13671369
it.name.as_ref().unwrap()
13681370
);
@@ -1384,7 +1386,7 @@ fn render_union(
13841386
write!(
13851387
w,
13861388
" {}{}: {},\n{}",
1387-
field.visibility.print_with_space(field.def_id, cx),
1389+
field.visibility.print_with_space(field.def_id.clone(), cx),
13881390
field.name.as_ref().unwrap(),
13891391
ty.print(cx),
13901392
tab
@@ -1414,7 +1416,7 @@ fn render_struct(
14141416
write!(
14151417
w,
14161418
"{}{}{}",
1417-
it.visibility.print_with_space(it.def_id, cx),
1419+
it.visibility.print_with_space(it.def_id.clone(), cx),
14181420
if structhead { "struct " } else { "" },
14191421
it.name.as_ref().unwrap()
14201422
);
@@ -1440,7 +1442,7 @@ fn render_struct(
14401442
w,
14411443
"\n{} {}{}: {},",
14421444
tab,
1443-
field.visibility.print_with_space(field.def_id, cx),
1445+
field.visibility.print_with_space(field.def_id.clone(), cx),
14441446
field.name.as_ref().unwrap(),
14451447
ty.print(cx),
14461448
);
@@ -1474,7 +1476,7 @@ fn render_struct(
14741476
write!(
14751477
w,
14761478
"{}{}",
1477-
field.visibility.print_with_space(field.def_id, cx),
1479+
field.visibility.print_with_space(field.def_id.clone(), cx),
14781480
ty.print(cx),
14791481
)
14801482
}

0 commit comments

Comments
 (0)