Skip to content

Commit 5ea7e13

Browse files
committed
avoid in-process serializing&deserializing
1 parent 15c5e34 commit 5ea7e13

File tree

2 files changed

+166
-176
lines changed

2 files changed

+166
-176
lines changed

src/librustdoc/html/render/mod.rs

Lines changed: 71 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ use rustc_middle::ty::print::PrintTraitRefExt;
6161
use rustc_middle::ty::{self, TyCtxt};
6262
use rustc_span::symbol::{Symbol, sym};
6363
use rustc_span::{BytePos, DUMMY_SP, FileName, RealFileName};
64+
use serde::ser::SerializeSeq as _;
65+
use serde::{Deserialize, Serialize};
6466
use tracing::{debug, info};
6567

6668
pub(crate) use self::context::*;
@@ -144,7 +146,7 @@ pub(crate) struct IndexItem {
144146
}
145147

146148
/// A type used for the search index.
147-
#[derive(Debug, Eq, PartialEq)]
149+
#[derive(Clone, Debug, Eq, PartialEq)]
148150
struct RenderType {
149151
id: Option<RenderTypeId>,
150152
generics: Option<Vec<RenderType>>,
@@ -311,7 +313,7 @@ impl RenderTypeId {
311313
}
312314

313315
/// Full type of functions/methods in the search index.
314-
#[derive(Debug, Eq, PartialEq)]
316+
#[derive(Clone, Debug, Eq, PartialEq)]
315317
pub(crate) struct IndexItemFunctionType {
316318
inputs: Vec<RenderType>,
317319
output: Vec<RenderType>,
@@ -410,6 +412,73 @@ impl IndexItemFunctionType {
410412
}
411413
}
412414

415+
impl Serialize for IndexItemFunctionType {
416+
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
417+
where
418+
S: serde::Serializer,
419+
{
420+
let mut seq = serializer.serialize_seq(Some(2))?;
421+
seq.serialize_element(&self.write_to_string_without_param_names().to_string())?;
422+
423+
struct ParamNames<'a>(&'a [Option<Symbol>]);
424+
425+
impl<'a> Serialize for ParamNames<'a> {
426+
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
427+
where
428+
S: serde::Serializer,
429+
{
430+
serializer.collect_seq(
431+
self.0
432+
.iter()
433+
.map(|symbol| symbol.as_ref().map(ToString::to_string).unwrap_or_default()),
434+
)
435+
}
436+
}
437+
438+
seq.serialize_element(&ParamNames(&self.param_names))?;
439+
seq.end()
440+
}
441+
}
442+
443+
impl<'de> Deserialize<'de> for IndexItemFunctionType {
444+
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
445+
where
446+
D: serde::Deserializer<'de>,
447+
{
448+
use serde::de::{self, Error as _};
449+
450+
struct FunctionDataVisitor;
451+
impl<'de> de::Visitor<'de> for FunctionDataVisitor {
452+
type Value = IndexItemFunctionType;
453+
fn expecting(&self, formatter: &mut std::fmt::Formatter<'_>) -> fmt::Result {
454+
write!(formatter, "fn data")
455+
}
456+
fn visit_seq<A: de::SeqAccess<'de>>(self, mut v: A) -> Result<Self::Value, A::Error> {
457+
let (mut function_signature, _) = v
458+
.next_element()?
459+
.map(|fn_: String| {
460+
IndexItemFunctionType::read_from_string_without_param_names(fn_.as_bytes())
461+
})
462+
.ok_or_else(|| A::Error::missing_field("function_signature"))?;
463+
let param_names: Vec<Option<Symbol>> = v
464+
.next_element()?
465+
.map(|param_names: Vec<String>| {
466+
param_names
467+
.into_iter()
468+
.map(|symbol| {
469+
if symbol.is_empty() { None } else { Some(Symbol::intern(&symbol)) }
470+
})
471+
.collect()
472+
})
473+
.ok_or_else(|| A::Error::missing_field("param_names"))?;
474+
function_signature.param_names = param_names;
475+
Ok(function_signature)
476+
}
477+
}
478+
deserializer.deserialize_any(FunctionDataVisitor)
479+
}
480+
}
481+
413482
#[derive(Debug, Clone)]
414483
pub(crate) struct StylePath {
415484
/// The path to the theme

0 commit comments

Comments
 (0)