Skip to content

Commit

Permalink
rustdoc-search: use ES6 Map for Result instead of Object
Browse files Browse the repository at this point in the history
  • Loading branch information
notriddle committed Apr 14, 2023
1 parent 8642c96 commit 53f499d
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 21 deletions.
7 changes: 6 additions & 1 deletion src/librustdoc/html/static/js/externs.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@ let Row;
*/
let ResultsTable;

/**
* @typedef {Map<String, ResultObject>}
*/
let Results;

/**
* @typedef {{
* desc: string,
Expand All @@ -80,7 +85,7 @@ let ResultsTable;
* ty: number,
* }}
*/
let Results;
let ResultObject;

/**
* A pair of [inputs, outputs], or 0 for null. This is stored in the search index.
Expand Down
54 changes: 34 additions & 20 deletions src/librustdoc/html/static/js/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -903,8 +903,16 @@ function initSearch(rawSearchIndex) {
* @return {ResultsTable}
*/
function execQuery(parsedQuery, searchWords, filterCrates, currentCrate) {
const results_others = {}, results_in_args = {}, results_returned = {};
const results_others = new Map(), results_in_args = new Map(),
results_returned = new Map();

/**
* Add extra data to result objects, and filter items that have been
* marked for removal.
*
* @param {[ResultObject]} results
* @returns {[ResultObject]}
*/
function transformResults(results) {
const duplicates = new Set();
const out = [];
Expand Down Expand Up @@ -934,24 +942,30 @@ function initSearch(rawSearchIndex) {
return out;
}

/**
* This function takes a result map, and sorts it by various criteria, including edit
* distance, substring match, and the crate it comes from.
*
* @param {Results} results
* @param {boolean} isType
* @param {string} preferredCrate
* @returns {[ResultObject]}
*/
function sortResults(results, isType, preferredCrate) {
const userQuery = parsedQuery.userQuery;
const ar = [];
for (const entry in results) {
if (hasOwnPropertyRustdoc(results, entry)) {
const result = results[entry];
result.word = searchWords[result.id];
result.item = searchIndex[result.id] || {};
ar.push(result);
}
}
results = ar;
// if there are no results then return to default and fail
if (results.length === 0) {
if (results.size === 0) {
return [];
}

results.sort((aaa, bbb) => {
const userQuery = parsedQuery.userQuery;
const result_list = [];
for (const result of results.values()) {
result.word = searchWords[result.id];
result.item = searchIndex[result.id] || {};
result_list.push(result);
}

result_list.sort((aaa, bbb) => {
let a, b;

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

for (const result of results) {
for (const result of result_list) {
// this validation does not make sense when searching by types
if (result.dontValidate) {
continue;
Expand All @@ -1073,7 +1087,7 @@ function initSearch(rawSearchIndex) {
result.id = -1;
}
}
return transformResults(results);
return transformResults(result_list);
}

/**
Expand Down Expand Up @@ -1487,19 +1501,19 @@ function initSearch(rawSearchIndex) {
function addIntoResults(results, fullId, id, index, dist, path_dist, maxEditDistance) {
const inBounds = dist <= maxEditDistance || index !== -1;
if (dist === 0 || (!parsedQuery.literalSearch && inBounds)) {
if (results[fullId] !== undefined) {
const result = results[fullId];
if (results.has(fullId)) {
const result = results.get(fullId);
if (result.dontValidate || result.dist <= dist) {
return;
}
}
results[fullId] = {
results.set(fullId, {
id: id,
index: index,
dontValidate: parsedQuery.literalSearch,
dist: dist,
path_dist: path_dist,
};
});
}
}

Expand Down

0 comments on commit 53f499d

Please sign in to comment.