Skip to content

Commit eac6c33

Browse files
committed
Auto merge of #100869 - nnethercote:replace-ThinVec, r=spastorino
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. r? `@spastorino`
2 parents b32223f + 78f83f0 commit eac6c33

Some content is hidden

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

44 files changed

+176
-362
lines changed

Cargo.lock

+15
Original file line numberDiff line numberDiff line change
@@ -3208,6 +3208,7 @@ dependencies = [
32083208
"rustc_serialize",
32093209
"rustc_span",
32103210
"smallvec",
3211+
"thin-vec",
32113212
"tracing",
32123213
]
32133214

@@ -3229,6 +3230,7 @@ dependencies = [
32293230
"rustc_span",
32303231
"rustc_target",
32313232
"smallvec",
3233+
"thin-vec",
32323234
"tracing",
32333235
]
32343236

@@ -3323,6 +3325,7 @@ dependencies = [
33233325
"rustc_span",
33243326
"rustc_target",
33253327
"smallvec",
3328+
"thin-vec",
33263329
"tracing",
33273330
]
33283331

@@ -3447,6 +3450,7 @@ dependencies = [
34473450
"stable_deref_trait",
34483451
"stacker",
34493452
"tempfile",
3453+
"thin-vec",
34503454
"tracing",
34513455
"winapi",
34523456
]
@@ -3829,6 +3833,7 @@ dependencies = [
38293833
"rustc_target",
38303834
"rustc_type_ir",
38313835
"smallvec",
3836+
"thin-vec",
38323837
"tracing",
38333838
]
38343839

@@ -4018,6 +4023,7 @@ dependencies = [
40184023
"rustc_session",
40194024
"rustc_span",
40204025
"rustc_target",
4026+
"thin-vec",
40214027
"tracing",
40224028
]
40234029

@@ -4040,6 +4046,7 @@ dependencies = [
40404046
"rustc_span",
40414047
"rustc_target",
40424048
"smallvec",
4049+
"thin-vec",
40434050
"tracing",
40444051
]
40454052

@@ -4095,6 +4102,7 @@ dependencies = [
40954102
"indexmap",
40964103
"rustc_macros",
40974104
"smallvec",
4105+
"thin-vec",
40984106
]
40994107

41004108
[[package]]
@@ -4331,6 +4339,7 @@ dependencies = [
43314339
"serde_json",
43324340
"smallvec",
43334341
"tempfile",
4342+
"thin-vec",
43344343
"tracing",
43354344
"tracing-subscriber",
43364345
"tracing-tree",
@@ -4884,6 +4893,12 @@ version = "0.15.0"
48844893
source = "registry+https://github.com/rust-lang/crates.io-index"
48854894
checksum = "b1141d4d61095b28419e22cb0bbf02755f5e54e0526f97f1e3d1d160e60885fb"
48864895

4896+
[[package]]
4897+
name = "thin-vec"
4898+
version = "0.2.8"
4899+
source = "registry+https://github.com/rust-lang/crates.io-index"
4900+
checksum = "104c2cb3180b6fb6d5b2278768e9b88b578d32ba751ea6e8d026688a40d7ed87"
4901+
48874902
[[package]]
48884903
name = "thiserror"
48894904
version = "1.0.30"

compiler/rustc_ast/Cargo.toml

+5-4
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

+1-3
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/ast_traits.rs

+1
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,7 @@ macro_rules! impl_has_attrs {
279279
impl HasAttrs for $T {
280280
const SUPPORTS_CUSTOM_INNER_ATTRS: bool = $inner;
281281

282+
#[inline]
282283
fn attrs(&self) -> &[Attribute] {
283284
&self.attrs
284285
}

compiler/rustc_ast/src/tokenstream.rs

+2-1
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

+7-6
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,18 @@ 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_macros = { path = "../rustc_macros" }
1919
rustc_query_system = { path = "../rustc_query_system" }
20-
rustc_span = { path = "../rustc_span" }
21-
rustc_errors = { path = "../rustc_errors" }
2220
rustc_session = { path = "../rustc_session" }
23-
rustc_ast = { path = "../rustc_ast" }
21+
rustc_span = { path = "../rustc_span" }
22+
rustc_target = { path = "../rustc_target" }
2423
smallvec = { version = "1.8.1", features = ["union", "may_dangle"] }
24+
thin-vec = "0.2.8"
25+
tracing = "0.1"

compiler/rustc_ast_lowering/src/expr.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ use super::errors::{
77
use super::ResolverAstLoweringExt;
88
use super::{ImplTraitContext, LoweringContext, ParamMode, ParenthesizedGenericArgs};
99
use crate::{FnDeclKind, ImplTraitPosition};
10-
1110
use rustc_ast::attr;
1211
use rustc_ast::ptr::P as AstP;
1312
use rustc_ast::*;
@@ -18,6 +17,7 @@ use rustc_hir::definitions::DefPathData;
1817
use rustc_span::source_map::{respan, DesugaringKind, Span, Spanned};
1918
use rustc_span::symbol::{sym, Ident};
2019
use rustc_span::DUMMY_SP;
20+
use thin_vec::thin_vec;
2121

2222
impl<'hir> LoweringContext<'_, 'hir> {
2323
fn lower_exprs(&mut self, exprs: &[AstP<Expr>]) -> &'hir [hir::Expr<'hir>] {
@@ -1541,7 +1541,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
15411541
};
15421542
attr::mk_attr_outer(allow)
15431543
};
1544-
let attrs: AttrVec = vec![attr].into();
1544+
let attrs: AttrVec = thin_vec![attr];
15451545

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

compiler/rustc_builtin_macros/Cargo.toml

+7-6
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

+3-3
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

+2-2
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

+2-1
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

+2-2
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

+2-2
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

+2-2
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

+2-2
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)