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 41677b3 commit 7bb1b2a
Showing 1 changed file with 19 additions and 11 deletions.
30 changes: 19 additions & 11 deletions src/librustdoc/html/static/js/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -2283,7 +2283,7 @@ function initSearch(rawSearchIndex) {
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 @@ -2847,23 +2847,32 @@ ${item.displayPath}<span class="${type}">${name}</span>\
* @param {Set<number>} fps - Set of distinct items
*/
function buildFunctionTypeFingerprint(type, output, fps) {

let input = type.id;
// All forms of `[]` get collapsed down to one thing in the bloom filter.
// Differentiating between arrays and slices, if the user asks for it, is
// still done in the matching algorithm.
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 Expand Up @@ -2892,7 +2901,6 @@ ${item.displayPath}<span class="${type}">${name}</span>\
* This function might return 0!
*/
function compareTypeFingerprints(fullId, queryFingerprint) {

const fh0 = functionTypeFingerprint[fullId * 4];
const fh1 = functionTypeFingerprint[(fullId * 4) + 1];
const fh2 = functionTypeFingerprint[(fullId * 4) + 2];
Expand Down

0 comments on commit 7bb1b2a

Please sign in to comment.