Skip to content

Commit

Permalink
Use SmallVec in ast::Path.
Browse files Browse the repository at this point in the history
This is up to a 1% speedup on a few benchmark runs.
  • Loading branch information
nnethercote committed Jan 16, 2019
1 parent e2f221c commit 7e5b9c3
Show file tree
Hide file tree
Showing 14 changed files with 36 additions and 18 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2601,6 +2601,7 @@ dependencies = [
"rustc_data_structures 0.0.0",
"rustc_errors 0.0.0",
"rustc_metadata 0.0.0",
"smallvec 0.6.7 (registry+https://github.com/rust-lang/crates.io-index)",
"syntax 0.0.0",
"syntax_pos 0.0.0",
]
Expand All @@ -2618,6 +2619,7 @@ dependencies = [
"rustc_data_structures 0.0.0",
"rustc_target 0.0.0",
"rustc_typeck 0.0.0",
"smallvec 0.6.7 (registry+https://github.com/rust-lang/crates.io-index)",
"syntax 0.0.0",
"syntax_pos 0.0.0",
]
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/hir/lowering.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2774,7 +2774,7 @@ impl<'a> LoweringContext<'a> {
ItemKind::Use(ref use_tree) => {
// Start with an empty prefix
let prefix = Path {
segments: vec![],
segments: smallvec![],
span: use_tree.span,
};

Expand Down
2 changes: 2 additions & 0 deletions src/librustc_resolve/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,5 @@ rustc_errors = { path = "../librustc_errors" }
syntax_pos = { path = "../libsyntax_pos" }
rustc_data_structures = { path = "../librustc_data_structures" }
rustc_metadata = { path = "../librustc_metadata" }
smallvec = { version = "0.6.7", features = ["union", "may_dangle"] }

12 changes: 8 additions & 4 deletions src/librustc_resolve/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
extern crate bitflags;
#[macro_use]
extern crate log;
extern crate smallvec;
#[macro_use]
extern crate syntax;
extern crate syntax_pos;
Expand Down Expand Up @@ -66,6 +67,7 @@ use syntax::ptr::P;
use syntax_pos::{Span, DUMMY_SP, MultiSpan};
use errors::{Applicability, DiagnosticBuilder, DiagnosticId};

use smallvec::SmallVec;
use std::cell::{Cell, RefCell};
use std::{cmp, fmt, iter, mem, ptr};
use std::collections::BTreeSet;
Expand Down Expand Up @@ -1677,7 +1679,7 @@ impl<'a> hir::lowering::Resolver for Resolver<'a> {
crate_root.into_iter()
.chain(components.iter().cloned())
.map(Ident::from_str)
).map(|i| self.new_ast_path_segment(i)).collect::<Vec<_>>();
).map(|i| self.new_ast_path_segment(i)).collect::<SmallVec<[_; 1]>>();


let path = ast::Path {
Expand Down Expand Up @@ -4621,7 +4623,8 @@ impl<'a> Resolver<'a> {
let mut candidates = Vec::new();
let mut seen_modules = FxHashSet::default();
let not_local_module = crate_name != keywords::Crate.ident();
let mut worklist = vec![(start_module, Vec::<ast::PathSegment>::new(), not_local_module)];
let mut worklist =
vec![(start_module, SmallVec::<[ast::PathSegment; 1]>::new(), not_local_module)];

while let Some((in_module,
path_segments,
Expand Down Expand Up @@ -4737,7 +4740,7 @@ impl<'a> Resolver<'a> {
{
let mut result = None;
let mut seen_modules = FxHashSet::default();
let mut worklist = vec![(self.graph_root, Vec::new())];
let mut worklist = vec![(self.graph_root, SmallVec::new())];

while let Some((in_module, path_segments)) = worklist.pop() {
// abort if the module is already found
Expand Down Expand Up @@ -5214,9 +5217,10 @@ fn import_candidate_to_enum_paths(suggestion: &ImportSuggestion) -> (String, Str
let variant_path_string = path_names_to_string(variant_path);

let path_len = suggestion.path.segments.len();
let segments = suggestion.path.segments[0..path_len - 1].iter().cloned().collect();
let enum_path = ast::Path {
span: suggestion.path.span,
segments: suggestion.path.segments[0..path_len - 1].to_vec(),
segments,
};
let enum_path_string = path_names_to_string(&enum_path);

Expand Down
1 change: 1 addition & 0 deletions src/librustc_save_analysis/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ rustc_data_structures = { path = "../librustc_data_structures" }
rustc_codegen_utils = { path = "../librustc_codegen_utils" }
rustc_target = { path = "../librustc_target" }
rustc_typeck = { path = "../librustc_typeck" }
smallvec = { version = "0.6.7", features = ["union", "may_dangle"] }
syntax = { path = "../libsyntax" }
syntax_pos = { path = "../libsyntax_pos" }
rls-data = "0.18.1"
Expand Down
3 changes: 2 additions & 1 deletion src/librustc_save_analysis/dump_visitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ use syntax_pos::*;

use {escape, generated_code, lower_attributes, PathCollector, SaveContext};
use json_dumper::{Access, DumpOutput, JsonDumper};
use smallvec::smallvec;
use span_utils::SpanUtils;
use sig;

Expand Down Expand Up @@ -1338,7 +1339,7 @@ impl<'l, 'tcx: 'l, 'll, O: DumpOutput + 'll> Visitor<'l> for DumpVisitor<'l, 'tc
match item.node {
Use(ref use_tree) => {
let prefix = ast::Path {
segments: vec![],
segments: smallvec![],
span: DUMMY_SP,
};
self.process_use_tree(use_tree, item.id, item, &prefix);
Expand Down
1 change: 1 addition & 0 deletions src/librustc_save_analysis/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ extern crate rustc_codegen_utils;
extern crate rustc_serialize;
extern crate rustc_target;
extern crate rustc_typeck;
extern crate smallvec;
#[macro_use]
extern crate syntax;
extern crate syntax_pos;
Expand Down
1 change: 1 addition & 0 deletions src/librustdoc/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ extern crate rustc_metadata;
extern crate rustc_target;
extern crate rustc_typeck;
extern crate serialize;
extern crate smallvec;
extern crate syntax;
extern crate syntax_pos;
extern crate test as testing;
Expand Down
3 changes: 2 additions & 1 deletion src/librustdoc/passes/collect_intra_doc_links.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use rustc::lint as lint;
use rustc::hir;
use rustc::hir::def::Def;
use rustc::ty;
use smallvec::smallvec;
use syntax;
use syntax::ast::{self, Ident, NodeId};
use syntax::feature_gate::UnstableFeatures;
Expand Down Expand Up @@ -425,7 +426,7 @@ impl<'a, 'tcx, 'rcx> DocFolder for LinkCollector<'a, 'tcx, 'rcx> {
fn macro_resolve(cx: &DocContext, path_str: &str) -> Option<Def> {
use syntax::ext::base::{MacroKind, SyntaxExtension};
let segment = ast::PathSegment::from_ident(Ident::from_str(path_str));
let path = ast::Path { segments: vec![segment], span: DUMMY_SP };
let path = ast::Path { segments: smallvec![segment], span: DUMMY_SP };
let mut resolver = cx.resolver.borrow_mut();
let parent_scope = resolver.dummy_parent_scope();
if let Ok(def) = resolver.resolve_macro_to_def_inner(&path, MacroKind::Bang,
Expand Down
7 changes: 4 additions & 3 deletions src/libsyntax/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ use ThinVec;
use rustc_data_structures::fx::FxHashSet;
use rustc_data_structures::sync::Lrc;
use serialize::{self, Decoder, Encoder};
use smallvec::SmallVec;
use std::fmt;

pub use rustc_target::abi::FloatTy;
Expand Down Expand Up @@ -64,7 +65,7 @@ pub struct Path {
pub span: Span,
/// The segments in the path: the things separated by `::`.
/// Global paths begin with `keywords::PathRoot`.
pub segments: Vec<PathSegment>,
pub segments: SmallVec<[PathSegment; 1]>,
}

impl<'a> PartialEq<&'a str> for Path {
Expand All @@ -90,7 +91,7 @@ impl Path {
// one-segment path.
pub fn from_ident(ident: Ident) -> Path {
Path {
segments: vec![PathSegment::from_ident(ident)],
segments: smallvec![PathSegment::from_ident(ident)],
span: ident.span,
}
}
Expand Down Expand Up @@ -887,7 +888,7 @@ pub struct Expr {

// `Expr` is used a lot. Make sure it doesn't unintentionally get bigger.
#[cfg(target_arch = "x86_64")]
static_assert!(MEM_SIZE_OF_EXPR: std::mem::size_of::<Expr>() == 88);
static_assert!(MEM_SIZE_OF_EXPR: std::mem::size_of::<Expr>() == 96);

impl Expr {
/// Whether this expression would be valid somewhere that expects a value; for example, an `if`
Expand Down
4 changes: 3 additions & 1 deletion src/libsyntax/attr/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ use parse::parser::Parser;
use parse::{self, ParseSess, PResult};
use parse::token::{self, Token};
use ptr::P;
use smallvec::{SmallVec, smallvec};
use symbol::Symbol;
use ThinVec;
use tokenstream::{TokenStream, TokenTree, DelimSpan};
Expand Down Expand Up @@ -483,7 +484,8 @@ impl MetaItem {
let ident = match tokens.next() {
Some(TokenTree::Token(span, Token::Ident(ident, _))) => {
if let Some(TokenTree::Token(_, Token::ModSep)) = tokens.peek() {
let mut segments = vec![PathSegment::from_ident(ident.with_span_pos(span))];
let mut segments: SmallVec<[_; 1]> =
smallvec![PathSegment::from_ident(ident.with_span_pos(span))];
tokens.next();
loop {
if let Some(TokenTree::Token(span,
Expand Down
3 changes: 2 additions & 1 deletion src/libsyntax/ext/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use syntax_pos::{Pos, Span, DUMMY_SP};
use source_map::{dummy_spanned, respan, Spanned};
use ext::base::ExtCtxt;
use ptr::P;
use smallvec::SmallVec;
use symbol::{Symbol, keywords};
use ThinVec;

Expand Down Expand Up @@ -310,7 +311,7 @@ impl<'a> AstBuilder for ExtCtxt<'a> {
-> ast::Path {
assert!(!idents.is_empty());
let add_root = global && !idents[0].is_path_segment_keyword();
let mut segments = Vec::with_capacity(idents.len() + add_root as usize);
let mut segments = SmallVec::with_capacity(idents.len() + add_root as usize);
if add_root {
segments.push(ast::PathSegment::path_root(span));
}
Expand Down
2 changes: 1 addition & 1 deletion src/libsyntax/ext/placeholders.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use rustc_data_structures::fx::FxHashMap;
pub fn placeholder(kind: AstFragmentKind, id: ast::NodeId) -> AstFragment {
fn mac_placeholder() -> ast::Mac {
dummy_spanned(ast::Mac_ {
path: ast::Path { span: DUMMY_SP, segments: Vec::new() },
path: ast::Path { span: DUMMY_SP, segments: smallvec![] },
tts: TokenStream::empty().into(),
delim: ast::MacDelimiter::Brace,
})
Expand Down
11 changes: 6 additions & 5 deletions src/libsyntax/parse/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ use ptr::P;
use parse::PResult;
use ThinVec;
use tokenstream::{self, DelimSpan, ThinTokenStream, TokenTree, TokenStream};
use smallvec::SmallVec;
use symbol::{Symbol, keywords};

use std::borrow::Cow;
Expand Down Expand Up @@ -1738,7 +1739,7 @@ impl<'a> Parser<'a> {
};

self.bump(); // `::`
let mut segments = Vec::new();
let mut segments = smallvec![];
self.parse_path_segments(&mut segments, T::PATH_STYLE, true)?;

let span = ty.span.to(self.prev_span);
Expand Down Expand Up @@ -2075,7 +2076,7 @@ impl<'a> Parser<'a> {
path = self.parse_path(PathStyle::Type)?;
path_span = path_lo.to(self.prev_span);
} else {
path = ast::Path { segments: Vec::new(), span: syntax_pos::DUMMY_SP };
path = ast::Path { segments: smallvec![], span: syntax_pos::DUMMY_SP };
path_span = self.span.to(self.span);
}

Expand Down Expand Up @@ -2113,7 +2114,7 @@ impl<'a> Parser<'a> {
});

let lo = self.meta_var_span.unwrap_or(self.span);
let mut segments = Vec::new();
let mut segments = smallvec![];
let mod_sep_ctxt = self.span.ctxt();
if self.eat(&token::ModSep) {
segments.push(PathSegment::path_root(lo.shrink_to_lo().with_ctxt(mod_sep_ctxt)));
Expand Down Expand Up @@ -2144,7 +2145,7 @@ impl<'a> Parser<'a> {
}

fn parse_path_segments(&mut self,
segments: &mut Vec<PathSegment>,
segments: &mut SmallVec<[PathSegment; 1]>,
style: PathStyle,
enable_warning: bool)
-> PResult<'a, ()> {
Expand Down Expand Up @@ -7822,7 +7823,7 @@ impl<'a> Parser<'a> {
fn parse_use_tree(&mut self) -> PResult<'a, UseTree> {
let lo = self.span;

let mut prefix = ast::Path { segments: Vec::new(), span: lo.shrink_to_lo() };
let mut prefix = ast::Path { segments: smallvec![], span: lo.shrink_to_lo() };
let kind = if self.check(&token::OpenDelim(token::Brace)) ||
self.check(&token::BinOp(token::Star)) ||
self.is_import_coupler() {
Expand Down

0 comments on commit 7e5b9c3

Please sign in to comment.