Skip to content

Commit a145dff

Browse files
committed
search.js: introduce optimized removeIdxListAsc routine
1 parent 2dd00d3 commit a145dff

File tree

1 file changed

+30
-9
lines changed

1 file changed

+30
-9
lines changed

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

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1076,6 +1076,34 @@ function isPathSeparator(c) {
10761076
return c === ":" || c === " ";
10771077
}
10781078

1079+
/**
1080+
* Given an array and an ascending list of indices,
1081+
* efficiently removes each index in the array.
1082+
*
1083+
* @template T
1084+
* @param {Array<T>} a
1085+
* @param {Array<number>} idxList
1086+
*/
1087+
function removeIdxListAsc(a, idxList) {
1088+
if (idxList.length === 0) {
1089+
return;
1090+
}
1091+
let removed = 0;
1092+
let i = idxList[0];
1093+
let nextToRemove = idxList[0];
1094+
while (i < a.length - idxList.length) {
1095+
while (i === nextToRemove && removed < idxList.length) {
1096+
removed++;
1097+
i++;
1098+
nextToRemove = idxList[removed];
1099+
}
1100+
a[i] = a[i + removed];
1101+
i++;
1102+
}
1103+
// truncate array
1104+
a.length -= idxList.length;
1105+
}
1106+
10791107
/**
10801108
* @template T
10811109
*/
@@ -2615,7 +2643,7 @@ class DocSearch {
26152643
*/
26162644
const transformResults = (results, typeInfo, duplicates) => {
26172645
/** @type {rustdoc.ResultObject[]} */
2618-
let out = [];
2646+
const out = [];
26192647

26202648
// if we match a trait-associated item, we want to go back and
26212649
// remove all the items that are their equivalent but in an impl block.
@@ -2712,16 +2740,9 @@ class DocSearch {
27122740
list.push(out.length);
27132741
traitImplIdxMap.set(obj.traitPath, list);
27142742
} else {
2715-
// FIXME: this is `O(n*m)` because we're repeatedly
2716-
// shifting with Array.splice, but could be `O(n+m)` if
2717-
// we did the shifting manually in a more clever way.
27182743
const toRemoveList = traitImplIdxMap.get(obj.fullPath);
27192744
if (toRemoveList) {
2720-
// iterate in reverse order so we don't shift the indexes
2721-
for (let i = toRemoveList.length - 1; i >= 0; i--) {
2722-
const rmIdx = toRemoveList[i];
2723-
out = out.splice(rmIdx, 1);
2724-
}
2745+
removeIdxListAsc(out, toRemoveList);
27252746
}
27262747
traitImplIdxMap.delete(obj.fullPath);
27272748
}

0 commit comments

Comments
 (0)