Skip to content

Commit 478226c

Browse files
committed
Auto merge of #51599 - GuillaumeGomez:reduce-search-index, r=QuietMisdreavus
reduce search-index size Reduce size of `search-index.js` of around 16%: Going from `2996785` bytes to `2545583`. r? @QuietMisdreavus
2 parents 94eb176 + 115df57 commit 478226c

File tree

3 files changed

+54
-32
lines changed

3 files changed

+54
-32
lines changed

src/librustdoc/html/render.rs

+26-13
Original file line numberDiff line numberDiff line change
@@ -414,13 +414,13 @@ impl ToJson for Type {
414414
fn to_json(&self) -> Json {
415415
match self.name {
416416
Some(ref name) => {
417-
let mut data = BTreeMap::new();
418-
data.insert("n".to_owned(), name.to_json());
417+
let mut data = Vec::with_capacity(2);
418+
data.push(name.to_json());
419419
if let Some(ref generics) = self.generics {
420-
data.insert("g".to_owned(), generics.to_json());
420+
data.push(generics.to_json());
421421
}
422-
Json::Object(data)
423-
},
422+
Json::Array(data)
423+
}
424424
None => Json::Null
425425
}
426426
}
@@ -439,14 +439,12 @@ impl ToJson for IndexItemFunctionType {
439439
if self.inputs.iter().chain(self.output.iter()).any(|ref i| i.name.is_none()) {
440440
Json::Null
441441
} else {
442-
let mut data = BTreeMap::new();
443-
if !self.inputs.is_empty() {
444-
data.insert("i".to_owned(), self.inputs.to_json());
445-
}
442+
let mut data = Vec::with_capacity(2);
443+
data.push(self.inputs.to_json());
446444
if let Some(ref output) = self.output {
447-
data.insert("o".to_owned(), output.to_json());
445+
data.push(output.to_json());
448446
}
449-
Json::Object(data)
447+
Json::Array(data)
450448
}
451449
}
452450
}
@@ -963,9 +961,11 @@ themePicker.onblur = handleThemeButtonsBlur;
963961
// with rustdoc running in parallel.
964962
all_indexes.sort();
965963
let mut w = try_err!(File::create(&dst), &dst);
966-
try_err!(writeln!(&mut w, "var searchIndex = {{}};"), &dst);
964+
try_err!(writeln!(&mut w, "var N = null;var searchIndex = {{}};"), &dst);
967965
for index in &all_indexes {
968-
try_err!(writeln!(&mut w, "{}", *index), &dst);
966+
try_err!(write_minify_replacer(&mut w, &*index, enable_minification,
967+
&[(minifier::js::Keyword::Null, "N")]),
968+
&dst);
969969
}
970970
try_err!(writeln!(&mut w, "initSearch(searchIndex);"), &dst);
971971

@@ -1076,6 +1076,19 @@ fn write_minify(dst: PathBuf, contents: &str, enable_minification: bool) -> Resu
10761076
}
10771077
}
10781078

1079+
fn write_minify_replacer<W: Write>(dst: &mut W,
1080+
contents: &str,
1081+
enable_minification: bool,
1082+
keywords_to_replace: &[(minifier::js::Keyword, &str)])
1083+
-> io::Result<()> {
1084+
if enable_minification {
1085+
writeln!(dst, "{}",
1086+
minifier::js::minify_and_replace_keywords(contents, keywords_to_replace))
1087+
} else {
1088+
writeln!(dst, "{}", contents)
1089+
}
1090+
}
1091+
10791092
/// Takes a path to a source file and cleans the path to it. This canonicalizes
10801093
/// things like ".." to components which preserve the "top down" hierarchy of a
10811094
/// static HTML tree. Each component in the cleaned path will be passed as an

src/librustdoc/html/static/main.js

+27-19
Original file line numberDiff line numberDiff line change
@@ -415,6 +415,10 @@
415415
var currentResults, index, searchIndex;
416416
var MAX_LEV_DISTANCE = 3;
417417
var MAX_RESULTS = 200;
418+
var GENERICS_DATA = 1;
419+
var NAME = 0;
420+
var INPUTS_DATA = 0;
421+
var OUTPUT_DATA = 1;
418422
var params = getQueryStringParams();
419423

