Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit f76ecd0

Browse files
committedDec 15, 2020
Auto merge of #80044 - jyn514:smaller-name, r=GuillaumeGomez
[rustdoc] Switch to Symbol for item.name This decreases the size of `Item` from 680 to 616 bytes. It also does a lot less work since it no longer has to copy as much. Helps with #79103. r? `@GuillaumeGomez`
2 parents e15ec66 + a16904f commit f76ecd0

File tree

10 files changed

+78
-46
lines changed

10 files changed

+78
-46
lines changed
 

‎compiler/rustc_span/src/symbol.rs

+4
Original file line numberDiff line numberDiff line change
@@ -1469,6 +1469,10 @@ impl Symbol {
14691469
self.0.as_u32()
14701470
}
14711471

1472+
pub fn is_empty(self) -> bool {
1473+
self == kw::Invalid
1474+
}
1475+
14721476
/// This method is supposed to be used in error messages, so it's expected to be
14731477
/// identical to printing the original identifier token written in source code
14741478
/// (`token_to_string`, `Ident::to_string`), except that symbols don't keep the rawness flag

‎src/librustdoc/clean/inline.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ crate fn try_inline(
124124
let attrs = merge_attrs(cx, Some(parent_module), target_attrs, attrs_clone);
125125

126126
cx.renderinfo.borrow_mut().inlined.insert(did);
127-
let what_rustc_thinks = clean::Item::from_def_id_and_parts(did, Some(name.clean(cx)), kind, cx);
127+
let what_rustc_thinks = clean::Item::from_def_id_and_parts(did, Some(name), kind, cx);
128128
ret.push(clean::Item { attrs, ..what_rustc_thinks });
129129
Some(ret)
130130
}

‎src/librustdoc/clean/mod.rs

+9-14
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ impl Clean<ExternalCrate> for CrateNum {
169169
for attr in attrs.lists(sym::doc) {
170170
if attr.has_name(sym::keyword) {
171171
if let Some(v) = attr.value_str() {
172-
keyword = Some(v.to_string());
172+
keyword = Some(v);
173173
break;
174174
}
175175
}
@@ -253,12 +253,7 @@ impl Clean<Item> for doctree::Module<'_> {
253253
ModuleItem(Module { is_crate: self.is_crate, items }),
254254
cx,
255255
);
256-
Item {
257-
name: Some(what_rustc_thinks.name.unwrap_or_default()),
258-
attrs,
259-
source: span.clean(cx),
260-
..what_rustc_thinks
261-
}
256+
Item { attrs, source: span.clean(cx), ..what_rustc_thinks }
262257
}
263258
}
264259

@@ -1096,7 +1091,7 @@ impl Clean<Item> for hir::TraitItem<'_> {
10961091
AssocTypeItem(bounds.clean(cx), default.clean(cx))
10971092
}
10981093
};
1099-
Item::from_def_id_and_parts(local_did, Some(self.ident.name.clean(cx)), inner, cx)
1094+
Item::from_def_id_and_parts(local_did, Some(self.ident.name), inner, cx)
11001095
})
11011096
}
11021097
}
@@ -1124,7 +1119,7 @@ impl Clean<Item> for hir::ImplItem<'_> {
11241119
TypedefItem(Typedef { type_, generics: Generics::default(), item_type }, true)
11251120
}
11261121
};
1127-
Item::from_def_id_and_parts(local_did, Some(self.ident.name.clean(cx)), inner, cx)
1122+
Item::from_def_id_and_parts(local_did, Some(self.ident.name), inner, cx)
11281123
})
11291124
}
11301125
}
@@ -1281,7 +1276,7 @@ impl Clean<Item> for ty::AssocItem {
12811276
}
12821277
};
12831278

1284-
Item::from_def_id_and_parts(self.def_id, Some(self.ident.name.clean(cx)), kind, cx)
1279+
Item::from_def_id_and_parts(self.def_id, Some(self.ident.name), kind, cx)
12851280
}
12861281
}
12871282

