rustdoc: Sligthly optimize Attributes construction and processing before doc link resolution

This commit is contained in:
Vadim Petrochenkov 2022-04-17 02:01:46 +03:00
parent de287df862
commit 5cce8cb4ec
3 changed files with 31 additions and 44 deletions

View File

@ -1089,42 +1089,35 @@ impl Attributes {
attrs: &[ast::Attribute], attrs: &[ast::Attribute],
additional_attrs: Option<(&[ast::Attribute], DefId)>, additional_attrs: Option<(&[ast::Attribute], DefId)>,
) -> Attributes { ) -> Attributes {
Attributes::from_ast_iter(attrs.iter(), additional_attrs) // Additional documentation should be shown before the original documentation.
let attrs1 = additional_attrs
.into_iter()
.flat_map(|(attrs, def_id)| attrs.iter().map(move |attr| (attr, Some(def_id))));
let attrs2 = attrs.iter().map(|attr| (attr, None));
Attributes::from_ast_iter(attrs1.chain(attrs2), false)
} }
crate fn from_ast_iter<'a>( crate fn from_ast_iter<'a>(
attrs: impl Iterator<Item = &'a ast::Attribute>, attrs: impl Iterator<Item = (&'a ast::Attribute, Option<DefId>)>,
additional_attrs: Option<(&[ast::Attribute], DefId)>, doc_only: bool,
) -> Attributes { ) -> Attributes {
let mut doc_strings: Vec<DocFragment> = vec![]; let mut doc_strings = Vec::new();
let clean_attr = |(attr, parent_module): (&ast::Attribute, Option<DefId>)| { let mut other_attrs = Vec::new();
if let Some((value, kind)) = attr.doc_str_and_comment_kind() { for (attr, parent_module) in attrs {
trace!("got doc_str={:?}", value); if let Some((doc_str, comment_kind)) = attr.doc_str_and_comment_kind() {
let value = beautify_doc_string(value, kind); trace!("got doc_str={doc_str:?}");
let doc = beautify_doc_string(doc_str, comment_kind);
let kind = if attr.is_doc_comment() { let kind = if attr.is_doc_comment() {
DocFragmentKind::SugaredDoc DocFragmentKind::SugaredDoc
} else { } else {
DocFragmentKind::RawDoc DocFragmentKind::RawDoc
}; };
let fragment = DocFragment { span: attr.span, doc, kind, parent_module, indent: 0 };
let frag = doc_strings.push(fragment);
DocFragment { span: attr.span, doc: value, kind, parent_module, indent: 0 }; } else if !doc_only {
other_attrs.push(attr.clone());
doc_strings.push(frag);
None
} else {
Some(attr.clone())
} }
}; }
// Additional documentation should be shown before the original documentation
let other_attrs = additional_attrs
.into_iter()
.flat_map(|(attrs, id)| attrs.iter().map(move |attr| (attr, Some(id))))
.chain(attrs.map(|attr| (attr, None)))
.filter_map(clean_attr)
.collect();
Attributes { doc_strings, other_attrs } Attributes { doc_strings, other_attrs }
} }
@ -1145,23 +1138,17 @@ impl Attributes {
} }
/// Return the doc-comments on this item, grouped by the module they came from. /// Return the doc-comments on this item, grouped by the module they came from.
///
/// The module can be different if this is a re-export with added documentation. /// The module can be different if this is a re-export with added documentation.
crate fn collapsed_doc_value_by_module_level(&self) -> FxHashMap<Option<DefId>, String> { ///
let mut ret = FxHashMap::default(); /// The last newline is not trimmed so the produced strings are reusable between
if self.doc_strings.len() == 0 { /// early and late doc link resolution regardless of their position.
return ret; crate fn prepare_to_doc_link_resolution(&self) -> FxHashMap<Option<DefId>, String> {
let mut res = FxHashMap::default();
for fragment in &self.doc_strings {
let out_str = res.entry(fragment.parent_module).or_default();
add_doc_fragment(out_str, fragment);
} }
let last_index = self.doc_strings.len() - 1; res
for (i, new_frag) in self.doc_strings.iter().enumerate() {
let out = ret.entry(new_frag.parent_module).or_default();
add_doc_fragment(out, new_frag);
if i == last_index {
out.pop();
}
}
ret
} }
/// Finds all `doc` attributes as NameValues and returns their corresponding values, joined /// Finds all `doc` attributes as NameValues and returns their corresponding values, joined

View File

@ -1050,7 +1050,7 @@ impl<'a, 'tcx> DocVisitor for LinkCollector<'a, 'tcx> {
// In the presence of re-exports, this is not the same as the module of the item. // In the presence of re-exports, this is not the same as the module of the item.
// Rather than merging all documentation into one, resolve it one attribute at a time // Rather than merging all documentation into one, resolve it one attribute at a time
// so we know which module it came from. // so we know which module it came from.
for (parent_module, doc) in item.attrs.collapsed_doc_value_by_module_level() { for (parent_module, doc) in item.attrs.prepare_to_doc_link_resolution() {
if !may_have_doc_links(&doc) { if !may_have_doc_links(&doc) {
continue; continue;
} }

View File

@ -63,7 +63,7 @@ crate fn early_resolve_intra_doc_links(
} }
fn doc_attrs<'a>(attrs: impl Iterator<Item = &'a ast::Attribute>) -> Attributes { fn doc_attrs<'a>(attrs: impl Iterator<Item = &'a ast::Attribute>) -> Attributes {
let mut attrs = Attributes::from_ast_iter(attrs.filter(|attr| attr.doc_str().is_some()), None); let mut attrs = Attributes::from_ast_iter(attrs.map(|attr| (attr, None)), true);
attrs.unindent_doc_comments(); attrs.unindent_doc_comments();
attrs attrs
} }
@ -201,7 +201,7 @@ impl EarlyDocLinkResolver<'_, '_> {
fn resolve_doc_links(&mut self, attrs: Attributes, module_id: DefId) { fn resolve_doc_links(&mut self, attrs: Attributes, module_id: DefId) {
let mut need_traits_in_scope = false; let mut need_traits_in_scope = false;
for (doc_module, doc) in attrs.collapsed_doc_value_by_module_level() { for (doc_module, doc) in attrs.prepare_to_doc_link_resolution() {
assert_eq!(doc_module, None); assert_eq!(doc_module, None);
let links = self let links = self
.markdown_links .markdown_links