Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rustdoc: reduce per-page HTML overhead #123706

Merged
merged 5 commits into from
Apr 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/librustdoc/html/render/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ impl<'tcx> Context<'tcx> {
rust_logo: has_doc_flag(self.tcx(), LOCAL_CRATE.as_def_id(), sym::rust_logo),
};
let mut page_buffer = Buffer::html();
print_item(self, it, &mut page_buffer, &page);
print_item(self, it, &mut page_buffer);
layout::render(
&clone_shared.layout,
&page,
Expand Down
42 changes: 14 additions & 28 deletions src/librustdoc/html/render/print_item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,10 @@ use crate::html::format::{
display_fn, join_with_double_colon, print_abi_with_space, print_constness_with_space,
print_where_clause, visibility_print_with_space, Buffer, Ending, PrintWithSpace,
};
use crate::html::layout::Page;
use crate::html::highlight;
use crate::html::markdown::{HeadingOffset, MarkdownSummaryLine};
use crate::html::render::{document_full, document_item_info};
use crate::html::url_parts_builder::UrlPartsBuilder;
use crate::html::{highlight, static_files};

use askama::Template;
use itertools::Itertools;
Expand Down Expand Up @@ -157,8 +156,6 @@ struct PathComponent {
#[derive(Template)]
#[template(path = "print_item.html")]
struct ItemVars<'a> {
static_root_path: &'a str,
clipboard_svg: &'static static_files::StaticFile,
typ: &'a str,
name: &'a str,
item_type: &'a str,
Expand All @@ -178,12 +175,7 @@ fn print_where_clause_and_check<'a, 'tcx: 'a>(
len_before != buffer.len()
}

pub(super) fn print_item(
cx: &mut Context<'_>,
item: &clean::Item,
buf: &mut Buffer,
page: &Page<'_>,
) {
pub(super) fn print_item(cx: &mut Context<'_>, item: &clean::Item, buf: &mut Buffer) {
debug_assert!(!item.is_stripped());
let typ = match *item.kind {
clean::ModuleItem(_) => {
Expand Down Expand Up @@ -252,8 +244,6 @@ pub(super) fn print_item(
};

let item_vars = ItemVars {
static_root_path: &page.get_static_root_path(),
clipboard_svg: &static_files::STATIC_FILES.clipboard_svg,
typ,
name: item.name.as_ref().unwrap().as_str(),
item_type: &item.type_().to_string(),
Expand Down Expand Up @@ -1237,22 +1227,18 @@ fn item_opaque_ty(
}

fn item_type_alias(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, t: &clean::TypeAlias) {
fn write_content(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, t: &clean::TypeAlias) {
wrap_item(w, |w| {
write!(
w,
"{attrs}{vis}type {name}{generics}{where_clause} = {type_};",
attrs = render_attributes_in_pre(it, "", cx),
vis = visibility_print_with_space(it, cx),
name = it.name.unwrap(),
generics = t.generics.print(cx),
where_clause = print_where_clause(&t.generics, cx, 0, Ending::Newline),
type_ = t.type_.print(cx),
);
});
}

write_content(w, cx, it, t);
wrap_item(w, |w| {
write!(
w,
"{attrs}{vis}type {name}{generics}{where_clause} = {type_};",
attrs = render_attributes_in_pre(it, "", cx),
vis = visibility_print_with_space(it, cx),
name = it.name.unwrap(),
generics = t.generics.print(cx),
where_clause = print_where_clause(&t.generics, cx, 0, Ending::Newline),
type_ = t.type_.print(cx),
);
});

write!(w, "{}", document(cx, it, None, HeadingOffset::H2));

Expand Down
20 changes: 18 additions & 2 deletions src/librustdoc/html/static/css/rustdoc.css
Original file line number Diff line number Diff line change
Expand Up @@ -1603,6 +1603,16 @@ a.tooltip:hover::after {
border-color: var(--settings-button-border-focus);
}

#settings-menu > a {
line-height: 0;
font-size: 0;
}
#settings-menu > a:before {
content: url('wheel-63255fc4502dca9a.svg');
width: 22px;
height: 22px;
}

#sidebar-button > a:before {
content: url('data:image/svg+xml,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 22 22" \
fill="none" stroke="black">\
Expand All @@ -1622,11 +1632,17 @@ a.tooltip:hover::after {
padding-left: 2px;
border: 0;
width: 33px;
line-height: 0;
font-size: 0;
}
#copy-path > img {

#copy-path:before {
filter: var(--copy-path-img-filter);
content: url('clipboard-24048e6d87f63d07.svg');
width: 19px;
height: 18px;
}
#copy-path:hover > img {
#copy-path:hover:before {
filter: var(--copy-path-img-hover-filter);
}

Expand Down
2 changes: 1 addition & 1 deletion src/librustdoc/html/static/images/clipboard.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed src/librustdoc/html/static/images/favicon-16x16.png
Binary file not shown.
2 changes: 1 addition & 1 deletion src/librustdoc/html/static/images/wheel.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 0 additions & 1 deletion src/librustdoc/html/static_files.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,6 @@ static_files! {
license_mit => "static/LICENSE-MIT.txt",
rust_logo_svg => "static/images/rust-logo.svg",
rust_favicon_svg => "static/images/favicon.svg",
rust_favicon_png_16 => "static/images/favicon-16x16.png",
rust_favicon_png_32 => "static/images/favicon-32x32.png",
fira_sans_regular => "static/fonts/FiraSans-Regular.woff2",
fira_sans_medium => "static/fonts/FiraSans-Medium.woff2",
Expand Down
20 changes: 7 additions & 13 deletions src/librustdoc/html/templates/page.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,10 @@
<meta name="generator" content="rustdoc"> {# #}
<meta name="description" content="{{page.description}}"> {# #}
<title>{{page.title}}</title> {# #}
<script> if (window.location.protocol !== "file:") document.write(` {# Hack to skip preloading fonts locally - see #98769 #}
<link rel="preload" as="font" type="font/woff2" crossorigin href="{{static_root_path|safe}}{{files.source_serif_4_regular}}"> {# #}
<link rel="preload" as="font" type="font/woff2" crossorigin href="{{static_root_path|safe}}{{files.fira_sans_regular}}"> {# #}
<link rel="preload" as="font" type="font/woff2" crossorigin href="{{static_root_path|safe}}{{files.fira_sans_medium}}"> {# #}
<link rel="preload" as="font" type="font/woff2" crossorigin href="{{static_root_path|safe}}{{files.source_code_pro_regular}}"> {# #}
<link rel="preload" as="font" type="font/woff2" crossorigin href="{{static_root_path|safe}}{{files.source_code_pro_semibold}}"> {# #}
`)</script> {# #}
<script>if(window.location.protocol!=="file:") {# Hack to skip preloading fonts locally - see #98769 #}
for(f of "{{files.source_serif_4_regular}},{{files.fira_sans_regular}},{{files.fira_sans_medium}},{{files.source_code_pro_regular}},{{files.source_code_pro_semibold}}".split(",")) {# #}
document.write(`<link rel="preload" as="font" type="font/woff2" crossorigin href="{{static_root_path|safe}}${f}">`) {# #}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using document.write breaks speculative parsing of the html afaik.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good to know! I'll send an improvement.

</script> {# #}
<link rel="stylesheet" {#+ #}
href="{{static_root_path|safe}}{{files.normalize_css}}"> {# #}
<link rel="stylesheet" {#+ #}
Expand Down Expand Up @@ -61,8 +58,6 @@
{% if !layout.favicon.is_empty() %}
<link rel="icon" href="{{layout.favicon}}"> {# #}
{% else %}
<link rel="alternate icon" type="image/png" {#+ #}
href="{{static_root_path|safe}}{{files.rust_favicon_png_16}}"> {# #}
<link rel="alternate icon" type="image/png" {#+ #}
href="{{static_root_path|safe}}{{files.rust_favicon_png_32}}"> {# #}
<link rel="icon" type="image/svg+xml" {#+ #}
Expand Down Expand Up @@ -114,13 +109,13 @@ <h2> {# #}
<div class="version">{{+ display_krate_version_extra}}</div> {# #}
{% endif %}
{% else %}
<div class="src-sidebar-title">
<div class="src-sidebar-title"> {# #}
<h2>Files</h2> {# #}
</div> {# #}
{% endif %}
{{ sidebar|safe }}
</nav> {# #}
<div class="sidebar-resizer"></div>
<div class="sidebar-resizer"></div> {# #}
<main> {# #}
{% if page.css_class != "src" %}<div class="width-limiter">{% endif %}
<nav class="sub"> {# #}
Expand All @@ -142,8 +137,7 @@ <h2>Files</h2> {# #}
</div> {# #}
<div id="settings-menu" tabindex="-1"> {# #}
<a href="{{page.root_path|safe}}settings.html" title="settings"> {# #}
<img width="22" height="22" alt="Change settings" {#+ #}
src="{{static_root_path|safe}}{{files.wheel_svg}}"> {# #}
Settings {# #}
</a> {# #}
</div> {# #}
</form> {# #}
Expand Down
4 changes: 1 addition & 3 deletions src/librustdoc/html/templates/print_item.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@ <h1>
{% endfor %}
<a class="{{item_type}}" href="#">{{name}}</a> {# #}
<button id="copy-path" title="Copy item path to clipboard"> {# #}
<img src="{{static_root_path|safe}}{{clipboard_svg}}" {#+ #}
width="19" height="18" {#+ #}
alt="Copy item path"> {# #}
Copy item path {# #}
</button> {# #}
</h1> {# #}
<span class="out-of-band">
Expand Down
2 changes: 1 addition & 1 deletion src/librustdoc/html/templates/sidebar.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ <h2 class="location"> {# #}
{% endif %}
<div class="sidebar-elems">
{% if is_crate %}
<ul class="block">
<ul class="block"> {# #}
<li><a id="all-types" href="all.html">All Items</a></li> {# #}
</ul>
{% endif %}
Expand Down
6 changes: 3 additions & 3 deletions tests/rustdoc-gui/search-result-go-to-first.goml
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@
// First, we check that the first page doesn't have the string we're looking for to ensure
// that the feature is changing page as expected.
go-to: "file://" + |DOC_PATH| + "/test_docs/index.html"
assert-text-false: (".main-heading h1", "Struct test_docs::Foo")
assert-text-false: (".main-heading h1", "Struct test_docs::FooCopy item path")

// We now check that we land on the search result page if "go_to_first" isn't set.
go-to: "file://" + |DOC_PATH| + "/test_docs/index.html?search=struct%3AFoo"
// Waiting for the search results to appear...
wait-for: "#search-tabs"
assert-text-false: (".main-heading h1", "Struct test_docs::Foo")
assert-text-false: (".main-heading h1", "Struct test_docs::FooCopy item path")
// Ensure that the search results are displayed, not the "normal" content.
assert-css: ("#main-content", {"display": "none"})

// Now we can check that the feature is working as expected!
go-to: "file://" + |DOC_PATH| + "/test_docs/index.html?search=struct%3AFoo&go_to_first=true"
// Waiting for the page to load...
wait-for-text: (".main-heading h1", "Struct test_docs::Foo")
wait-for-text: (".main-heading h1", "Struct test_docs::FooCopy item path")
2 changes: 1 addition & 1 deletion tests/rustdoc-gui/toggle-click-deadspace.goml
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ assert-attribute-false: (".impl-items .toggle", {"open": ""})

// Click the "Trait" part of "impl Trait" and verify it navigates.
click: "#impl-Trait-for-Foo h3 a:first-of-type"
assert-text: (".main-heading h1", "Trait lib2::Trait")
assert-text: (".main-heading h1", "Trait lib2::TraitCopy item path")
Loading