Skip to content

Commit 09a3dfc

Browse files
committed
rustdoc: use a more compact encoding for source-files.js
This reduces the compiler-doc file from 40K to 36K, a 10% reduction in size.
1 parent 06f4950 commit 09a3dfc

File tree

2 files changed

+31
-20
lines changed

2 files changed

+31
-20
lines changed

src/librustdoc/html/render/write_shared.rs

+20-13
Original file line numberDiff line numberDiff line change
@@ -366,13 +366,15 @@ pub(super) fn write_shared(
366366
.collect::<Vec<_>>();
367367
files.sort_unstable();
368368
let subs = subs.iter().map(|s| s.to_json_string()).collect::<Vec<_>>().join(",");
369-
let dirs =
370-
if subs.is_empty() { String::new() } else { format!(",\"dirs\":[{}]", subs) };
369+
let dirs = if subs.is_empty() && files.is_empty() {
370+
String::new()
371+
} else {
372+
format!(",[{}]", subs)
373+
};
371374
let files = files.join(",");
372-
let files =
373-
if files.is_empty() { String::new() } else { format!(",\"files\":[{}]", files) };
375+
let files = if files.is_empty() { String::new() } else { format!(",[{}]", files) };
374376
format!(
375-
"{{\"name\":\"{name}\"{dirs}{files}}}",
377+
"[\"{name}\"{dirs}{files}]",
376378
name = self.elem.to_str().expect("invalid osstring conversion"),
377379
dirs = dirs,
378380
files = files
@@ -411,18 +413,23 @@ pub(super) fn write_shared(
411413
let dst = cx.dst.join(&format!("source-files{}.js", cx.shared.resource_suffix));
412414
let make_sources = || {
413415
let (mut all_sources, _krates) =
414-
try_err!(collect(&dst, krate.name(cx.tcx()).as_str(), "sourcesIndex"), &dst);
416+
try_err!(collect_json(&dst, krate.name(cx.tcx()).as_str()), &dst);
415417
all_sources.push(format!(
416-
"sourcesIndex[\"{}\"] = {};",
418+
r#""{}":{}"#,
417419
&krate.name(cx.tcx()),
418-
hierarchy.to_json_string()
420+
hierarchy
421+
.to_json_string()
422+
// All these `replace` calls are because we have to go through JS string for JSON content.
423+
.replace('\\', r"\\")
424+
.replace('\'', r"\'")
425+
// We need to escape double quotes for the JSON.
426+
.replace("\\\"", "\\\\\"")
419427
));
420428
all_sources.sort();
421-
Ok(format!(
422-
"var sourcesIndex = {{}};\n{}\ncreateSourceSidebar();\n",
423-
all_sources.join("\n")
424-
)
425-
.into_bytes())
429+
let mut v = String::from("var sourcesIndex = JSON.parse('{\\\n");
430+
v.push_str(&all_sources.join(",\\\n"));
431+
v.push_str("\\\n}');\ncreateSourceSidebar();\n");
432+
Ok(v.into_bytes())
426433
};
427434
write_crate("source-files.js", &make_sources)?;
428435
}

src/librustdoc/html/static/js/source-script.js

+11-7
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@
1212
const rootPath = document.getElementById("rustdoc-vars").attributes["data-root-path"].value;
1313
let oldScrollPosition = 0;
1414

15+
const NAME_OFFSET = 0;
16+
const DIRS_OFFSET = 1;
17+
const FILES_OFFSET = 2;
18+
1519
function closeSidebarIfMobile() {
1620
if (window.innerWidth < window.RUSTDOC_MOBILE_BREAKPOINT) {
1721
updateLocalStorage("source-sidebar-show", "false");
@@ -24,15 +28,15 @@ function createDirEntry(elem, parent, fullPath, hasFoundFile) {
2428

2529
dirEntry.className = "dir-entry";
2630

27-
fullPath += elem["name"] + "/";
31+
fullPath += elem[NAME_OFFSET] + "/";
2832

29-
summary.innerText = elem["name"];
33+
summary.innerText = elem[NAME_OFFSET];
3034
dirEntry.appendChild(summary);
3135

3236
const folders = document.createElement("div");
3337
folders.className = "folders";
34-
if (elem.dirs) {
35-
for (const dir of elem.dirs) {
38+
if (elem[DIRS_OFFSET]) {
39+
for (const dir of elem[DIRS_OFFSET]) {
3640
if (createDirEntry(dir, folders, fullPath, false)) {
3741
dirEntry.open = true;
3842
hasFoundFile = true;
@@ -43,8 +47,8 @@ function createDirEntry(elem, parent, fullPath, hasFoundFile) {
4347

4448
const files = document.createElement("div");
4549
files.className = "files";
46-
if (elem.files) {
47-
for (const file_text of elem.files) {
50+
if (elem[FILES_OFFSET]) {
51+
for (const file_text of elem[FILES_OFFSET]) {
4852
const file = document.createElement("a");
4953
file.innerText = file_text;
5054
file.href = rootPath + "src/" + fullPath + file_text + ".html";
@@ -125,7 +129,7 @@ function createSourceSidebar() {
125129
title.innerText = "Files";
126130
sidebar.appendChild(title);
127131
Object.keys(sourcesIndex).forEach(key => {
128-
sourcesIndex[key].name = key;
132+
sourcesIndex[key][NAME_OFFSET] = key;
129133
hasFoundFile = createDirEntry(sourcesIndex[key], sidebar, "",
130134
hasFoundFile);
131135
});

0 commit comments

Comments
 (0)