Skip to content

Commit 7a58dea

Browse files
rustdoc-search: address performance review nits
Co-authored-by: Guillaume Gomez <[email protected]>
1 parent 7cef758 commit 7a58dea

File tree

1 file changed

+22
-37
lines changed

1 file changed

+22
-37
lines changed

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

+22-37
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,6 @@ if (!Array.prototype.toSpliced) {
1515
};
1616
}
1717

18-
function takeFromArray(array, count) {
19-
const result = [];
20-
while (count > 0) {
21-
result.push(array.shift());
22-
count -= 1;
23-
}
24-
return result;
25-
}
26-
2718
function onEachBtwn(arr, func, funcBtwn) {
2819
let skipped = true;
2920
for (const value of arr) {
@@ -1448,31 +1439,7 @@ function initSearch(rawSearchIndex) {
14481439
let fnInputs = null;
14491440
let fnOutput = null;
14501441
let mgens = null;
1451-
if (typeInfo === "elems") {
1452-
fnInputs = unifyFunctionTypes(
1453-
obj.type.inputs,
1454-
parsedQuery.elems,
1455-
obj.type.where_clause,
1456-
null,
1457-
mgensOut => {
1458-
mgens = mgensOut;
1459-
return true;
1460-
},
1461-
0,
1462-
);
1463-
} else if (typeInfo === "returned") {
1464-
fnOutput = unifyFunctionTypes(
1465-
obj.type.output,
1466-
parsedQuery.elems,
1467-
obj.type.where_clause,
1468-
null,
1469-
mgensOut => {
1470-
mgens = mgensOut;
1471-
return true;
1472-
},
1473-
0,
1474-
);
1475-
} else {
1442+
if (typeInfo !== "elems" && typeInfo !== "returned") {
14761443
fnInputs = unifyFunctionTypes(
14771444
obj.type.inputs,
14781445
parsedQuery.elems,
@@ -1494,6 +1461,24 @@ function initSearch(rawSearchIndex) {
14941461
},
14951462
0,
14961463
);
1464+
} else {
1465+
const arr = typeInfo === "elems" ? obj.type.inputs : obj.type.output;
1466+
const highlighted = unifyFunctionTypes(
1467+
arr,
1468+
parsedQuery.elems,
1469+
obj.type.where_clause,
1470+
null,
1471+
mgensOut => {
1472+
mgens = mgensOut;
1473+
return true;
1474+
},
1475+
0,
1476+
);
1477+
if (typeInfo === "elems") {
1478+
fnInputs = highlighted;
1479+
} else {
1480+
fnOutput = highlighted;
1481+
}
14971482
}
14981483
if (!fnInputs) {
14991484
fnInputs = obj.type.inputs;
@@ -2000,7 +1985,7 @@ function initSearch(rawSearchIndex) {
20001985
highlighted[i] = Object.assign({}, fnType, {
20011986
generics: highlightedGenerics,
20021987
bindings: new Map([...fnType.bindings.entries()].map(([k, v]) => {
2003-
return [k, takeFromArray(highlightedGenerics, v.length)];
1988+
return [k, highlightedGenerics.splice(0, v.length)];
20041989
})),
20051990
});
20061991
return highlighted;
@@ -2116,7 +2101,7 @@ function initSearch(rawSearchIndex) {
21162101
unifiedGenericsMgens,
21172102
solutionCb,
21182103
unboxingDepth,
2119-
) : takeFromArray(unifiedGenerics, v.length)];
2104+
) : unifiedGenerics.splice(0, v.length)];
21202105
})),
21212106
});
21222107
return passesUnification;
@@ -2169,7 +2154,7 @@ function initSearch(rawSearchIndex) {
21692154
const highlightedFnType = Object.assign({}, fnType, {
21702155
generics: highlightedGenerics,
21712156
bindings: new Map([...fnType.bindings.entries()].map(([k, v]) => {
2172-
return [k, takeFromArray(highlightedGenerics, v.length)];
2157+
return [k, highlightedGenerics.splice(0, v.length)];
21732158
})),
21742159
});
21752160
return passesUnification.toSpliced(

0 commit comments

Comments
 (0)