Skip to content

Commit

Permalink
Rollup merge of rust-lang#100956 - GuillaumeGomez:reduce-rightside-do…
Browse files Browse the repository at this point in the history
…m-size, r=notriddle

Reduce right-side DOM size

This is another follow-up of rust-lang#100429 but not in code blocks this time.

So the idea is: if there is only one element in the `.rightside` element, there is no need to wrap it, we can just create one node.

On each page, I run this JS: `document.getElementsByTagName('*').length`. Important to note: the bigger the number of elements inside the page, the greater the gain. It also doesn't work very nicely on std docs because there are a lot of version annotations. So with this PR, It allows to get the following results:

| file name | before this PR | with this PR | diff |
|-|-|-|-|
| std/default/trait.Default.html | 2189 | 1331 | 39.2% |
| std/vec/struct.Vec.html | 14073 | 13842 | 1.7% |
| std/fmt/trait.Debug.html | 5313 | 4907 | 7.7% |
| std/ops/trait.Index.html | 642 | 630 | 1.9% |
| gtk4/WidgetExt | 3269 | 3061 | 6.4% |

You can test it [here](https://rustdoc.crud.net/imperio/reduce-rightsize-dom-size/gtk4/prelude/trait.WidgetExt.html).

r? `@notriddle`
  • Loading branch information
GuillaumeGomez authored Aug 26, 2022
2 parents 7881e05 + 38eb33b commit 378f851
Show file tree
Hide file tree
Showing 18 changed files with 186 additions and 60 deletions.
49 changes: 34 additions & 15 deletions src/librustdoc/html/render/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,12 +191,6 @@ impl StylePath {
}
}

fn write_srclink(cx: &Context<'_>, item: &clean::Item, buf: &mut Buffer) {
if let Some(l) = cx.src_href(item) {
write!(buf, "<a class=\"srclink\" href=\"{}\">source</a>", l)
}
}

