diff options
| author | León Orell Valerian Liehr <me@fmease.dev> | 2025-07-24 15:08:26 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-07-24 15:08:26 +0200 |
| commit | 7413ecee4bf47f237fbe9b62c69ba448ab330177 (patch) | |
| tree | ac1b1996f367db7ed2d056a82c355664075d6107 | |
| parent | 352e02bce172fd59313f66339e60371bb7a13ed9 (diff) | |
| parent | 9e75032a9dc06d8286aebcb4ed00ad5d81bbf0e9 (diff) | |
| download | rust-7413ecee4bf47f237fbe9b62c69ba448ab330177.tar.gz rust-7413ecee4bf47f237fbe9b62c69ba448ab330177.zip | |
Rollup merge of #144320 - lolbinarycat:rustdoc-search_index-BTreeMap-str, r=GuillaumeGomez
rustdoc: avoid allocating a temp String for aliases in search index Here's the optimization I talked about in https://github.com/rust-lang/rust/pull/143988#discussion_r2208524163 I got around the Serialize issue using the newtype pattern. The wrapper type could be factored out into a helper that would work with anything that impls `AsRef<&str>`, but I'm not sure if that would be helpful anywhere else. r? ``````@GuillaumeGomez``````
| -rw-r--r-- | src/librustdoc/html/render/search_index.rs | 19 |
1 files changed, 16 insertions, 3 deletions
diff --git a/src/librustdoc/html/render/search_index.rs b/src/librustdoc/html/render/search_index.rs index 3c9be29ccc3..e2f86b8a854 100644 --- a/src/librustdoc/html/render/search_index.rs +++ b/src/librustdoc/html/render/search_index.rs @@ -100,9 +100,22 @@ pub(crate) fn build_index( let crate_doc = short_markdown_summary(&krate.module.doc_value(), &krate.module.link_names(cache)); + #[derive(Eq, Ord, PartialEq, PartialOrd)] + struct SerSymbolAsStr(Symbol); + + impl Serialize for SerSymbolAsStr { + fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> + where + S: Serializer, + { + self.0.as_str().serialize(serializer) + } + } + + type AliasMap = BTreeMap<SerSymbolAsStr, Vec<usize>>; // Aliases added through `#[doc(alias = "...")]`. Since a few items can have the same alias, // we need the alias element to have an array of items. - let mut aliases: BTreeMap<String, Vec<usize>> = BTreeMap::new(); + let mut aliases: AliasMap = BTreeMap::new(); // Sort search index items. This improves the compressibility of the search index. cache.search_index.sort_unstable_by(|k1, k2| { @@ -116,7 +129,7 @@ pub(crate) fn build_index( // Set up alias indexes. for (i, item) in cache.search_index.iter().enumerate() { for alias in &item.aliases[..] { - aliases.entry(alias.to_string()).or_default().push(i); + aliases.entry(SerSymbolAsStr(*alias)).or_default().push(i); } } @@ -474,7 +487,7 @@ pub(crate) fn build_index( // The String is alias name and the vec is the list of the elements with this alias. // // To be noted: the `usize` elements are indexes to `items`. - aliases: &'a BTreeMap<String, Vec<usize>>, + aliases: &'a AliasMap, // Used when a type has more than one impl with an associated item with the same name. associated_item_disambiguators: &'a Vec<(usize, String)>, // A list of shard lengths encoded as vlqhex. See the comment in write_vlqhex_to_string |
