Skip to content

Commit 7e5b9c3

Browse files
committed
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

Lines changed: 2 additions & 0 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 2 additions & 0 deletions
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

Lines changed: 8 additions & 4 deletions
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

Lines changed: 1 addition & 0 deletions
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

Lines changed: 2 additions & 1 deletion
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

Lines changed: 1 addition & 0 deletions
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

Lines changed: 1 addition & 0 deletions
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

Lines changed: 2 additions & 1 deletion
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

Lines changed: 4 additions & 3 deletions
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`

0 commit comments

Comments
 (0)