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

@@ -43,7 +43,7 @@ impl<'a> Parser<'a> {
just_parsed_doc_comment = false;
}
token::DocComment(s) => {
let attr = attr::mk_sugared_doc_attr(s, self.token.span);
let attr = attr::mk_doc_comment(s, self.token.span);
if attr.style != ast::AttrStyle::Outer {
let mut err = self.fatal("expected outer doc comment");
err.note("inner doc comments like this (starting with \
@@ -150,10 +150,9 @@ impl<'a> Parser<'a> {
};
Ok(ast::Attribute {
item,
kind: ast::AttrKind::Normal(item),
id: attr::mk_attr_id(),
style,
is_sugared_doc: false,
span,
})
}
@@ -229,7 +228,7 @@ impl<'a> Parser<'a> {
}
token::DocComment(s) => {
// We need to get the position of this token before we bump.
let attr = attr::mk_sugared_doc_attr(s, self.token.span);
let attr = attr::mk_doc_comment(s, self.token.span);
if attr.style == ast::AttrStyle::Inner {
attrs.push(attr);
self.bump();