From f4687d5381499ce36f51ffeac4ce61ff627d4bec Mon Sep 17 00:00:00 2001 From: Noah Lev Date: Thu, 18 Nov 2021 22:57:09 -0500 Subject: [PATCH 1/3] rustdoc: Remove unused `DocFragment.line` field --- src/librustdoc/clean/types.rs | 5 ----- src/librustdoc/passes/unindent_comments/tests.rs | 1 - 2 files changed, 6 deletions(-) diff --git a/src/librustdoc/clean/types.rs b/src/librustdoc/clean/types.rs index fb08ced205d86..41ebf270ba630 100644 --- a/src/librustdoc/clean/types.rs +++ b/src/librustdoc/clean/types.rs @@ -906,7 +906,6 @@ impl + IntoIterator, ) -> Attributes { let mut doc_strings: Vec = vec![]; - let mut doc_line = 0; fn update_need_backline(doc_strings: &mut Vec) { if let Some(prev) = doc_strings.last_mut() { @@ -1045,10 +1043,7 @@ impl Attributes { DocFragmentKind::RawDoc }; - let line = doc_line; - doc_line += value.as_str().lines().count(); let frag = DocFragment { - line, span: attr.span, doc: value, kind, diff --git a/src/librustdoc/passes/unindent_comments/tests.rs b/src/librustdoc/passes/unindent_comments/tests.rs index 82d1afac5eceb..daec04e11cd18 100644 --- a/src/librustdoc/passes/unindent_comments/tests.rs +++ b/src/librustdoc/passes/unindent_comments/tests.rs @@ -5,7 +5,6 @@ use rustc_span::symbol::Symbol; fn create_doc_fragment(s: &str) -> Vec { vec![DocFragment { - line: 0, span: DUMMY_SP, parent_module: None, doc: Symbol::intern(s), From a792234388461e3c6b6104cae490aa21b15102e4 Mon Sep 17 00:00:00 2001 From: Noah Lev Date: Thu, 18 Nov 2021 23:04:51 -0500 Subject: [PATCH 2/3] rustdoc: Avoid using `Iterator::count()` where possible `count()` iterates over the whole collection. Using `len()` instead, or `.next().is_none()` when comparing to zero, should be faster. --- src/librustdoc/html/markdown.rs | 2 +- src/librustdoc/passes/unindent_comments.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/librustdoc/html/markdown.rs b/src/librustdoc/html/markdown.rs index 47772651bf9b9..d811c85ea58d1 100644 --- a/src/librustdoc/html/markdown.rs +++ b/src/librustdoc/html/markdown.rs @@ -656,7 +656,7 @@ impl<'a, I> Footnotes<'a, I> { } fn get_entry(&mut self, key: &str) -> &mut (Vec>, u16) { - let new_id = self.footnotes.keys().count() + 1; + let new_id = self.footnotes.len() + 1; let key = key.to_owned(); self.footnotes.entry(key).or_insert((Vec::new(), new_id as u16)) } diff --git a/src/librustdoc/passes/unindent_comments.rs b/src/librustdoc/passes/unindent_comments.rs index da2eda7364122..f42e7a97eb785 100644 --- a/src/librustdoc/passes/unindent_comments.rs +++ b/src/librustdoc/passes/unindent_comments.rs @@ -87,7 +87,7 @@ fn unindent_fragments(docs: &mut Vec) { }; for fragment in docs { - if fragment.doc.as_str().lines().count() == 0 { + if fragment.doc.as_str().lines().next().is_none() { continue; } From 7a4e2ceb922b4b0861735b333f9c983ebed7029f Mon Sep 17 00:00:00 2001 From: Noah Lev Date: Fri, 19 Nov 2021 11:13:24 -0500 Subject: [PATCH 3/3] Use fast comparison against `kw::Empty` We think `.as_str().lines().next().is_none()` should be equivalent to `== kw::Empty`. Co-authored-by: Joshua Nelson --- src/librustdoc/passes/unindent_comments.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/librustdoc/passes/unindent_comments.rs b/src/librustdoc/passes/unindent_comments.rs index f42e7a97eb785..97f4f941e0686 100644 --- a/src/librustdoc/passes/unindent_comments.rs +++ b/src/librustdoc/passes/unindent_comments.rs @@ -1,5 +1,7 @@ use std::cmp; +use rustc_span::symbol::kw; + use crate::clean::{self, DocFragment, DocFragmentKind, Item}; use crate::core::DocContext; use crate::fold::{self, DocFolder}; @@ -87,7 +89,7 @@ fn unindent_fragments(docs: &mut Vec) { }; for fragment in docs { - if fragment.doc.as_str().lines().next().is_none() { + if fragment.doc == kw::Empty { continue; }