Skip to content

Commit 512aa5e

Browse files
committed
rustdoc-search: simplify the checkTypes fast path
This reduces code size while still matching the common case for plain, concrete types.
1 parent 0b24479 commit 512aa5e

File tree

1 file changed

+10
-61
lines changed

1 file changed

+10
-61
lines changed

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

+10-61
Original file line numberDiff line numberDiff line change
@@ -1308,36 +1308,6 @@ function initSearch(rawSearchIndex) {
13081308
return transformResults(result_list);
13091309
}
13101310

1311-
/**
1312-
* This function checks generics in search query `queryElem` can all be found in the
1313-
* search index (`fnType`),
1314-
*
1315-
* This function returns `true` if it matches, and also writes the results to mgensInout.
1316-
* It returns `false` if no match is found, and leaves mgensInout untouched.
1317-
*
1318-
* @param {FunctionType} fnType - The object to check.
1319-
* @param {QueryElement} queryElem - The element from the parsed query.
1320-
* @param {[FunctionType]} whereClause - Trait bounds for generic items.
1321-
* @param {Map<number,number>|null} mgensInout - Map functions generics to query generics.
1322-
*
1323-
* @return {boolean} - Returns true if a match, false otherwise.
1324-
*/
1325-
function checkGenerics(fnType, queryElem, whereClause, mgensInout) {
1326-
return unifyFunctionTypes(
1327-
fnType.generics,
1328-
queryElem.generics,
1329-
whereClause,
1330-
mgensInout,
1331-
mgens => {
1332-
if (mgensInout) {
1333-
for (const [fid, qid] of mgens.entries()) {
1334-
mgensInout.set(fid, qid);
1335-
}
1336-
}
1337-
return true;
1338-
}
1339-
);
1340-
}
13411311
/**
13421312
* This function checks if a list of search query `queryElems` can all be found in the
13431313
* search index (`fnTypes`).
@@ -1561,7 +1531,7 @@ function initSearch(rawSearchIndex) {
15611531
) {
15621532
// [] matches primitive:array or primitive:slice
15631533
// if it matches, then we're fine, and this is an appropriate match candidate
1564-
} else if (fnType.id !== queryElem.id) {
1534+
} else if (fnType.id !== queryElem.id || queryElem.id === null) {
15651535
return false;
15661536
}
15671537
// If the query elem has generics, and the function doesn't,
@@ -1649,38 +1619,17 @@ function initSearch(rawSearchIndex) {
16491619
* @return {boolean} - Returns true if the type matches, false otherwise.
16501620
*/
16511621
function checkType(row, elem, whereClause) {
1652-
if (row.id === null) {
1653-
// This is a pure "generic" search, no need to run other checks.
1654-
return row.generics.length > 0
1655-
? checkIfInList(row.generics, elem, whereClause)
1656-
: false;
1657-
}
1658-
1659-
if (row.id < 0 && elem.id >= 0) {
1660-
const gid = (-row.id) - 1;
1661-
return checkIfInList(whereClause[gid], elem, whereClause);
1662-
}
1663-
1664-
if (row.id < 0 && elem.id < 0) {
1665-
return true;
1622+
if (elem.id < 0) {
1623+
return row.id < 0 || checkIfInList(row.generics, elem, whereClause);
16661624
}
1667-
1668-
const matchesExact = row.id === elem.id;
1669-
const matchesArrayOrSlice = elem.id === typeNameIdOfArrayOrSlice &&
1670-
(row.id === typeNameIdOfSlice || row.id === typeNameIdOfArray);
1671-
1672-
if ((matchesExact || matchesArrayOrSlice) &&
1673-
typePassesFilter(elem.typeFilter, row.ty)) {
1674-
if (elem.generics.length > 0) {
1675-
return checkGenerics(row, elem, whereClause, new Map());
1676-
}
1677-
return true;
1625+
if (row.id > 0 && elem.id > 0 && elem.pathWithoutLast.length === 0 &&
1626+
typePassesFilter(elem.typeFilter, row.ty) && elem.generics.length === 0 &&
1627+
// special case
1628+
elem.id !== typeNameIdOfArrayOrSlice
1629+
) {
1630+
return row.id === elem.id || checkIfInList(row.generics, elem, whereClause);
16781631
}
1679-
1680-
// If the current item does not match, try [unboxing] the generic.
1681-
// [unboxing]:
1682-
// https://ndmitchell.com/downloads/slides-hoogle_fast_type_searching-09_aug_2008.pdf
1683-
return checkIfInList(row.generics, elem, whereClause);
1632+
return unifyFunctionTypes([row], [elem], whereClause);
16841633
}
16851634

16861635
function checkPath(contains, ty, maxEditDistance) {

0 commit comments

Comments
 (0)