Skip to content

Commit

Permalink
Auto merge of #100869 - nnethercote:replace-ThinVec, r=spastorino
Browse files Browse the repository at this point in the history
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`
  • Loading branch information
bors committed Sep 1, 2022
2 parents b32223f + 78f83f0 commit eac6c33
Show file tree
Hide file tree
Showing 44 changed files with 176 additions and 362 deletions.
15 changes: 15 additions & 0 deletions Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3208,6 +3208,7 @@ dependencies = [
"rustc_serialize",
"rustc_span",
"smallvec",
"thin-vec",
"tracing",
]

Expand All @@ -3229,6 +3230,7 @@ dependencies = [
"rustc_span",
"rustc_target",
"smallvec",
"thin-vec",
"tracing",
]

Expand Down Expand Up @@ -3323,6 +3325,7 @@ dependencies = [
"rustc_span",
"rustc_target",
"smallvec",
"thin-vec",
"tracing",
]

Expand Down Expand Up @@ -3447,6 +3450,7 @@ dependencies = [
"stable_deref_trait",
"stacker",
"tempfile",
"thin-vec",
"tracing",
"winapi",
]
Expand Down Expand Up @@ -3829,6 +3833,7 @@ dependencies = [
"rustc_target",
"rustc_type_ir",
"smallvec",
"thin-vec",
"tracing",
]

Expand Down Expand Up @@ -4018,6 +4023,7 @@ dependencies = [
"rustc_session",
"rustc_span",
"rustc_target",
"thin-vec",
"tracing",
]

Expand All @@ -4040,6 +4046,7 @@ dependencies = [
"rustc_span",
"rustc_target",
"smallvec",
"thin-vec",
"tracing",
]

Expand Down Expand Up @@ -4095,6 +4102,7 @@ dependencies = [
"indexmap",
"rustc_macros",
"smallvec",
"thin-vec",
]

[[package]]
Expand Down Expand Up @@ -4331,6 +4339,7 @@ dependencies = [
"serde_json",
"smallvec",
"tempfile",
"thin-vec",
"tracing",
"tracing-subscriber",
"tracing-tree",
Expand Down Expand Up @@ -4884,6 +4893,12 @@ version = "0.15.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b1141d4d61095b28419e22cb0bbf02755f5e54e0526f97f1e3d1d160e60885fb"

[[package]]
name = "thin-vec"
version = "0.2.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "104c2cb3180b6fb6d5b2278768e9b88b578d32ba751ea6e8d026688a40d7ed87"

[[package]]
name = "thiserror"
version = "1.0.30"
Expand Down
9 changes: 5 additions & 4 deletions compiler/rustc_ast/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@ edition = "2021"
doctest = false

[dependencies]
rustc_serialize = { path = "../rustc_serialize" }
tracing = "0.1"
rustc_span = { path = "../rustc_span" }
bitflags = "1.2.1"
rustc_data_structures = { path = "../rustc_data_structures" }
rustc_index = { path = "../rustc_index" }
rustc_lexer = { path = "../rustc_lexer" }
rustc_macros = { path = "../rustc_macros" }
rustc_serialize = { path = "../rustc_serialize" }
rustc_span = { path = "../rustc_span" }
smallvec = { version = "1.8.1", features = ["union", "may_dangle"] }
bitflags = "1.2.1"
thin-vec = "0.2.8"
tracing = "0.1"
4 changes: 1 addition & 3 deletions compiler/rustc_ast/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,21 +25,19 @@ pub use UnsafeSource::*;
use crate::ptr::P;
use crate::token::{self, CommentKind, Delimiter};
use crate::tokenstream::{DelimSpan, LazyTokenStream, TokenStream};

use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
use rustc_data_structures::stack::ensure_sufficient_stack;
use rustc_data_structures::sync::Lrc;
use rustc_data_structures::thin_vec::ThinVec;
use rustc_macros::HashStable_Generic;
use rustc_serialize::{Decodable, Decoder, Encodable, Encoder};
use rustc_span::source_map::{respan, Spanned};
use rustc_span::symbol::{kw, sym, Ident, Symbol};
use rustc_span::{Span, DUMMY_SP};

use std::cmp::Ordering;
use std::convert::TryFrom;
use std::fmt;
use std::mem;
use thin_vec::ThinVec;

/// A "Label" is an identifier of some point in sources,
/// e.g. in the following code:
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_ast/src/ast_traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,7 @@ macro_rules! impl_has_attrs {
impl HasAttrs for $T {
const SUPPORTS_CUSTOM_INNER_ATTRS: bool = $inner;

#[inline]
fn attrs(&self) -> &[Attribute] {
&self.attrs
}
Expand Down
3 changes: 2 additions & 1 deletion compiler/rustc_ast/src/tokenstream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,8 @@ impl TokenStream {
let attr_annotated = if attrs.is_empty() {
tokens.create_token_stream()
} else {
let attr_data = AttributesData { attrs: attrs.to_vec().into(), tokens: tokens.clone() };
let attr_data =
AttributesData { attrs: attrs.iter().cloned().collect(), tokens: tokens.clone() };
AttrAnnotatedTokenStream::new(vec![(
AttrAnnotatedTokenTree::Attributes(attr_data),
Spacing::Alone,
Expand Down
13 changes: 7 additions & 6 deletions compiler/rustc_ast_lowering/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,18 @@ doctest = false

[dependencies]
rustc_arena = { path = "../rustc_arena" }
tracing = "0.1"
rustc_ast = { path = "../rustc_ast" }
rustc_ast_pretty = { path = "../rustc_ast_pretty" }
rustc_hir = { path = "../rustc_hir" }
rustc_target = { path = "../rustc_target" }
rustc_data_structures = { path = "../rustc_data_structures" }
rustc_errors = { path = "../rustc_errors" }
rustc_hir = { path = "../rustc_hir" }
rustc_index = { path = "../rustc_index" }
rustc_middle = { path = "../rustc_middle" }
rustc_macros = { path = "../rustc_macros" }
rustc_query_system = { path = "../rustc_query_system" }
rustc_span = { path = "../rustc_span" }
rustc_errors = { path = "../rustc_errors" }
rustc_session = { path = "../rustc_session" }
rustc_ast = { path = "../rustc_ast" }
rustc_span = { path = "../rustc_span" }
rustc_target = { path = "../rustc_target" }
smallvec = { version = "1.8.1", features = ["union", "may_dangle"] }
thin-vec = "0.2.8"
tracing = "0.1"
4 changes: 2 additions & 2 deletions compiler/rustc_ast_lowering/src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ use super::errors::{
use super::ResolverAstLoweringExt;
use super::{ImplTraitContext, LoweringContext, ParamMode, ParenthesizedGenericArgs};
use crate::{FnDeclKind, ImplTraitPosition};

use rustc_ast::attr;
use rustc_ast::ptr::P as AstP;
use rustc_ast::*;
Expand All @@ -18,6 +17,7 @@ use rustc_hir::definitions::DefPathData;
use rustc_span::source_map::{respan, DesugaringKind, Span, Spanned};
use rustc_span::symbol::{sym, Ident};
use rustc_span::DUMMY_SP;
use thin_vec::thin_vec;

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

// `ControlFlow::Continue(val) => #[allow(unreachable_code)] val,`
let continue_arm = {
Expand Down
13 changes: 7 additions & 6 deletions compiler/rustc_builtin_macros/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,21 @@ edition = "2021"
doctest = false

[dependencies]
rustc_parse_format = { path = "../rustc_parse_format" }
tracing = "0.1"
rustc_ast = { path = "../rustc_ast" }
rustc_ast_pretty = { path = "../rustc_ast_pretty" }
rustc_attr = { path = "../rustc_attr" }
rustc_data_structures = { path = "../rustc_data_structures" }
rustc_errors = { path = "../rustc_errors" }
rustc_expand = { path = "../rustc_expand" }
rustc_feature = { path = "../rustc_feature" }
rustc_lexer = { path = "../rustc_lexer" }
rustc_lint_defs = { path = "../rustc_lint_defs" }
rustc_macros = { path = "../rustc_macros" }
rustc_parse_format = { path = "../rustc_parse_format" }
rustc_parse = { path = "../rustc_parse" }
rustc_target = { path = "../rustc_target" }
rustc_session = { path = "../rustc_session" }
smallvec = { version = "1.8.1", features = ["union", "may_dangle"] }
rustc_ast = { path = "../rustc_ast" }
rustc_expand = { path = "../rustc_expand" }
rustc_span = { path = "../rustc_span" }
rustc_target = { path = "../rustc_target" }
smallvec = { version = "1.8.1", features = ["union", "may_dangle"] }
thin-vec = "0.2.8"
tracing = "0.1"
6 changes: 3 additions & 3 deletions compiler/rustc_builtin_macros/src/assert/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use rustc_span::{
symbol::{sym, Ident, Symbol},
Span,
};
use thin_vec::thin_vec;

pub(super) struct Context<'cx, 'a> {
// An optimization.
Expand Down Expand Up @@ -116,11 +117,10 @@ impl<'cx, 'a> Context<'cx, 'a> {
self.cx.item(
self.span,
Ident::empty(),
vec![self.cx.attribute(attr::mk_list_item(
thin_vec![self.cx.attribute(attr::mk_list_item(
Ident::new(sym::allow, self.span),
vec![attr::mk_nested_word_item(Ident::new(sym::unused_imports, self.span))],
))]
.into(),
))],
ItemKind::Use(UseTree {
prefix: self.cx.path(self.span, self.cx.std_path(&[sym::asserting])),
kind: UseTreeKind::Nested(vec![
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_builtin_macros/src/deriving/clone.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use crate::deriving::generic::ty::*;
use crate::deriving::generic::*;
use crate::deriving::path_std;

use rustc_ast::{self as ast, Generics, ItemKind, MetaItem, VariantData};
use rustc_data_structures::fx::FxHashSet;
use rustc_expand::base::{Annotatable, ExtCtxt};
use rustc_span::symbol::{kw, sym, Ident};
use rustc_span::Span;
use thin_vec::thin_vec;

pub fn expand_deriving_clone(
cx: &mut ExtCtxt<'_>,
Expand Down Expand Up @@ -68,7 +68,7 @@ pub fn expand_deriving_clone(
}

let inline = cx.meta_word(span, sym::inline);
let attrs = vec![cx.attribute(inline)].into();
let attrs = thin_vec![cx.attribute(inline)];
let trait_def = TraitDef {
span,
path: path_std!(clone::Clone),
Expand Down
3 changes: 2 additions & 1 deletion compiler/rustc_builtin_macros/src/deriving/cmp/eq.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use rustc_data_structures::fx::FxHashSet;
use rustc_expand::base::{Annotatable, ExtCtxt};
use rustc_span::symbol::{sym, Ident};
use rustc_span::Span;
use thin_vec::thin_vec;

pub fn expand_deriving_eq(
cx: &mut ExtCtxt<'_>,
Expand All @@ -20,7 +21,7 @@ pub fn expand_deriving_eq(
let hidden = rustc_ast::attr::mk_nested_word_item(Ident::new(sym::hidden, span));
let doc = rustc_ast::attr::mk_list_item(Ident::new(sym::doc, span), vec![hidden]);
let no_coverage = cx.meta_word(span, sym::no_coverage);
let attrs = vec![cx.attribute(inline), cx.attribute(doc), cx.attribute(no_coverage)].into();
let attrs = thin_vec![cx.attribute(inline), cx.attribute(doc), cx.attribute(no_coverage)];
let trait_def = TraitDef {
span,
path: path_std!(cmp::Eq),
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_builtin_macros/src/deriving/cmp/ord.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use crate::deriving::generic::ty::*;
use crate::deriving::generic::*;
use crate::deriving::path_std;

use rustc_ast::MetaItem;
use rustc_expand::base::{Annotatable, ExtCtxt};
use rustc_span::symbol::{sym, Ident};
use rustc_span::Span;
use thin_vec::thin_vec;

pub fn expand_deriving_ord(
cx: &mut ExtCtxt<'_>,
Expand All @@ -15,7 +15,7 @@ pub fn expand_deriving_ord(
push: &mut dyn FnMut(Annotatable),
) {
let inline = cx.meta_word(span, sym::inline);
let attrs = vec![cx.attribute(inline)].into();
let attrs = thin_vec![cx.attribute(inline)];
let trait_def = TraitDef {
span,
path: path_std!(cmp::Ord),
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_builtin_macros/src/deriving/cmp/partial_eq.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use crate::deriving::generic::ty::*;
use crate::deriving::generic::*;
use crate::deriving::{path_local, path_std};

use rustc_ast::ptr::P;
use rustc_ast::{BinOpKind, BorrowKind, Expr, ExprKind, MetaItem, Mutability};
use rustc_expand::base::{Annotatable, ExtCtxt};
use rustc_span::symbol::sym;
use rustc_span::Span;
use thin_vec::thin_vec;

pub fn expand_deriving_partial_eq(
cx: &mut ExtCtxt<'_>,
Expand Down Expand Up @@ -68,7 +68,7 @@ pub fn expand_deriving_partial_eq(
// No need to generate `ne`, the default suffices, and not generating it is
// faster.
let inline = cx.meta_word(span, sym::inline);
let attrs = vec![cx.attribute(inline)].into();
let attrs = thin_vec![cx.attribute(inline)];
let methods = vec![MethodDef {
name: sym::eq,
generics: Bounds::empty(),
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_builtin_macros/src/deriving/cmp/partial_ord.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use crate::deriving::generic::ty::*;
use crate::deriving::generic::*;
use crate::deriving::{path_std, pathvec_std};

use rustc_ast::MetaItem;
use rustc_expand::base::{Annotatable, ExtCtxt};
use rustc_span::symbol::{sym, Ident};
use rustc_span::Span;
use thin_vec::thin_vec;

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

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

let partial_cmp_def = MethodDef {
name: sym::partial_cmp,
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_builtin_macros/src/deriving/default.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use crate::deriving::generic::ty::*;
use crate::deriving::generic::*;

use rustc_ast as ast;
use rustc_ast::{walk_list, EnumDef, VariantData};
use rustc_errors::Applicability;
Expand All @@ -9,6 +8,7 @@ use rustc_span::symbol::Ident;
use rustc_span::symbol::{kw, sym};
use rustc_span::Span;
use smallvec::SmallVec;
use thin_vec::thin_vec;

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

let inline = cx.meta_word(span, sym::inline);
let attrs = vec![cx.attribute(inline)].into();
let attrs = thin_vec![cx.attribute(inline)];
let trait_def = TraitDef {
span,
path: Path::new(vec![kw::Default, sym::Default]),
Expand Down
Loading

0 comments on commit eac6c33

Please sign in to comment.