Skip to content

Commit cbf0365

Browse files
committed
rustdoc-search: remove parallel searchWords array
This might have made sense if the algorithm could use `searchWords` to skip having to look at `searchIndex`, but since it always does a substring check on both the stock word and the normalizedName, it doesn't seem to help performance anyway.
1 parent e6707df commit cbf0365

File tree

2 files changed

+28
-50
lines changed

2 files changed

+28
-50
lines changed

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

Lines changed: 25 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -499,6 +499,7 @@ function initSearch(rawSearchIndex) {
499499
fullPath: ["never"],
500500
pathWithoutLast: [],
501501
pathLast: "never",
502+
normalizedPathLast: "never",
502503
generics: [],
503504
bindings: new Map(),
504505
typeFilter: "primitive",
@@ -537,12 +538,14 @@ function initSearch(rawSearchIndex) {
537538
const bindingName = parserState.isInBinding;
538539
parserState.isInBinding = null;
539540
const bindings = new Map();
541+
const pathLast = pathSegments[pathSegments.length - 1];
540542
return {
541543
name: name.trim(),
542544
id: null,
543545
fullPath: pathSegments,
544546
pathWithoutLast: pathSegments.slice(0, pathSegments.length - 1),
545-
pathLast: pathSegments[pathSegments.length - 1],
547+
pathLast,
548+
normalizedPathLast: pathLast.replace(/_/g, ""),
546549
generics: generics.filter(gen => {
547550
// Syntactically, bindings are parsed as generics,
548551
// but the query engine treats them differently.
@@ -689,6 +692,7 @@ function initSearch(rawSearchIndex) {
689692
fullPath: ["[]"],
690693
pathWithoutLast: [],
691694
pathLast: "[]",
695+
normalizedPathLast: "[]",
692696
generics,
693697
typeFilter: "primitive",
694698
bindingName: isInBinding,
@@ -1168,13 +1172,12 @@ function initSearch(rawSearchIndex) {
11681172
* Executes the parsed query and builds a {ResultsTable}.
11691173
*
11701174
* @param {ParsedQuery} parsedQuery - The parsed user query
1171-
* @param {Object} searchWords - The list of search words to query against
11721175
* @param {Object} [filterCrates] - Crate to search in if defined
11731176
* @param {Object} [currentCrate] - Current crate, to rank results from this crate higher
11741177
*
11751178
* @return {ResultsTable}
11761179
*/
1177-
function execQuery(parsedQuery, searchWords, filterCrates, currentCrate) {
1180+
function execQuery(parsedQuery, filterCrates, currentCrate) {
11781181
const results_others = new Map(), results_in_args = new Map(),
11791182
results_returned = new Map();
11801183

@@ -1232,8 +1235,8 @@ function initSearch(rawSearchIndex) {
12321235
const userQuery = parsedQuery.userQuery;
12331236
const result_list = [];
12341237
for (const result of results.values()) {
1235-
result.word = searchWords[result.id];
1236-
result.item = searchIndex[result.id] || {};
1238+
result.item = searchIndex[result.id];
1239+
result.word = searchIndex[result.id].word;
12371240
result_list.push(result);
12381241
}
12391242

@@ -1928,7 +1931,7 @@ function initSearch(rawSearchIndex) {
19281931
* The `results` map contains information which will be used to sort the search results:
19291932
*
19301933
* * `fullId` is a `string`` used as the key of the object we use for the `results` map.
1931-
* * `id` is the index in both `searchWords` and `searchIndex` arrays for this element.
1934+
* * `id` is the index in the `searchIndex` array for this element.
19321935
* * `index` is an `integer`` used to sort by the position of the word in the item's name.
19331936
* * `dist` is the main metric used to sort the search results.
19341937
* * `path_dist` is zero if a single-component search query is used, otherwise it's the
@@ -1986,9 +1989,8 @@ function initSearch(rawSearchIndex) {
19861989
if (!row || (filterCrates !== null && row.crate !== filterCrates)) {
19871990
return;
19881991
}
1989-
let index = -1, path_dist = 0;
1992+
let path_dist = 0;
19901993
const fullId = row.id;
1991-
const searchWord = searchWords[pos];
19921994

19931995
// fpDist is a minimum possible type distance, where "type distance" is the number of
19941996
// atoms in the function not present in the query
@@ -2021,20 +2023,7 @@ function initSearch(rawSearchIndex) {
20212023
return;
20222024
}
20232025

2024-
const row_index = row.normalizedName.indexOf(elem.pathLast);
2025-
const word_index = searchWord.indexOf(elem.pathLast);
2026-
2027-
// lower indexes are "better" matches
2028-
// rank based on the "best" match
2029-
if (row_index === -1) {
2030-
index = word_index;
2031-
} else if (word_index === -1) {
2032-
index = row_index;
2033-
} else if (word_index < row_index) {
2034-
index = word_index;
2035-
} else {
2036-
index = row_index;
2037-
}
2026+
const index = row.word.indexOf(elem.pathLast);
20382027

20392028
if (elem.fullPath.length > 1) {
20402029
path_dist = checkPath(elem.pathWithoutLast, row, maxEditDistance);
@@ -2044,13 +2033,13 @@ function initSearch(rawSearchIndex) {
20442033
}
20452034

20462035
if (parsedQuery.literalSearch) {
2047-
if (searchWord === elem.name) {
2036+
if (row.word === elem.pathLast) {
20482037
addIntoResults(results_others, fullId, pos, index, 0, path_dist);
20492038
}
20502039
return;
20512040
}
20522041

2053-
const dist = editDistance(searchWord, elem.pathLast, maxEditDistance);
2042+
const dist = editDistance(row.normalizedName, elem.normalizedPathLast, maxEditDistance);
20542043

20552044
if (index === -1 && dist + path_dist > maxEditDistance) {
20562045
return;
@@ -2135,15 +2124,15 @@ function initSearch(rawSearchIndex) {
21352124
* @param {boolean} isAssocType
21362125
*/
21372126
function convertNameToId(elem, isAssocType) {
2138-
if (typeNameIdMap.has(elem.pathLast) &&
2139-
(isAssocType || !typeNameIdMap.get(elem.pathLast).assocOnly)) {
2140-
elem.id = typeNameIdMap.get(elem.pathLast).id;
2127+
if (typeNameIdMap.has(elem.normalizedPathLast) &&
2128+
(isAssocType || !typeNameIdMap.get(elem.normalizedPathLast).assocOnly)) {
2129+
elem.id = typeNameIdMap.get(elem.normalizedPathLast).id;
21412130
} else if (!parsedQuery.literalSearch) {
21422131
let match = null;
21432132
let matchDist = maxEditDistance + 1;
21442133
let matchName = "";
21452134
for (const [name, {id, assocOnly}] of typeNameIdMap) {
2146-
const dist = editDistance(name, elem.pathLast, maxEditDistance);
2135+
const dist = editDistance(name, elem.normalizedPathLast, maxEditDistance);
21472136
if (dist <= matchDist && dist <= maxEditDistance &&
21482137
(isAssocType || !assocOnly)) {
21492138
if (dist === matchDist && matchName > name) {
@@ -2236,7 +2225,7 @@ function initSearch(rawSearchIndex) {
22362225
if (parsedQuery.foundElems === 1 && parsedQuery.returned.length === 0) {
22372226
if (parsedQuery.elems.length === 1) {
22382227
const elem = parsedQuery.elems[0];
2239-
for (let i = 0, nSearchWords = searchWords.length; i < nSearchWords; ++i) {
2228+
for (let i = 0, nSearchIndex = searchIndex.length; i < nSearchIndex; ++i) {
22402229
// It means we want to check for this element everywhere (in names, args and
22412230
// returned).
22422231
handleSingleArg(
@@ -2267,7 +2256,7 @@ function initSearch(rawSearchIndex) {
22672256
};
22682257
parsedQuery.elems.sort(sortQ);
22692258
parsedQuery.returned.sort(sortQ);
2270-
for (let i = 0, nSearchWords = searchWords.length; i < nSearchWords; ++i) {
2259+
for (let i = 0, nSearchIndex = searchIndex.length; i < nSearchIndex; ++i) {
22712260
handleArgs(searchIndex[i], i, results_others);
22722261
}
22732262
}
@@ -2651,7 +2640,7 @@ ${item.displayPath}<span class="${type}">${name}</span>\
26512640
updateSearchHistory(buildUrl(query.original, filterCrates));
26522641

26532642
showResults(
2654-
execQuery(query, searchWords, filterCrates, window.currentCrate),
2643+
execQuery(query, filterCrates, window.currentCrate),
26552644
params.go_to_first,
26562645
filterCrates);
26572646
}
@@ -2920,12 +2909,6 @@ ${item.displayPath}<span class="${type}">${name}</span>\
29202909

29212910
function buildIndex(rawSearchIndex) {
29222911
searchIndex = [];
2923-
/**
2924-
* List of normalized search words (ASCII lowercased, and undescores removed).
2925-
*
2926-
* @type {Array<string>}
2927-
*/
2928-
const searchWords = [];
29292912
typeNameIdMap = new Map();
29302913
const charA = "A".charCodeAt(0);
29312914
let currentIndex = 0;
@@ -3004,7 +2987,6 @@ ${item.displayPath}<span class="${type}">${name}</span>\
30042987
* }}
30052988
*/
30062989
for (const [crate, crateCorpus] of rawSearchIndex) {
3007-
searchWords.push(crate);
30082990
// This object should have exactly the same set of fields as the "row"
30092991
// object defined below. Your JavaScript runtime will thank you.
30102992
// https://mathiasbynens.be/notes/shapes-ics
@@ -3017,6 +2999,7 @@ ${item.displayPath}<span class="${type}">${name}</span>\
30172999
parent: undefined,
30183000
type: null,
30193001
id: id,
3002+
word: crate,
30203003
normalizedName: crate.indexOf("_") === -1 ? crate : crate.replace(/_/g, ""),
30213004
deprecated: null,
30223005
implDisambiguator: null,
@@ -3084,12 +3067,9 @@ ${item.displayPath}<span class="${type}">${name}</span>\
30843067
len = itemTypes.length;
30853068
for (let i = 0; i < len; ++i) {
30863069
let word = "";
3087-
// This object should have exactly the same set of fields as the "crateRow"
3088-
// object defined above.
30893070
if (typeof itemNames[i] === "string") {
30903071
word = itemNames[i].toLowerCase();
30913072
}
3092-
searchWords.push(word);
30933073
const path = itemPaths.has(i) ? itemPaths.get(i) : lastPath;
30943074
let type = null;
30953075
if (itemFunctionSearchTypes[i] !== 0) {
@@ -3113,6 +3093,8 @@ ${item.displayPath}<span class="${type}">${name}</span>\
31133093
}
31143094
}
31153095
}
3096+
// This object should have exactly the same set of fields as the "crateRow"
3097+
// object defined above.
31163098
const row = {
31173099
crate: crate,
31183100
ty: itemTypes.charCodeAt(i) - charA,
@@ -3122,6 +3104,7 @@ ${item.displayPath}<span class="${type}">${name}</span>\
31223104
parent: itemParentIdxs[i] > 0 ? paths[itemParentIdxs[i] - 1] : undefined,
31233105
type,
31243106
id: id,
3107+
word,
31253108
normalizedName: word.indexOf("_") === -1 ? word : word.replace(/_/g, ""),
31263109
deprecated: deprecatedItems.has(i),
31273110
implDisambiguator: implDisambiguator.has(i) ? implDisambiguator.get(i) : null,
@@ -3153,7 +3136,6 @@ ${item.displayPath}<span class="${type}">${name}</span>\
31533136
}
31543137
currentIndex += itemTypes.length;
31553138
}
3156-
return searchWords;
31573139
}
31583140

31593141
/**
@@ -3332,10 +3314,7 @@ ${item.displayPath}<span class="${type}">${name}</span>\
33323314
search(true);
33333315
}
33343316

3335-
/**
3336-
* @type {Array<string>}
3337-
*/
3338-
const searchWords = buildIndex(rawSearchIndex);
3317+
buildIndex(rawSearchIndex);
33393318
if (typeof window !== "undefined") {
33403319
registerSearchEvents();
33413320
// If there's a search term in the URL, execute the search now.
@@ -3349,7 +3328,6 @@ ${item.displayPath}<span class="${type}">${name}</span>\
33493328
exports.execQuery = execQuery;
33503329
exports.parseQuery = parseQuery;
33513330
}
3352-
return searchWords;
33533331
}
33543332

33553333
if (typeof window !== "undefined") {

src/tools/rustdoc-js/tester.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -396,16 +396,16 @@ function loadSearchJS(doc_folder, resource_suffix) {
396396
const staticFiles = path.join(doc_folder, "static.files");
397397
const searchJs = fs.readdirSync(staticFiles).find(f => f.match(/search.*\.js$/));
398398
const searchModule = require(path.join(staticFiles, searchJs));
399-
const searchWords = searchModule.initSearch(searchIndex.searchIndex);
399+
searchModule.initSearch(searchIndex.searchIndex);
400400

401401
return {
402402
doSearch: function(queryStr, filterCrate, currentCrate) {
403-
return searchModule.execQuery(searchModule.parseQuery(queryStr), searchWords,
403+
return searchModule.execQuery(searchModule.parseQuery(queryStr),
404404
filterCrate, currentCrate);
405405
},
406406
getCorrections: function(queryStr, filterCrate, currentCrate) {
407407
const parsedQuery = searchModule.parseQuery(queryStr);
408-
searchModule.execQuery(parsedQuery, searchWords, filterCrate, currentCrate);
408+
searchModule.execQuery(parsedQuery, filterCrate, currentCrate);
409409
return parsedQuery.correction;
410410
},
411411
parseQuery: searchModule.parseQuery,

0 commit comments

Comments
 (0)