Skip to content

Commit 5dae27a

Browse files
authored
Rollup merge of #83237 - notriddle:short-links, r=jyn514
rustdoc: use more precise relative URLs This is a fairly large diff, and will probably conflict with #82815 since it reduces (but does not eliminate) the use of the old depth variable. Instead of using a depth counter and adding "../" to get to the top, this commit makes rustdoc actually compare the path of what it's linking from to the path that it's linking to. This makes the resulting HTML shorter. Here's a comparison of one of the largest (non-source) files in the Rust standard library docs (about 4% improvement before gzipping). $ wc -c struct.Wrapping.old.html struct.Wrapping.new.html 2387389 struct.Wrapping.old.html 2298538 struct.Wrapping.new.html Most if it can be efficiently gzipped away. $ wc -c struct.Wrapping.old.html.gz struct.Wrapping.new.html.gz 70679 struct.Wrapping.old.html.gz 70050 struct.Wrapping.new.html.gz But it also makes a difference in the final DOM size, reducing it from 91MiB to 82MiB.
2 parents 42bee5a + 755b4fb commit 5dae27a

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+576
-547
lines changed

src/librustdoc/clean/types.rs

+21-12
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ use crate::core::DocContext;
4141
use crate::formats::cache::Cache;
4242
use crate::formats::item_type::ItemType;
4343
use crate::html::render::cache::ExternalLocation;
44+
use crate::html::render::Context;
4445

4546
use self::FnRetTy::*;
4647
use self::ItemKind::*;
@@ -193,19 +194,18 @@ impl Item {
193194
self.attrs.collapsed_doc_value()
194195
}
195196

196-
crate fn links(&self, cache: &Cache) -> Vec<RenderedLink> {
197+
crate fn links(&self, cx: &Context<'_>) -> Vec<RenderedLink> {
197198
use crate::html::format::href;
198-
use crate::html::render::CURRENT_DEPTH;
199199

200-
cache
200+
cx.cache()
201201
.intra_doc_links
202202
.get(&self.def_id)
203203
.map_or(&[][..], |v| v.as_slice())
204204
.iter()
205-
.filter_map(|ItemLink { link: s, link_text, did, fragment }| {
205+
.filter_map(|ItemLink { link: s, link_text, did, ref fragment }| {
206206
match *did {
207207
Some(did) => {
208-
if let Some((mut href, ..)) = href(did, cache) {
208+
if let Some((mut href, ..)) = href(did, cx) {
209209
if let Some(ref fragment) = *fragment {
210210
href.push('#');
211211
href.push_str(fragment);
@@ -219,16 +219,26 @@ impl Item {
219219
None
220220
}
221221
}
222+
// FIXME(83083): using fragments as a side-channel for
223+
// primitive names is very unfortunate
222224
None => {
225+
let relative_to = &cx.current;
223226
if let Some(ref fragment) = *fragment {
224-
let url = match cache.extern_locations.get(&self.def_id.krate) {
227+
let url = match cx.cache().extern_locations.get(&self.def_id.krate) {
225228
Some(&(_, _, ExternalLocation::Local)) => {
226-
let depth = CURRENT_DEPTH.with(|l| l.get());
227-
"../".repeat(depth)
229+
if relative_to[0] == "std" {
230+
let depth = relative_to.len() - 1;
231+
"../".repeat(depth)
232+
} else {
233+
let depth = relative_to.len();
234+
format!("{}std/", "../".repeat(depth))
235+
}
236+
}
237+
Some(&(_, _, ExternalLocation::Remote(ref s))) => {
238+
format!("{}/std/", s.trim_end_matches('/'))
228239
}
229-
Some(&(_, _, ExternalLocation::Remote(ref s))) => s.to_string(),
230240
Some(&(_, _, ExternalLocation::Unknown)) | None => format!(
231-
"https://doc.rust-lang.org/{}",
241+
"https://doc.rust-lang.org/{}/std/",
232242
crate::doc_rust_lang_org_channel(),
233243
),
234244
};
@@ -238,9 +248,8 @@ impl Item {
238248
original_text: s.clone(),
239249
new_text: link_text.clone(),
240250
href: format!(
241-
"{}{}std/primitive.{}.html{}",
251+
"{}primitive.{}.html{}",
242252
url,
243-
if !url.ends_with('/') { "/" } else { "" },
244253
&fragment[..tail],
245254
&fragment[tail..]
246255
),

0 commit comments

Comments
 (0)