Skip to content

Commit 9833b71

Browse files
committed
Replace rustc_data_structures::thin_vec::ThinVec with thin_vec::ThinVec.
`rustc_data_structures::thin_vec::ThinVec` looks like this: ``` pub struct ThinVec<T>(Option<Box<Vec<T>>>); ``` It's just a zero word if the vector is empty, but requires two allocations if it is non-empty. So it's only usable in cases where the vector is empty most of the time. This commit removes it in favour of `thin_vec::ThinVec`, which is also word-sized, but stores the length and capacity in the same allocation as the elements. It's good in a wider variety of situation, e.g. in enum variants where the vector is usually/always non-empty. The commit also: - Sorts some `Cargo.toml` dependency lists, to make additions easier. - Sorts some `use` item lists, to make additions easier. - Changes `clean_trait_ref_with_bindings` to take a `ThinVec<TypeBinding>` rather than a `&[TypeBinding]`, because this avoid some unnecessary allocations.
1 parent a1bea15 commit 9833b71

File tree

42 files changed

+163
-355
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+163
-355
lines changed

Cargo.lock

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3561,6 +3561,7 @@ dependencies = [
35613561
"rustc_serialize",
35623562
"rustc_span",
35633563
"smallvec",
3564+
"thin-vec",
35643565
"tracing",
35653566
]
35663567

@@ -3581,6 +3582,7 @@ dependencies = [
35813582
"rustc_span",
35823583
"rustc_target",
35833584
"smallvec",
3585+
"thin-vec",
35843586
"tracing",
35853587
]
35863588

@@ -3675,6 +3677,7 @@ dependencies = [
36753677
"rustc_span",
36763678
"rustc_target",
36773679
"smallvec",
3680+
"thin-vec",
36783681
"tracing",
36793682
]
36803683

@@ -3799,6 +3802,7 @@ dependencies = [
37993802
"stable_deref_trait",
38003803
"stacker",
38013804
"tempfile",
3805+
"thin-vec",
38023806
"tracing",
38033807
"winapi",
38043808
]
@@ -4179,6 +4183,7 @@ dependencies = [
41794183
"rustc_target",
41804184
"rustc_type_ir",
41814185
"smallvec",
4186+
"thin-vec",
41824187
"tracing",
41834188
]
41844189

@@ -4363,6 +4368,7 @@ dependencies = [
43634368
"rustc_session",
43644369
"rustc_span",
43654370
"rustc_target",
4371+
"thin-vec",
43664372
"tracing",
43674373
]
43684374

@@ -4385,6 +4391,7 @@ dependencies = [
43854391
"rustc_span",
43864392
"rustc_target",
43874393
"smallvec",
4394+
"thin-vec",
43884395
"tracing",
43894396
]
43904397

@@ -4438,6 +4445,7 @@ dependencies = [
44384445
"indexmap",
44394446
"rustc_macros",
44404447
"smallvec",
4448+
"thin-vec",
44414449
]
44424450

