Skip to content

Commit 4461dd9

Browse files
authored
Rollup merge of #147402 - yotamofek:wip/rustdoc/search_index/impl-display, r=GuillaumeGomez
[rustdoc] Don't serialize & deserialize data that doesn't go OTW `@GuillaumeGomez`
2 parents 7ab1fd1 + c5ab530 commit 4461dd9

File tree

3 files changed

+203
-178
lines changed

3 files changed

+203
-178
lines changed

src/librustdoc/html/render/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ pub(crate) struct IndexItem {
144144
}
145145

146146
/// A type used for the search index.
147-
#[derive(Debug, Eq, PartialEq)]
147+
#[derive(Clone, Debug, Eq, PartialEq)]
148148
struct RenderType {
149149
id: Option<RenderTypeId>,
150150
generics: Option<Vec<RenderType>>,
@@ -301,7 +301,7 @@ impl RenderTypeId {
301301
}
302302

303303
/// Full type of functions/methods in the search index.
304-
#[derive(Debug, Eq, PartialEq)]
304+
#[derive(Clone, Debug, Eq, PartialEq)]
305305
pub(crate) struct IndexItemFunctionType {
306306
inputs: Vec<RenderType>,
307307
output: Vec<RenderType>,

src/librustdoc/html/render/search_index.rs

Lines changed: 99 additions & 176 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
pub(crate) mod encode;
2+
mod serde;
23

34
use std::collections::BTreeSet;
45
use std::collections::hash_map::Entry;
56
use std::path::Path;
67

8+
use ::serde::de::{self, Deserializer, Error as _};
9+
use ::serde::ser::{SerializeSeq, Serializer};
10+
use ::serde::{Deserialize, Serialize};
711
use rustc_ast::join_path_syms;
812
use rustc_data_structures::fx::{FxHashMap, FxHashSet, FxIndexMap};
913
use rustc_hir::attrs::AttributeKind;
@@ -12,9 +16,6 @@ use rustc_middle::ty::TyCtxt;
1216
use rustc_span::def_id::DefId;
1317
use rustc_span::sym;
1418
use rustc_span::symbol::{Symbol, kw};
15-
use serde::de::{self, Deserializer, Error as _};
16-
use serde::ser::{SerializeSeq, Serializer};
17-
use serde::{Deserialize, Serialize};
1819
use stringdex::internals as stringdex_internals;
1920
use thin_vec::ThinVec;
2021
use tracing::instrument;
@@ -34,7 +35,7 @@ pub(crate) struct SerializedSearchIndex {
3435
path_data: Vec<Option<PathData>>,
3536
entry_data: Vec<Option<EntryData>>,
3637
descs: Vec<String>,
37-
function_data: Vec<Option<FunctionData>>,
38+
function_data: Vec<Option<IndexItemFunctionType>>,
3839
alias_pointers: Vec<Option<usize>>,
3940
// inverted index for concrete types and generics
4041
type_data: Vec<Option<TypeData>>,
@@ -61,7 +62,7 @@ impl SerializedSearchIndex {
6162
let mut path_data: Vec<Option<PathData>> = Vec::new();
6263
let mut entry_data: Vec<Option<EntryData>> = Vec::new();
6364
let mut descs: Vec<String> = Vec::new();
64-
let mut function_data: Vec<Option<FunctionData>> = Vec::new();
65+
let mut function_data: Vec<Option<IndexItemFunctionType>> = Vec::new();
6566
let mut type_data: Vec<Option<TypeData>> = Vec::new();
6667
let mut alias_pointers: Vec<Option<usize>> = Vec::new();
6768

@@ -207,7 +208,7 @@ impl SerializedSearchIndex {
207208
path_data: Option<PathData>,
208209
entry_data: Option<EntryData>,
209210
desc: String,
210-
function_data: Option<FunctionData>,
211+
function_data: Option<IndexItemFunctionType>,
211212
type_data: Option<TypeData>,
212213
alias_pointer: Option<usize>,
213214
) -> usize {
@@ -446,73 +447,62 @@ impl SerializedSearchIndex {
446447
..other_entry_data.clone()
447448
}),
448449
other.descs[other_entryid].clone(),
449-
other.function_data[other_entryid].as_ref().map(|function_data| FunctionData {
450-
function_signature: {
451-
let (mut func, _offset) =
452-
IndexItemFunctionType::read_from_string_without_param_names(
453-
function_data.function_signature.as_bytes(),
454-
);
455-
fn map_fn_sig_item(
456-
map_other_pathid_to_self_pathid: &mut Vec<usize>,
457-
ty: &mut RenderType,
458-
) {
459-
match ty.id {
460-
None => {}
461-
Some(RenderTypeId::Index(generic)) if generic < 0 => {}
462-
Some(RenderTypeId::Index(id)) => {
463-
let id = usize::try_from(id).unwrap();
464-
let id = map_other_pathid_to_self_pathid[id];
465-
assert!(id != !0);
466-
ty.id =
467-
Some(RenderTypeId::Index(isize::try_from(id).unwrap()));
468-
}
469-
_ => unreachable!(),
450+
other.function_data[other_entryid].clone().map(|mut func| {
451+
fn map_fn_sig_item(
452+
map_other_pathid_to_self_pathid: &mut Vec<usize>,
453+
ty: &mut RenderType,
454+
) {
455+
match ty.id {
456+
None => {}
457+
Some(RenderTypeId::Index(generic)) if generic < 0 => {}
458+
Some(RenderTypeId::Index(id)) => {
459+
let id = usize::try_from(id).unwrap();
460+
let id = map_other_pathid_to_self_pathid[id];
461+
assert!(id != !0);
462+
ty.id = Some(RenderTypeId::Index(isize::try_from(id).unwrap()));
470463
}
471-
if let Some(generics) = &mut ty.generics {
472-
for generic in generics {
473-
map_fn_sig_item(map_other_pathid_to_self_pathid, generic);
474-
}
464+
_ => unreachable!(),
465+
}
466+
if let Some(generics) = &mut ty.generics {
467+
for generic in generics {
468+
map_fn_sig_item(map_other_pathid_to_self_pathid, generic);
475469
}
476-
if let Some(bindings) = &mut ty.bindings {
477-
for (param, constraints) in bindings {
478-
*param = match *param {
479-
param @ RenderTypeId::Index(generic) if generic < 0 => {
480-
param
481-
}
482-
RenderTypeId::Index(id) => {
483-
let id = usize::try_from(id).unwrap();
484-
let id = map_other_pathid_to_self_pathid[id];
485-
assert!(id != !0);
486-
RenderTypeId::Index(isize::try_from(id).unwrap())
487-
}
488-
_ => unreachable!(),
489-
};
490-
for constraint in constraints {
491-
map_fn_sig_item(
492-
map_other_pathid_to_self_pathid,
493-
constraint,
494-
);
470+
}
471+
if let Some(bindings) = &mut ty.bindings {
472+
for (param, constraints) in bindings {
473+
*param = match *param {
474+
param @ RenderTypeId::Index(generic) if generic < 0 => {
475+
param
476+
}
477+
RenderTypeId::Index(id) => {
478+
let id = usize::try_from(id).unwrap();
479+
let id = map_other_pathid_to_self_pathid[id];
480+
assert!(id != !0);
481+
RenderTypeId::Index(isize::try_from(id).unwrap())
495482
}
483+
_ => unreachable!(),
484+
};
485+
for constraint in constraints {
486+
map_fn_sig_item(
487+
map_other_pathid_to_self_pathid,
488+
constraint,
489+
);
496490
}
497491
}
498492
}
499-
for input in &mut func.inputs {
500-
map_fn_sig_item(&mut map_other_pathid_to_self_pathid, input);
501-
}
502-
for output in &mut func.output {
503-
map_fn_sig_item(&mut map_other_pathid_to_self_pathid, output);
504-
}
505-
for clause in &mut func.where_clause {
506-
for entry in clause {
507-
map_fn_sig_item(&mut map_other_pathid_to_self_pathid, entry);
508-
}
493+
}
494+
for input in &mut func.inputs {
495+
map_fn_sig_item(&mut map_other_pathid_to_self_pathid, input);
496+
}
497+
for output in &mut func.output {
498+
map_fn_sig_item(&mut map_other_pathid_to_self_pathid, output);
499+
}
500+
for clause in &mut func.where_clause {
501+
for entry in clause {
502+
map_fn_sig_item(&mut map_other_pathid_to_self_pathid, entry);
509503
}
510-
let mut result =
511-
String::with_capacity(function_data.function_signature.len());
512-
func.write_to_string_without_param_names(&mut result);
513-
result
514-
},
515-
param_names: function_data.param_names.clone(),
504+
}
505+
func
516506
}),
517507
other.type_data[other_entryid].as_ref().map(|type_data| TypeData {
518508
inverted_function_inputs_index: type_data
@@ -626,69 +616,55 @@ impl SerializedSearchIndex {
626616
},
627617
),
628618
self.descs[id].clone(),
629-
self.function_data[id].as_ref().map(
630-
|FunctionData { function_signature, param_names }| FunctionData {
631-
function_signature: {
632-
let (mut func, _offset) =
633-
IndexItemFunctionType::read_from_string_without_param_names(
634-
function_signature.as_bytes(),
635-
);
636-
fn map_fn_sig_item(map: &FxHashMap<usize, usize>, ty: &mut RenderType) {
637-
match ty.id {
638-
None => {}
639-
Some(RenderTypeId::Index(generic)) if generic < 0 => {}
640-
Some(RenderTypeId::Index(id)) => {
619+
self.function_data[id].clone().map(|mut func| {
620+
fn map_fn_sig_item(map: &FxHashMap<usize, usize>, ty: &mut RenderType) {
621+
match ty.id {
622+
None => {}
623+
Some(RenderTypeId::Index(generic)) if generic < 0 => {}
624+
Some(RenderTypeId::Index(id)) => {
625+
let id = usize::try_from(id).unwrap();
626+
let id = *map.get(&id).unwrap();
627+
assert!(id != !0);
628+
ty.id = Some(RenderTypeId::Index(isize::try_from(id).unwrap()));
629+
}
630+
_ => unreachable!(),
631+
}
632+
if let Some(generics) = &mut ty.generics {
633+
for generic in generics {
634+
map_fn_sig_item(map, generic);
635+
}
636+
}
637+
if let Some(bindings) = &mut ty.bindings {
638+
for (param, constraints) in bindings {
639+
*param = match *param {
640+
param @ RenderTypeId::Index(generic) if generic < 0 => param,
641+
RenderTypeId::Index(id) => {
641642
let id = usize::try_from(id).unwrap();
642643
let id = *map.get(&id).unwrap();
643644
assert!(id != !0);
644-
ty.id =
645-
Some(RenderTypeId::Index(isize::try_from(id).unwrap()));
645+
RenderTypeId::Index(isize::try_from(id).unwrap())
646646
}
647647
_ => unreachable!(),
648+
};
649+
for constraint in constraints {
650+
map_fn_sig_item(map, constraint);
648651
}
649-
if let Some(generics) = &mut ty.generics {
650-
for generic in generics {
651-
map_fn_sig_item(map, generic);
652-
}
653-
}
654-
if let Some(bindings) = &mut ty.bindings {
655-
for (param, constraints) in bindings {
656-
*param = match *param {
657-
param @ RenderTypeId::Index(generic) if generic < 0 => {
658-
param
659-
}
660-
RenderTypeId::Index(id) => {
661-
let id = usize::try_from(id).unwrap();
662-
let id = *map.get(&id).unwrap();
663-
assert!(id != !0);
664-
RenderTypeId::Index(isize::try_from(id).unwrap())
665-
}
666-
_ => unreachable!(),
667-
};
668-
for constraint in constraints {
669-
map_fn_sig_item(map, constraint);
670-
}
671-
}
672-
}
673-
}
674-
for input in &mut func.inputs {
675-
map_fn_sig_item(&map, input);
676-
}
677-
for output in &mut func.output {
678-
map_fn_sig_item(&map, output);
679652
}
680-
for clause in &mut func.where_clause {
681-
for entry in clause {
682-
map_fn_sig_item(&map, entry);
683-
}
684-
}
685-
let mut result = String::with_capacity(function_signature.len());
686-
func.write_to_string_without_param_names(&mut result);
687-
result
688-
},
689-
param_names: param_names.clone(),
690-
},
691-
),
653+
}
654+
}
655+
for input in &mut func.inputs {
656+
map_fn_sig_item(&map, input);
657+
}
658+
for output in &mut func.output {
659+
map_fn_sig_item(&map, output);
660+
}
661+
for clause in &mut func.where_clause {
662+
for entry in clause {
663+
map_fn_sig_item(&map, entry);
664+
}
665+
}
666+
func
667+
}),
692668
self.type_data[id].as_ref().map(
693669
|TypeData {
694670
search_unbox,
@@ -1259,48 +1235,6 @@ impl<'de> Deserialize<'de> for SerializedOptional32 {
12591235
}
12601236
}
12611237

1262-
#[derive(Clone, Debug)]
1263-
pub struct FunctionData {
1264-
function_signature: String,
1265-
param_names: Vec<String>,
1266-
}
1267-
1268-
impl Serialize for FunctionData {
1269-
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
1270-
where
1271-
S: Serializer,
1272-
{
1273-
let mut seq = serializer.serialize_seq(None)?;
1274-
seq.serialize_element(&self.function_signature)?;
1275-
seq.serialize_element(&self.param_names)?;
1276-
seq.end()
1277-
}
1278-
}
1279-
1280-
impl<'de> Deserialize<'de> for FunctionData {
1281-
fn deserialize<D>(deserializer: D) -> Result<FunctionData, D::Error>
1282-
where
1283-
D: Deserializer<'de>,
1284-
{
1285-
struct FunctionDataVisitor;
1286-
impl<'de> de::Visitor<'de> for FunctionDataVisitor {
1287-
type Value = FunctionData;
1288-
fn expecting(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
1289-
write!(formatter, "fn data")
1290-
}
1291-
fn visit_seq<A: de::SeqAccess<'de>>(self, mut v: A) -> Result<FunctionData, A::Error> {
1292-
let function_signature: String = v
1293-
.next_element()?
1294-
.ok_or_else(|| A::Error::missing_field("function_signature"))?;
1295-
let param_names: Vec<String> =
1296-
v.next_element()?.ok_or_else(|| A::Error::missing_field("param_names"))?;
1297-
Ok(FunctionData { function_signature, param_names })
1298-
}
1299-
}
1300-
deserializer.deserialize_any(FunctionDataVisitor)
1301-
}
1302-
}
1303-
13041238
/// Builds the search index from the collected metadata
13051239
pub(crate) fn build_index(
13061240
krate: &clean::Crate,
@@ -1927,18 +1861,7 @@ pub(crate) fn build_index(
19271861
// because the postings list has to fill in an empty array for each
19281862
// unoccupied size.
19291863
if item.ty.is_fn_like() { 0 } else { 16 };
1930-
serialized_index.function_data[new_entry_id] = Some(FunctionData {
1931-
function_signature: {
1932-
let mut function_signature = String::new();
1933-
search_type.write_to_string_without_param_names(&mut function_signature);
1934-
function_signature
1935-
},
1936-
param_names: search_type
1937-
.param_names
1938-
.iter()
1939-
.map(|sym| sym.map(|sym| sym.to_string()).unwrap_or(String::new()))
1940-
.collect::<Vec<String>>(),
1941-
});
1864+
serialized_index.function_data[new_entry_id] = Some(search_type.clone());
19421865
for index in used_in_function_inputs {
19431866
let postings = if index >= 0 {
19441867
assert!(serialized_index.path_data[index as usize].is_some());

0 commit comments

Comments
 (0)