Skip to content

Commit 2953edc

Browse files
committed
Auto merge of #98475 - notriddle:notriddle/index-fn-signatures, r=GuillaumeGomez
rustdoc: reference function signature types from the `p` array This reduces the size of the function signature index, because it's common to have many functions that operate on the same types. $ wc -c search-index-old.js search-index-new.js 5224374 search-index-old.js 3932314 search-index-new.js By my math, this reduces the uncompressed size of the search index by 32%. On compressed signatures, the wins are less drastic, a mere 8%: $ wc -c search-index-old.js.gz search-index-new.js.gz 404532 search-index-old.js.gz 371635 search-index-new.js.gz
2 parents 8308806 + 33cf9ea commit 2953edc

File tree

7 files changed

+380
-146
lines changed

7 files changed

+380
-146
lines changed

src/librustdoc/clean/types.rs

-4
Original file line numberDiff line numberDiff line change
@@ -1671,10 +1671,6 @@ impl Type {
16711671
matches!(self, Type::ImplTrait(_))
16721672
}
16731673

1674-
pub(crate) fn is_primitive(&self) -> bool {
1675-
self.primitive_type().is_some()
1676-
}
1677-
16781674
pub(crate) fn projection(&self) -> Option<(&Type, DefId, PathSegment)> {
16791675
if let QPath { self_type, trait_, assoc, .. } = self {
16801676
Some((self_type, trait_.def_id(), *assoc.clone()))

src/librustdoc/formats/item_type.rs

-2
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ pub(crate) enum ItemType {
4848
ProcAttribute = 23,
4949
ProcDerive = 24,
5050
TraitAlias = 25,
51-
Generic = 26,
5251
}
5352

5453
impl Serialize for ItemType {
@@ -175,7 +174,6 @@ impl ItemType {
175174
ItemType::ProcAttribute => "attr",
176175
ItemType::ProcDerive => "derive",
177176
ItemType::TraitAlias => "traitalias",
178-
ItemType::Generic => "generic",
179177
}
180178
}
181179
}

src/librustdoc/html/render/mod.rs

+45-37
Original file line numberDiff line numberDiff line change
@@ -110,63 +110,72 @@ pub(crate) struct IndexItem {
110110
/// A type used for the search index.
111111
#[derive(Debug)]
112112
pub(crate) struct RenderType {
113-
name: Option<String>,
114-
generics: Option<Vec<TypeWithKind>>,
113+
id: Option<RenderTypeId>,
114+
generics: Option<Vec<RenderType>>,
115115
}
116116

117-
/// Full type of functions/methods in the search index.
118-
#[derive(Debug)]
119-
pub(crate) struct IndexItemFunctionType {
120-
inputs: Vec<TypeWithKind>,
121-
output: Vec<TypeWithKind>,
122-
}
123-
124-
impl Serialize for IndexItemFunctionType {
117+
impl Serialize for RenderType {
125118
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
126119
where
127120
S: Serializer,
128121
{
129-
// If we couldn't figure out a type, just write `null`.
130-
let has_missing = self.inputs.iter().chain(self.output.iter()).any(|i| i.ty.name.is_none());
131-
if has_missing {
132-
serializer.serialize_none()
133-
} else {
122+
let id = match &self.id {
123+
// 0 is a sentinel, everything else is one-indexed
124+
None => 0,
125+
Some(RenderTypeId::Index(idx)) => idx + 1,
126+
_ => panic!("must convert render types to indexes before serializing"),
127+
};
128+
if let Some(generics) = &self.generics {
134129
let mut seq = serializer.serialize_seq(None)?;
135-
seq.serialize_element(&self.inputs)?;
136-
match self.output.as_slice() {
137-
[] => {}
138-
[one] => seq.serialize_element(one)?,
139-
all => seq.serialize_element(all)?,
140-
}
130+
seq.serialize_element(&id)?;
131+
seq.serialize_element(generics)?;
141132
seq.end()
133+
} else {
134+
id.serialize(serializer)
142135
}
143136
}
144137
}
145138

146-
#[derive(Debug)]
147-
pub(crate) struct TypeWithKind {
148-
ty: RenderType,
149-
kind: ItemType,
139+
#[derive(Clone, Debug)]
140+
pub(crate) enum RenderTypeId {
141+
DefId(DefId),
142+
Primitive(clean::PrimitiveType),
143+
Index(usize),
150144
}
151145

152-
impl From<(RenderType, ItemType)> for TypeWithKind {
153-
fn from(x: (RenderType, ItemType)) -> TypeWithKind {
154-
TypeWithKind { ty: x.0, kind: x.1 }
155-
}
146+
/// Full type of functions/methods in the search index.
147+
#[derive(Debug)]
148+
pub(crate) struct IndexItemFunctionType {
149+
inputs: Vec<RenderType>,
150+
output: Vec<RenderType>,
156151
}
157152

158-
impl Serialize for TypeWithKind {
153+
impl Serialize for IndexItemFunctionType {
159154
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
160155
where
161156
S: Serializer,
162157
{
163-
let mut seq = serializer.serialize_seq(None)?;
164-
seq.serialize_element(&self.ty.name)?;
165-
seq.serialize_element(&self.kind)?;
166-
if let Some(generics) = &self.ty.generics {
167-
seq.serialize_element(generics)?;
158+
// If we couldn't figure out a type, just write `0`.
159+
let has_missing = self
160+
.inputs
161+
.iter()
162+
.chain(self.output.iter())
163+
.any(|i| i.id.is_none() && i.generics.is_none());
164+
if has_missing {
165+
0.serialize(serializer)
166+
} else {
167+
let mut seq = serializer.serialize_seq(None)?;
168+
match &self.inputs[..] {
169+
[one] if one.generics.is_none() => seq.serialize_element(one)?,
170+
_ => seq.serialize_element(&self.inputs)?,
171+
}
172+
match &self.output[..] {
173+
[] => {}
174+
[one] if one.generics.is_none() => seq.serialize_element(one)?,
175+
_ => seq.serialize_element(&self.output)?,
176+
}
177+
seq.end()
168178
}
169-
seq.end()
170179
}
171180
}
172181

@@ -2517,7 +2526,6 @@ fn item_ty_to_section(ty: ItemType) -> ItemSection {
25172526
ItemType::ProcAttribute => ItemSection::AttributeMacros,
25182527
ItemType::ProcDerive => ItemSection::DeriveMacros,
25192528
ItemType::TraitAlias => ItemSection::TraitAliases,
2520-
ItemType::Generic => unreachable!(),
25212529
}
25222530
}
25232531

0 commit comments

Comments
 (0)