Skip to content

Commit 35a3493

Browse files
authored
Rollup merge of #110305 - notriddle:notriddle/es6-map-set, r=GuillaumeGomez
rustdoc-search: use ES6 `Map` and `Set` where they make sense Since all supported browsers now support these classes, and rustdoc has started using them in some places, it might as well use them everywhere it makes sense (because, as [MDN's Map page] says, it "performs better in scenarios involving frequent additions and removals of key-value pairs."). [MDN's Map page]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map
2 parents ad0a935 + 53f499d commit 35a3493

File tree

2 files changed

+73
-58
lines changed

2 files changed

+73
-58
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

+67-57
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ function initSearch(rawSearchIndex) {
191191
*/
192192
let searchIndex;
193193
let currentResults;
194-
const ALIASES = Object.create(null);
194+
const ALIASES = new Map();
195195

196196
function isWhitespace(c) {
197197
return " \t\n\r".indexOf(c) !== -1;
@@ -903,10 +903,18 @@ 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) {
909-
const duplicates = {};
917+
const duplicates = new Set();
910918
const out = [];
911919

912920
for (const result of results) {
@@ -919,10 +927,10 @@ function initSearch(rawSearchIndex) {
919927
// To be sure than it some items aren't considered as duplicate.
920928
obj.fullPath += "|" + obj.ty;
921929

922-
if (duplicates[obj.fullPath]) {
930+
if (duplicates.has(obj.fullPath)) {
923931
continue;
924932
}
925-
duplicates[obj.fullPath] = true;
933+
duplicates.add(obj.fullPath);
926934

927935
obj.href = res[1];
928936
out.push(obj);
@@ -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
/**
@@ -1096,7 +1110,7 @@ function initSearch(rawSearchIndex) {
10961110
// The names match, but we need to be sure that all generics kinda
10971111
// match as well.
10981112
if (elem.generics.length > 0 && row.generics.length >= elem.generics.length) {
1099-
const elems = Object.create(null);
1113+
const elems = new Map();
11001114
for (const entry of row.generics) {
11011115
if (entry.name === "") {
11021116
// Pure generic, needs to check into it.
@@ -1106,39 +1120,30 @@ function initSearch(rawSearchIndex) {
11061120
}
11071121
continue;
11081122
}
1109-
if (elems[entry.name] === undefined) {
1110-
elems[entry.name] = [];
1123+
let currentEntryElems;
1124+
if (elems.has(entry.name)) {
1125+
currentEntryElems = elems.get(entry.name);
1126+
} else {
1127+
currentEntryElems = [];
1128+
elems.set(entry.name, currentEntryElems);
11111129
}
1112-
elems[entry.name].push(entry.ty);
1130+
currentEntryElems.push(entry.ty);
11131131
}
11141132
// We need to find the type that matches the most to remove it in order
11151133
// to move forward.
11161134
const handleGeneric = generic => {
1117-
let match = null;
1118-
if (elems[generic.name]) {
1119-
match = generic.name;
1120-
} else {
1121-
for (const elem_name in elems) {
1122-
if (!hasOwnPropertyRustdoc(elems, elem_name)) {
1123-
continue;
1124-
}
1125-
if (elem_name === generic) {
1126-
match = elem_name;
1127-
break;
1128-
}
1129-
}
1130-
}
1131-
if (match === null) {
1135+
if (!elems.has(generic.name)) {
11321136
return false;
11331137
}
1134-
const matchIdx = elems[match].findIndex(tmp_elem =>
1138+
const matchElems = elems.get(generic.name);
1139+
const matchIdx = matchElems.findIndex(tmp_elem =>
11351140
typePassesFilter(generic.typeFilter, tmp_elem));
11361141
if (matchIdx === -1) {
11371142
return false;
11381143
}
1139-
elems[match].splice(matchIdx, 1);
1140-
if (elems[match].length === 0) {
1141-
delete elems[match];
1144+
matchElems.splice(matchIdx, 1);
1145+
if (matchElems.length === 0) {
1146+
elems.delete(generic.name);
11421147
}
11431148
return true;
11441149
};
@@ -1424,22 +1429,22 @@ function initSearch(rawSearchIndex) {
14241429
const aliases = [];
14251430
const crateAliases = [];
14261431
if (filterCrates !== null) {
1427-
if (ALIASES[filterCrates] && ALIASES[filterCrates][lowerQuery]) {
1428-
const query_aliases = ALIASES[filterCrates][lowerQuery];
1432+
if (ALIASES.has(filterCrates) && ALIASES.get(filterCrates).has(lowerQuery)) {
1433+
const query_aliases = ALIASES.get(filterCrates).get(lowerQuery);
14291434
for (const alias of query_aliases) {
14301435
aliases.push(createAliasFromItem(searchIndex[alias]));
14311436
}
14321437
}
14331438
} else {
1434-
Object.keys(ALIASES).forEach(crate => {
1435-
if (ALIASES[crate][lowerQuery]) {
1439+
for (const [crate, crateAliasesIndex] of ALIASES) {
1440+
if (crateAliasesIndex.has(lowerQuery)) {
14361441
const pushTo = crate === currentCrate ? crateAliases : aliases;
1437-
const query_aliases = ALIASES[crate][lowerQuery];
1442+
const query_aliases = crateAliasesIndex.get(lowerQuery);
14381443
for (const alias of query_aliases) {
14391444
pushTo.push(createAliasFromItem(searchIndex[alias]));
14401445
}
14411446
}
1442-
});
1447+
}
14431448
}
14441449

14451450
const sortFunc = (aaa, bbb) => {
@@ -1496,19 +1501,19 @@ function initSearch(rawSearchIndex) {
14961501
function addIntoResults(results, fullId, id, index, dist, path_dist, maxEditDistance) {
14971502
const inBounds = dist <= maxEditDistance || index !== -1;
14981503
if (dist === 0 || (!parsedQuery.literalSearch && inBounds)) {
1499-
if (results[fullId] !== undefined) {
1500-
const result = results[fullId];
1504+
if (results.has(fullId)) {
1505+
const result = results.get(fullId);
15011506
if (result.dontValidate || result.dist <= dist) {
15021507
return;
15031508
}
15041509
}
1505-
results[fullId] = {
1510+
results.set(fullId, {
15061511
id: id,
15071512
index: index,
15081513
dontValidate: parsedQuery.literalSearch,
15091514
dist: dist,
15101515
path_dist: path_dist,
1511-
};
1516+
});
15121517
}
15131518
}
15141519

@@ -2345,17 +2350,22 @@ function initSearch(rawSearchIndex) {
23452350
}
23462351

23472352
if (aliases) {
2348-
ALIASES[crate] = Object.create(null);
2353+
const currentCrateAliases = new Map();
2354+
ALIASES.set(crate, currentCrateAliases);
23492355
for (const alias_name in aliases) {
23502356
if (!hasOwnPropertyRustdoc(aliases, alias_name)) {
23512357
continue;
23522358
}
23532359

2354-
if (!hasOwnPropertyRustdoc(ALIASES[crate], alias_name)) {
2355-
ALIASES[crate][alias_name] = [];
2360+
let currentNameAliases;
2361+
if (currentCrateAliases.has(alias_name)) {
2362+
currentNameAliases = currentCrateAliases.get(alias_name);
2363+
} else {
2364+
currentNameAliases = [];
2365+
currentCrateAliases.set(alias_name, currentNameAliases);
23562366
}
23572367
for (const local_alias of aliases[alias_name]) {
2358-
ALIASES[crate][alias_name].push(local_alias + currentIndex);
2368+
currentNameAliases.push(local_alias + currentIndex);
23592369
}
23602370
}
23612371
}

0 commit comments

Comments
 (0)