Skip to content

Commit

Permalink
Adjust ranking so that duplicates count against rank
Browse files Browse the repository at this point in the history
  • Loading branch information
notriddle committed Oct 31, 2024
1 parent 12dc24f commit 9900ea4
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 30 deletions.
25 changes: 11 additions & 14 deletions src/librustdoc/html/static/js/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -1412,17 +1412,16 @@ class DocSearch {
* query fingerprint. If any bits are set in the query but not in the function, it can't
* match.
*
* - The fourth section has the number of distinct items in the set.
* - The fourth section has the number of items in the set.
* This is the distance function, used for filtering and for sorting.
*
* [^1]: Distance is the relatively naive metric of counting the number of distinct items in
* the function that are not present in the query.
*
* @param {FunctionType|QueryElement} type - a single type
* @param {Uint32Array} output - write the fingerprint to this data structure: uses 128 bits
* @param {Set<number>} fps - Set of distinct items
*/
buildFunctionTypeFingerprint(type, output, fps) {
buildFunctionTypeFingerprint(type, output) {
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
Expand Down Expand Up @@ -1468,10 +1467,11 @@ class DocSearch {
output[0] |= (1 << (h0a % 32)) | (1 << (h1b % 32));
output[1] |= (1 << (h1a % 32)) | (1 << (h2b % 32));
output[2] |= (1 << (h2a % 32)) | (1 << (h0b % 32));
fps.add(input);
// output[3] is the total number of items in the type signature
output[3] += 1;
}
for (const g of type.generics) {
this.buildFunctionTypeFingerprint(g, output, fps);
this.buildFunctionTypeFingerprint(g, output);
}
const fb = {
id: null,
Expand All @@ -1482,9 +1482,8 @@ class DocSearch {
for (const [k, v] of type.bindings.entries()) {
fb.id = k;
fb.generics = v;
this.buildFunctionTypeFingerprint(fb, output, fps);
this.buildFunctionTypeFingerprint(fb, output);
}
output[3] = fps.size;
}

/**
Expand Down Expand Up @@ -1734,16 +1733,15 @@ class DocSearch {
if (type !== null) {
if (type) {
const fp = this.functionTypeFingerprint.subarray(id * 4, (id + 1) * 4);
const fps = new Set();
for (const t of type.inputs) {
this.buildFunctionTypeFingerprint(t, fp, fps);
this.buildFunctionTypeFingerprint(t, fp);
}
for (const t of type.output) {
this.buildFunctionTypeFingerprint(t, fp, fps);
this.buildFunctionTypeFingerprint(t, fp);
}
for (const w of type.where_clause) {
for (const t of w) {
this.buildFunctionTypeFingerprint(t, fp, fps);
this.buildFunctionTypeFingerprint(t, fp);
}
}
}
Expand Down Expand Up @@ -3885,14 +3883,13 @@ class DocSearch {
);
};

const fps = new Set();
for (const elem of parsedQuery.elems) {
convertNameToId(elem);
this.buildFunctionTypeFingerprint(elem, parsedQuery.typeFingerprint, fps);
this.buildFunctionTypeFingerprint(elem, parsedQuery.typeFingerprint);
}
for (const elem of parsedQuery.returned) {
convertNameToId(elem);
this.buildFunctionTypeFingerprint(elem, parsedQuery.typeFingerprint, fps);
this.buildFunctionTypeFingerprint(elem, parsedQuery.typeFingerprint);
}

if (parsedQuery.foundElems === 1 && !parsedQuery.hasReturnArrow) {
Expand Down
20 changes: 10 additions & 10 deletions tests/rustdoc-js-std/simd-type-signatures.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,6 @@ const EXPECTED = [
'name': 'simd_min',
'href': '../std/simd/prelude/struct.Simd.html#impl-SimdOrd-for-Simd%3Ci16,+N%3E/method.simd_min'
},
{
'path': 'std::simd::prelude::Simd',
'name': 'simd_clamp',
'href': '../std/simd/prelude/struct.Simd.html#impl-SimdOrd-for-Simd%3Ci16,+N%3E/method.simd_clamp'
},
{
'path': 'std::simd::prelude::Simd',
'name': 'saturating_add',
Expand All @@ -35,6 +30,11 @@ const EXPECTED = [
'name': 'saturating_sub',
'href': '../std/simd/prelude/struct.Simd.html#impl-SimdInt-for-Simd%3Ci16,+N%3E/method.saturating_sub'
},
{
'path': 'std::simd::prelude::Simd',
'name': 'simd_clamp',
'href': '../std/simd/prelude/struct.Simd.html#impl-SimdOrd-for-Simd%3Ci16,+N%3E/method.simd_clamp'
},
],
},
{
Expand All @@ -50,11 +50,6 @@ const EXPECTED = [
'name': 'simd_min',
'href': '../std/simd/prelude/struct.Simd.html#impl-SimdOrd-for-Simd%3Ci8,+N%3E/method.simd_min'
},
{
'path': 'std::simd::prelude::Simd',
'name': 'simd_clamp',
'href': '../std/simd/prelude/struct.Simd.html#impl-SimdOrd-for-Simd%3Ci8,+N%3E/method.simd_clamp'
},
{
'path': 'std::simd::prelude::Simd',
'name': 'saturating_add',
Expand All @@ -65,6 +60,11 @@ const EXPECTED = [
'name': 'saturating_sub',
'href': '../std/simd/prelude/struct.Simd.html#impl-SimdInt-for-Simd%3Ci8,+N%3E/method.saturating_sub'
},
{
'path': 'std::simd::prelude::Simd',
'name': 'simd_clamp',
'href': '../std/simd/prelude/struct.Simd.html#impl-SimdOrd-for-Simd%3Ci8,+N%3E/method.simd_clamp'
},
],
},
];
13 changes: 13 additions & 0 deletions tests/rustdoc-js-std/transmute-fail.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// should-fail
const EXPECTED = [
{
// Keep this test case identical to `transmute`, except the
// should-fail tag and the search query below:
'query': 'generic:T -> generic:T',
'others': [
{ 'path': 'std::intrinsics::simd', 'name': 'simd_as' },
{ 'path': 'std::intrinsics::simd', 'name': 'simd_cast' },
{ 'path': 'std::intrinsics', 'name': 'transmute' },
],
},
];
12 changes: 12 additions & 0 deletions tests/rustdoc-js-std/transmute.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
const EXPECTED = [
{
// Keep this test case identical to `transmute-fail`, except the
// should-fail tag and the search query below:
'query': 'generic:T -> generic:U',
'others': [
{ 'path': 'std::intrinsics::simd', 'name': 'simd_as' },
{ 'path': 'std::intrinsics::simd', 'name': 'simd_cast' },
{ 'path': 'std::intrinsics', 'name': 'transmute' },
],
},
];
6 changes: 3 additions & 3 deletions tests/rustdoc-js/impl-trait.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ const EXPECTED = [
'others': [
{ 'path': 'impl_trait', 'name': 'bbbbbbb' },
{ 'path': 'impl_trait::Ccccccc', 'name': 'ddddddd' },
{ 'path': 'impl_trait::Ccccccc', 'name': 'fffffff' },
{ 'path': 'impl_trait::Ccccccc', 'name': 'ggggggg' },
{ 'path': 'impl_trait::Ccccccc', 'name': 'fffffff' },
],
},
{
Expand All @@ -39,14 +39,14 @@ const EXPECTED = [
{ 'path': 'impl_trait', 'name': 'Aaaaaaa' },
],
'in_args': [
{ 'path': 'impl_trait::Ccccccc', 'name': 'fffffff' },
{ 'path': 'impl_trait::Ccccccc', 'name': 'eeeeeee' },
{ 'path': 'impl_trait::Ccccccc', 'name': 'fffffff' },
],
'returned': [
{ 'path': 'impl_trait', 'name': 'bbbbbbb' },
{ 'path': 'impl_trait::Ccccccc', 'name': 'ddddddd' },
{ 'path': 'impl_trait::Ccccccc', 'name': 'fffffff' },
{ 'path': 'impl_trait::Ccccccc', 'name': 'ggggggg' },
{ 'path': 'impl_trait::Ccccccc', 'name': 'fffffff' },
],
},
];
6 changes: 3 additions & 3 deletions tests/rustdoc-js/type-parameters.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ const EXPECTED = [
{
query: '-> generic:T',
others: [
{ path: 'foo', name: 'beta' },
{ path: 'foo', name: 'bet' },
{ path: 'foo', name: 'alef' },
{ path: 'foo', name: 'beta' },
],
},
{
Expand Down Expand Up @@ -50,17 +50,17 @@ const EXPECTED = [
{
query: 'generic:T',
in_args: [
{ path: 'foo', name: 'beta' },
{ path: 'foo', name: 'bet' },
{ path: 'foo', name: 'beta' },
{ path: 'foo', name: 'alternate' },
{ path: 'foo', name: 'other' },
],
},
{
query: 'generic:Other',
in_args: [
{ path: 'foo', name: 'beta' },
{ path: 'foo', name: 'bet' },
{ path: 'foo', name: 'beta' },
{ path: 'foo', name: 'alternate' },
{ path: 'foo', name: 'other' },
],
Expand Down

0 comments on commit 9900ea4

Please sign in to comment.