Skip to content

Commit

Permalink
Rollup merge of #80515 - GuillaumeGomez:js-for-loop-perf, r=Nemo157,j…
Browse files Browse the repository at this point in the history
…yn514

Improve JS performance by storing length before comparing to it in loops

Since #79052 is quite complicated to review, I suggested to split into smaller parts. This first part is mostly about saving the array length into a variable (I tried to not change anything else as much as possible 😃 ).

r? `@jyn514`
  • Loading branch information
GuillaumeGomez authored Jan 15, 2021
2 parents b7a9d6a + e727600 commit 033faf9
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 56 deletions.
98 changes: 45 additions & 53 deletions src/librustdoc/html/static/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -648,8 +648,7 @@ function defocusSearchBar() {
*/
function execQuery(query, searchWords, filterCrates) {
function itemTypeFromName(typename) {
var length = itemTypes.length;
for (var i = 0; i < length; ++i) {
for (var i = 0, len = itemTypes.length; i < len; ++i) {
if (itemTypes[i] === typename) {
return i;
}
Expand All @@ -667,8 +666,7 @@ function defocusSearchBar() {

function transformResults(results, isType) {
var out = [];
var length = results.length;
for (var i = 0; i < length; ++i) {
for (var i = 0, len = results.length; i < len; ++i) {
if (results[i].id > -1) {
var obj = searchIndex[results[i].id];
obj.lev = results[i].lev;
Expand Down Expand Up @@ -697,11 +695,11 @@ function defocusSearchBar() {
}
}
results = ar;
var i;
var nresults = results.length;
for (i = 0; i < nresults; ++i) {
results[i].word = searchWords[results[i].id];
results[i].item = searchIndex[results[i].id] || {};
var i, len, result;
for (i = 0, len = results.length; i < len; ++i) {
result = results[i];
result.word = searchWords[result.id];
result.item = searchIndex[result.id] || {};
}
// if there are no results then return to default and fail
if (results.length === 0) {
Expand Down Expand Up @@ -775,8 +773,7 @@ function defocusSearchBar() {
return 0;
});

var length = results.length;
for (i = 0; i < length; ++i) {
for (i = 0, len = results.length; i < len; ++i) {
var result = results[i];

// this validation does not make sense when searching by types
Expand Down Expand Up @@ -833,11 +830,10 @@ function defocusSearchBar() {
var vlength = val.generics.length;
for (var y = 0; y < vlength; ++y) {
var lev = { pos: -1, lev: MAX_LEV_DISTANCE + 1};
var elength = elems.length;
var firstGeneric = getObjectFromId(val.generics[y]).name;
for (var x = 0; x < elength; ++x) {
for (var x = 0, elength = elems.length; x < elength; ++x) {
var tmp_lev = levenshtein(getObjectFromId(elems[x]).name,
firstGeneric);
firstGeneric);
if (tmp_lev < lev.lev) {
lev.lev = tmp_lev;
lev.pos = x;
Expand All @@ -861,7 +857,7 @@ function defocusSearchBar() {
// Check for type name and type generics (if any).
function checkType(obj, val, literalSearch) {
var lev_distance = MAX_LEV_DISTANCE + 1;
var x;
var len, x, y, e_len, firstGeneric;
if (obj[NAME] === val.name) {
if (literalSearch === true) {
if (val.generics && val.generics.length !== 0) {
Expand All @@ -870,10 +866,12 @@ function defocusSearchBar() {
var elems = obj[GENERICS_DATA].slice(0);
var allFound = true;

for (var y = 0; allFound === true && y < val.generics.length; ++y) {
len = val.generics.length;
for (y = 0; allFound === true && y < len; ++y) {
allFound = false;
var firstGeneric = getObjectFromId(val.generics[y]).name;
for (x = 0; allFound === false && x < elems.length; ++x) {
firstGeneric = getObjectFromId(val.generics[y]).name;
e_len = elems.length;
for (x = 0; allFound === false && x < e_len; ++x) {
allFound = getObjectFromId(elems[x]).name === firstGeneric;
}
if (allFound === true) {
Expand Down Expand Up @@ -903,12 +901,10 @@ function defocusSearchBar() {
// Names didn't match so let's check if one of the generic types could.
if (literalSearch === true) {
if (obj.length > GENERICS_DATA && obj[GENERICS_DATA].length > 0) {
var length = obj[GENERICS_DATA].length;
for (x = 0; x < length; ++x) {
if (obj[GENERICS_DATA][x] === val.name) {
return true;
}
}
return obj[GENERICS_DATA].some(
function(name) {
return name === val.name;
});
}
return false;
}
Expand Down Expand Up @@ -965,7 +961,7 @@ function defocusSearchBar() {
if (typeof ret[0] === "string") {
ret = [ret];
}
for (var x = 0; x < ret.length; ++x) {
for (var x = 0, len = ret.length; x < len; ++x) {
var tmp = ret[x];
if (typePassesFilter(typeFilter, tmp[1]) === false) {
continue;
Expand Down Expand Up @@ -1072,23 +1068,22 @@ function defocusSearchBar() {
// aliases to be before the others in the displayed results.
var aliases = [];
var crateAliases = [];
var i;
if (filterCrates !== undefined) {
if (ALIASES[filterCrates] && ALIASES[filterCrates][query.search]) {
for (i = 0; i < ALIASES[filterCrates][query.search].length; ++i) {
aliases.push(
createAliasFromItem(
searchIndex[ALIASES[filterCrates][query.search][i]]));
var query_aliases = ALIASES[filterCrates][query.search];
var len = query_aliases.length;
for (var i = 0; i < len; ++i) {
aliases.push(createAliasFromItem(searchIndex[query_aliases[i]]));
}
}
} else {
Object.keys(ALIASES).forEach(function(crate) {
if (ALIASES[crate][query.search]) {
var pushTo = crate === window.currentCrate ? crateAliases : aliases;
for (i = 0; i < ALIASES[crate][query.search].length; ++i) {
pushTo.push(
createAliasFromItem(
searchIndex[ALIASES[crate][query.search][i]]));
var query_aliases = ALIASES[crate][query.search];
var len = query_aliases.length;
for (var i = 0; i < len; ++i) {
pushTo.push(createAliasFromItem(searchIndex[query_aliases[i]]));
}
}
});
Expand Down Expand Up @@ -1123,11 +1118,12 @@ function defocusSearchBar() {

// quoted values mean literal search
var nSearchWords = searchWords.length;
var i;
var i, it;
var ty;
var fullId;
var returned;
var in_args;
var len;
if ((val.charAt(0) === "\"" || val.charAt(0) === "'") &&
val.charAt(val.length - 1) === val.charAt(0))
{
Expand Down Expand Up @@ -1175,7 +1171,7 @@ function defocusSearchBar() {
var input = parts[0];
// sort inputs so that order does not matter
var inputs = input.split(",").map(trimmer).sort();
for (i = 0; i < inputs.length; ++i) {
for (i = 0, len = inputs.length; i < len; ++i) {
inputs[i] = extractGenerics(inputs[i]);
}
var output = extractGenerics(parts[1]);
Expand All @@ -1200,7 +1196,7 @@ function defocusSearchBar() {
is_module = true;
} else {
var allFound = true;
for (var it = 0; allFound === true && it < inputs.length; it++) {
for (it = 0, len = inputs.length; allFound === true && it < len; it++) {
allFound = checkType(type, inputs[it], true);
}
in_args = allFound;
Expand Down Expand Up @@ -1243,7 +1239,7 @@ function defocusSearchBar() {

var paths = valLower.split("::");
var j;
for (j = 0; j < paths.length; ++j) {
for (j = 0, len = paths.length; j < len; ++j) {
if (paths[j] === "") {
paths.splice(j, 1);
j -= 1;
Expand Down Expand Up @@ -1365,7 +1361,7 @@ function defocusSearchBar() {
* @return {[boolean]} [Whether the result is valid or not]
*/
function validateResult(name, path, keys, parent) {
for (var i = 0; i < keys.length; ++i) {
for (var i = 0, len = keys.length; i < len; ++i) {
// each check is for validation so we negate the conditions and invalidate
if (!(
// check for an exact name match
Expand Down Expand Up @@ -1686,7 +1682,7 @@ function defocusSearchBar() {
function getSmallest(arrays, positions, notDuplicates) {
var start = null;

for (var it = 0; it < positions.length; ++it) {
for (var it = 0, len = positions.length; it < len; ++it) {
if (arrays[it].length > positions[it] &&
(start === null || start > arrays[it][positions[it]].lev) &&
!notDuplicates[arrays[it][positions[it]].fullPath]) {
Expand All @@ -1701,7 +1697,7 @@ function defocusSearchBar() {
var positions = [];
var notDuplicates = {};

for (var x = 0; x < arrays.length; ++x) {
for (var x = 0, arrays_len = arrays.length; x < arrays_len; ++x) {
positions.push(0);
}
while (ret.length < MAX_RESULTS) {
Expand All @@ -1710,7 +1706,7 @@ function defocusSearchBar() {
if (smallest === null) {
break;
}
for (x = 0; x < arrays.length && ret.length < MAX_RESULTS; ++x) {
for (x = 0; x < arrays_len && ret.length < MAX_RESULTS; ++x) {
if (arrays[x].length > positions[x] &&
arrays[x][positions[x]].lev === smallest &&
!notDuplicates[arrays[x][positions[x]].fullPath]) {
Expand All @@ -1730,7 +1726,7 @@ function defocusSearchBar() {
"others": [],
};

for (var i = 0; i < queries.length; ++i) {
for (var i = 0, len = queries.length; i < len; ++i) {
query = queries[i].trim();
if (query.length !== 0) {
var tmp = execQuery(getQuery(query), searchWords, filterCrates);
Expand Down Expand Up @@ -1884,7 +1880,7 @@ function defocusSearchBar() {
ALIASES[crate][alias_name] = [];
}
local_aliases = aliases[alias_name];
for (j = 0; j < local_aliases.length; ++j) {
for (j = 0, len = local_aliases.length; j < len; ++j) {
ALIASES[crate][alias_name].push(local_aliases[j] + currentIndex);
}
}
Expand Down Expand Up @@ -2052,8 +2048,7 @@ function defocusSearchBar() {
div.appendChild(h3);
var ul = document.createElement("ul");

var length = filtered.length;
for (var i = 0; i < length; ++i) {
for (var i = 0, len = filtered.length; i < len; ++i) {
var item = filtered[i];
var name = item[0];
var desc = item[1]; // can be null
Expand Down Expand Up @@ -2122,21 +2117,18 @@ function defocusSearchBar() {
}

var libs = Object.getOwnPropertyNames(imp);
var llength = libs.length;
for (var i = 0; i < llength; ++i) {
for (var i = 0, llength = libs.length; i < llength; ++i) {
if (libs[i] === currentCrate) { continue; }
var structs = imp[libs[i]];

var slength = structs.length;
struct_loop:
for (var j = 0; j < slength; ++j) {
for (var j = 0, slength = structs.length; j < slength; ++j) {
var struct = structs[j];

var list = struct.synthetic ? synthetic_implementors : implementors;

if (struct.synthetic) {
var stlength = struct.types.length;
for (var k = 0; k < stlength; k++) {
for (var k = 0, stlength = struct.types.length; k < stlength; k++) {
if (inlined_types.has(struct.types[k])) {
continue struct_loop;
}
Expand Down Expand Up @@ -2853,7 +2845,7 @@ function defocusSearchBar() {
return 0;
});
var savedCrate = getSettingValue("saved-filter-crate");
for (var i = 0; i < crates_text.length; ++i) {
for (var i = 0, len = crates_text.length; i < len; ++i) {
var option = document.createElement("option");
option.value = crates_text[i];
option.innerText = crates_text[i];
Expand Down
8 changes: 5 additions & 3 deletions src/librustdoc/html/static/source-script.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ function getCurrentFilePath() {
var parts = window.location.pathname.split("/");
var rootPathParts = window.rootPath.split("/");

for (var i = 0; i < rootPathParts.length; ++i) {
for (var i = 0, len = rootPathParts.length; i < len; ++i) {
if (rootPathParts[i] === "..") {
parts.pop();
}
Expand All @@ -35,12 +35,14 @@ function createDirEntry(elem, parent, fullPath, currentFile, hasFoundFile) {
};
name.innerText = elem["name"];

var i, len;

var children = document.createElement("div");
children.className = "children";
var folders = document.createElement("div");
folders.className = "folders";
if (elem.dirs) {
for (var i = 0; i < elem.dirs.length; ++i) {
for (i = 0, len = elem.dirs.length; i < len; ++i) {
if (createDirEntry(elem.dirs[i], folders, fullPath, currentFile,
hasFoundFile) === true) {
addClass(name, "expand");
Expand All @@ -53,7 +55,7 @@ function createDirEntry(elem, parent, fullPath, currentFile, hasFoundFile) {
var files = document.createElement("div");
files.className = "files";
if (elem.files) {
for (i = 0; i < elem.files.length; ++i) {
for (i = 0, len = elem.files.length; i < len; ++i) {
var file = document.createElement("a");
file.innerText = elem.files[i];
file.href = window.rootPath + "src/" + fullPath + elem.files[i] + ".html";
Expand Down

0 comments on commit 033faf9

Please sign in to comment.