Skip to content

Commit

Permalink
rustdoc-search: address performance review nits
Browse files Browse the repository at this point in the history
Co-authored-by: Guillaume Gomez <guillaume1.gomez@gmail.com>
  • Loading branch information
notriddle and GuillaumeGomez committed Jun 27, 2024
1 parent 7cef758 commit 7a58dea
Showing 1 changed file with 22 additions and 37 deletions.
59 changes: 22 additions & 37 deletions src/librustdoc/html/static/js/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,6 @@ if (!Array.prototype.toSpliced) {
};
}

function takeFromArray(array, count) {
const result = [];
while (count > 0) {
result.push(array.shift());
count -= 1;
}
return result;
}

function onEachBtwn(arr, func, funcBtwn) {
let skipped = true;
for (const value of arr) {
Expand Down Expand Up @@ -1448,31 +1439,7 @@ function initSearch(rawSearchIndex) {
let fnInputs = null;
let fnOutput = null;
let mgens = null;
if (typeInfo === "elems") {
fnInputs = unifyFunctionTypes(
obj.type.inputs,
parsedQuery.elems,
obj.type.where_clause,
null,
mgensOut => {
mgens = mgensOut;
return true;
},
0,
);
} else if (typeInfo === "returned") {
fnOutput = unifyFunctionTypes(
obj.type.output,
parsedQuery.elems,
obj.type.where_clause,
null,
mgensOut => {
mgens = mgensOut;
return true;
},
0,
);
} else {
if (typeInfo !== "elems" && typeInfo !== "returned") {
fnInputs = unifyFunctionTypes(
obj.type.inputs,
parsedQuery.elems,
Expand All @@ -1494,6 +1461,24 @@ function initSearch(rawSearchIndex) {
},
0,
);
} else {
const arr = typeInfo === "elems" ? obj.type.inputs : obj.type.output;
const highlighted = unifyFunctionTypes(
arr,
parsedQuery.elems,
obj.type.where_clause,
null,
mgensOut => {
mgens = mgensOut;
return true;
},
0,
);
if (typeInfo === "elems") {
fnInputs = highlighted;
} else {
fnOutput = highlighted;
}
}
if (!fnInputs) {
fnInputs = obj.type.inputs;
Expand Down Expand Up @@ -2000,7 +1985,7 @@ function initSearch(rawSearchIndex) {
highlighted[i] = Object.assign({}, fnType, {
generics: highlightedGenerics,
bindings: new Map([...fnType.bindings.entries()].map(([k, v]) => {
return [k, takeFromArray(highlightedGenerics, v.length)];
return [k, highlightedGenerics.splice(0, v.length)];
})),
});
return highlighted;
Expand Down Expand Up @@ -2116,7 +2101,7 @@ function initSearch(rawSearchIndex) {
unifiedGenericsMgens,
solutionCb,
unboxingDepth,
) : takeFromArray(unifiedGenerics, v.length)];
) : unifiedGenerics.splice(0, v.length)];
})),
});
return passesUnification;
Expand Down Expand Up @@ -2169,7 +2154,7 @@ function initSearch(rawSearchIndex) {
const highlightedFnType = Object.assign({}, fnType, {
generics: highlightedGenerics,
bindings: new Map([...fnType.bindings.entries()].map(([k, v]) => {
return [k, takeFromArray(highlightedGenerics, v.length)];
return [k, highlightedGenerics.splice(0, v.length)];
})),
});
return passesUnification.toSpliced(
Expand Down

0 comments on commit 7a58dea

Please sign in to comment.