Skip to content

Commit 3c93f68

Browse files
committed
rustdoc-search: prevent struct fields from blocking functions
This perf hack tweaks the inverted indexes so that fields appear further down. This doesn't affect the results, because fields are pushed to the bottom anyway, but it prevents us from having to load the metadata (in order to actually figure out that it *is* a field).
1 parent 16b4ccc commit 3c93f68

File tree

3 files changed

+15
-1
lines changed

3 files changed

+15
-1
lines changed

src/librustdoc/formats/item_type.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,10 @@ impl ItemType {
243243
pub(crate) fn is_adt(&self) -> bool {
244244
matches!(self, ItemType::Struct | ItemType::Union | ItemType::Enum)
245245
}
246+
/// Keep this the same as isFnLikeTy in search.js
247+
pub(crate) fn is_fn_like(&self) -> bool {
248+
matches!(self, ItemType::Function | ItemType::Method | ItemType::TyMethod)
249+
}
246250
}
247251

248252
impl fmt::Display for ItemType {

src/librustdoc/html/render/search_index.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1732,7 +1732,15 @@ pub(crate) fn build_index(
17321732
);
17331733
}
17341734
}
1735-
let search_type_size = search_type.size();
1735+
let search_type_size = search_type.size() +
1736+
// Artificially give struct fields a size of 8 instead of their real
1737+
// size of 2. This is because search.js sorts them to the end, so
1738+
// by pushing them down, we prevent them from blocking real 2-arity functions.
1739+
//
1740+
// The number 8 is arbitrary. We want it big, but not enormous,
1741+
// because the postings list has to fill in an empty array for each
1742+
// unoccupied size.
1743+
if item.ty.is_fn_like() { 0 } else { 16 };
17361744
serialized_index.function_data[new_entry_id] = Some(FunctionData {
17371745
function_signature: {
17381746
let mut function_signature = String::new();

src/librustdoc/html/static/js/search.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,8 @@ function isEndCharacter(c) {
292292
}
293293

294294
/**
295+
* Same thing as ItemType::is_fn_like in item_type.rs
296+
*
295297
* @param {rustdoc.ItemType} ty
296298
* @returns
297299
*/

0 commit comments

Comments
 (0)