Skip to content

Commit 710a362

Browse files
committed
Auto merge of #66828 - GuillaumeGomez:less-minification, r=kinnison
Less minification The goal of this PR is to remove the minification process on the `search-index.js` file. It provides great result in term of space reduction but the computation time is far too long. I'll work on this issue and will put it back once it's fast enough. cc @nox @lqd r? @kinnison
2 parents a0312c1 + 2d0f0ca commit 710a362

File tree

2 files changed

+14
-91
lines changed

2 files changed

+14
-91
lines changed

src/librustdoc/html/render.rs

+13-90
Original file line numberDiff line numberDiff line change
@@ -644,10 +644,9 @@ themePicker.onblur = handleThemeButtonsBlur;
644644
themes.appendChild(but);
645645
}});"#,
646646
as_json(&themes));
647-
write(cx.dst.join(&format!("theme{}.js", cx.shared.resource_suffix)),
648-
theme_js.as_bytes()
649-
)?;
650-
647+
write_minify(&cx.shared.fs, cx.path("theme.js"),
648+
&theme_js,
649+
options.enable_minification)?;
651650
write_minify(&cx.shared.fs, cx.path("main.js"),
652651
static_files::MAIN_JS,
653652
options.enable_minification)?;
@@ -715,19 +714,13 @@ themePicker.onblur = handleThemeButtonsBlur;
715714
path: &Path,
716715
krate: &str,
717716
key: &str,
718-
for_search_index: bool,
719-
) -> io::Result<(Vec<String>, Vec<String>, Vec<String>)> {
717+
) -> io::Result<(Vec<String>, Vec<String>)> {
720718
let mut ret = Vec::new();
721719
let mut krates = Vec::new();
722-
let mut variables = Vec::new();
723720

724721
if path.exists() {
725722
for line in BufReader::new(File::open(path)?).lines() {
726723
let line = line?;
727-
if for_search_index && line.starts_with("var R") {
728-
variables.push(line.clone());
729-
continue;
730-
}
731724
if !line.starts_with(key) {
732725
continue;
733726
}
@@ -741,7 +734,7 @@ themePicker.onblur = handleThemeButtonsBlur;
741734
.unwrap_or_else(|| String::new()));
742735
}
743736
}
744-
Ok((ret, krates, variables))
737+
Ok((ret, krates))
745738
}
746739

