Skip to content

Commit dbdb2a1

Browse files
authored
Rollup merge of #83077 - notriddle:gc-cleanup-rustdoc-search, r=GuillaumeGomez
rustdoc: reduce GC work during search
2 parents b6df781 + dcba95f commit dbdb2a1

File tree

1 file changed

+78
-49
lines changed

1 file changed

+78
-49
lines changed

src/librustdoc/html/static/main.js

+78-49
Original file line numberDiff line numberDiff line change
@@ -833,39 +833,52 @@ function defocusSearchBar() {
833833
};
834834
}
835835

836-
function getObjectFromId(id) {
836+
function getObjectNameFromId(id) {
837837
if (typeof id === "number") {
838-
return searchIndex[id];
838+
return searchIndex[id].name;
839839
}
840-
return {'name': id};
840+
return id;
841841
}
842842

843843
function checkGenerics(obj, val) {
844844
// The names match, but we need to be sure that all generics kinda
845845
// match as well.
846+
var tmp_lev, elem_name;
846847
if (val.generics.length > 0) {
847848
if (obj.length > GENERICS_DATA &&
848849
obj[GENERICS_DATA].length >= val.generics.length) {
849-
var elems = obj[GENERICS_DATA].slice(0);
850+
var elems = Object.create(null);
851+
var elength = object[GENERICS_DATA].length;
852+
for (var x = 0; x < elength; ++x) {
853+
elems[getObjectNameFromId(obj[GENERICS_DATA][x])] += 1;
854+
}
850855
var total = 0;
851856
var done = 0;
852857
// We need to find the type that matches the most to remove it in order
853858
// to move forward.
854859
var vlength = val.generics.length;
855-
for (var y = 0; y < vlength; ++y) {
856-
var lev = { pos: -1, lev: MAX_LEV_DISTANCE + 1};
857-
var firstGeneric = getObjectFromId(val.generics[y]).name;
858-
for (var x = 0, elength = elems.length; x < elength; ++x) {
859-
var tmp_lev = levenshtein(getObjectFromId(elems[x]).name,
860-
firstGeneric);
861-
if (tmp_lev < lev.lev) {
862-
lev.lev = tmp_lev;
863-
lev.pos = x;
860+
for (x = 0; x < vlength; ++x) {
861+
var lev = MAX_LEV_DISTANCE + 1;
862+
var firstGeneric = getObjectNameFromId(val.generics[x]);
863+
var match = null;
864+
if (elems[firstGeneric]) {
865+
match = firstGeneric;
866+
lev = 0;
867+
} else {
868+
for (elem_name in elems) {
869+
tmp_lev = levenshtein(elem_name, firstGeneric);
870+
if (tmp_lev < lev) {
871+
lev = tmp_lev;
872+
match = elem_name;
873+
}
864874
}
865875
}
866-
if (lev.pos !== -1) {
867-
elems.splice(lev.pos, 1);
868-
total += lev.lev;
876+
if (match !== null) {
877+
elems[match] -= 1;
878+
if (elems[match] == 0) {
879+
delete elems[match];
880+
}
881+
total += lev;
869882
done += 1;
870883
} else {
871884
return MAX_LEV_DISTANCE + 1;
@@ -880,25 +893,27 @@ function defocusSearchBar() {
880893
// Check for type name and type generics (if any).
881894
function checkType(obj, val, literalSearch) {
882895
var lev_distance = MAX_LEV_DISTANCE + 1;
883-
var len, x, y, e_len, firstGeneric;
896+
var len, x, firstGeneric;
884897
if (obj[NAME] === val.name) {
885898
if (literalSearch === true) {
886899
if (val.generics && val.generics.length !== 0) {
887900
if (obj.length > GENERICS_DATA &&
888901
obj[GENERICS_DATA].length >= val.generics.length) {
889-
var elems = obj[GENERICS_DATA].slice(0);
890-
var allFound = true;
902+
var elems = Object.create(null);
903+
len = obj[GENERICS_DATA].length;
904+
for (x = 0; x < len; ++x) {
905+
elems[getObjectNameFromId(obj[GENERICS_DATA][x])] += 1;
906+
}
891907

908+
var allFound = true;
892909
len = val.generics.length;
893-
for (y = 0; allFound === true && y < len; ++y) {
894-
allFound = false;
895-
firstGeneric = getObjectFromId(val.generics[y]).name;
896-
e_len = elems.length;
897-
for (x = 0; allFound === false && x < e_len; ++x) {
898-
allFound = getObjectFromId(elems[x]).name === firstGeneric;
899-
}
900-
if (allFound === true) {
901-
elems.splice(x - 1, 1);
910+
for (x = 0; x < len; ++x) {
911+
firstGeneric = getObjectNameFromId(val.generics[x]);
912+
if (elems[firstGeneric]) {
913+
elems[firstGeneric] -= 1;
914+
} else {
915+
allFound = false;
916+
break;
902917
}
903918
}
904919
if (allFound === true) {
@@ -1066,13 +1081,6 @@ function defocusSearchBar() {
10661081
return false;
10671082
}
10681083

1069-
function generateId(ty) {
1070-
if (ty.parent && ty.parent.name) {
1071-
return itemTypes[ty.ty] + ty.path + ty.parent.name + ty.name;
1072-
}
1073-
return itemTypes[ty.ty] + ty.path + ty.name;
1074-
}
1075-
10761084
function createAliasFromItem(item) {
10771085
return {
10781086
crate: item.crate,
@@ -1158,7 +1166,7 @@ function defocusSearchBar() {
11581166
in_args = findArg(searchIndex[i], val, true, typeFilter);
11591167
returned = checkReturned(searchIndex[i], val, true, typeFilter);
11601168
ty = searchIndex[i];
1161-
fullId = generateId(ty);
1169+
fullId = ty.id;
11621170

11631171
if (searchWords[i] === val.name
11641172
&& typePassesFilter(typeFilter, searchIndex[i].ty)
@@ -1208,7 +1216,7 @@ function defocusSearchBar() {
12081216
if (!type) {
12091217
continue;
12101218
}
1211-
fullId = generateId(ty);
1219+
fullId = ty.id;
12121220

12131221
returned = checkReturned(ty, output, true, NO_TYPE_FILTER);
12141222
if (output.name === "*" || returned === true) {
@@ -1292,15 +1300,15 @@ function defocusSearchBar() {
12921300
var index = -1;
12931301
// we want lev results to go lower than others
12941302
lev = MAX_LEV_DISTANCE + 1;
1295-
fullId = generateId(ty);
1303+
fullId = ty.id;
12961304

12971305
if (searchWords[j].indexOf(split[i]) > -1 ||
12981306
searchWords[j].indexOf(val) > -1 ||
1299-
searchWords[j].replace(/_/g, "").indexOf(val) > -1)
1307+
ty.normalizedName.indexOf(val) > -1)
13001308
{
13011309
// filter type: ... queries
13021310
if (typePassesFilter(typeFilter, ty.ty) && results[fullId] === undefined) {
1303-
index = searchWords[j].replace(/_/g, "").indexOf(val);
1311+
index = ty.normalizedName.indexOf(val);
13041312
}
13051313
}
13061314
if ((lev = levenshtein(searchWords[j], val)) <= MAX_LEV_DISTANCE) {
@@ -1828,23 +1836,35 @@ function defocusSearchBar() {
18281836
function buildIndex(rawSearchIndex) {
18291837
searchIndex = [];
18301838
var searchWords = [];
1831-
var i;
1839+
var i, word;
18321840
var currentIndex = 0;
1841+
var id = 0;
18331842

18341843
for (var crate in rawSearchIndex) {
18351844
if (!hasOwnProperty(rawSearchIndex, crate)) { continue; }
18361845

18371846
var crateSize = 0;
18381847

18391848
searchWords.push(crate);
1840-
searchIndex.push({
1849+
var normalizedName = crate.indexOf("_") === -1
1850+
? crate
1851+
: crate.replace(/_/g, "");
1852+
// This object should have exactly the same set of fields as the "row"
1853+
// object defined below. Your JavaScript runtime will thank you.
1854+
// https://mathiasbynens.be/notes/shapes-ics
1855+
var crateRow = {
18411856
crate: crate,
18421857
ty: 1, // == ExternCrate
18431858
name: crate,
18441859
path: "",
18451860
desc: rawSearchIndex[crate].doc,
1861+
parent: undefined,
18461862
type: null,
1847-
});
1863+
id: id,
1864+
normalizedName: normalizedName,
1865+
};
1866+
id += 1;
1867+
searchIndex.push(crateRow);
18481868
currentIndex += 1;
18491869

18501870
// an array of (Number) item types
@@ -1882,6 +1902,18 @@ function defocusSearchBar() {
18821902
len = itemTypes.length;
18831903
var lastPath = "";
18841904
for (i = 0; i < len; ++i) {
1905+
// This object should have exactly the same set of fields as the "crateRow"
1906+
// object defined above.
1907+
if (typeof itemNames[i] === "string") {
1908+
word = itemNames[i].toLowerCase();
1909+
searchWords.push(word);
1910+
} else {
1911+
word = "";
1912+
searchWords.push("");
1913+
}
1914+
var normalizedName = word.indexOf("_") === -1
1915+
? word
1916+
: word.replace(/_/g, "");
18851917
var row = {
18861918
crate: crate,
18871919
ty: itemTypes[i],
@@ -1890,14 +1922,11 @@ function defocusSearchBar() {
18901922
desc: itemDescs[i],
18911923
parent: itemParentIdxs[i] > 0 ? paths[itemParentIdxs[i] - 1] : undefined,
18921924
type: itemFunctionSearchTypes[i],
1925+
id: id,
1926+
normalizedName: normalizedName,
18931927
};
1928+
id += 1;
18941929
searchIndex.push(row);
1895-
if (typeof row.name === "string") {
1896-
var word = row.name.toLowerCase();
1897-
searchWords.push(word);
1898-
} else {
1899-
searchWords.push("");
1900-
}
19011930
lastPath = row.path;
19021931
crateSize += 1;
19031932
}

0 commit comments

Comments
 (0)