Skip to content

Commit 956dba4

Browse files
committedJan 17, 2019
Auto merge of #56869 - GuillaumeGomez:index-size-reduction, r=QuietMisdreavus
Reduce search-index.js size Coming from: ``` 1735683 Dec 16 01:35 build/x86_64-apple-darwin/doc/search-index.js ``` to: ``` 727755 Dec 16 01:51 build/x86_64-apple-darwin/doc/search-index.js ``` r? @QuietMisdreavus
2 parents c40b977 + d405606 commit 956dba4

File tree

4 files changed

+101
-28
lines changed

4 files changed

+101
-28
lines changed
 

‎Cargo.lock

+3-3
Original file line numberDiff line numberDiff line change
@@ -1332,7 +1332,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
13321332

13331333
[[package]]
13341334
name = "minifier"
1335-
version = "0.0.20"
1335+
version = "0.0.26"
13361336
source = "registry+https://github.com/rust-lang/crates.io-index"
13371337
dependencies = [
13381338
"macro-utils 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)",
@@ -2692,7 +2692,7 @@ dependencies = [
26922692
name = "rustdoc"
26932693
version = "0.0.0"
26942694
dependencies = [
2695-
"minifier 0.0.20 (registry+https://github.com/rust-lang/crates.io-index)",
2695+
"minifier 0.0.26 (registry+https://github.com/rust-lang/crates.io-index)",
26962696
"parking_lot 0.6.4 (registry+https://github.com/rust-lang/crates.io-index)",
26972697
"pulldown-cmark 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)",
26982698
"tempfile 3.0.5 (registry+https://github.com/rust-lang/crates.io-index)",
@@ -3510,7 +3510,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
35103510
"checksum memchr 2.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "0a3eb002f0535929f1199681417029ebea04aadc0c7a4224b46be99c7f5d6a16"
35113511
"checksum memmap 0.6.2 (registry+https://github.com/rust-lang/crates.io-index)" = "e2ffa2c986de11a9df78620c01eeaaf27d94d3ff02bf81bfcca953102dd0c6ff"
35123512
"checksum memoffset 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "0f9dc261e2b62d7a622bf416ea3c5245cdd5d9a7fcc428c0d06804dfce1775b3"
3513-
"checksum minifier 0.0.20 (registry+https://github.com/rust-lang/crates.io-index)" = "96c269bb45c39b333392b2b18ad71760b34ac65666591386b0e959ed58b3f474"
3513+
"checksum minifier 0.0.26 (registry+https://github.com/rust-lang/crates.io-index)" = "f299df45afd73332044ea9f717c816a84fc90c8b631409abf339ba93642a7985"
35143514
"checksum miniz-sys 0.1.11 (registry+https://github.com/rust-lang/crates.io-index)" = "0300eafb20369952951699b68243ab4334f4b10a88f411c221d444b36c40e649"
35153515
"checksum miniz_oxide 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "5ad30a47319c16cde58d0314f5d98202a80c9083b5f61178457403dfb14e509c"
35163516
"checksum miniz_oxide_c_api 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "28edaef377517fd9fe3e085c37d892ce7acd1fbeab9239c5a36eec352d8a8b7e"

‎src/librustdoc/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@ path = "lib.rs"
99

1010
[dependencies]
1111
pulldown-cmark = { version = "0.1.2", default-features = false }
12-
minifier = "0.0.20"
12+
minifier = "0.0.26"
1313
tempfile = "3"
1414
parking_lot = "0.6.4"

‎src/librustdoc/html/render.rs

+95-22
Original file line numberDiff line numberDiff line change
@@ -741,8 +741,8 @@ fn build_index(krate: &clean::Crate, cache: &mut Cache) -> String {
741741

742742
let mut crate_data = BTreeMap::new();
743743
crate_data.insert("doc".to_owned(), Json::String(crate_doc));
744-
crate_data.insert("items".to_owned(), Json::Array(crate_items));
745-
crate_data.insert("paths".to_owned(), Json::Array(crate_paths));
744+
crate_data.insert("i".to_owned(), Json::Array(crate_items));
745+
crate_data.insert("p".to_owned(), Json::Array(crate_paths));
746746

747747
// Collect the index into a string
748748
format!("searchIndex[{}] = {};",
@@ -914,12 +914,44 @@ themePicker.onblur = handleThemeButtonsBlur;
914914
write(cx.dst.join("COPYRIGHT.txt"),
915915
static_files::COPYRIGHT)?;
916916

917-
fn collect(path: &Path, krate: &str, key: &str) -> io::Result<(Vec<String>, Vec<String>)> {
917+
fn collect(
918+
path: &Path,
919+
krate: &str,
920+
key: &str,
921+
for_search_index: bool,
922+
) -> io::Result<(Vec<String>, Vec<String>, Vec<String>)> {
923+
use minifier::js;
924+
918925
let mut ret = Vec::new();
919926
let mut krates = Vec::new();
927+
let mut variables = Vec::new();
928+
929+
let mut krate = krate.to_owned();
930+
920931
if path.exists() {
921932
for line in BufReader::new(File::open(path)?).lines() {
922933
let line = line?;
934+
if for_search_index && line.starts_with("var r_") {
935+
variables.push(line.clone());
936+
// We need to check if the crate name has been put into a variable as well.
937+
let tokens = js::simple_minify(&line).apply(js::clean_tokens);
938+
let mut pos = 0;
939+
while pos < tokens.len() {
940+
if let Some((var_pos, Some(value_pos))) =
941+
js::get_variable_name_and_value_positions(&tokens, pos) {
942+
if let Some(s) = tokens.0[value_pos].get_string() {
943+
if &s[1..s.len() - 1] == krate {
944+
if let Some(var) = tokens[var_pos].get_other() {
945+
krate = var.to_owned();
946+
break
947+
}
948+
}
949+
}
950+
}
951+
pos += 1;
952+
}
953+
continue;
954+
}
923955
if !line.starts_with(key) {
924956
continue;
925957
}
@@ -933,7 +965,7 @@ themePicker.onblur = handleThemeButtonsBlur;
933965
.unwrap_or_else(|| String::new()));
934966
}
935967
}
936-
Ok((ret, krates))
968+
Ok((ret, krates, variables))
937969
}
938970

939971
fn show_item(item: &IndexItem, krate: &str) -> String {
@@ -948,7 +980,7 @@ themePicker.onblur = handleThemeButtonsBlur;
948980

949981
let dst = cx.dst.join("aliases.js");
950982
{
951-
let (mut all_aliases, _) = try_err!(collect(&dst, &krate.name, "ALIASES"), &dst);
983+
let (mut all_aliases, _, _) = try_err!(collect(&dst, &krate.name, "ALIASES", false), &dst);
952984
let mut w = try_err!(File::create(&dst), &dst);
953985
let mut output = String::with_capacity(100);
954986
for (alias, items) in &cache.aliases {
@@ -1035,7 +1067,9 @@ themePicker.onblur = handleThemeButtonsBlur;
10351067
}
10361068

10371069
let dst = cx.dst.join("source-files.js");
1038-
let (mut all_sources, _krates) = try_err!(collect(&dst, &krate.name, "sourcesIndex"), &dst);
1070+
let (mut all_sources, _krates, _) = try_err!(collect(&dst, &krate.name, "sourcesIndex",
1071+
false),
1072+
&dst);
10391073
all_sources.push(format!("sourcesIndex[\"{}\"] = {};",
10401074
&krate.name,
10411075
hierarchy.to_json_string()));
@@ -1049,20 +1083,22 @@ themePicker.onblur = handleThemeButtonsBlur;
10491083

10501084
// Update the search index
10511085
let dst = cx.dst.join("search-index.js");
1052-
let (mut all_indexes, mut krates) = try_err!(collect(&dst, &krate.name, "searchIndex"), &dst);
1086+
let (mut all_indexes, mut krates, variables) = try_err!(collect(&dst,
1087+
&krate.name,
1088+
"searchIndex",
1089+
true), &dst);
10531090
all_indexes.push(search_index);
10541091

10551092
// Sort the indexes by crate so the file will be generated identically even
10561093
// with rustdoc running in parallel.
10571094
all_indexes.sort();
10581095
let mut w = try_err!(File::create(&dst), &dst);
1059-
try_err!(writeln!(&mut w, "var N = null;var searchIndex = {{}};"), &dst);
1060-
for index in &all_indexes {
1061-
try_err!(write_minify_replacer(&mut w, &*index, options.enable_minification,
1062-
&[(minifier::js::Keyword::Null, "N")]),
1063-
&dst);
1064-
}
1065-
try_err!(writeln!(&mut w, "initSearch(searchIndex);addSearchOptions(searchIndex);"), &dst);
1096+
try_err!(writeln!(&mut w, "var N=null,E=\"\",T=\"t\",U=\"u\",searchIndex={{}};"), &dst);
1097+
try_err!(write_minify_replacer(&mut w,
1098+
&format!("{}\n{}", variables.join(""), all_indexes.join("\n")),
1099+
options.enable_minification),
1100+
&dst);
1101+
try_err!(write!(&mut w, "initSearch(searchIndex);addSearchOptions(searchIndex);"), &dst);
10661102

10671103
if options.enable_index_page {
10681104
if let Some(index_page) = options.index_page.clone() {
@@ -1161,8 +1197,9 @@ themePicker.onblur = handleThemeButtonsBlur;
11611197
remote_item_type.css_class(),
11621198
remote_path[remote_path.len() - 1]));
11631199

1164-
let (mut all_implementors, _) = try_err!(collect(&mydst, &krate.name, "implementors"),
1165-
&mydst);
1200+
let (mut all_implementors, _, _) = try_err!(collect(&mydst, &krate.name, "implementors",
1201+
false),
1202+
&mydst);
11661203
all_implementors.push(implementors);
11671204
// Sort the implementors by crate so the file will be generated
11681205
// identically even with rustdoc running in parallel.
@@ -1216,14 +1253,50 @@ fn write_minify(dst: PathBuf, contents: &str, enable_minification: bool) -> Resu
12161253
}
12171254
}
12181255

1219-
fn write_minify_replacer<W: Write>(dst: &mut W,
1220-
contents: &str,
1221-
enable_minification: bool,
1222-
keywords_to_replace: &[(minifier::js::Keyword, &str)])
1223-
-> io::Result<()> {
1256+
fn write_minify_replacer<W: Write>(
1257+
dst: &mut W,
1258+
contents: &str,
1259+
enable_minification: bool,
1260+
) -> io::Result<()> {
1261+
use minifier::js::{Keyword, ReservedChar, Token};
1262+
12241263
if enable_minification {
12251264
writeln!(dst, "{}",
1226-
minifier::js::minify_and_replace_keywords(contents, keywords_to_replace))
1265+
minifier::js::simple_minify(contents)
1266+
.apply(|f| {
1267+
// We keep backlines.
1268+
minifier::js::clean_tokens_except(f, |c| {
1269+
c.get_char() != Some(ReservedChar::Backline)
1270+
})
1271+
})
1272+
.apply(|f| {
1273+
minifier::js::replace_token_with(f, |t| {
1274+
match *t {
1275+
Token::Keyword(Keyword::Null) => Some(Token::Other("N")),
1276+
Token::String(s) => {
1277+
let s = &s[1..s.len() -1]; // The quotes are included
1278+
if s.is_empty() {
1279+
Some(Token::Other("E"))
1280+
} else if s == "t" {
1281+
Some(Token::Other("T"))
1282+
} else if s == "u" {
1283+
Some(Token::Other("U"))
1284+
} else {
1285+
None
1286+
}
1287+
}
1288+
_ => None,
1289+
}
1290+
})
1291+
})
1292+
.apply(|f| {
1293+
// We add a backline after the newly created variables.
1294+
minifier::js::aggregate_strings_with_separation(
1295+
f,
1296+
Token::Char(ReservedChar::Backline),
1297+
)
1298+
})
1299+
.to_string())
12271300
} else {
12281301
writeln!(dst, "{}", contents)
12291302
}

‎src/librustdoc/html/static/main.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1555,10 +1555,10 @@ if (!DOMTokenList.prototype.remove) {
15551555
// (String) description,
15561556
// (Number | null) the parent path index to `paths`]
15571557
// (Object | null) the type of the function (if any)
1558-
var items = rawSearchIndex[crate].items;
1558+
var items = rawSearchIndex[crate].i;
15591559
// an array of [(Number) item type,
15601560
// (String) name]
1561-
var paths = rawSearchIndex[crate].paths;
1561+
var paths = rawSearchIndex[crate].p;
15621562

15631563
// convert `paths` into an object form
15641564
var len = paths.length;

0 commit comments

Comments
 (0)