Skip to content

Commit 53f499d

Browse files
committed
rustdoc-search: use ES6 Map for Result instead of Object
1 parent 8642c96 commit 53f499d

File tree

2 files changed

+40
-21
lines changed

2 files changed

+40
-21
lines changed

src/librustdoc/html/static/js/externs.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,11 @@ let Row;
6565
*/
6666
let ResultsTable;
6767

68+
/**
69+
* @typedef {Map<String, ResultObject>}
70+
*/
71+
let Results;
72+
6873
/**
6974
* @typedef {{
7075
* desc: string,
@@ -80,7 +85,7 @@ let ResultsTable;
8085
* ty: number,
8186
* }}
8287
*/
83-
let Results;
88+
let ResultObject;
8489

8590
/**
8691
* A pair of [inputs, outputs], or 0 for null. This is stored in the search index.

src/librustdoc/html/static/js/search.js

+34-20
Original file line numberDiff line numberDiff line change
@@ -903,8 +903,16 @@ function initSearch(rawSearchIndex) {
903903
* @return {ResultsTable}
904904
*/
905905
function execQuery(parsedQuery, searchWords, filterCrates, currentCrate) {
906-
const results_others = {}, results_in_args = {}, results_returned = {};
906+
const results_others = new Map(), results_in_args = new Map(),
907+
results_returned = new Map();
907908

909+
/**
910+
* Add extra data to result objects, and filter items that have been
911+
* marked for removal.
912+
*
913+
* @param {[ResultObject]} results
914+
* @returns {[ResultObject]}
915+
*/
908916
function transformResults(results) {
909917
const duplicates = new Set();
910918
const out = [];
@@ -934,24 +942,30 @@ function initSearch(rawSearchIndex) {
934942
return out;
935943
}
936944

945+
/**
946+
* This function takes a result map, and sorts it by various criteria, including edit
947+
* distance, substring match, and the crate it comes from.
948+
*
949+
* @param {Results} results
950+
* @param {boolean} isType
951+
* @param {string} preferredCrate
952+
* @returns {[ResultObject]}
953+
*/
937954
function sortResults(results, isType, preferredCrate) {
938-
const userQuery = parsedQuery.userQuery;
939-
const ar = [];
940-
for (const entry in results) {
941-
if (hasOwnPropertyRustdoc(results, entry)) {
942-
const result = results[entry];
943-
result.word = searchWords[result.id];
944-
result.item = searchIndex[result.id] || {};
945-
ar.push(result);
946-
}
947-
}
948-
results = ar;
949955
// if there are no results then return to default and fail
950-
if (results.length === 0) {
956+
if (results.size === 0) {
951957
return [];
952958
}
953959

954-
results.sort((aaa, bbb) => {
960+
const userQuery = parsedQuery.userQuery;
961+
const result_list = [];
962+
for (const result of results.values()) {
963+
result.word = searchWords[result.id];
964+
result.item = searchIndex[result.id] || {};
965+
result_list.push(result);
966+
}
967+
968+
result_list.sort((aaa, bbb) => {
955969
let a, b;
956970

957971
// sort by exact match with regard to the last word (mismatch goes later)
@@ -1060,7 +1074,7 @@ function initSearch(rawSearchIndex) {
10601074
nameSplit = hasPath ? null : parsedQuery.elems[0].path;
10611075
}
10621076

1063-
for (const result of results) {
1077+
for (const result of result_list) {
10641078
// this validation does not make sense when searching by types
10651079
if (result.dontValidate) {
10661080
continue;
@@ -1073,7 +1087,7 @@ function initSearch(rawSearchIndex) {
10731087
result.id = -1;
10741088
}
10751089
}
1076-
return transformResults(results);
1090+
return transformResults(result_list);
10771091
}
10781092

10791093
/**
@@ -1487,19 +1501,19 @@ function initSearch(rawSearchIndex) {
14871501
function addIntoResults(results, fullId, id, index, dist, path_dist, maxEditDistance) {
14881502
const inBounds = dist <= maxEditDistance || index !== -1;
14891503
if (dist === 0 || (!parsedQuery.literalSearch && inBounds)) {
1490-
if (results[fullId] !== undefined) {
1491-
const result = results[fullId];
1504+
if (results.has(fullId)) {
1505+
const result = results.get(fullId);
14921506
if (result.dontValidate || result.dist <= dist) {
14931507
return;
14941508
}
14951509
}
1496-
results[fullId] = {
1510+
results.set(fullId, {
14971511
id: id,
14981512
index: index,
14991513
dontValidate: parsedQuery.literalSearch,
15001514
dist: dist,
15011515
path_dist: path_dist,
1502-
};
1516+
});
15031517
}
15041518
}
15051519

0 commit comments

Comments
 (0)