diff options
| author | bors <bors@rust-lang.org> | 2019-05-13 00:28:38 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2019-05-13 00:28:38 +0000 |
| commit | fe5f42cdb88d8ce31f746130099321e7c95e1ef0 (patch) | |
| tree | d895b0ba0c1aca391e69c34a3b96b3eba48d9dd2 /src/libsyntax/attr/mod.rs | |
| parent | 4443957f272e304e083a8d98583e608d65a712aa (diff) | |
| parent | ea9fac5687c24abad493caf4ec5042af47458af9 (diff) | |
| download | rust-fe5f42cdb88d8ce31f746130099321e7c95e1ef0.tar.gz rust-fe5f42cdb88d8ce31f746130099321e7c95e1ef0.zip | |
Auto merge of #60630 - nnethercote:use-Symbol-more, r=petrochenkov
Use `Symbol` more A `Symbol` can be equated with a string (e.g. `&str`). This involves a TLS lookup to get the chars (and a Mutex lock in a parallel compiler) and then a char-by-char comparison. This functionality is convenient but avoids one of the main benefits of `Symbol`s, which is fast equality comparisons. This PR removes the `Symbol`/string equality operations, forcing a lot of existing string occurrences to become `Symbol`s. Fortunately, these are almost all static strings (many are attribute names) and we can add static `Symbol`s as necessary, and very little extra interning occurs. The benefits are (a) a slight speedup (possibly greater in a parallel compiler), and (b) the code is a lot more principled about `Symbol` use. The main downside is verbosity, particularly with more `use syntax::symbol::symbols` items. r? @Zoxc
Diffstat (limited to 'src/libsyntax/attr/mod.rs')
| -rw-r--r-- | src/libsyntax/attr/mod.rs | 39 |
1 files changed, 15 insertions, 24 deletions
diff --git a/src/libsyntax/attr/mod.rs b/src/libsyntax/attr/mod.rs index 07e4bbf78ff..d94a3165b0f 100644 --- a/src/libsyntax/attr/mod.rs +++ b/src/libsyntax/attr/mod.rs @@ -22,7 +22,7 @@ use crate::parse::parser::Parser; use crate::parse::{self, ParseSess, PResult}; use crate::parse::token::{self, Token}; use crate::ptr::P; -use crate::symbol::{keywords, LocalInternedString, Symbol}; +use crate::symbol::{keywords, Symbol}; use crate::ThinVec; use crate::tokenstream::{TokenStream, TokenTree, DelimSpan}; use crate::GLOBALS; @@ -81,10 +81,7 @@ impl NestedMetaItem { } /// Returns `true` if this list item is a MetaItem with a name of `name`. - pub fn check_name<T>(&self, name: T) -> bool - where - Path: PartialEq<T>, - { + pub fn check_name(&self, name: Symbol) -> bool { self.meta_item().map_or(false, |meta_item| meta_item.check_name(name)) } @@ -92,8 +89,8 @@ impl NestedMetaItem { pub fn ident(&self) -> Option<Ident> { self.meta_item().and_then(|meta_item| meta_item.ident()) } - pub fn name_or_empty(&self) -> LocalInternedString { - self.ident().unwrap_or(keywords::Invalid.ident()).name.as_str() + pub fn name_or_empty(&self) -> Symbol { + self.ident().unwrap_or(keywords::Invalid.ident()).name } /// Gets the string value if self is a MetaItem and the MetaItem is a @@ -154,10 +151,7 @@ impl Attribute { /// attribute is marked as used. /// /// To check the attribute name without marking it used, use the `path` field directly. - pub fn check_name<T>(&self, name: T) -> bool - where - Path: PartialEq<T>, - { + pub fn check_name(&self, name: Symbol) -> bool { let matches = self.path == name; if matches { mark_used(self); @@ -173,8 +167,8 @@ impl Attribute { None } } - pub fn name_or_empty(&self) -> LocalInternedString { - self.ident().unwrap_or(keywords::Invalid.ident()).name.as_str() + pub fn name_or_empty(&self) -> Symbol { + self.ident().unwrap_or(keywords::Invalid.ident()).name } pub fn value_str(&self) -> Option<Symbol> { @@ -211,8 +205,8 @@ impl MetaItem { None } } - pub fn name_or_empty(&self) -> LocalInternedString { - self.ident().unwrap_or(keywords::Invalid.ident()).name.as_str() + pub fn name_or_empty(&self) -> Symbol { + self.ident().unwrap_or(keywords::Invalid.ident()).name } // #[attribute(name = "value")] @@ -250,10 +244,7 @@ impl MetaItem { } } - pub fn check_name<T>(&self, name: T) -> bool - where - Path: PartialEq<T>, - { + pub fn check_name(&self, name: Symbol) -> bool { self.path == name } @@ -430,28 +421,28 @@ pub fn mk_sugared_doc_attr(id: AttrId, text: Symbol, span: Span) -> Attribute { } } -pub fn list_contains_name(items: &[NestedMetaItem], name: &str) -> bool { +pub fn list_contains_name(items: &[NestedMetaItem], name: Symbol) -> bool { items.iter().any(|item| { item.check_name(name) }) } -pub fn contains_name(attrs: &[Attribute], name: &str) -> bool { +pub fn contains_name(attrs: &[Attribute], name: Symbol) -> bool { attrs.iter().any(|item| { item.check_name(name) }) } -pub fn find_by_name<'a>(attrs: &'a [Attribute], name: &str) -> Option<&'a Attribute> { +pub fn find_by_name<'a>(attrs: &'a [Attribute], name: Symbol) -> Option<&'a Attribute> { attrs.iter().find(|attr| attr.check_name(name)) } -pub fn filter_by_name<'a>(attrs: &'a [Attribute], name: &'a str) +pub fn filter_by_name<'a>(attrs: &'a [Attribute], name: Symbol) -> impl Iterator<Item = &'a Attribute> { attrs.iter().filter(move |attr| attr.check_name(name)) } -pub fn first_attr_value_str_by_name(attrs: &[Attribute], name: &str) -> Option<Symbol> { +pub fn first_attr_value_str_by_name(attrs: &[Attribute], name: Symbol) -> Option<Symbol> { attrs.iter() .find(|at| at.check_name(name)) .and_then(|at| at.value_str()) |
