Skip to content

Commit b255ecd

Browse files
Rollup merge of #162976 - xonx4l:rustdoc-sidebar-quadratic, r=lolbinarycat,GuillaumeGomez
fix quadratic naming of duplicate sidebar links This PR fixes quadratic naming of duplicate sidebar links as discussed in #158174. Fixes #158174 `get_next_url` gives duplicate sidebar links with unique names but it started counting from 1 every time which makes the page quadratic. So to make it more optimal I changed that to remember the next unique number for each name making it linear per page. Also added the test at `tests/rustdoc-html/deref/sidebar-links-deref-chain.rs` ### Results | N | Instructions (before) | Instructions (after) | Time (before) | Time (after) | |-----:|----------------------:|---------------------:|--------------:|-------------:| | 200 | 5.71 B | 3.69 B | 1.20 s | 1.01 s | | 400 | 22.40 B | 9.52 B | 3.79 s | 1.98 s | | 800 | 126.83 B | 30.82 B | 19.68 s | 6.04 s | | 1600 | — | — | 118.59 s | 37.76 s | r? @GuillaumeGomez
2 parents 3670d25 + 1875276 commit b255ecd

2 files changed

Lines changed: 35 additions & 15 deletions

File tree

‎src/librustdoc/html/render/sidebar.rs‎

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::cmp::Ordering;
33
use std::fmt;
44

55
use askama::Template;
6-
use rustc_data_structures::fx::FxHashSet;
6+
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
77
use rustc_hir::def::CtorKind;
88
use rustc_hir::def_id::{DefIdMap, DefIdSet};
99
use rustc_middle::ty::TyCtxt;
@@ -442,7 +442,7 @@ fn sidebar_assoc_items<'a>(
442442
let mut assoc_fns = Vec::new();
443443
let mut methods = Vec::new();
444444
if let Some(v) = cache.impls.get(&did) {
445-
let mut used_links = FxHashSet::default();
445+
let mut used_links = UsedLinks::default();
446446
let mut id_map = IdMap::new();
447447

448448
{
@@ -524,7 +524,7 @@ fn sidebar_deref_methods<'a>(
524524
impl_: &Impl,
525525
v: &[Impl],
526526
derefs: &mut DefIdSet,
527-
used_links: &mut FxHashSet<String>,
527+
used_links: &mut UsedLinks,
528528
deref_id_map: &'a DefIdMap<String>,
529529
) {
530530
let c = cx.cache();
@@ -745,15 +745,14 @@ fn sidebar_render_assoc_items(
745745
]);
746746
}
747747

748-
fn get_next_url(used_links: &mut FxHashSet<String>, url: String) -> String {
749-
if used_links.insert(url.clone()) {
750-
return url;
751-
}
752-
let mut add = 1;
753-
while !used_links.insert(format!("{url}-{add}")) {
754-
add += 1;
755-
}
756-
format!("{url}-{add}")
748+
/// Tracks sidebar link anchors so duplicates get unique names.
749+
type UsedLinks = FxHashMap<String, usize>;
750+
751+
fn get_next_url(used_links: &mut UsedLinks, url: String) -> String {
752+
let count = used_links.entry(url.clone()).or_insert(0);
753+
let res = if *count == 0 { url } else { format!("{url}-{count}") };
754+
*count += 1;
755+
res
757756
}
758757

759758
enum GetMethodsMode<'r, 'l> {
@@ -764,7 +763,7 @@ enum GetMethodsMode<'r, 'l> {
764763
fn get_methods<'a>(
765764
i: &'a clean::Impl,
766765
mut mode: GetMethodsMode<'_, 'a>,
767-
used_links: &mut FxHashSet<String>,
766+
used_links: &mut UsedLinks,
768767
tcx: TyCtxt<'_>,
769768
) -> impl Iterator<Item = Link<'a>> {
770769
i.items.iter().filter_map(move |item| {
@@ -802,7 +801,7 @@ fn get_methods<'a>(
802801

803802
fn get_associated_constants<'a>(
804803
i: &'a clean::Impl,
805-
used_links: &mut FxHashSet<String>,
804+
used_links: &mut UsedLinks,
806805
) -> impl Iterator<Item = Link<'a>> {
807806
i.items.iter().filter_map(|item| {
808807
if let Some(ref name) = item.name
@@ -820,7 +819,7 @@ fn get_associated_constants<'a>(
820819

821820
fn get_associated_types<'a>(
822821
i: &'a clean::Impl,
823-
used_links: &mut FxHashSet<String>,
822+
used_links: &mut UsedLinks,
824823
) -> impl Iterator<Item = Link<'a>> {
825824
i.items.iter().filter_map(|item| {
826825
if let Some(ref name) = item.name
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Regression test for <https://github.com/rust-lang/rust/issues/158174>.
2+
#![crate_name = "foo"]
3+
4+
pub struct S0;
5+
pub struct S1;
6+
pub struct S2;
7+
pub struct S3;
8+
9+
impl S0 { pub fn foo(&self) {} }
10+
impl S1 { pub fn foo(&self) {} }
11+
impl S2 { pub fn foo(&self) {} }
12+
impl S3 { pub fn foo(&self) {} }
13+
14+
impl std::ops::Deref for S0 { type Target = S1; fn deref(&self) -> &S1 { &S1 } }
15+
impl std::ops::Deref for S1 { type Target = S2; fn deref(&self) -> &S2 { &S2 } }
16+
impl std::ops::Deref for S2 { type Target = S3; fn deref(&self) -> &S3 { &S3 } }
17+
18+
//@ has foo/struct.S0.html '//*[@class="sidebar-elems"]//section//li/a[@href="#method.foo"]' 'foo'
19+
//@ has foo/struct.S0.html '//*[@class="sidebar-elems"]//section//li/a[@href="#method.foo-1"]' 'foo'
20+
//@ has foo/struct.S0.html '//*[@class="sidebar-elems"]//section//li/a[@href="#method.foo-2"]' 'foo'
21+
//@ has foo/struct.S0.html '//*[@class="sidebar-elems"]//section//li/a[@href="#method.foo-3"]' 'foo'

0 commit comments

Comments
 (0)