44434451
[[package]]
@@ -4677,6 +4685,7 @@ dependencies = [
46774685
"serde_json",
46784686
"smallvec",
46794687
"tempfile",
4688+
"thin-vec",
46804689
"tracing",
46814690
"tracing-subscriber",
46824691
"tracing-tree",
@@ -5250,6 +5259,12 @@ version = "0.15.0"
52505259
source = "registry+https://github.com/rust-lang/crates.io-index"
52515260
checksum = "b1141d4d61095b28419e22cb0bbf02755f5e54e0526f97f1e3d1d160e60885fb"
52525261

5262+
[[package]]
5263+
name = "thin-vec"
5264+
version = "0.2.8"
5265+
source = "registry+https://github.com/rust-lang/crates.io-index"
5266+
checksum = "104c2cb3180b6fb6d5b2278768e9b88b578d32ba751ea6e8d026688a40d7ed87"
5267+
52535268
[[package]]
52545269
name = "thiserror"
52555270
version = "1.0.30"

compiler/rustc_ast/Cargo.toml

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,13 @@ edition = "2021"
77
doctest = false
88

99
[dependencies]
10-
rustc_serialize = { path = "../rustc_serialize" }
11-
tracing = "0.1"
12-
rustc_span = { path = "../rustc_span" }
10+
bitflags = "1.2.1"
1311
rustc_data_structures = { path = "../rustc_data_structures" }
1412
rustc_index = { path = "../rustc_index" }
1513
rustc_lexer = { path = "../rustc_lexer" }
1614
rustc_macros = { path = "../rustc_macros" }
15+
rustc_serialize = { path = "../rustc_serialize" }
16+
rustc_span = { path = "../rustc_span" }
1717
smallvec = { version = "1.8.1", features = ["union", "may_dangle"] }
18-
bitflags = "1.2.1"
18+
thin-vec = "0.2.8"
19+
tracing = "0.1"

compiler/rustc_ast/src/ast.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,21 +25,19 @@ pub use UnsafeSource::*;
2525
use crate::ptr::P;
2626
use crate::token::{self, CommentKind, Delimiter};
2727
use crate::tokenstream::{DelimSpan, LazyTokenStream, TokenStream};
28-
2928
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
3029
use rustc_data_structures::stack::ensure_sufficient_stack;
3130
use rustc_data_structures::sync::Lrc;
32-
use rustc_data_structures::thin_vec::ThinVec;
3331
use rustc_macros::HashStable_Generic;
3432
use rustc_serialize::{Decodable, Decoder, Encodable, Encoder};
3533
use rustc_span::source_map::{respan, Spanned};
3634
use rustc_span::symbol::{kw, sym, Ident, Symbol};
3735
use rustc_span::{Span, DUMMY_SP};
38-
3936
use std::cmp::Ordering;
4037
use std::convert::TryFrom;
4138
use std::fmt;
4239
use std::mem;
40+
use thin_vec::ThinVec;
4341

4442
/// A "Label" is an identifier of some point in sources,
4543
/// e.g. in the following code:

compiler/rustc_ast/src/tokenstream.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -426,7 +426,8 @@ impl TokenStream {
426426
let attr_annotated = if attrs.is_empty() {
427427
tokens.create_token_stream()
428428
} else {
429-
let attr_data = AttributesData { attrs: attrs.to_vec().into(), tokens: tokens.clone() };
429+
let attr_data =
430+
AttributesData { attrs: attrs.iter().cloned().collect(), tokens: tokens.clone() };
430431
AttrAnnotatedTokenStream::new(vec![(
431432
AttrAnnotatedTokenTree::Attributes(attr_data),
432433
Spacing::Alone,

compiler/rustc_ast_lowering/Cargo.toml

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,17 @@ doctest = false
88

99
[dependencies]
1010
rustc_arena = { path = "../rustc_arena" }
11-
tracing = "0.1"
11+
rustc_ast = { path = "../rustc_ast" }
1212
rustc_ast_pretty = { path = "../rustc_ast_pretty" }
13-
rustc_hir = { path = "../rustc_hir" }
14-
rustc_target = { path = "../rustc_target" }
1513
rustc_data_structures = { path = "../rustc_data_structures" }
14+
rustc_errors = { path = "../rustc_errors" }
15+
rustc_hir = { path = "../rustc_hir" }
1616
rustc_index = { path = "../rustc_index" }
1717
rustc_middle = { path = "../rustc_middle" }
1818
rustc_query_system = { path = "../rustc_query_system" }
19-
rustc_span = { path = "../rustc_span" }
20-
rustc_errors = { path = "../rustc_errors" }
2119
rustc_session = { path = "../rustc_session" }
22-
rustc_ast = { path = "../rustc_ast" }
20+
rustc_span = { path = "../rustc_span" }
21+
rustc_target = { path = "../rustc_target" }
2322
smallvec = { version = "1.8.1", features = ["union", "may_dangle"] }
23+
thin-vec = "0.2.8"
24+
tracing = "0.1"

compiler/rustc_ast_lowering/src/expr.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use super::ResolverAstLoweringExt;
22
use super::{ImplTraitContext, LoweringContext, ParamMode, ParenthesizedGenericArgs};
33
use crate::{FnDeclKind, ImplTraitPosition};
4-
54
use rustc_ast::attr;
65
use rustc_ast::ptr::P as AstP;
76
use rustc_ast::*;
@@ -13,6 +12,7 @@ use rustc_hir::definitions::DefPathData;
1312
use rustc_span::source_map::{respan, DesugaringKind, Span, Spanned};
1413
use rustc_span::symbol::{sym, Ident};
1514
use rustc_span::DUMMY_SP;
15+
use thin_vec::thin_vec;
1616

1717
impl<'hir> LoweringContext<'_, 'hir> {
1818
fn lower_exprs(&mut self, exprs: &[AstP<Expr>]) -> &'hir [hir::Expr<'hir>] {
@@ -1595,7 +1595,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
15951595
};
15961596
attr::mk_attr_outer(allow)
15971597
};
1598-
let attrs: AttrVec = vec![attr].into();
1598+
let attrs: AttrVec = thin_vec![attr];
15991599

16001600
// `ControlFlow::Continue(val) => #[allow(unreachable_code)] val,`
16011601
let continue_arm = {

compiler/rustc_builtin_macros/Cargo.toml

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,21 @@ edition = "2021"
77
doctest = false
88

99
[dependencies]
10-
rustc_parse_format = { path = "../rustc_parse_format" }
11-
tracing = "0.1"
10+
rustc_ast = { path = "../rustc_ast" }
1211
rustc_ast_pretty = { path = "../rustc_ast_pretty" }
1312
rustc_attr = { path = "../rustc_attr" }
1413
rustc_data_structures = { path = "../rustc_data_structures" }
1514
rustc_errors = { path = "../rustc_errors" }
15+
rustc_expand = { path = "../rustc_expand" }
1616
rustc_feature = { path = "../rustc_feature" }
1717
rustc_lexer = { path = "../rustc_lexer" }
1818
rustc_lint_defs = { path = "../rustc_lint_defs" }
1919
rustc_macros = { path = "../rustc_macros" }
20+
rustc_parse_format = { path = "../rustc_parse_format" }
2021
rustc_parse = { path = "../rustc_parse" }
21-
rustc_target = { path = "../rustc_target" }
2222
rustc_session = { path = "../rustc_session" }
23-
smallvec = { version = "1.8.1", features = ["union", "may_dangle"] }
24-
rustc_ast = { path = "../rustc_ast" }
25-
rustc_expand = { path = "../rustc_expand" }
2623
rustc_span = { path = "../rustc_span" }
24+
rustc_target = { path = "../rustc_target" }
25+
smallvec = { version = "1.8.1", features = ["union", "may_dangle"] }
26+
thin-vec = "0.2.8"
27+
tracing = "0.1"

compiler/rustc_builtin_macros/src/assert/context.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ use rustc_span::{
1313
symbol::{sym, Ident, Symbol},
1414
Span,
1515
};
16+
use thin_vec::thin_vec;
1617

1718
pub(super) struct Context<'cx, 'a> {
1819
// An optimization.
@@ -116,11 +117,10 @@ impl<'cx, 'a> Context<'cx, 'a> {
116117
self.cx.item(
117118
self.span,
118119
Ident::empty(),
119-
vec![self.cx.attribute(attr::mk_list_item(
120+
thin_vec![self.cx.attribute(attr::mk_list_item(
120121
Ident::new(sym::allow, self.span),
121122
vec![attr::mk_nested_word_item(Ident::new(sym::unused_imports, self.span))],
122-
))]
123-
.into(),
123+
))],
124124
ItemKind::Use(UseTree {
125125
prefix: self.cx.path(self.span, self.cx.std_path(&[sym::asserting])),
126126
kind: UseTreeKind::Nested(vec![

compiler/rustc_builtin_macros/src/deriving/clone.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
use crate::deriving::generic::ty::*;
22
use crate::deriving::generic::*;
33
use crate::deriving::path_std;
4-
54
use rustc_ast::{self as ast, Generics, ItemKind, MetaItem, VariantData};
65
use rustc_data_structures::fx::FxHashSet;
76
use rustc_expand::base::{Annotatable, ExtCtxt};
87
use rustc_span::symbol::{kw, sym, Ident};
98
use rustc_span::Span;
9+
use thin_vec::thin_vec;
1010

1111
pub fn expand_deriving_clone(
1212
cx: &mut ExtCtxt<'_>,
@@ -68,7 +68,7 @@ pub fn expand_deriving_clone(
6868
}
6969

7070
let inline = cx.meta_word(span, sym::inline);
71-
let attrs = vec![cx.attribute(inline)].into();
71+
let attrs = thin_vec![cx.attribute(inline)];
7272
let trait_def = TraitDef {
7373
span,
7474
path: path_std!(clone::Clone),

compiler/rustc_builtin_macros/src/deriving/cmp/eq.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use rustc_data_structures::fx::FxHashSet;
77
use rustc_expand::base::{Annotatable, ExtCtxt};
88
use rustc_span::symbol::{sym, Ident};
99
use rustc_span::Span;
10+
use thin_vec::thin_vec;
1011

1112
pub fn expand_deriving_eq(
1213
cx: &mut ExtCtxt<'_>,
@@ -20,7 +21,7 @@ pub fn expand_deriving_eq(
2021
let hidden = rustc_ast::attr::mk_nested_word_item(Ident::new(sym::hidden, span));
2122
let doc = rustc_ast::attr::mk_list_item(Ident::new(sym::doc, span), vec![hidden]);
2223
let no_coverage = cx.meta_word(span, sym::no_coverage);
23-
let attrs = vec![cx.attribute(inline), cx.attribute(doc), cx.attribute(no_coverage)].into();
24+
let attrs = thin_vec![cx.attribute(inline), cx.attribute(doc), cx.attribute(no_coverage)];
2425
let trait_def = TraitDef {
2526
span,
2627
path: path_std!(cmp::Eq),

compiler/rustc_builtin_macros/src/deriving/cmp/ord.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
use crate::deriving::generic::ty::*;
22
use crate::deriving::generic::*;
33
use crate::deriving::path_std;
4-
54
use rustc_ast::MetaItem;
65
use rustc_expand::base::{Annotatable, ExtCtxt};
76
use rustc_span::symbol::{sym, Ident};
87
use rustc_span::Span;
8+
use thin_vec::thin_vec;
99

1010
pub fn expand_deriving_ord(
1111
cx: &mut ExtCtxt<'_>,
@@ -15,7 +15,7 @@ pub fn expand_deriving_ord(
1515
push: &mut dyn FnMut(Annotatable),
1616
) {
1717
let inline = cx.meta_word(span, sym::inline);
18-
let attrs = vec![cx.attribute(inline)].into();
18+
let attrs = thin_vec![cx.attribute(inline)];
1919
let trait_def = TraitDef {
2020
span,
2121
path: path_std!(cmp::Ord),

compiler/rustc_builtin_macros/src/deriving/cmp/partial_eq.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
use crate::deriving::generic::ty::*;
22
use crate::deriving::generic::*;
33
use crate::deriving::{path_local, path_std};
4-
54
use rustc_ast::ptr::P;
65
use rustc_ast::{BinOpKind, BorrowKind, Expr, ExprKind, MetaItem, Mutability};
76
use rustc_expand::base::{Annotatable, ExtCtxt};
87
use rustc_span::symbol::sym;
98
use rustc_span::Span;
9+
use thin_vec::thin_vec;
1010

1111
pub fn expand_deriving_partial_eq(
1212
cx: &mut ExtCtxt<'_>,
@@ -68,7 +68,7 @@ pub fn expand_deriving_partial_eq(
6868
// No need to generate `ne`, the default suffices, and not generating it is
6969
// faster.
7070
let inline = cx.meta_word(span, sym::inline);
71-
let attrs = vec![cx.attribute(inline)].into();
71+
let attrs = thin_vec![cx.attribute(inline)];
7272
let methods = vec![MethodDef {
7373
name: sym::eq,
7474
generics: Bounds::empty(),

compiler/rustc_builtin_macros/src/deriving/cmp/partial_ord.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
use crate::deriving::generic::ty::*;
22
use crate::deriving::generic::*;
33
use crate::deriving::{path_std, pathvec_std};
4-
54
use rustc_ast::MetaItem;
65
use rustc_expand::base::{Annotatable, ExtCtxt};
76
use rustc_span::symbol::{sym, Ident};
87
use rustc_span::Span;
8+
use thin_vec::thin_vec;
99

1010
pub fn expand_deriving_partial_ord(
1111
cx: &mut ExtCtxt<'_>,
@@ -19,7 +19,7 @@ pub fn expand_deriving_partial_ord(
1919
Path(Path::new_(pathvec_std!(option::Option), vec![Box::new(ordering_ty)], PathKind::Std));
2020

2121
let inline = cx.meta_word(span, sym::inline);
22-
let attrs = vec![cx.attribute(inline)].into();
22+
let attrs = thin_vec![cx.attribute(inline)];
2323

2424
let partial_cmp_def = MethodDef {
2525
name: sym::partial_cmp,

compiler/rustc_builtin_macros/src/deriving/default.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use crate::deriving::generic::ty::*;
22
use crate::deriving::generic::*;
3-
43
use rustc_ast as ast;
54
use rustc_ast::{walk_list, EnumDef, VariantData};
65
use rustc_errors::Applicability;
@@ -9,6 +8,7 @@ use rustc_span::symbol::Ident;
98
use rustc_span::symbol::{kw, sym};
109
use rustc_span::Span;
1110
use smallvec::SmallVec;
11+
use thin_vec::thin_vec;
1212

1313
pub fn expand_deriving_default(
1414
cx: &mut ExtCtxt<'_>,
@@ -20,7 +20,7 @@ pub fn expand_deriving_default(
2020
item.visit_with(&mut DetectNonVariantDefaultAttr { cx });
2121

2222
let inline = cx.meta_word(span, sym::inline);
23-
let attrs = vec![cx.attribute(inline)].into();
23+
let attrs = thin_vec![cx.attribute(inline)];
2424
let trait_def = TraitDef {
2525
span,
2626
path: Path::new(vec![kw::Default, sym::Default]),

0 commit comments

Comments
 (0)