@@ -1771,7 +1766,7 @@ impl Clean<Item> for ty::FieldDef {
17711766
fn clean(&self, cx: &DocContext<'_>) -> Item {
17721767
let what_rustc_thinks = Item::from_def_id_and_parts(
17731768
self.did,
1774-
Some(self.ident.name.clean(cx)),
1769+
Some(self.ident.name),
17751770
StructFieldItem(cx.tcx.type_of(self.did).clean(cx)),
17761771
cx,
17771772
);
@@ -1847,7 +1842,7 @@ impl Clean<Item> for ty::VariantDef {
18471842
.fields
18481843
.iter()
18491844
.map(|field| {
1850-
let name = Some(field.ident.name.clean(cx));
1845+
let name = Some(field.ident.name);
18511846
let kind = StructFieldItem(cx.tcx.type_of(field.did).clean(cx));
18521847
let what_rustc_thinks =
18531848
Item::from_def_id_and_parts(field.did, name, kind, cx);
@@ -1859,7 +1854,7 @@ impl Clean<Item> for ty::VariantDef {
18591854
};
18601855
let what_rustc_thinks = Item::from_def_id_and_parts(
18611856
self.def_id,
1862-
Some(self.ident.name.clean(cx)),
1857+
Some(self.ident.name),
18631858
VariantItem(Variant { kind }),
18641859
cx,
18651860
);
@@ -2033,7 +2028,7 @@ impl Clean<Vec<Item>> for (&hir::Item<'_>, Option<Symbol>) {
20332028
_ => unreachable!("not yet converted"),
20342029
};
20352030

2036-
vec![Item::from_def_id_and_parts(def_id, Some(name.clean(cx)), kind, cx)]
2031+
vec![Item::from_def_id_and_parts(def_id, Some(name), kind, cx)]
20372032
})
20382033
}
20392034
}

‎src/librustdoc/clean/types.rs

+38-10
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ crate struct ExternalCrate {
7070
crate src: FileName,
7171
crate attrs: Attributes,
7272
crate primitives: Vec<(DefId, PrimitiveType)>,
73-
crate keywords: Vec<(DefId, String)>,
73+
crate keywords: Vec<(DefId, Symbol)>,
7474
}
7575

7676
/// Anything with a source location and set of attributes and, optionally, a
@@ -81,7 +81,7 @@ crate struct Item {
8181
/// Stringified span
8282
crate source: Span,
8383
/// Not everything has a name. E.g., impls
84-
crate name: Option<String>,
84+
crate name: Option<Symbol>,
8585
crate attrs: Attributes,
8686
crate visibility: Visibility,
8787
crate kind: ItemKind,
@@ -123,17 +123,12 @@ impl Item {
123123
kind: ItemKind,
124124
cx: &DocContext<'_>,
125125
) -> Item {
126-
Item::from_def_id_and_parts(
127-
cx.tcx.hir().local_def_id(hir_id).to_def_id(),
128-
name.clean(cx),
129-
kind,
130-
cx,
131-
)
126+
Item::from_def_id_and_parts(cx.tcx.hir().local_def_id(hir_id).to_def_id(), name, kind, cx)
132127
}
133128

134129
pub fn from_def_id_and_parts(
135130
def_id: DefId,
136-
name: Option<String>,
131+
name: Option<Symbol>,
137132
kind: ItemKind,
138133
cx: &DocContext<'_>,
139134
) -> Item {
@@ -334,7 +329,7 @@ crate enum ItemKind {
334329
AssocTypeItem(Vec<GenericBound>, Option<Type>),
335330
/// An item that has been stripped by a rustdoc pass
336331
StrippedItem(Box<ItemKind>),
337-
KeywordItem(String),
332+
KeywordItem(Symbol),
338333
}
339334

340335
impl ItemKind {
@@ -1163,6 +1158,8 @@ crate enum Type {
11631158
}
11641159

11651160
#[derive(Clone, PartialEq, Eq, Hash, Copy, Debug)]
1161+
/// N.B. this has to be different from `hir::PrimTy` because it also includes types that aren't
1162+
/// paths, like `Unit`.
11661163
crate enum PrimitiveType {
11671164
Isize,
11681165
I8,
@@ -1502,6 +1499,37 @@ impl PrimitiveType {
15021499
crate fn to_url_str(&self) -> &'static str {
15031500
self.as_str()
15041501
}
1502+
1503+
crate fn as_sym(&self) -> Symbol {
1504+
use PrimitiveType::*;
1505+
match self {
1506+
Isize => sym::isize,
1507+
I8 => sym::i8,
1508+
I16 => sym::i16,
1509+
I32 => sym::i32,
1510+
I64 => sym::i64,
1511+
I128 => sym::i128,
1512+
Usize => sym::usize,
1513+
U8 => sym::u8,
1514+
U16 => sym::u16,
1515+
U32 => sym::u32,
1516+
U64 => sym::u64,
1517+
U128 => sym::u128,
1518+
F32 => sym::f32,
1519+
F64 => sym::f64,
1520+
Str => sym::str,
1521+
Bool => sym::bool,
1522+
Char => sym::char,
1523+
Array => sym::array,
1524+
Slice => sym::slice,
1525+
Tuple => sym::tuple,
1526+
Unit => sym::unit,
1527+
RawPointer => sym::pointer,
1528+
Reference => sym::reference,
1529+
Fn => kw::Fn,
1530+
Never => sym::never,
1531+
}
1532+
}
15051533
}
15061534

15071535
impl From<ast::IntTy> for PrimitiveType {

‎src/librustdoc/clean/utils.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ crate fn krate(mut cx: &mut DocContext<'_>) -> Crate {
6868
m.items.extend(primitives.iter().map(|&(def_id, prim)| {
6969
Item::from_def_id_and_parts(
7070
def_id,
71-
Some(prim.to_url_str().to_owned()),
71+
Some(prim.as_sym()),
7272
ItemKind::PrimitiveItem(prim),
7373
cx,
7474
)

‎src/librustdoc/formats/renderer.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use std::sync::Arc;
33
use rustc_data_structures::sync::Lrc;
44
use rustc_session::Session;
55
use rustc_span::edition::Edition;
6+
use rustc_span::Symbol;
67

78
use crate::clean;
89
use crate::config::{RenderInfo, RenderOptions};
@@ -75,7 +76,7 @@ crate fn run_format<T: FormatRenderer>(
7576
None => return Ok(()),
7677
};
7778

78-
item.name = Some(krate.name.clone());
79+
item.name = Some(Symbol::intern(&krate.name));
7980

8081
// Render the crate documentation
8182
let mut work = vec![(format_renderer.clone(), item)];

‎src/librustdoc/html/render/cache.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ crate fn build_index(krate: &clean::Crate, cache: &mut Cache) -> String {
7676
if let Some(&(ref fqp, _)) = paths.get(&did) {
7777
search_index.push(IndexItem {
7878
ty: item.type_(),
79-
name: item.name.clone().unwrap(),
79+
name: item.name.unwrap().to_string(),
8080
path: fqp[..fqp.len() - 1].join("::"),
8181
desc: item.doc_value().map_or_else(|| String::new(), short_markdown_summary),
8282
parent: Some(did),

‎src/librustdoc/html/render/mod.rs

+12-15
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ use rustc_session::Session;
6161
use rustc_span::edition::Edition;
6262
use rustc_span::hygiene::MacroKind;
6363
use rustc_span::source_map::FileName;
64-
use rustc_span::symbol::{sym, Symbol};
64+
use rustc_span::symbol::{kw, sym, Symbol};
6565
use serde::ser::SerializeSeq;
6666
use serde::{Serialize, Serializer};
6767

@@ -665,7 +665,7 @@ impl FormatRenderer for Context {
665665
if !buf.is_empty() {
666666
let name = item.name.as_ref().unwrap();
667667
let item_type = item.type_();
668-
let file_name = &item_path(item_type, name);
668+
let file_name = &item_path(item_type, &name.as_str());
669669
self.shared.ensure_dir(&self.dst)?;
670670
let joint_dst = self.dst.join(file_name);
671671
self.shared.fs.write(&joint_dst, buf.as_bytes())?;
@@ -1543,7 +1543,7 @@ impl Context {
15431543
if !title.is_empty() {
15441544
title.push_str("::");
15451545
}
1546-
title.push_str(it.name.as_ref().unwrap());
1546+
title.push_str(&it.name.unwrap().as_str());
15471547
}
15481548
title.push_str(" - Rust");
15491549
let tyname = it.type_();
@@ -1815,7 +1815,7 @@ fn item_path(ty: ItemType, name: &str) -> String {
18151815
fn full_path(cx: &Context, item: &clean::Item) -> String {
18161816
let mut s = cx.current.join("::");
18171817
s.push_str("::");
1818-
s.push_str(item.name.as_ref().unwrap());
1818+
s.push_str(&item.name.unwrap().as_str());
18191819
s
18201820
}
18211821

@@ -2065,9 +2065,9 @@ fn item_module(w: &mut Buffer, cx: &Context, item: &clean::Item, items: &[clean:
20652065
(true, false) => return Ordering::Greater,
20662066
}
20672067
}
2068-
let lhs = i1.name.as_ref().map_or("", |s| &**s);
2069-
let rhs = i2.name.as_ref().map_or("", |s| &**s);
2070-
compare_names(lhs, rhs)
2068+
let lhs = i1.name.unwrap_or(kw::Invalid).as_str();
2069+
let rhs = i2.name.unwrap_or(kw::Invalid).as_str();
2070+
compare_names(&lhs, &rhs)
20712071
}
20722072

20732073
if cx.shared.sort_modules_alphabetically {
@@ -2191,7 +2191,7 @@ fn item_module(w: &mut Buffer, cx: &Context, item: &clean::Item, items: &[clean:
21912191
add = add,
21922192
stab = stab.unwrap_or_else(String::new),
21932193
unsafety_flag = unsafety_flag,
2194-
href = item_path(myitem.type_(), myitem.name.as_ref().unwrap()),
2194+
href = item_path(myitem.type_(), &myitem.name.unwrap().as_str()),
21952195
title = [full_path(cx, myitem), myitem.type_().to_string()]
21962196
.iter()
21972197
.filter_map(|s| if !s.is_empty() { Some(s.as_str()) } else { None })
@@ -2623,7 +2623,7 @@ fn item_trait(w: &mut Buffer, cx: &Context, it: &clean::Item, t: &clean::Trait,
26232623

26242624
fn trait_item(w: &mut Buffer, cx: &Context, m: &clean::Item, t: &clean::Item, cache: &Cache) {
26252625
let name = m.name.as_ref().unwrap();
2626-
info!("Documenting {} on {}", name, t.name.as_deref().unwrap_or_default());
2626+
info!("Documenting {} on {:?}", name, t.name);
26272627
let item_type = m.type_();
26282628
let id = cx.derive_id(format!("{}.{}", item_type, name));
26292629
write!(w, "<h3 id=\"{id}\" class=\"method\"><code>", id = id,);
@@ -2951,7 +2951,7 @@ fn render_assoc_item(
29512951
AssocItemLink::GotoSource(did, provided_methods) => {
29522952
// We're creating a link from an impl-item to the corresponding
29532953
// trait-item and need to map the anchored type accordingly.
2954-
let ty = if provided_methods.contains(name) {
2954+
let ty = if provided_methods.contains(&*name.as_str()) {
29552955
ItemType::Method
29562956
} else {
29572957
ItemType::TyMethod
@@ -3434,10 +3434,7 @@ fn render_assoc_items(
34343434
what: AssocItemRender<'_>,
34353435
cache: &Cache,
34363436
) {
3437-
info!(
3438-
"Documenting associated items of {}",
3439-
containing_item.name.as_deref().unwrap_or_default()
3440-
);
3437+
info!("Documenting associated items of {:?}", containing_item.name);
34413438
let v = match cache.impls.get(&it) {
34423439
Some(v) => v,
34433440
None => return,
@@ -4139,7 +4136,7 @@ fn print_sidebar(cx: &Context, it: &clean::Item, buffer: &mut Buffer, cache: &Ca
41394136
ty: \"{ty}\", \
41404137
relpath: \"{path}\"\
41414138
}};</script>",
4142-
name = it.name.as_ref().map(|x| &x[..]).unwrap_or(""),
4139+
name = it.name.unwrap_or(kw::Invalid),
41434140
ty = it.type_(),
41444141
path = relpath
41454142
);

‎src/librustdoc/json/conversions.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ impl JsonRenderer {
3333
_ => Some(Item {
3434
id: def_id.into(),
3535
crate_id: def_id.krate.as_u32(),
36-
name,
36+
name: name.map(|sym| sym.to_string()),
3737
source: self.convert_span(source),
3838
visibility: visibility.into(),
3939
docs: attrs.collapsed_doc_value().unwrap_or_default(),

‎src/librustdoc/passes/collect_intra_doc_links.rs

+9-2
Original file line numberDiff line numberDiff line change
@@ -696,7 +696,7 @@ fn resolve_associated_trait_item(
696696
// Give precedence to methods that were overridden
697697
if !impl_.provided_trait_methods.contains(&*item_name.as_str()) {
698698
let mut items = impl_.items.into_iter().filter_map(|assoc| {
699-
if assoc.name.as_deref() != Some(&*item_name.as_str()) {
699+
if assoc.name != Some(item_name) {
700700
return None;
701701
}
702702
let kind = assoc
@@ -2015,7 +2015,14 @@ fn privacy_error(
20152015
dox: &str,
20162016
link_range: Option<Range<usize>>,
20172017
) {
2018-
let item_name = item.name.as_deref().unwrap_or("<unknown>");
2018+
let sym;
2019+
let item_name = match item.name {
2020+
Some(name) => {
2021+
sym = name.as_str();
2022+
&*sym
2023+
}
2024+
None => "<unknown>",
2025+
};
20192026
let msg =
20202027
format!("public documentation for `{}` links to private item `{}`", item_name, path_str);
20212028

0 commit comments

Comments
 (0)
Failed to load comments.