747740
fn show_item(item: &IndexItem, krate: &str) -> String {
@@ -756,7 +749,7 @@ themePicker.onblur = handleThemeButtonsBlur;
756749

757750
let dst = cx.dst.join(&format!("aliases{}.js", cx.shared.resource_suffix));
758751
{
759-
let (mut all_aliases, _, _) = try_err!(collect(&dst, &krate.name, "ALIASES", false), &dst);
752+
let (mut all_aliases, _) = try_err!(collect(&dst, &krate.name, "ALIASES"), &dst);
760753
let mut output = String::with_capacity(100);
761754
for (alias, items) in &cx.cache.aliases {
762755
if items.is_empty() {
@@ -853,9 +846,7 @@ themePicker.onblur = handleThemeButtonsBlur;
853846
}
854847

855848
let dst = cx.dst.join(&format!("source-files{}.js", cx.shared.resource_suffix));
856-
let (mut all_sources, _krates, _) = try_err!(collect(&dst, &krate.name, "sourcesIndex",
857-
false),
858-
&dst);
849+
let (mut all_sources, _krates) = try_err!(collect(&dst, &krate.name, "sourcesIndex"), &dst);
859850
all_sources.push(format!("sourcesIndex[\"{}\"] = {};",
860851
&krate.name,
861852
hierarchy.to_json_string()));
@@ -867,23 +858,18 @@ themePicker.onblur = handleThemeButtonsBlur;
867858

868859
// Update the search index
869860
let dst = cx.dst.join(&format!("search-index{}.js", cx.shared.resource_suffix));
870-
let (mut all_indexes, mut krates, variables) = try_err!(collect(&dst,
871-
&krate.name,
872-
"searchIndex",
873-
true), &dst);
861+
let (mut all_indexes, mut krates) = try_err!(collect(&dst, &krate.name, "searchIndex"), &dst);
874862
all_indexes.push(search_index);
875863

876864
// Sort the indexes by crate so the file will be generated identically even
877865
// with rustdoc running in parallel.
878866
all_indexes.sort();
879867
{
880-
let mut v = String::from("var N=null,E=\"\",T=\"t\",U=\"u\",searchIndex={};\n");
881-
v.push_str(&minify_replacer(
882-
&format!("{}\n{}", variables.join(""), all_indexes.join("\n")),
883-
options.enable_minification));
868+
let mut v = String::from("var searchIndex={};\n");
869+
v.push_str(&all_indexes.join("\n"));
884870
// "addSearchOptions" has to be called first so the crate filtering can be set before the
885871
// search might start (if it's set into the URL for example).
886-
v.push_str("addSearchOptions(searchIndex);initSearch(searchIndex);");
872+
v.push_str("\naddSearchOptions(searchIndex);initSearch(searchIndex);");
887873
cx.shared.fs.write(&dst, &v)?;
888874
}
889875
if options.enable_index_page {
@@ -981,9 +967,8 @@ themePicker.onblur = handleThemeButtonsBlur;
981967
remote_item_type,
982968
remote_path[remote_path.len() - 1]));
983969

984-
let (mut all_implementors, _, _) = try_err!(collect(&mydst, &krate.name, "implementors",
985-
false),
986-
&mydst);
970+
let (mut all_implementors, _) = try_err!(collect(&mydst, &krate.name, "implementors"),
971+
&mydst);
987972
all_implementors.push(implementors);
988973
// Sort the implementors by crate so the file will be generated
989974
// identically even with rustdoc running in parallel.
@@ -1020,68 +1005,6 @@ fn write_minify(fs:&DocFS, dst: PathBuf, contents: &str, enable_minification: bo
10201005
}
10211006
}
10221007

1023-
fn minify_replacer(
1024-
contents: &str,
1025-
enable_minification: bool,
1026-
) -> String {
1027-
use minifier::js::{simple_minify, Keyword, ReservedChar, Token, Tokens};
1028-
1029-
if enable_minification {
1030-
let tokens: Tokens<'_> = simple_minify(contents)
1031-
.into_iter()
1032-
.filter(|(f, next)| {
1033-
// We keep backlines.
1034-
minifier::js::clean_token_except(f, next, &|c: &Token<'_>| {
1035-
c.get_char() != Some(ReservedChar::Backline)
1036-
})
1037-
})
1038-
.map(|(f, _)| {
1039-
minifier::js::replace_token_with(f, &|t: &Token<'_>| {
1040-
match *t {
1041-
Token::Keyword(Keyword::Null) => Some(Token::Other("N")),
1042-
Token::String(s) => {
1043-
let s = &s[1..s.len() -1]; // The quotes are included
1044-
if s.is_empty() {
1045-
Some(Token::Other("E"))
1046-
} else if s == "t" {
1047-
Some(Token::Other("T"))
1048-
} else if s == "u" {
1049-
Some(Token::Other("U"))
1050-
} else {
1051-
None
1052-
}
1053-
}
1054-
_ => None,
1055-
}
1056-
})
1057-
})
1058-
.collect::<Vec<_>>()
1059-
.into();
1060-
let o = tokens.apply(|f| {
1061-
// We add a backline after the newly created variables.
1062-
minifier::js::aggregate_strings_into_array_with_separation_filter(
1063-
f,
1064-
"R",
1065-
Token::Char(ReservedChar::Backline),
1066-
// This closure prevents crates' names from being aggregated.
1067-
//
1068-
// The point here is to check if the string is preceded by '[' and
1069-
// "searchIndex". If so, it means this is a crate name and that it
1070-
// shouldn't be aggregated.
1071-
|tokens, pos| {
1072-
pos < 2 ||
1073-
!tokens[pos - 1].eq_char(ReservedChar::OpenBracket) ||
1074-
tokens[pos - 2].get_other() != Some("searchIndex")
1075-
}
1076-
)
1077-
})
1078-
.to_string();
1079-
format!("{}\n", o)
1080-
} else {
1081-
format!("{}\n", contents)
1082-
}
1083-
}
1084-
10851008
#[derive(Debug, Eq, PartialEq, Hash)]
10861009
struct ItemEntry {
10871010
url: String,

src/librustdoc/html/static/main.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1221,7 +1221,7 @@ function getSearchElement() {
12211221
// then an exact path match
12221222
path.indexOf(keys[i]) > -1 ||
12231223
// next if there is a parent, check for exact parent match
1224-
(parent !== undefined &&
1224+
(parent !== undefined && parent.name !== undefined &&
12251225
parent.name.toLowerCase().indexOf(keys[i]) > -1) ||
12261226
// lastly check to see if the name was a levenshtein match
12271227
levenshtein(name, keys[i]) <= MAX_LEV_DISTANCE)) {

0 commit comments

Comments
 (0)