Skip to content

Shrink ast::{Generics,Impl,Block} #101472

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions compiler/rustc_ast/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,7 @@ impl GenericParam {
/// a function, enum, trait, etc.
#[derive(Clone, Encodable, Decodable, Debug)]
pub struct Generics {
pub params: Vec<GenericParam>,
pub params: Box<[GenericParam]>,
pub where_clause: WhereClause,
pub span: Span,
}
Expand All @@ -423,7 +423,7 @@ impl Default for Generics {
/// Creates an instance of `Generics`.
fn default() -> Generics {
Generics {
params: Vec::new(),
params: Box::new([]),
where_clause: WhereClause {
has_where_token: false,
predicates: Vec::new(),
Expand Down Expand Up @@ -559,7 +559,7 @@ pub enum MetaItemKind {
#[derive(Clone, Encodable, Decodable, Debug)]
pub struct Block {
/// The statements in the block.
pub stmts: Vec<Stmt>,
pub stmts: Box<[Stmt]>,
pub id: NodeId,
/// Distinguishes between `unsafe { ... }` and `{ ... }`.
pub rules: BlockCheckMode,
Expand Down Expand Up @@ -2440,7 +2440,7 @@ pub struct ForeignMod {

#[derive(Clone, Encodable, Decodable, Debug)]
pub struct EnumDef {
pub variants: Vec<Variant>,
pub variants: Box<[Variant]>,
Copy link
Contributor

@Kobzol Kobzol Sep 6, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we could use std::boxed::ThinBox or something similar to further reduce the size from 16 to 8 bytes? By storing the slice size on the heap.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is also thin_vec::ThinVec, which I introduced to the compiler in #100869. Unfortunately, getting any of these kinds of changes to show a clear performance benefit has been hard.

}
/// Enum variant.
#[derive(Clone, Encodable, Decodable, Debug)]
Expand Down Expand Up @@ -2801,7 +2801,7 @@ pub struct Impl {
/// The trait being implemented, if any.
pub of_trait: Option<TraitRef>,
pub self_ty: P<Ty>,
pub items: Vec<P<AssocItem>>,
pub items: Box<[P<AssocItem>]>,
}

#[derive(Clone, Encodable, Decodable, Debug)]
Expand Down Expand Up @@ -3039,18 +3039,18 @@ mod size_asserts {
static_assert_size!(AssocItem, 104);
static_assert_size!(AssocItemKind, 32);
static_assert_size!(Attribute, 32);
static_assert_size!(Block, 48);
static_assert_size!(Block, 40);
static_assert_size!(Expr, 104);
static_assert_size!(ExprKind, 72);
static_assert_size!(Fn, 192);
static_assert_size!(Fn, 184);
static_assert_size!(ForeignItem, 96);
static_assert_size!(ForeignItemKind, 24);
static_assert_size!(GenericArg, 24);
static_assert_size!(GenericBound, 88);
static_assert_size!(Generics, 72);
static_assert_size!(Impl, 200);
static_assert_size!(Item, 184);
static_assert_size!(ItemKind, 112);
static_assert_size!(Generics, 64);
static_assert_size!(Impl, 184);
static_assert_size!(Item, 176);
static_assert_size!(ItemKind, 104);
static_assert_size!(Lit, 48);
static_assert_size!(LitKind, 24);
static_assert_size!(Local, 72);
Expand Down
8 changes: 4 additions & 4 deletions compiler/rustc_ast/src/visit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ pub fn walk_item<'a, V: Visitor<'a>>(visitor: &mut V, item: &'a Item) {
visitor.visit_generics(generics);
walk_list!(visitor, visit_trait_ref, of_trait);
visitor.visit_ty(self_ty);
walk_list!(visitor, visit_assoc_item, items, AssocCtxt::Impl);
walk_list!(visitor, visit_assoc_item, items.iter(), AssocCtxt::Impl);
}
ItemKind::Struct(ref struct_definition, ref generics)
| ItemKind::Union(ref struct_definition, ref generics) => {
Expand Down Expand Up @@ -369,7 +369,7 @@ pub fn walk_item<'a, V: Visitor<'a>>(visitor: &mut V, item: &'a Item) {
}

pub fn walk_enum_def<'a, V: Visitor<'a>>(visitor: &mut V, enum_definition: &'a EnumDef) {
walk_list!(visitor, visit_variant, &enum_definition.variants);
walk_list!(visitor, visit_variant, enum_definition.variants.iter());
}

pub fn walk_variant<'a, V: Visitor<'a>>(visitor: &mut V, variant: &'a Variant)
Expand Down Expand Up @@ -608,7 +608,7 @@ pub fn walk_generic_param<'a, V: Visitor<'a>>(visitor: &mut V, param: &'a Generi
}

pub fn walk_generics<'a, V: Visitor<'a>>(visitor: &mut V, generics: &'a Generics) {
walk_list!(visitor, visit_generic_param, &generics.params);
walk_list!(visitor, visit_generic_param, generics.params.iter());
walk_list!(visitor, visit_where_predicate, &generics.where_clause.predicates);
}

Expand Down Expand Up @@ -714,7 +714,7 @@ pub fn walk_field_def<'a, V: Visitor<'a>>(visitor: &mut V, field: &'a FieldDef)
}

pub fn walk_block<'a, V: Visitor<'a>>(visitor: &mut V, block: &'a Block) {
walk_list!(visitor, visit_stmt, &block.stmts);
walk_list!(visitor, visit_stmt, block.stmts.iter());
}

pub fn walk_stmt<'a, V: Visitor<'a>>(visitor: &mut V, statement: &'a Stmt) {
Expand Down
12 changes: 6 additions & 6 deletions compiler/rustc_ast_passes/src/ast_validation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1052,7 +1052,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
this.visit_trait_ref(t);
this.visit_ty(self_ty);

walk_list!(this, visit_assoc_item, items, AssocCtxt::Impl);
walk_list!(this, visit_assoc_item, items.iter(), AssocCtxt::Impl);
});
return; // Avoid visiting again.
}
Expand Down Expand Up @@ -1145,7 +1145,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
return; // Avoid visiting again.
}
ItemKind::Enum(ref def, _) => {
for variant in &def.variants {
for variant in def.variants.iter() {
self.invalid_visibility(&variant.vis, None);
for field in variant.data.fields() {
self.invalid_visibility(&field.vis, None);
Expand Down Expand Up @@ -1330,7 +1330,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> {

fn visit_generics(&mut self, generics: &'a Generics) {
let mut prev_param_default = None;
for param in &generics.params {
for param in generics.params.iter() {
match param.kind {
GenericParamKind::Lifetime => (),
GenericParamKind::Type { default: Some(_), .. }
Expand All @@ -1357,7 +1357,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
deny_equality_constraints(self, predicate, generics);
}
}
walk_list!(self, visit_generic_param, &generics.params);
walk_list!(self, visit_generic_param, generics.params.iter());
for predicate in &generics.where_clause.predicates {
match predicate {
WherePredicate::BoundPredicate(bound_pred) => {
Expand Down Expand Up @@ -1658,7 +1658,7 @@ fn deny_equality_constraints(
if let TyKind::Path(None, path) = &qself.ty.kind {
match &path.segments[..] {
[PathSegment { ident, args: None, .. }] => {
for param in &generics.params {
for param in generics.params.iter() {
if param.ident == *ident {
let param = ident;
match &full_path.segments[qself.position..] {
Expand Down Expand Up @@ -1722,7 +1722,7 @@ fn deny_equality_constraints(
// Given `A: Foo, A::Bar = RhsTy`, suggest `A: Foo<Bar = RhsTy>`.
if let TyKind::Path(None, full_path) = &predicate.lhs_ty.kind {
if let [potential_param, potential_assoc] = &full_path.segments[..] {
for param in &generics.params {
for param in generics.params.iter() {
if param.ident == potential_param.ident {
for bound in &param.bounds {
if let ast::GenericBound::Trait(trait_ref, TraitBoundModifier::None) = bound
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_ast_passes/src/feature_gate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -477,7 +477,7 @@ impl<'a> Visitor<'a> for PostExpansionVisitor<'a> {
}

ast::ItemKind::Enum(ast::EnumDef { ref variants, .. }, ..) => {
for variant in variants {
for variant in variants.iter() {
match (&variant.data, &variant.disr_expr) {
(ast::VariantData::Unit(..), _) => {}
(_, Some(disr_expr)) => gate_feature_post!(
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_ast_pretty/src/pprust/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1714,7 +1714,7 @@ impl<'a> State<'a> {
self.ibox(INDENT_UNIT);
self.print_formal_generic_params(generic_params);
let generics = ast::Generics {
params: Vec::new(),
params: Box::new([]),
where_clause: ast::WhereClause {
has_where_token: false,
predicates: Vec::new(),
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_ast_pretty/src/pprust/state/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ impl<'a> State<'a> {
self.space();
self.bopen();
self.print_inner_attributes(&item.attrs);
for impl_item in items {
for impl_item in items.iter() {
self.print_assoc_item(impl_item);
}
let empty = item.attrs.is_empty() && items.is_empty();
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_builtin_macros/src/deriving/clone.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ fn cs_clone_simple(
process_variant(vdata);
}
StaticEnum(enum_def, ..) => {
for variant in &enum_def.variants {
for variant in enum_def.variants.iter() {
process_variant(&variant.data);
}
}
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_builtin_macros/src/deriving/cmp/eq.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ fn cs_total_eq_assert(
process_variant(vdata);
}
StaticEnum(enum_def, ..) => {
for variant in &enum_def.variants {
for variant in enum_def.variants.iter() {
process_variant(&variant.data);
}
}
Expand Down
10 changes: 5 additions & 5 deletions compiler/rustc_builtin_macros/src/deriving/generic/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -575,7 +575,7 @@ impl<'a> TraitDef<'a> {
})
});

let Generics { mut params, mut where_clause, .. } =
let (mut params, mut where_clause) =
self.generics.to_generics(cx, self.span, type_ident, generics);
where_clause.span = generics.where_clause.span;
let ctxt = self.span.ctxt();
Expand Down Expand Up @@ -687,7 +687,7 @@ impl<'a> TraitDef<'a> {
}
}

let trait_generics = Generics { params, where_clause, span };
let trait_generics = Generics { params: params.into_boxed_slice(), where_clause, span };

// Create the reference to the trait.
let trait_ref = cx.trait_ref(trait_path);
Expand Down Expand Up @@ -799,7 +799,7 @@ impl<'a> TraitDef<'a> {
) -> P<ast::Item> {
let mut field_tys = Vec::new();

for variant in &enum_def.variants {
for variant in enum_def.variants.iter() {
field_tys.extend(variant.data.fields().iter().map(|field| field.ty.clone()));
}

Expand Down Expand Up @@ -932,7 +932,7 @@ impl<'a> MethodDef<'a> {
) -> P<ast::AssocItem> {
let span = trait_.span;
// Create the generics that aren't for `Self`.
let fn_generics = self.generics.to_generics(cx, span, type_ident, generics);
let (params, where_clause) = self.generics.to_generics(cx, span, type_ident, generics);

let args = {
let self_arg = explicit_self.map(|explicit_self| {
Expand Down Expand Up @@ -969,7 +969,7 @@ impl<'a> MethodDef<'a> {
kind: ast::AssocItemKind::Fn(Box::new(ast::Fn {
defaultness,
sig,
generics: fn_generics,
generics: Generics { params: params.into_boxed_slice(), where_clause, span },
body: Some(body_block),
})),
tokens: None,
Expand Down
12 changes: 5 additions & 7 deletions compiler/rustc_builtin_macros/src/deriving/generic/ty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
pub use Ty::*;

use rustc_ast::ptr::P;
use rustc_ast::{self as ast, Expr, GenericArg, GenericParamKind, Generics, SelfKind};
use rustc_ast::{self as ast, Expr, GenericArg, GenericParam, GenericParamKind, Generics, SelfKind, WhereClause};
use rustc_expand::base::ExtCtxt;
use rustc_span::source_map::{respan, DUMMY_SP};
use rustc_span::symbol::{kw, Ident, Symbol};
Expand Down Expand Up @@ -176,7 +176,7 @@ impl Bounds {
span: Span,
self_ty: Ident,
self_generics: &Generics,
) -> Generics {
) -> (Vec<GenericParam>, WhereClause) {
let params = self
.bounds
.iter()
Expand All @@ -186,11 +186,9 @@ impl Bounds {
})
.collect();

Generics {
params,
where_clause: ast::WhereClause { has_where_token: false, predicates: Vec::new(), span },
span,
}
let where_clause =
ast::WhereClause { has_where_token: false, predicates: Vec::new(), span };
(params, where_clause)
}
}

Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_builtin_macros/src/deriving/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ fn call_unreachable(cx: &ExtCtxt<'_>, span: Span) -> P<ast::Expr> {
let call = cx.expr_call_global(span, path, vec![]);

cx.expr_block(P(ast::Block {
stmts: vec![cx.stmt_expr(call)],
stmts: vec![cx.stmt_expr(call)].into_boxed_slice(),
id: ast::DUMMY_NODE_ID,
rules: ast::BlockCheckMode::Unsafe(ast::CompilerGenerated),
span,
Expand Down Expand Up @@ -187,7 +187,7 @@ fn inject_impl_of_structural_trait(
generics,
of_trait: Some(trait_ref),
self_ty: self_type,
items: Vec::new(),
items: Box::new([]),
})),
);

Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_builtin_macros/src/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1090,7 +1090,7 @@ impl<'a, 'b> Context<'a, 'b> {
let path = self.ecx.std_path(&[sym::fmt, sym::UnsafeArg, sym::new]);
let unsafe_arg = self.ecx.expr_call_global(self.macsp, path, Vec::new());
let unsafe_expr = self.ecx.expr_block(P(ast::Block {
stmts: vec![self.ecx.stmt_expr(unsafe_arg)],
stmts: vec![self.ecx.stmt_expr(unsafe_arg)].into_boxed_slice(),
id: ast::DUMMY_NODE_ID,
rules: BlockCheckMode::Unsafe(UnsafeSource::CompilerGenerated),
span: self.macsp,
Expand Down
13 changes: 13 additions & 0 deletions compiler/rustc_data_structures/src/map_in_place.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,19 @@ impl<T> MapInPlace<T> for Vec<T> {
flat_map_in_place!();
}

impl<T> MapInPlace<T> for Box<[T]> {
fn flat_map_in_place<F, I>(&mut self, f: F)
where
F: FnMut(T) -> I,
I: IntoIterator<Item = T>,
{
let slice = std::mem::take(self);
let mut vec = slice.into_vec();
vec.flat_map_in_place(f);
*self = vec.into_boxed_slice();
}
}

impl<T, A: Array<Item = T>> MapInPlace<T> for SmallVec<A> {
flat_map_in_place!();
}
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_expand/src/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ impl<'a> ExtCtxt<'a> {
}
pub fn block(&self, span: Span, stmts: Vec<ast::Stmt>) -> P<ast::Block> {
P(ast::Block {
stmts,
stmts: stmts.into_boxed_slice(),
id: ast::DUMMY_NODE_ID,
rules: BlockCheckMode::Default,
span,
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_lint/src/unused.rs
Original file line number Diff line number Diff line change
Expand Up @@ -797,7 +797,7 @@ impl EarlyLintPass for UnusedParens {
None,
None,
);
for stmt in &block.stmts {
for stmt in block.stmts.iter() {
<Self as UnusedDelimLint>::check_stmt(self, cx, stmt);
}
if let Some(e) = else_ {
Expand Down Expand Up @@ -960,7 +960,7 @@ impl UnusedDelimLint for UnusedBraces {
// (do not lint `struct A<const N: usize>; let _: A<{ 2 + 3 }>;`)
//
// FIXME(const_generics): handle paths when #67075 is fixed.
if let [stmt] = inner.stmts.as_slice() {
if let [stmt] = &*inner.stmts {
if let ast::StmtKind::Expr(ref expr) = stmt.kind {
if !Self::is_expr_delims_necessary(expr, followed_by_block, false)
&& (ctx != UnusedDelimsCtx::AnonConst
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_parse/src/parser/generics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ impl<'a> Parser<'a> {
(vec![], self.prev_token.span.shrink_to_hi())
};
Ok(ast::Generics {
params,
params: params.into_boxed_slice(),
where_clause: WhereClause {
has_where_token: false,
predicates: Vec::new(),
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_parse/src/parser/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -637,7 +637,7 @@ impl<'a> Parser<'a> {
generics,
of_trait: Some(trait_ref),
self_ty: ty_second,
items: impl_items,
items: impl_items.into_boxed_slice(),
}))
}
None => {
Expand All @@ -650,7 +650,7 @@ impl<'a> Parser<'a> {
generics,
of_trait: None,
self_ty: ty_first,
items: impl_items,
items: impl_items.into_boxed_slice(),
}))
}
};
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_parse/src/parser/stmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -645,7 +645,7 @@ impl<'a> Parser<'a> {

pub(super) fn mk_block(&self, stmts: Vec<Stmt>, rules: BlockCheckMode, span: Span) -> P<Block> {
P(Block {
stmts,
stmts: stmts.into_boxed_slice(),
id: DUMMY_NODE_ID,
rules,
span,
Expand Down
Loading