Skip to content

Commit 7e5b9c3

Browse files
committedJan 16, 2019
Use SmallVec in ast::Path.
This is up to a 1% speedup on a few benchmark runs.
1 parent e2f221c commit 7e5b9c3

File tree

14 files changed

+36
-18
lines changed

14 files changed

+36
-18
lines changed
 

‎Cargo.lock

+2
Original file line numberDiff line numberDiff line change
@@ -2601,6 +2601,7 @@ dependencies = [
26012601
"rustc_data_structures 0.0.0",
26022602
"rustc_errors 0.0.0",
26032603
"rustc_metadata 0.0.0",
2604+
"smallvec 0.6.7 (registry+https://github.com/rust-lang/crates.io-index)",
26042605
"syntax 0.0.0",
26052606
"syntax_pos 0.0.0",
26062607
]
@@ -2618,6 +2619,7 @@ dependencies = [
26182619
"rustc_data_structures 0.0.0",
26192620
"rustc_target 0.0.0",
26202621
"rustc_typeck 0.0.0",
2622+
"smallvec 0.6.7 (registry+https://github.com/rust-lang/crates.io-index)",
26212623
"syntax 0.0.0",
26222624
"syntax_pos 0.0.0",
26232625
]

‎src/librustc/hir/lowering.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2774,7 +2774,7 @@ impl<'a> LoweringContext<'a> {
27742774
ItemKind::Use(ref use_tree) => {
27752775
// Start with an empty prefix
27762776
let prefix = Path {
2777-
segments: vec![],
2777+
segments: smallvec![],
27782778
span: use_tree.span,
27792779
};
27802780

‎src/librustc_resolve/Cargo.toml

+2
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,5 @@ rustc_errors = { path = "../librustc_errors" }
1919
syntax_pos = { path = "../libsyntax_pos" }
2020
rustc_data_structures = { path = "../librustc_data_structures" }
2121
rustc_metadata = { path = "../librustc_metadata" }
22+
smallvec = { version = "0.6.7", features = ["union", "may_dangle"] }
23+

‎src/librustc_resolve/lib.rs

+8-4
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
extern crate bitflags;
1515
#[macro_use]
1616
extern crate log;
17+
extern crate smallvec;
1718
#[macro_use]
1819
extern crate syntax;
1920
extern crate syntax_pos;
@@ -66,6 +67,7 @@ use syntax::ptr::P;
6667
use syntax_pos::{Span, DUMMY_SP, MultiSpan};
6768
use errors::{Applicability, DiagnosticBuilder, DiagnosticId};
6869

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

16821684

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

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

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

52165219
let path_len = suggestion.path.segments.len();
5220+
let segments = suggestion.path.segments[0..path_len - 1].iter().cloned().collect();
52175221
let enum_path = ast::Path {
52185222
span: suggestion.path.span,
5219-
segments: suggestion.path.segments[0..path_len - 1].to_vec(),
5223+
segments,
52205224
};
52215225
let enum_path_string = path_names_to_string(&enum_path);
52225226

‎src/librustc_save_analysis/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ rustc_data_structures = { path = "../librustc_data_structures" }
1515
rustc_codegen_utils = { path = "../librustc_codegen_utils" }
1616
rustc_target = { path = "../librustc_target" }
1717
rustc_typeck = { path = "../librustc_typeck" }
18+
smallvec = { version = "0.6.7", features = ["union", "may_dangle"] }
1819
syntax = { path = "../libsyntax" }
1920
syntax_pos = { path = "../libsyntax_pos" }
2021
rls-data = "0.18.1"

‎src/librustc_save_analysis/dump_visitor.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ use syntax_pos::*;
3636

3737
use {escape, generated_code, lower_attributes, PathCollector, SaveContext};
3838
use json_dumper::{Access, DumpOutput, JsonDumper};
39+
use smallvec::smallvec;
3940
use span_utils::SpanUtils;
4041
use sig;
4142

@@ -1338,7 +1339,7 @@ impl<'l, 'tcx: 'l, 'll, O: DumpOutput + 'll> Visitor<'l> for DumpVisitor<'l, 'tc
13381339
match item.node {
13391340
Use(ref use_tree) => {
13401341
let prefix = ast::Path {
1341-
segments: vec![],
1342+
segments: smallvec![],
13421343
span: DUMMY_SP,
13431344
};
13441345
self.process_use_tree(use_tree, item.id, item, &prefix);

‎src/librustc_save_analysis/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ extern crate rustc_codegen_utils;
1717
extern crate rustc_serialize;
1818
extern crate rustc_target;
1919
extern crate rustc_typeck;
20+
extern crate smallvec;
2021
#[macro_use]
2122
extern crate syntax;
2223
extern crate syntax_pos;

‎src/librustdoc/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ extern crate rustc_metadata;
3232
extern crate rustc_target;
3333
extern crate rustc_typeck;
3434
extern crate serialize;
35+
extern crate smallvec;
3536
extern crate syntax;
3637
extern crate syntax_pos;
3738
extern crate test as testing;

‎src/librustdoc/passes/collect_intra_doc_links.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use rustc::lint as lint;
22
use rustc::hir;
33
use rustc::hir::def::Def;
44
use rustc::ty;
5+
use smallvec::smallvec;
56
use syntax;
67
use syntax::ast::{self, Ident, NodeId};
78
use syntax::feature_gate::UnstableFeatures;
@@ -425,7 +426,7 @@ impl<'a, 'tcx, 'rcx> DocFolder for LinkCollector<'a, 'tcx, 'rcx> {
425426
fn macro_resolve(cx: &DocContext, path_str: &str) -> Option<Def> {
426427
use syntax::ext::base::{MacroKind, SyntaxExtension};
427428
let segment = ast::PathSegment::from_ident(Ident::from_str(path_str));
428-
let path = ast::Path { segments: vec![segment], span: DUMMY_SP };
429+
let path = ast::Path { segments: smallvec![segment], span: DUMMY_SP };
429430
let mut resolver = cx.resolver.borrow_mut();
430431
let parent_scope = resolver.dummy_parent_scope();
431432
if let Ok(def) = resolver.resolve_macro_to_def_inner(&path, MacroKind::Bang,

‎src/libsyntax/ast.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ use ThinVec;
2121
use rustc_data_structures::fx::FxHashSet;
2222
use rustc_data_structures::sync::Lrc;
2323
use serialize::{self, Decoder, Encoder};
24+
use smallvec::SmallVec;
2425
use std::fmt;
2526

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

7071
impl<'a> PartialEq<&'a str> for Path {
@@ -90,7 +91,7 @@ impl Path {
9091
// one-segment path.
9192
pub fn from_ident(ident: Ident) -> Path {
9293
Path {
93-
segments: vec![PathSegment::from_ident(ident)],
94+
segments: smallvec![PathSegment::from_ident(ident)],
9495
span: ident.span,
9596
}
9697
}
@@ -887,7 +888,7 @@ pub struct Expr {
887888

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

892893
impl Expr {
893894
/// Whether this expression would be valid somewhere that expects a value; for example, an `if`

‎src/libsyntax/attr/mod.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ use parse::parser::Parser;
2222
use parse::{self, ParseSess, PResult};
2323
use parse::token::{self, Token};
2424
use ptr::P;
25+
use smallvec::{SmallVec, smallvec};
2526
use symbol::Symbol;
2627
use ThinVec;
2728
use tokenstream::{TokenStream, TokenTree, DelimSpan};
@@ -483,7 +484,8 @@ impl MetaItem {
483484
let ident = match tokens.next() {
484485
Some(TokenTree::Token(span, Token::Ident(ident, _))) => {
485486
if let Some(TokenTree::Token(_, Token::ModSep)) = tokens.peek() {
486-
let mut segments = vec![PathSegment::from_ident(ident.with_span_pos(span))];
487+
let mut segments: SmallVec<[_; 1]> =
488+
smallvec![PathSegment::from_ident(ident.with_span_pos(span))];
487489
tokens.next();
488490
loop {
489491
if let Some(TokenTree::Token(span,

‎src/libsyntax/ext/build.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use syntax_pos::{Pos, Span, DUMMY_SP};
55
use source_map::{dummy_spanned, respan, Spanned};
66
use ext::base::ExtCtxt;
77
use ptr::P;
8+
use smallvec::SmallVec;
89
use symbol::{Symbol, keywords};
910
use ThinVec;
1011

@@ -310,7 +311,7 @@ impl<'a> AstBuilder for ExtCtxt<'a> {
310311
-> ast::Path {
311312
assert!(!idents.is_empty());
312313
let add_root = global && !idents[0].is_path_segment_keyword();
313-
let mut segments = Vec::with_capacity(idents.len() + add_root as usize);
314+
let mut segments = SmallVec::with_capacity(idents.len() + add_root as usize);
314315
if add_root {
315316
segments.push(ast::PathSegment::path_root(span));
316317
}

‎src/libsyntax/ext/placeholders.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use rustc_data_structures::fx::FxHashMap;
1616
pub fn placeholder(kind: AstFragmentKind, id: ast::NodeId) -> AstFragment {
1717
fn mac_placeholder() -> ast::Mac {
1818
dummy_spanned(ast::Mac_ {
19-
path: ast::Path { span: DUMMY_SP, segments: Vec::new() },
19+
path: ast::Path { span: DUMMY_SP, segments: smallvec![] },
2020
tts: TokenStream::empty().into(),
2121
delim: ast::MacDelimiter::Brace,
2222
})

‎src/libsyntax/parse/parser.rs

+6-5
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ use ptr::P;
4747
use parse::PResult;
4848
use ThinVec;
4949
use tokenstream::{self, DelimSpan, ThinTokenStream, TokenTree, TokenStream};
50+
use smallvec::SmallVec;
5051
use symbol::{Symbol, keywords};
5152

5253
use std::borrow::Cow;
@@ -1738,7 +1739,7 @@ impl<'a> Parser<'a> {
17381739
};
17391740

17401741
self.bump(); // `::`
1741-
let mut segments = Vec::new();
1742+
let mut segments = smallvec![];
17421743
self.parse_path_segments(&mut segments, T::PATH_STYLE, true)?;
17431744

17441745
let span = ty.span.to(self.prev_span);
@@ -2075,7 +2076,7 @@ impl<'a> Parser<'a> {
20752076
path = self.parse_path(PathStyle::Type)?;
20762077
path_span = path_lo.to(self.prev_span);
20772078
} else {
2078-
path = ast::Path { segments: Vec::new(), span: syntax_pos::DUMMY_SP };
2079+
path = ast::Path { segments: smallvec![], span: syntax_pos::DUMMY_SP };
20792080
path_span = self.span.to(self.span);
20802081
}
20812082

@@ -2113,7 +2114,7 @@ impl<'a> Parser<'a> {
21132114
});
21142115

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

21462147
fn parse_path_segments(&mut self,
2147-
segments: &mut Vec<PathSegment>,
2148+
segments: &mut SmallVec<[PathSegment; 1]>,
21482149
style: PathStyle,
21492150
enable_warning: bool)
21502151
-> PResult<'a, ()> {
@@ -7822,7 +7823,7 @@ impl<'a> Parser<'a> {
78227823
fn parse_use_tree(&mut self) -> PResult<'a, UseTree> {
78237824
let lo = self.span;
78247825

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

0 commit comments

Comments
 (0)