Rollup merge of #83077 - notriddle:gc-cleanup-rustdoc-search, r=GuillaumeGomez

rustdoc: reduce GC work during search
This commit is contained in:
Yuki Okushi 2021-03-16 23:53:55 +09:00 committed by GitHub
commit dbdb2a1312
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -833,39 +833,52 @@ function defocusSearchBar() {
}; };
} }
function getObjectFromId(id) { function getObjectNameFromId(id) {
if (typeof id === "number") { if (typeof id === "number") {
return searchIndex[id]; return searchIndex[id].name;
} }
return {'name': id}; return id;
} }
function checkGenerics(obj, val) { function checkGenerics(obj, val) {
// The names match, but we need to be sure that all generics kinda // The names match, but we need to be sure that all generics kinda
// match as well. // match as well.
var tmp_lev, elem_name;
if (val.generics.length > 0) { if (val.generics.length > 0) {
if (obj.length > GENERICS_DATA && if (obj.length > GENERICS_DATA &&
obj[GENERICS_DATA].length >= val.generics.length) { obj[GENERICS_DATA].length >= val.generics.length) {
var elems = obj[GENERICS_DATA].slice(0); var elems = Object.create(null);
var elength = object[GENERICS_DATA].length;
for (var x = 0; x < elength; ++x) {
elems[getObjectNameFromId(obj[GENERICS_DATA][x])] += 1;
}
var total = 0; var total = 0;
var done = 0; var done = 0;
// We need to find the type that matches the most to remove it in order // We need to find the type that matches the most to remove it in order
// to move forward. // to move forward.
var vlength = val.generics.length; var vlength = val.generics.length;
for (var y = 0; y < vlength; ++y) { for (x = 0; x < vlength; ++x) {
var lev = { pos: -1, lev: MAX_LEV_DISTANCE + 1}; var lev = MAX_LEV_DISTANCE + 1;
var firstGeneric = getObjectFromId(val.generics[y]).name; var firstGeneric = getObjectNameFromId(val.generics[x]);
for (var x = 0, elength = elems.length; x < elength; ++x) { var match = null;
var tmp_lev = levenshtein(getObjectFromId(elems[x]).name, if (elems[firstGeneric]) {
firstGeneric); match = firstGeneric;
if (tmp_lev < lev.lev) { lev = 0;
lev.lev = tmp_lev; } else {
lev.pos = x; for (elem_name in elems) {
tmp_lev = levenshtein(elem_name, firstGeneric);
if (tmp_lev < lev) {
lev = tmp_lev;
match = elem_name;
}
} }
} }
if (lev.pos !== -1) { if (match !== null) {
elems.splice(lev.pos, 1); elems[match] -= 1;
total += lev.lev; if (elems[match] == 0) {
delete elems[match];
}
total += lev;
done += 1; done += 1;
} else { } else {
return MAX_LEV_DISTANCE + 1; return MAX_LEV_DISTANCE + 1;
@ -880,25 +893,27 @@ function defocusSearchBar() {
// Check for type name and type generics (if any). // Check for type name and type generics (if any).
function checkType(obj, val, literalSearch) { function checkType(obj, val, literalSearch) {
var lev_distance = MAX_LEV_DISTANCE + 1; var lev_distance = MAX_LEV_DISTANCE + 1;
var len, x, y, e_len, firstGeneric; var len, x, firstGeneric;
if (obj[NAME] === val.name) { if (obj[NAME] === val.name) {
if (literalSearch === true) { if (literalSearch === true) {
if (val.generics && val.generics.length !== 0) { if (val.generics && val.generics.length !== 0) {
if (obj.length > GENERICS_DATA && if (obj.length > GENERICS_DATA &&
obj[GENERICS_DATA].length >= val.generics.length) { obj[GENERICS_DATA].length >= val.generics.length) {
var elems = obj[GENERICS_DATA].slice(0); var elems = Object.create(null);
var allFound = true; len = obj[GENERICS_DATA].length;
for (x = 0; x < len; ++x) {
elems[getObjectNameFromId(obj[GENERICS_DATA][x])] += 1;
}
var allFound = true;
len = val.generics.length; len = val.generics.length;
for (y = 0; allFound === true && y < len; ++y) { for (x = 0; x < len; ++x) {
allFound = false; firstGeneric = getObjectNameFromId(val.generics[x]);
firstGeneric = getObjectFromId(val.generics[y]).name; if (elems[firstGeneric]) {
e_len = elems.length; elems[firstGeneric] -= 1;
for (x = 0; allFound === false && x < e_len; ++x) { } else {
allFound = getObjectFromId(elems[x]).name === firstGeneric; allFound = false;
} break;
if (allFound === true) {
elems.splice(x - 1, 1);
} }
} }
if (allFound === true) { if (allFound === true) {
@ -1066,13 +1081,6 @@ function defocusSearchBar() {
return false; return false;
} }
function generateId(ty) {
if (ty.parent && ty.parent.name) {
return itemTypes[ty.ty] + ty.path + ty.parent.name + ty.name;
}
return itemTypes[ty.ty] + ty.path + ty.name;
}
function createAliasFromItem(item) { function createAliasFromItem(item) {
return { return {
crate: item.crate, crate: item.crate,
@ -1158,7 +1166,7 @@ function defocusSearchBar() {
in_args = findArg(searchIndex[i], val, true, typeFilter); in_args = findArg(searchIndex[i], val, true, typeFilter);
returned = checkReturned(searchIndex[i], val, true, typeFilter); returned = checkReturned(searchIndex[i], val, true, typeFilter);
ty = searchIndex[i]; ty = searchIndex[i];
fullId = generateId(ty); fullId = ty.id;
if (searchWords[i] === val.name if (searchWords[i] === val.name
&& typePassesFilter(typeFilter, searchIndex[i].ty) && typePassesFilter(typeFilter, searchIndex[i].ty)
@ -1208,7 +1216,7 @@ function defocusSearchBar() {
if (!type) { if (!type) {
continue; continue;
} }
fullId = generateId(ty); fullId = ty.id;
returned = checkReturned(ty, output, true, NO_TYPE_FILTER); returned = checkReturned(ty, output, true, NO_TYPE_FILTER);
if (output.name === "*" || returned === true) { if (output.name === "*" || returned === true) {
@ -1292,15 +1300,15 @@ function defocusSearchBar() {
var index = -1; var index = -1;
// we want lev results to go lower than others // we want lev results to go lower than others
lev = MAX_LEV_DISTANCE + 1; lev = MAX_LEV_DISTANCE + 1;
fullId = generateId(ty); fullId = ty.id;
if (searchWords[j].indexOf(split[i]) > -1 || if (searchWords[j].indexOf(split[i]) > -1 ||
searchWords[j].indexOf(val) > -1 || searchWords[j].indexOf(val) > -1 ||
searchWords[j].replace(/_/g, "").indexOf(val) > -1) ty.normalizedName.indexOf(val) > -1)
{ {
// filter type: ... queries // filter type: ... queries
if (typePassesFilter(typeFilter, ty.ty) && results[fullId] === undefined) { if (typePassesFilter(typeFilter, ty.ty) && results[fullId] === undefined) {
index = searchWords[j].replace(/_/g, "").indexOf(val); index = ty.normalizedName.indexOf(val);
} }
} }
if ((lev = levenshtein(searchWords[j], val)) <= MAX_LEV_DISTANCE) { if ((lev = levenshtein(searchWords[j], val)) <= MAX_LEV_DISTANCE) {
@ -1828,8 +1836,9 @@ function defocusSearchBar() {
function buildIndex(rawSearchIndex) { function buildIndex(rawSearchIndex) {
searchIndex = []; searchIndex = [];
var searchWords = []; var searchWords = [];
var i; var i, word;
var currentIndex = 0; var currentIndex = 0;
var id = 0;
for (var crate in rawSearchIndex) { for (var crate in rawSearchIndex) {
if (!hasOwnProperty(rawSearchIndex, crate)) { continue; } if (!hasOwnProperty(rawSearchIndex, crate)) { continue; }
@ -1837,14 +1846,25 @@ function defocusSearchBar() {
var crateSize = 0; var crateSize = 0;
searchWords.push(crate); searchWords.push(crate);
searchIndex.push({ var normalizedName = crate.indexOf("_") === -1
? crate
: crate.replace(/_/g, "");
// This object should have exactly the same set of fields as the "row"
// object defined below. Your JavaScript runtime will thank you.
// https://mathiasbynens.be/notes/shapes-ics
var crateRow = {
crate: crate, crate: crate,
ty: 1, // == ExternCrate ty: 1, // == ExternCrate
name: crate, name: crate,
path: "", path: "",
desc: rawSearchIndex[crate].doc, desc: rawSearchIndex[crate].doc,
parent: undefined,
type: null, type: null,
}); id: id,
normalizedName: normalizedName,
};
id += 1;
searchIndex.push(crateRow);
currentIndex += 1; currentIndex += 1;
// an array of (Number) item types // an array of (Number) item types
@ -1882,6 +1902,18 @@ function defocusSearchBar() {
len = itemTypes.length; len = itemTypes.length;
var lastPath = ""; var lastPath = "";
for (i = 0; i < len; ++i) { for (i = 0; i < len; ++i) {
// This object should have exactly the same set of fields as the "crateRow"
// object defined above.
if (typeof itemNames[i] === "string") {
word = itemNames[i].toLowerCase();
searchWords.push(word);
} else {
word = "";
searchWords.push("");
}
var normalizedName = word.indexOf("_") === -1
? word
: word.replace(/_/g, "");
var row = { var row = {
crate: crate, crate: crate,
ty: itemTypes[i], ty: itemTypes[i],
@ -1890,14 +1922,11 @@ function defocusSearchBar() {
desc: itemDescs[i], desc: itemDescs[i],
parent: itemParentIdxs[i] > 0 ? paths[itemParentIdxs[i] - 1] : undefined, parent: itemParentIdxs[i] > 0 ? paths[itemParentIdxs[i] - 1] : undefined,
type: itemFunctionSearchTypes[i], type: itemFunctionSearchTypes[i],
id: id,
normalizedName: normalizedName,
}; };
id += 1;
searchIndex.push(row); searchIndex.push(row);
if (typeof row.name === "string") {
var word = row.name.toLowerCase();
searchWords.push(word);
} else {
searchWords.push("");
}
lastPath = row.path; lastPath = row.path;
crateSize += 1; crateSize += 1;
} }