Skip to content

Commit fa2f355

Browse files
committed
Auto merge of #142667 - yotamofek:pr/rustdoc/more-write-shared-perf, r=nnethercote
Avoid a few more allocations in `write_shared.rs` Inspired by #141421 , avoids a few `Vec`, `PathBuf` and `String` allocations in `write_shared.rs`. I don't think these will show up on benchmarks, but are still worthwhile IMHO. Also includes a few small cleanups. r? nnethercote - if you'd like :)
2 parents d4e1159 + 2bf66e3 commit fa2f355

File tree

1 file changed

+12
-27
lines changed

1 file changed

+12
-27
lines changed

src/librustdoc/html/render/write_shared.rs

Lines changed: 12 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -439,24 +439,20 @@ impl CratesIndexPart {
439439
let content =
440440
format!("<h1>List of all crates</h1><ul class=\"all-items\">{DELIMITER}</ul>");
441441
let template = layout::render(layout, &page, "", content, style_files);
442-
match SortedTemplate::from_template(&template, DELIMITER) {
443-
Ok(template) => template,
444-
Err(e) => panic!(
445-
"Object Replacement Character (U+FFFC) should not appear in the --index-page: {e}"
446-
),
447-
}
442+
SortedTemplate::from_template(&template, DELIMITER)
443+
.expect("Object Replacement Character (U+FFFC) should not appear in the --index-page")
448444
}
449445

450446
/// Might return parts that are duplicate with ones in prexisting index.html
451447
fn get(crate_name: &str, external_crates: &[String]) -> Result<PartsAndLocations<Self>, Error> {
452448
let mut ret = PartsAndLocations::default();
453-
let path = PathBuf::from("index.html");
449+
let path = Path::new("index.html");
454450
for crate_name in external_crates.iter().map(|s| s.as_str()).chain(once(crate_name)) {
455451
let part = format!(
456452
"<li><a href=\"{trailing_slash}index.html\">{crate_name}</a></li>",
457453
trailing_slash = ensure_trailing_slash(crate_name),
458454
);
459-
ret.push(path.clone(), part);
455+
ret.push(path.to_path_buf(), part);
460456
}
461457
Ok(ret)
462458
}
@@ -737,7 +733,7 @@ impl TraitAliasPart {
737733
},
738734
};
739735

740-
let implementors = imps
736+
let mut implementors = imps
741737
.iter()
742738
.filter_map(|imp| {
743739
// If the trait and implementation are in the same crate, then
@@ -759,12 +755,12 @@ impl TraitAliasPart {
759755
})
760756
}
761757
})
762-
.collect::<Vec<_>>();
758+
.peekable();
763759

764760
// Only create a js file if we have impls to add to it. If the trait is
765761
// documented locally though we always create the file to avoid dead
766762
// links.
767-
if implementors.is_empty() && !cache.paths.contains_key(&did) {
763+
if implementors.peek().is_none() && !cache.paths.contains_key(&did) {
768764
continue;
769765
}
770766

@@ -775,11 +771,7 @@ impl TraitAliasPart {
775771
path.push(format!("{remote_item_type}.{}.js", remote_path[remote_path.len() - 1]));
776772

777773
let part = OrderedJson::array_sorted(
778-
implementors
779-
.iter()
780-
.map(OrderedJson::serialize)
781-
.collect::<Result<Vec<_>, _>>()
782-
.unwrap(),
774+
implementors.map(|implementor| OrderedJson::serialize(implementor).unwrap()),
783775
);
784776
path_parts.push(path, OrderedJson::array_unsorted([crate_name_json, &part]));
785777
}
@@ -874,9 +866,8 @@ impl<'item> DocVisitor<'item> for TypeImplCollector<'_, '_, 'item> {
874866
let impl_ = cache
875867
.impls
876868
.get(&target_did)
877-
.map(|v| &v[..])
878-
.unwrap_or_default()
879-
.iter()
869+
.into_iter()
870+
.flatten()
880871
.map(|impl_| {
881872
(impl_.impl_item.item_id, AliasedTypeImpl { impl_, type_aliases: Vec::new() })
882873
})
@@ -891,14 +882,8 @@ impl<'item> DocVisitor<'item> for TypeImplCollector<'_, '_, 'item> {
891882
// Exclude impls that are directly on this type. They're already in the HTML.
892883
// Some inlining scenarios can cause there to be two versions of the same
893884
// impl: one on the type alias and one on the underlying target type.
894-
let mut seen_impls: FxHashSet<ItemId> = cache
895-
.impls
896-
.get(&self_did)
897-
.map(|s| &s[..])
898-
.unwrap_or_default()
899-
.iter()
900-
.map(|i| i.impl_item.item_id)
901-
.collect();
885+
let mut seen_impls: FxHashSet<ItemId> =
886+
cache.impls.get(&self_did).into_iter().flatten().map(|i| i.impl_item.item_id).collect();
902887
for (impl_item_id, aliased_type_impl) in &mut aliased_type.impl_ {
903888
// Only include this impl if it actually unifies with this alias.
904889
// Synthetic impls are not included; those are also included in the HTML.

0 commit comments

Comments
 (0)