Skip to content

Commit eb48e97

Browse files
committed
Auto merge of rust-lang#109876 - jsha:uniquify-intra-doc, r=notriddle
rustdoc: make intra-doc link pass non-quadratic for repeated links In the collect_intra_doc_links pass, links to a given item that occurred repeatedly were getting inserted into a `Vec<clean::ItemLink>` repeatedly. This led to n^2 behavior (where n = the number of pages generated), particularly for the intra-doc link on the `Into<U> for T where U: From<T>` blanket implementation, since that link appears on every single struct page. Fixes rust-lang#109851
2 parents cf7ada2 + d9edb05 commit eb48e97

File tree

3 files changed

+14
-10
lines changed

3 files changed

+14
-10
lines changed

src/librustdoc/clean/types.rs

+11-7
Original file line numberDiff line numberDiff line change
@@ -452,10 +452,12 @@ impl Item {
452452
pub(crate) fn links(&self, cx: &Context<'_>) -> Vec<RenderedLink> {
453453
use crate::html::format::{href, link_tooltip};
454454

455-
cx.cache()
455+
let Some(links) = cx.cache()
456456
.intra_doc_links
457-
.get(&self.item_id)
458-
.map_or(&[][..], |v| v.as_slice())
457+
.get(&self.item_id) else {
458+
return vec![]
459+
};
460+
links
459461
.iter()
460462
.filter_map(|ItemLink { link: s, link_text, page_id: id, ref fragment }| {
461463
debug!(?id);
@@ -483,10 +485,12 @@ impl Item {
483485
/// the link text, but does need to know which `[]`-bracketed names
484486
/// are actually links.
485487
pub(crate) fn link_names(&self, cache: &Cache) -> Vec<RenderedLink> {
486-
cache
488+
let Some(links) = cache
487489
.intra_doc_links
488-
.get(&self.item_id)
489-
.map_or(&[][..], |v| v.as_slice())
490+
.get(&self.item_id) else {
491+
return vec![];
492+
};
493+
links
490494
.iter()
491495
.map(|ItemLink { link: s, link_text, .. }| RenderedLink {
492496
original_text: s.clone(),
@@ -1006,7 +1010,7 @@ pub(crate) fn collapse_doc_fragments(doc_strings: &[DocFragment]) -> String {
10061010
/// A link that has not yet been rendered.
10071011
///
10081012
/// This link will be turned into a rendered link by [`Item::links`].
1009-
#[derive(Clone, Debug, PartialEq, Eq)]
1013+
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
10101014
pub(crate) struct ItemLink {
10111015
/// The original link written in the markdown
10121016
pub(crate) link: Box<str>,

src/librustdoc/formats/cache.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::mem;
22

3-
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
3+
use rustc_data_structures::fx::{FxHashMap, FxHashSet, FxIndexSet};
44
use rustc_hir::def_id::{CrateNum, DefId, DefIdMap, DefIdSet};
55
use rustc_middle::ty::{self, TyCtxt};
66
use rustc_span::Symbol;
@@ -118,7 +118,7 @@ pub(crate) struct Cache {
118118
/// All intra-doc links resolved so far.
119119
///
120120
/// Links are indexed by the DefId of the item they document.
121-
pub(crate) intra_doc_links: FxHashMap<ItemId, Vec<clean::ItemLink>>,
121+
pub(crate) intra_doc_links: FxHashMap<ItemId, FxIndexSet<clean::ItemLink>>,
122122
/// Cfg that have been hidden via #![doc(cfg_hide(...))]
123123
pub(crate) hidden_cfg: FxHashSet<clean::cfg::Cfg>,
124124
}

src/librustdoc/passes/collect_intra_doc_links.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -978,7 +978,7 @@ impl LinkCollector<'_, '_> {
978978
for md_link in preprocessed_markdown_links(&doc) {
979979
let link = self.resolve_link(item, item_id, module_id, &doc, &md_link);
980980
if let Some(link) = link {
981-
self.cx.cache.intra_doc_links.entry(item.item_id).or_default().push(link);
981+
self.cx.cache.intra_doc_links.entry(item.item_id).or_default().insert(link);
982982
}
983983
}
984984
}

0 commit comments

Comments
 (0)