420424
// Populate search bar with query string search term when provided,
@@ -603,8 +607,9 @@
603607
// match as well.
604608
var lev_distance = MAX_LEV_DISTANCE + 1;
605609
if (val.generics.length > 0) {
606-
if (obj.g && obj.g.length >= val.generics.length) {
607-
var elems = obj.g.slice(0);
610+
if (obj.length > GENERICS_DATA &&
611+
obj[GENERICS_DATA].length >= val.generics.length) {
612+
var elems = obj[GENERICS_DATA].slice(0);
608613
var total = 0;
609614
var done = 0;
610615
// We need to find the type that matches the most to remove it in order
@@ -636,11 +641,12 @@
636641
// Check for type name and type generics (if any).
637642
function checkType(obj, val, literalSearch) {
638643
var lev_distance = MAX_LEV_DISTANCE + 1;
639-
if (obj.n === val.name) {
644+
if (obj[NAME] === val.name) {
640645
if (literalSearch === true) {
641646
if (val.generics && val.generics.length !== 0) {
642-
if (obj.g && obj.length >= val.generics.length) {
643-
var elems = obj.g.slice(0);
647+
if (obj.length > GENERICS_DATA &&
648+
obj[GENERICS_DATA].length >= val.generics.length) {
649+
var elems = obj[GENERICS_DATA].slice(0);
644650
var allFound = true;
645651
var x;
646652

@@ -664,7 +670,7 @@
664670
}
665671
// If the type has generics but don't match, then it won't return at this point.
666672
// Otherwise, `checkGenerics` will return 0 and it'll return.
667-
if (obj.g && obj.g.length !== 0) {
673+
if (obj.length > GENERICS_DATA && obj[GENERICS_DATA].length !== 0) {
668674
var tmp_lev = checkGenerics(obj, val);
669675
if (tmp_lev <= MAX_LEV_DISTANCE) {
670676
return tmp_lev;
@@ -675,22 +681,23 @@
675681
}
676682
// Names didn't match so let's check if one of the generic types could.
677683
if (literalSearch === true) {
678-
if (obj.g && obj.g.length > 0) {
679-
for (var x = 0; x < obj.g.length; ++x) {
680-
if (obj.g[x] === val.name) {
684+
if (obj.length > GENERICS_DATA && obj[GENERICS_DATA].length > 0) {
685+
for (var x = 0; x < obj[GENERICS_DATA].length; ++x) {
686+
if (obj[GENERICS_DATA][x] === val.name) {
681687
return true;
682688
}
683689
}
684690
}
685691
return false;
686692
}
687-
var lev_distance = Math.min(levenshtein(obj.n, val.name), lev_distance);
693+
var lev_distance = Math.min(levenshtein(obj[NAME], val.name),
694+
lev_distance);
688695
if (lev_distance <= MAX_LEV_DISTANCE) {
689696
lev_distance = Math.min(checkGenerics(obj, val), lev_distance);
690-
} else if (obj.g && obj.g.length > 0) {
697+
} else if (obj.length > GENERICS_DATA && obj[GENERICS_DATA].length > 0) {
691698
// We can check if the type we're looking for is inside the generics!
692-
for (var x = 0; x < obj.g.length; ++x) {
693-
lev_distance = Math.min(levenshtein(obj.g[x], val.name),
699+
for (var x = 0; x < obj[GENERICS_DATA].length; ++x) {
700+
lev_distance = Math.min(levenshtein(obj[GENERICS_DATA][x], val.name),
694701
lev_distance);
695702
}
696703
}
@@ -702,9 +709,10 @@
702709
function findArg(obj, val, literalSearch) {
703710
var lev_distance = MAX_LEV_DISTANCE + 1;
704711

705-
if (obj && obj.type && obj.type.i && obj.type.i.length > 0) {
706-
for (var i = 0; i < obj.type.i.length; i++) {
707-
var tmp = checkType(obj.type.i[i], val, literalSearch);
712+
if (obj && obj.type && obj.type[INPUTS_DATA] &&
713+
obj.type[INPUTS_DATA].length > 0) {
714+
for (var i = 0; i < obj.type[INPUTS_DATA].length; i++) {
715+
var tmp = checkType(obj.type[INPUTS_DATA][i], val, literalSearch);
708716
if (literalSearch === true && tmp === true) {
709717
return true;
710718
}
@@ -720,8 +728,8 @@
720728
function checkReturned(obj, val, literalSearch) {
721729
var lev_distance = MAX_LEV_DISTANCE + 1;
722730

723-
if (obj && obj.type && obj.type.o) {
724-
var tmp = checkType(obj.type.o, val, literalSearch);
731+
if (obj && obj.type && obj.type.length > OUTPUT_DATA) {
732+
var tmp = checkType(obj.type[OUTPUT_DATA], val, literalSearch);
725733
if (literalSearch === true && tmp === true) {
726734
return true;
727735
}
@@ -866,7 +874,7 @@
866874
var fullId = generateId(ty);
867875

868876
// allow searching for void (no output) functions as well
869-
var typeOutput = type.o ? type.o.name : "";
877+
var typeOutput = type.length > OUTPUT_DATA ? type[OUTPUT_DATA].name : "";
870878
var returned = checkReturned(ty, output, true);
871879
if (output.name === "*" || returned === true) {
872880
var in_args = false;

src/tools/rustdoc-js/tester.js

+1
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,7 @@ function main(argv) {
233233

234234
var arraysToLoad = ["itemTypes"];
235235
var variablesToLoad = ["MAX_LEV_DISTANCE", "MAX_RESULTS",
236+
"GENERICS_DATA", "NAME", "INPUTS_DATA", "OUTPUT_DATA",
236237
"TY_PRIMITIVE", "TY_KEYWORD",
237238
"levenshtein_row2"];
238239
// execQuery first parameter is built in getQuery (which takes in the search input).

0 commit comments

Comments
 (0)