Skip to content

Commit

Permalink
Auto merge of rust-lang#117007 - notriddle:notriddle/format-links-wit…
Browse files Browse the repository at this point in the history
…h-display, r=fmease

rustdoc: avoid allocating strings primitive link printing

This is aimed at hitting the allocator less in a function that gets called a lot.
  • Loading branch information
bors committed Oct 22, 2023
2 parents 3c92dcc + 4851cc9 commit 6aeac60
Showing 1 changed file with 51 additions and 26 deletions.
77 changes: 51 additions & 26 deletions src/librustdoc/html/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -847,7 +847,7 @@ fn resolved_path<'cx>(
fn primitive_link(
f: &mut fmt::Formatter<'_>,
prim: clean::PrimitiveType,
name: &str,
name: fmt::Arguments<'_>,
cx: &Context<'_>,
) -> fmt::Result {
primitive_link_fragment(f, prim, name, "", cx)
Expand All @@ -856,7 +856,7 @@ fn primitive_link(
fn primitive_link_fragment(
f: &mut fmt::Formatter<'_>,
prim: clean::PrimitiveType,
name: &str,
name: fmt::Arguments<'_>,
fragment: &str,
cx: &Context<'_>,
) -> fmt::Result {
Expand Down Expand Up @@ -907,7 +907,7 @@ fn primitive_link_fragment(
None => {}
}
}
f.write_str(name)?;
std::fmt::Display::fmt(&name, f)?;
if needs_termination {
write!(f, "</a>")?;
}
Expand Down Expand Up @@ -977,9 +977,11 @@ fn fmt_type<'cx>(
}
clean::Infer => write!(f, "_"),
clean::Primitive(clean::PrimitiveType::Never) => {
primitive_link(f, PrimitiveType::Never, "!", cx)
primitive_link(f, PrimitiveType::Never, format_args!("!"), cx)
}
clean::Primitive(prim) => {
primitive_link(f, prim, format_args!("{}", prim.as_sym().as_str()), cx)
}
clean::Primitive(prim) => primitive_link(f, prim, prim.as_sym().as_str(), cx),
clean::BareFunction(ref decl) => {
if f.alternate() {
write!(
Expand All @@ -998,16 +1000,16 @@ fn fmt_type<'cx>(
decl.unsafety.print_with_space(),
print_abi_with_space(decl.abi)
)?;
primitive_link(f, PrimitiveType::Fn, "fn", cx)?;
primitive_link(f, PrimitiveType::Fn, format_args!("fn"), cx)?;
write!(f, "{}", decl.decl.print(cx))
}
}
clean::Tuple(ref typs) => {
match &typs[..] {
&[] => primitive_link(f, PrimitiveType::Unit, "()", cx),
&[] => primitive_link(f, PrimitiveType::Unit, format_args!("()"), cx),
[one] => {
if let clean::Generic(name) = one {
primitive_link(f, PrimitiveType::Tuple, &format!("({name},)"), cx)
primitive_link(f, PrimitiveType::Tuple, format_args!("({name},)"), cx)
} else {
write!(f, "(")?;
// Carry `f.alternate()` into this display w/o branching manually.
Expand All @@ -1028,7 +1030,10 @@ fn fmt_type<'cx>(
primitive_link(
f,
PrimitiveType::Tuple,
&format!("({})", generic_names.iter().map(|s| s.as_str()).join(", ")),
format_args!(
"({})",
generic_names.iter().map(|s| s.as_str()).join(", ")
),
cx,
)
} else {
Expand All @@ -1047,7 +1052,7 @@ fn fmt_type<'cx>(
}
clean::Slice(ref t) => match **t {
clean::Generic(name) => {
primitive_link(f, PrimitiveType::Slice, &format!("[{name}]"), cx)
primitive_link(f, PrimitiveType::Slice, format_args!("[{name}]"), cx)
}
_ => {
write!(f, "[")?;
Expand All @@ -1059,7 +1064,7 @@ fn fmt_type<'cx>(
clean::Generic(name) if !f.alternate() => primitive_link(
f,
PrimitiveType::Array,
&format!("[{name}; {n}]", n = Escape(n)),
format_args!("[{name}; {n}]", n = Escape(n)),
cx,
),
_ => {
Expand All @@ -1069,7 +1074,12 @@ fn fmt_type<'cx>(
write!(f, "; {n}")?;
} else {
write!(f, "; ")?;
primitive_link(f, PrimitiveType::Array, &format!("{n}", n = Escape(n)), cx)?;
primitive_link(
f,
PrimitiveType::Array,
format_args!("{n}", n = Escape(n)),
cx,
)?;
}
write!(f, "]")
}
Expand All @@ -1081,30 +1091,40 @@ fn fmt_type<'cx>(
};

if matches!(**t, clean::Generic(_)) || t.is_assoc_ty() {
let text = if f.alternate() {
format!("*{m} {ty:#}", ty = t.print(cx))
let ty = t.print(cx);
if f.alternate() {
primitive_link(
f,
clean::PrimitiveType::RawPointer,
format_args!("*{m} {ty:#}"),
cx,
)
} else {
format!("*{m} {ty}", ty = t.print(cx))
};
primitive_link(f, clean::PrimitiveType::RawPointer, &text, cx)
primitive_link(
f,
clean::PrimitiveType::RawPointer,
format_args!("*{m} {ty}"),
cx,
)
}
} else {
primitive_link(f, clean::PrimitiveType::RawPointer, &format!("*{m} "), cx)?;
primitive_link(f, clean::PrimitiveType::RawPointer, format_args!("*{m} "), cx)?;
fmt::Display::fmt(&t.print(cx), f)
}
}
clean::BorrowedRef { lifetime: ref l, mutability, type_: ref ty } => {
let lt = match l {
Some(l) => format!("{} ", l.print()),
_ => String::new(),
};
let lt = display_fn(|f| match l {
Some(l) => write!(f, "{} ", l.print()),
_ => Ok(()),
});
let m = mutability.print_with_space();
let amp = if f.alternate() { "&" } else { "&amp;" };

if let clean::Generic(name) = **ty {
return primitive_link(
f,
PrimitiveType::Reference,
&format!("{amp}{lt}{m}{name}"),
format_args!("{amp}{lt}{m}{name}"),
cx,
);
}
Expand Down Expand Up @@ -1254,7 +1274,7 @@ impl clean::Impl {
{
// Hardcoded anchor library/core/src/primitive_docs.rs
// Link should match `# Trait implementations`
primitive_link_fragment(f, PrimitiveType::Tuple, &format!("({name}₁, {name}₂, …, {name}ₙ)"), "#trait-implementations-1", cx)?;
primitive_link_fragment(f, PrimitiveType::Tuple, format_args!("({name}₁, {name}₂, …, {name}ₙ)"), "#trait-implementations-1", cx)?;
} else if let clean::BareFunction(bare_fn) = &self.for_ &&
let [clean::Argument { type_: clean::Type::Generic(name), .. }] = &bare_fn.decl.inputs.values[..] &&
(self.kind.is_fake_variadic() || self.kind.is_auto())
Expand All @@ -1281,7 +1301,7 @@ impl clean::Impl {
} else {
""
};
primitive_link_fragment(f, PrimitiveType::Tuple, &format!("fn ({name}₁, {name}₂, …, {name}ₙ{ellipsis})"), "#trait-implementations-1", cx)?;
primitive_link_fragment(f, PrimitiveType::Tuple, format_args!("fn ({name}₁, {name}₂, …, {name}ₙ{ellipsis})"), "#trait-implementations-1", cx)?;
// Write output.
if !bare_fn.decl.output.is_unit() {
write!(f, " -> ")?;
Expand Down Expand Up @@ -1665,7 +1685,12 @@ impl clean::ImportSource {
}
let name = self.path.last();
if let hir::def::Res::PrimTy(p) = self.path.res {
primitive_link(f, PrimitiveType::from(p), name.as_str(), cx)?;
primitive_link(
f,
PrimitiveType::from(p),
format_args!("{}", name.as_str()),
cx,
)?;
} else {
f.write_str(name.as_str())?;
}
Expand Down

0 comments on commit 6aeac60

Please sign in to comment.