Make doc comments cheaper with AttrKind.

`AttrKind` is a new type with two variants, `Normal` and `DocComment`. It's a
big performance win (over 10% in some cases) because `DocComment` lets doc
comments (which are common) be represented very cheaply.

`Attribute` gets some new helper methods to ease the transition:
- `has_name()`: check if the attribute name matches a single `Symbol`; for
  `DocComment` variants it succeeds if the symbol is `sym::doc`.
- `is_doc_comment()`: check if it has a `DocComment` kind.
- `{get,unwrap}_normal_item()`: extract the item from a `Normal` variant;
  panic otherwise.

Fixes #60935.
This commit is contained in:
Nicholas Nethercote
2019-10-24 06:33:12 +11:00
parent 69bc4aba78
commit eea6f23a0e
25 changed files with 232 additions and 146 deletions

View File

@@ -622,16 +622,19 @@ pub trait PrintState<'a>: std::ops::Deref<Target = pp::Printer> + std::ops::Dere
self.hardbreak_if_not_bol();
}
self.maybe_print_comment(attr.span.lo());
if attr.is_sugared_doc {
self.word(attr.value_str().unwrap().to_string());
self.hardbreak()
} else {
match attr.style {
ast::AttrStyle::Inner => self.word("#!["),
ast::AttrStyle::Outer => self.word("#["),
match attr.kind {
ast::AttrKind::Normal(ref item) => {
match attr.style {
ast::AttrStyle::Inner => self.word("#!["),
ast::AttrStyle::Outer => self.word("#["),
}
self.print_attr_item(&item, attr.span);
self.word("]");
}
ast::AttrKind::DocComment(comment) => {
self.word(comment.to_string());
self.hardbreak()
}
self.print_attr_item(&attr.item, attr.span);
self.word("]");
}
}