#[derive(Debug, Eq, PartialEq, Hash)]
struct ItemEntry {
url: String,
Expand Down Expand Up @@ -840,12 +834,13 @@ fn assoc_method(
/// Note that it is possible for an unstable function to be const-stable. In that case, the span
/// will include the const-stable version, but no stable version will be emitted, as a natural
/// consequence of the above rules.
fn render_stability_since_raw(
fn render_stability_since_raw_with_extra(
w: &mut Buffer,
ver: Option<Symbol>,
const_stability: Option<ConstStability>,
containing_ver: Option<Symbol>,
containing_const_ver: Option<Symbol>,
extra_class: &str,
) -> bool {
let stable_version = ver.filter(|inner| !inner.is_empty() && Some(*inner) != containing_ver);

Expand Down Expand Up @@ -893,12 +888,30 @@ fn render_stability_since_raw(
}

if !stability.is_empty() {
write!(w, r#"<span class="since" title="{}">{}</span>"#, title, stability);
write!(w, r#"<span class="since{extra_class}" title="{title}">{stability}</span>"#);
}

!stability.is_empty()
}

#[inline]
fn render_stability_since_raw(
w: &mut Buffer,
ver: Option<Symbol>,
const_stability: Option<ConstStability>,
containing_ver: Option<Symbol>,
containing_const_ver: Option<Symbol>,
) -> bool {
render_stability_since_raw_with_extra(
w,
ver,
const_stability,
containing_ver,
containing_const_ver,
"",
)
}

fn render_assoc_item(
w: &mut Buffer,
item: &clean::Item,
Expand Down Expand Up @@ -1681,23 +1694,29 @@ fn render_rightside(
RenderMode::Normal => (item.const_stability(tcx), containing_item.const_stable_since(tcx)),
RenderMode::ForDeref { .. } => (None, None),
};
let src_href = cx.src_href(item);
let has_src_ref = src_href.is_some();

let mut rightside = Buffer::new();
let has_stability = render_stability_since_raw(
let has_stability = render_stability_since_raw_with_extra(
&mut rightside,
item.stable_since(tcx),
const_stability,
containing_item.stable_since(tcx),
const_stable_since,
if has_src_ref { "" } else { " rightside" },
);
let mut srclink = Buffer::empty_from(w);
write_srclink(cx, item, &mut srclink);
if has_stability && !srclink.is_empty() {
rightside.write_str(" · ");
if let Some(l) = src_href {
if has_stability {
write!(rightside, " · <a class=\"srclink\" href=\"{}\">source</a>", l)
} else {
write!(rightside, "<a class=\"srclink rightside\" href=\"{}\">source</a>", l)
}
}
rightside.push_buffer(srclink);
if !rightside.is_empty() {
if has_stability && has_src_ref {
write!(w, "<span class=\"rightside\">{}</span>", rightside.into_inner());
} else {
w.push_buffer(rightside);
}
}

Expand Down
34 changes: 9 additions & 25 deletions src/librustdoc/html/render/print_item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use std::rc::Rc;
use super::{
collect_paths_for_type, document, ensure_trailing_slash, item_ty_to_section,
notable_traits_decl, render_assoc_item, render_assoc_items, render_attributes_in_code,
render_attributes_in_pre, render_impl, render_stability_since_raw, write_srclink,
render_attributes_in_pre, render_impl, render_rightside, render_stability_since_raw,
AssocItemLink, Context, ImplRenderingParameters,
};
use crate::clean;
Expand Down Expand Up @@ -709,14 +709,7 @@ fn item_trait(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, t: &clean:
write!(w, "<details class=\"rustdoc-toggle\" open><summary>");
}
write!(w, "<div id=\"{}\" class=\"method has-srclink\">", id);
write!(w, "<div class=\"rightside\">");

let has_stability = render_stability_since(w, m, t, cx.tcx());
if has_stability {
w.write_str(" · ");
}
write_srclink(cx, m, w);
write!(w, "</div>");
render_rightside(w, cx, m, t, RenderMode::Normal);
write!(w, "<h4 class=\"code-header\">");
render_assoc_item(
w,
Expand Down Expand Up @@ -1260,7 +1253,13 @@ fn item_enum(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, e: &clean::
w.write_str(")");
}
w.write_str("</code>");
render_stability_since(w, variant, it, cx.tcx());
render_stability_since_raw(
w,
variant.stable_since(cx.tcx()),
variant.const_stability(cx.tcx()),
it.stable_since(cx.tcx()),
it.const_stable_since(cx.tcx()),
);
w.write_str("</h3>");

use crate::clean::Variant;
Expand Down Expand Up @@ -1591,21 +1590,6 @@ where
w.write_str("</code></pre>");
}

fn render_stability_since(
w: &mut Buffer,
item: &clean::Item,
containing_item: &clean::Item,
tcx: TyCtxt<'_>,
) -> bool {
render_stability_since_raw(
w,
item.stable_since(tcx),
item.const_stability(tcx),
containing_item.stable_since(tcx),
containing_item.const_stable_since(tcx),
)
}

fn compare_impl<'a, 'b>(lhs: &'a &&Impl, rhs: &'b &&Impl, cx: &Context<'_>) -> Ordering {
let lhss = format!("{}", lhs.inner_impl().print(false, cx));
let rhss = format!("{}", rhs.inner_impl().print(false, cx));
Expand Down
2 changes: 1 addition & 1 deletion src/librustdoc/html/static/css/themes/ayu.css
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ details.rustdoc-toggle > summary::before {
background: none;
}

.rightside,
.rightside:not(a),
.out-of-band {
color: grey;
}
Expand Down
2 changes: 1 addition & 1 deletion src/librustdoc/html/static/css/themes/dark.css
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ details.rustdoc-toggle > summary::before {
background: none;
}

.rightside,
.rightside:not(a),
.out-of-band {
color: grey;
}
Expand Down
2 changes: 1 addition & 1 deletion src/librustdoc/html/static/css/themes/light.css
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ details.rustdoc-toggle > summary::before {
.stab { background: #FFF5D6; border-color: #FFC600; }
.stab.portability > code { background: none; }

.rightside,
.rightside:not(a),
.out-of-band {
color: grey;
}
Expand Down
128 changes: 124 additions & 4 deletions src/test/rustdoc-gui/anchors.goml
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
// This test is to ensure that the anchors (`§`) have the expected color and position.
goto: file://|DOC_PATH|/test_docs/struct.HeavilyDocumentedStruct.html
show-text: true
goto: file://|DOC_PATH|/staged_api/struct.Foo.html

// This is needed to ensure that the text color is computed.
show-text: true
Expand All @@ -13,10 +12,31 @@ reload:
assert-css: ("#toggle-all-docs", {"color": "rgb(0, 0, 0)"})
assert-css: (".fqn .in-band a:nth-of-type(1)", {"color": "rgb(0, 0, 0)"})
assert-css: (".fqn .in-band a:nth-of-type(2)", {"color": "rgb(173, 55, 138)"})
assert-css: (".srclink", {"color": "rgb(56, 115, 173)"})
assert-css: (
".rightside .srclink",
{"color": "rgb(56, 115, 173)", "text-decoration": "none solid rgb(56, 115, 173)"},
ALL,
)
compare-elements-css: (".rightside .srclink", ".rightside.srclink", ["color", "text-decoration"])
compare-elements-css: (".main-heading .srclink", ".rightside.srclink", ["color", "text-decoration"])

move-cursor-to: ".main-heading .srclink"
assert-css: (".srclink", {"text-decoration": "underline solid rgb(56, 115, 173)"})
assert-css: (
".main-heading .srclink",
{"color": "rgb(56, 115, 173)", "text-decoration": "underline solid rgb(56, 115, 173)"},
)
move-cursor-to: ".impl-items .rightside .srclink"
assert-css: (
".impl-items .rightside .srclink",
{"color": "rgb(56, 115, 173)", "text-decoration": "none solid rgb(56, 115, 173)"},
)
move-cursor-to: ".impl-items .rightside.srclink"
assert-css: (
".impl-items .rightside.srclink",
{"color": "rgb(56, 115, 173)", "text-decoration": "none solid rgb(56, 115, 173)"},
)

goto: file://|DOC_PATH|/test_docs/struct.HeavilyDocumentedStruct.html

assert-css: ("#top-doc-prose-title", {"color": "rgb(0, 0, 0)"})

Expand All @@ -32,3 +52,103 @@ move-cursor-to: "#impl-HeavilyDocumentedStruct"
assert-css: ("#impl-HeavilyDocumentedStruct a.anchor", {"color": "rgb(0, 0, 0)"})

assert-css: ("#title-for-struct-impl-item-doc", {"margin-left": "0px"})

//
// We do the same checks with the dark theme now.
//
local-storage: {"rustdoc-theme": "dark", "rustdoc-use-system-theme": "false"}
goto: file://|DOC_PATH|/staged_api/struct.Foo.html

assert-css: ("#toggle-all-docs", {"color": "rgb(221, 221, 221)"})
assert-css: (".fqn .in-band a:nth-of-type(1)", {"color": "rgb(221, 221, 221)"})
assert-css: (".fqn .in-band a:nth-of-type(2)", {"color": "rgb(45, 191, 184)"})
assert-css: (
".rightside .srclink",
{"color": "rgb(210, 153, 29)", "text-decoration": "none solid rgb(210, 153, 29)"},
ALL,
)
compare-elements-css: (".rightside .srclink", ".rightside.srclink", ["color", "text-decoration"])
compare-elements-css: (".main-heading .srclink", ".rightside.srclink", ["color", "text-decoration"])

move-cursor-to: ".main-heading .srclink"
assert-css: (
".main-heading .srclink",
{"color": "rgb(210, 153, 29)", "text-decoration": "underline solid rgb(210, 153, 29)"},
)
move-cursor-to: ".impl-items .rightside .srclink"
assert-css: (
".impl-items .rightside .srclink",
{"color": "rgb(210, 153, 29)", "text-decoration": "none solid rgb(210, 153, 29)"},
)
move-cursor-to: ".impl-items .rightside.srclink"
assert-css: (
".impl-items .rightside.srclink",
{"color": "rgb(210, 153, 29)", "text-decoration": "none solid rgb(210, 153, 29)"},
)

goto: file://|DOC_PATH|/test_docs/struct.HeavilyDocumentedStruct.html

assert-css: ("#top-doc-prose-title", {"color": "rgb(221, 221, 221)"})

assert-css: (".sidebar a", {"color": "rgb(253, 191, 53)"})
assert-css: (".in-band a", {"color": "rgb(221, 221, 221)"})

// We move the cursor over the "Implementations" title so the anchor is displayed.
move-cursor-to: "h2#implementations"
assert-css: ("h2#implementations a.anchor", {"color": "rgb(221, 221, 221)"})

// Same thing with the impl block title.
move-cursor-to: "#impl-HeavilyDocumentedStruct"
assert-css: ("#impl-HeavilyDocumentedStruct a.anchor", {"color": "rgb(221, 221, 221)"})

assert-css: ("#title-for-struct-impl-item-doc", {"margin-left": "0px"})

//
// We do the same checks with the ayu theme now.
//
local-storage: {"rustdoc-theme": "ayu", "rustdoc-use-system-theme": "false"}
goto: file://|DOC_PATH|/staged_api/struct.Foo.html

assert-css: ("#toggle-all-docs", {"color": "rgb(197, 197, 197)"})
assert-css: (".fqn .in-band a:nth-of-type(1)", {"color": "rgb(255, 255, 255)"})
assert-css: (".fqn .in-band a:nth-of-type(2)", {"color": "rgb(255, 160, 165)"})
assert-css: (
".rightside .srclink",
{"color": "rgb(57, 175, 215)", "text-decoration": "none solid rgb(57, 175, 215)"},
ALL,
)
compare-elements-css: (".rightside .srclink", ".rightside.srclink", ["color", "text-decoration"])
compare-elements-css: (".main-heading .srclink", ".rightside.srclink", ["color", "text-decoration"])

move-cursor-to: ".main-heading .srclink"
assert-css: (
".main-heading .srclink",
{"color": "rgb(57, 175, 215)", "text-decoration": "underline solid rgb(57, 175, 215)"},
)
move-cursor-to: ".impl-items .rightside .srclink"
assert-css: (
".impl-items .rightside .srclink",
{"color": "rgb(57, 175, 215)", "text-decoration": "none solid rgb(57, 175, 215)"},
)
move-cursor-to: ".impl-items .rightside.srclink"
assert-css: (
".impl-items .rightside.srclink",
{"color": "rgb(57, 175, 215)", "text-decoration": "none solid rgb(57, 175, 215)"},
)

goto: file://|DOC_PATH|/test_docs/struct.HeavilyDocumentedStruct.html

assert-css: ("#top-doc-prose-title", {"color": "rgb(255, 255, 255)"})

assert-css: (".sidebar a", {"color": "rgb(83, 177, 219)"})
assert-css: (".in-band a", {"color": "rgb(255, 255, 255)"})

// We move the cursor over the "Implementations" title so the anchor is displayed.
move-cursor-to: "h2#implementations"
assert-css: ("h2#implementations a.anchor", {"color": "rgb(197, 197, 197)"})

// Same thing with the impl block title.
move-cursor-to: "#impl-HeavilyDocumentedStruct"
assert-css: ("#impl-HeavilyDocumentedStruct a.anchor", {"color": "rgb(197, 197, 197)"})

assert-css: ("#title-for-struct-impl-item-doc", {"margin-left": "0px"})
6 changes: 3 additions & 3 deletions src/test/rustdoc-gui/headings.goml
Original file line number Diff line number Diff line change
Expand Up @@ -247,12 +247,12 @@ assert-css: (

local-storage: {"rustdoc-theme": "light"}
goto: file://|DOC_PATH|/staged_api/struct.Foo.html
assert-css: (".since", {"color": "rgb(128, 128, 128)"})
assert-css: (".since", {"color": "rgb(128, 128, 128)"}, ALL)

local-storage: {"rustdoc-theme": "dark"}
reload:
assert-css: (".since", {"color": "rgb(128, 128, 128)"})
assert-css: (".since", {"color": "rgb(128, 128, 128)"}, ALL)

local-storage: {"rustdoc-theme": "ayu"}
reload:
assert-css: (".since", {"color": "rgb(128, 128, 128)"})
assert-css: (".since", {"color": "rgb(128, 128, 128)"}, ALL)
3 changes: 2 additions & 1 deletion src/test/rustdoc-gui/src/staged_api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ edition = "2021"
path = "lib.rs"

[features]
default = ["some_feature"]
default = ["some_feature", "some_other_feature"]
some_feature = []
some_other_feature = []
2 changes: 2 additions & 0 deletions src/test/rustdoc-gui/src/staged_api/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,6 @@ pub struct Foo {}
impl Foo {
#[stable(feature = "some_feature", since = "1.3.5")]
pub fn bar() {}
#[stable(feature = "some_other_feature", since = "1.3.6")]
pub fn yo() {}
}
2 changes: 1 addition & 1 deletion src/test/rustdoc/anchors.no_const_anchor.html
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<div id="associatedconstant.YOLO" class="method has-srclink"><div class="rightside"><a class="srclink" href="../src/foo/anchors.rs.html#16">source</a></div><h4 class="code-header">const <a href="#associatedconstant.YOLO" class="constant">YOLO</a>: <a class="primitive" href="{{channel}}/std/primitive.u32.html">u32</a></h4></div>
<div id="associatedconstant.YOLO" class="method has-srclink"><a class="srclink rightside" href="../src/foo/anchors.rs.html#16">source</a><h4 class="code-header">const <a href="#associatedconstant.YOLO" class="constant">YOLO</a>: <a class="primitive" href="{{channel}}/std/primitive.u32.html">u32</a></h4></div>
2 changes: 1 addition & 1 deletion src/test/rustdoc/anchors.no_const_anchor2.html
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<section id="associatedconstant.X" class="associatedconstant has-srclink"><span class="rightside"><a class="srclink" href="../src/foo/anchors.rs.html#42">source</a></span><h4 class="code-header">pub const <a href="#associatedconstant.X" class="constant">X</a>: <a class="primitive" href="{{channel}}/std/primitive.i32.html">i32</a> = 0i32</h4></section>
<section id="associatedconstant.X" class="associatedconstant has-srclink"><a class="srclink rightside" href="../src/foo/anchors.rs.html#42">source</a><h4 class="code-header">pub const <a href="#associatedconstant.X" class="constant">X</a>: <a class="primitive" href="{{channel}}/std/primitive.i32.html">i32</a> = 0i32</h4></section>
2 changes: 1 addition & 1 deletion src/test/rustdoc/anchors.no_method_anchor.html
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<section id="method.new" class="method has-srclink"><span class="rightside"><a class="srclink" href="../src/foo/anchors.rs.html#48">source</a></span><h4 class="code-header">pub fn <a href="#method.new" class="fnname">new</a>() -&gt; Self</h4></section>
<section id="method.new" class="method has-srclink"><a class="srclink rightside" href="../src/foo/anchors.rs.html#48">source</a><h4 class="code-header">pub fn <a href="#method.new" class="fnname">new</a>() -&gt; Self</h4></section>
2 changes: 1 addition & 1 deletion src/test/rustdoc/anchors.no_trait_method_anchor.html
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<div id="method.bar" class="method has-srclink"><div class="rightside"><a class="srclink" href="../src/foo/anchors.rs.html#23">source</a></div><h4 class="code-header">fn <a href="#method.bar" class="fnname">bar</a>()</h4></div>
<div id="method.bar" class="method has-srclink"><a class="srclink rightside" href="../src/foo/anchors.rs.html#23">source</a><h4 class="code-header">fn <a href="#method.bar" class="fnname">bar</a>()</h4></div>
2 changes: 1 addition & 1 deletion src/test/rustdoc/anchors.no_tymethod_anchor.html
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<div id="tymethod.foo" class="method has-srclink"><div class="rightside"><a class="srclink" href="../src/foo/anchors.rs.html#20">source</a></div><h4 class="code-header">fn <a href="#tymethod.foo" class="fnname">foo</a>()</h4></div>
<div id="tymethod.foo" class="method has-srclink"><a class="srclink rightside" href="../src/foo/anchors.rs.html#20">source</a><h4 class="code-header">fn <a href="#tymethod.foo" class="fnname">foo</a>()</h4></div>
2 changes: 1 addition & 1 deletion src/test/rustdoc/anchors.no_type_anchor.html
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<div id="associatedtype.T" class="method has-srclink"><div class="rightside"><a class="srclink" href="../src/foo/anchors.rs.html#13">source</a></div><h4 class="code-header">type <a href="#associatedtype.T" class="associatedtype">T</a></h4></div>
<div id="associatedtype.T" class="method has-srclink"><a class="srclink rightside" href="../src/foo/anchors.rs.html#13">source</a><h4 class="code-header">type <a href="#associatedtype.T" class="associatedtype">T</a></h4></div>
2 changes: 1 addition & 1 deletion src/test/rustdoc/ensure-src-link.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@

// This test ensures that the [src] link is present on traits items.

// @has foo/trait.Iterator.html '//*[@id="method.zip"]//a[@class="srclink"]' "source"
// @has foo/trait.Iterator.html '//*[@id="method.zip"]//a[@class="srclink rightside"]' "source"
pub use std::iter::Iterator;
Loading

0 comments on commit 378f851

Please sign in to comment.