Skip to content

Commit

Permalink
rustdoc-search: better hashing, faster unification
Browse files Browse the repository at this point in the history
The hash changes are based on some tests with `arti` and various
specific queries, aimed at reducing the false positive rate.

Sorting the query elements so that generics always come first is
instead aimed at reducing the number of Map operations on mgens,
assuming if the bloom filter does find a false positive, it'll
be able to reject the row without having to track a mapping.
  • Loading branch information
notriddle committed Dec 11, 2023
1 parent bada62b commit f2579f2
Showing 1 changed file with 38 additions and 9 deletions.
47 changes: 38 additions & 9 deletions src/librustdoc/html/static/js/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -2268,8 +2268,27 @@ function initSearch(rawSearchIndex) {
);
}
} else if (parsedQuery.foundElems > 0) {
// Sort input and output so that generic type variables go first and
// types with generic parameters go last.
// That's because of the way unification is structured: it eats off
// the end, and hits a fast path if the last item is a simple atom.
const sortQ = (a, b) => {
const ag = a.generics.length === 0 && a.bindings.size === 0;
const bg = b.generics.length === 0 && b.bindings.size === 0;
if (ag !== bg) {
return ag - bg;
}
const ai = a.id > 0;
const bi = b.id > 0;
if (ai !== bi) {
return ai - bi;
}
return 0;
};
parsedQuery.elems.sort(sortQ);
parsedQuery.returned.sort(sortQ);
for (i = 0, nSearchWords = searchWords.length; i < nSearchWords; ++i) {
handleArgs(searchIndex[i], i, results_others);
handleArgs(searchIndex[i], searchIndex[i].id, results_others);
}
}
}
Expand Down Expand Up @@ -2841,15 +2860,25 @@ ${item.displayPath}<span class="${type}">${name}</span>\
if (input === typeNameIdOfArray || input === typeNameIdOfSlice) {
input = typeNameIdOfArrayOrSlice;
}
if (input !== null) {
const fxScram = k => {
// https://docs.rs/rustc-hash/1.1.0/src/rustc_hash/lib.rs.html#60
// Rotate is skipped because we're only doing one cycle anyway.
const h0 = Math.imul(input, 0x9e3779b9);
const h1 = Math.imul(479001599 ^ input, 0x9e3779b9);
const h2 = Math.imul(433494437 ^ input, 0x9e3779b9);
output[0] |= 1 << (h0 % 32);
output[1] |= 1 << (h1 % 32);
output[2] |= 1 << (h2 % 32);
return Math.imul((k << 5) | (k >> 27), 0x9e3779b9);
};
const murmurScram = k => {
// https://en.wikipedia.org/wiki/MurmurHash
k = Math.imul(k, 0xcc9e2d51);
return Math.imul((k << 15) | (k >> 17), 0x1b873593);
};
if (input !== null) {
const h0a = fxScram(input);
const h0b = murmurScram(input);
const h1a = fxScram(479001599 ^ input);
const h1b = murmurScram(479001599 ^ input);
const h2a = fxScram(433494437 ^ input);
const h2b = murmurScram(433494437 ^ input);
output[0] |= (1 << (h0a % 32)) | (1 << (h0b % 32));
output[1] |= (1 << (h1a % 32)) | (1 << (h1b % 32));
output[2] |= (1 << (h2a % 32)) | (1 << (h2b % 32));
fps.add(input);
}
for (const g of type.generics) {
Expand Down

0 comments on commit f2579f2

Please sign in to comment.