Skip to content

Commit d7971e5

Browse files
committed
In checkGenerics and checkType, don't use Array.prototype.splice so much
Every time splice() is called, another temporary object is created. This version, which uses plain objects as a sort of Hash Bag, should only produce one temporary object each time it's called.
1 parent 5fe3b87 commit d7971e5

File tree

1 file changed

+57
-37
lines changed

1 file changed

+57
-37
lines changed

src/librustdoc/html/static/main.js

+57-37
Original file line numberDiff line numberDiff line change
@@ -846,26 +846,38 @@ function defocusSearchBar() {
846846
if (val.generics.length > 0) {
847847
if (obj.length > GENERICS_DATA &&
848848
obj[GENERICS_DATA].length >= val.generics.length) {
849-
var elems = obj[GENERICS_DATA].slice(0);
849+
var elems = {};
850+
var elength = object[GENERICS_DATA].length;
851+
for (var x = 0; x < elength; ++x) {
852+
elems[getObjectNameFromId(obj[GENERICS_DATA][x])] += 1;
853+
}
850854
var total = 0;
851855
var done = 0;
852856
// We need to find the type that matches the most to remove it in order
853857
// to move forward.
854858
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 = getObjectNameFromId(val.generics[y]);
858-
for (var x = 0, elength = elems.length; x < elength; ++x) {
859-
var tmp_lev = levenshtein(getObjectNameFromId(elems[x]),
860-
firstGeneric);
861-
if (tmp_lev < lev.lev) {
862-
lev.lev = tmp_lev;
863-
lev.pos = x;
859+
for (var x = 0; x < vlength; ++x) {
860+
var lev = MAX_LEV_DISTANCE + 1;
861+
var firstGeneric = getObjectNameFromId(val.generics[x]);
862+
var match = undefined;
863+
if (elems[firstGeneric]) {
864+
match = firstGeneric;
865+
lev = 0;
866+
} else {
867+
for (var elem_name in elems) {
868+
var tmp_lev = levenshtein(elem_name, firstGeneric);
869+
if (tmp_lev < lev) {
870+
lev = tmp_lev;
871+
match = elem_name;
872+
}
864873
}
865874
}
866-
if (lev.pos !== -1) {
867-
elems.splice(lev.pos, 1);
868-
total += lev.lev;
875+
if (match !== undefined) {
876+
elems[match] -= 1;
877+
if (elems[match] == 0) {
878+
delete elems[match];
879+
}
880+
total += lev;
869881
done += 1;
870882
} else {
871883
return MAX_LEV_DISTANCE + 1;
@@ -880,25 +892,27 @@ function defocusSearchBar() {
880892
// Check for type name and type generics (if any).
881893
function checkType(obj, val, literalSearch) {
882894
var lev_distance = MAX_LEV_DISTANCE + 1;
883-
var len, x, y, e_len, firstGeneric;
895+
var len, x, firstGeneric;
884896
if (obj[NAME] === val.name) {
885897
if (literalSearch === true) {
886898
if (val.generics && val.generics.length !== 0) {
887899
if (obj.length > GENERICS_DATA &&
888900
obj[GENERICS_DATA].length >= val.generics.length) {
889-
var elems = obj[GENERICS_DATA].slice(0);
890-
var allFound = true;
901+
var elems = {};
902+
len = obj[GENERICS_DATA].length;
903+
for (x = 0; x < len; ++x) {
904+
elems[getObjectNameFromId(obj[GENERICS_DATA][x])] += 1;
905+
}
891906

907+
var allFound = true;
892908
len = val.generics.length;
893-
for (y = 0; allFound === true && y < len; ++y) {
894-
allFound = false;
895-
firstGeneric = getObjectNameFromId(val.generics[y]);
896-
e_len = elems.length;
897-
for (x = 0; allFound === false && x < e_len; ++x) {
898-
allFound = getObjectNameFromId(elems[x]) === firstGeneric;
899-
}
900-
if (allFound === true) {
901-
elems.splice(x - 1, 1);
909+
for (x = 0; x < len; ++x) {
910+
firstGeneric = getObjectNameFromId(val.generics[x]);
911+
if (elems[firstGeneric]) {
912+
elems[firstGeneric] -= 1;
913+
} else {
914+
allFound = false;
915+
break;
902916
}
903917
}
904918
if (allFound === true) {
@@ -1066,13 +1080,6 @@ function defocusSearchBar() {
10661080
return false;
10671081
}
10681082

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-
10761083
function createAliasFromItem(item) {
10771084
return {
10781085
crate: item.crate,
@@ -1158,7 +1165,7 @@ function defocusSearchBar() {
11581165
in_args = findArg(searchIndex[i], val, true, typeFilter);
11591166
returned = checkReturned(searchIndex[i], val, true, typeFilter);
11601167
ty = searchIndex[i];
1161-
fullId = generateId(ty);
1168+
fullId = ty.id;
11621169

11631170
if (searchWords[i] === val.name
11641171
&& typePassesFilter(typeFilter, searchIndex[i].ty)
@@ -1208,7 +1215,7 @@ function defocusSearchBar() {
12081215
if (!type) {
12091216
continue;
12101217
}
1211-
fullId = generateId(ty);
1218+
fullId = ty.id;
12121219

12131220
returned = checkReturned(ty, output, true, NO_TYPE_FILTER);
12141221
if (output.name === "*" || returned === true) {
@@ -1292,7 +1299,7 @@ function defocusSearchBar() {
12921299
var index = -1;
12931300
// we want lev results to go lower than others
12941301
lev = MAX_LEV_DISTANCE + 1;
1295-
fullId = generateId(ty);
1302+
fullId = ty.id;
12961303

12971304
if (searchWords[j].indexOf(split[i]) > -1 ||
12981305
searchWords[j].indexOf(val) > -1 ||
@@ -1825,6 +1832,13 @@ function defocusSearchBar() {
18251832
showResults(execSearch(query, index, filterCrates));
18261833
}
18271834

1835+
function generateId(ty) {
1836+
if (ty.parent && ty.parent.name) {
1837+
return itemTypes[ty.ty] + ty.path + ty.parent.name + ty.name;
1838+
}
1839+
return itemTypes[ty.ty] + ty.path + ty.name;
1840+
}
1841+
18281842
function buildIndex(rawSearchIndex) {
18291843
searchIndex = [];
18301844
var searchWords = [];
@@ -1837,14 +1851,18 @@ function defocusSearchBar() {
18371851
var crateSize = 0;
18381852

18391853
searchWords.push(crate);
1840-
searchIndex.push({
1854+
var crateRow = {
18411855
crate: crate,
18421856
ty: 1, // == ExternCrate
18431857
name: crate,
18441858
path: "",
18451859
desc: rawSearchIndex[crate].doc,
1860+
parent: undefined,
18461861
type: null,
1847-
});
1862+
id: "",
1863+
};
1864+
crateRow.id = generateId(crateRow);
1865+
searchIndex.push(crateRow);
18481866
currentIndex += 1;
18491867

18501868
// an array of (Number) item types
@@ -1890,7 +1908,9 @@ function defocusSearchBar() {
18901908
desc: itemDescs[i],
18911909
parent: itemParentIdxs[i] > 0 ? paths[itemParentIdxs[i] - 1] : undefined,
18921910
type: itemFunctionSearchTypes[i],
1911+
id: "",
18931912
};
1913+
row.id = generateId(row);
18941914
searchIndex.push(row);
18951915
if (typeof row.name === "string") {
18961916
var word = row.name.toLowerCase();

0 commit comments

Comments
 (0)