1 // Copyright 2016 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
12 use ty::layout::{Align, Size};
14 use rustc_data_structures::fx::{FxHashSet};
16 use std::cmp::{self, Ordering};
18 #[derive(Clone, PartialEq, Eq, Hash, Debug)]
19 pub struct VariantInfo {
20 pub name: Option<String>,
24 pub fields: Vec<FieldInfo>,
27 #[derive(Copy, Clone, PartialEq, Eq, Hash, Debug)]
33 #[derive(Clone, PartialEq, Eq, Hash, Debug)]
34 pub struct FieldInfo {
41 impl From<AdtKind> for DataTypeKind {
42 fn from(kind: AdtKind) -> Self {
44 AdtKind::Struct => DataTypeKind::Struct,
45 AdtKind::Enum => DataTypeKind::Enum,
46 AdtKind::Union => DataTypeKind::Union,
51 #[derive(Copy, Clone, PartialEq, Eq, Hash, Debug)]
52 pub enum DataTypeKind {
59 #[derive(PartialEq, Eq, Hash, Debug)]
60 pub struct TypeSizeInfo {
61 pub kind: DataTypeKind,
62 pub type_description: String,
64 pub overall_size: u64,
65 pub opt_discr_size: Option<u64>,
66 pub variants: Vec<VariantInfo>,
69 #[derive(PartialEq, Eq, Debug)]
70 pub struct CodeStats {
71 type_sizes: FxHashSet<TypeSizeInfo>,
75 pub fn new() -> Self { CodeStats { type_sizes: FxHashSet() } }
77 pub fn record_type_size<S: ToString>(&mut self,
82 opt_discr_size: Option<Size>,
83 variants: Vec<VariantInfo>) {
84 let info = TypeSizeInfo {
86 type_description: type_desc.to_string(),
88 overall_size: overall_size.bytes(),
89 opt_discr_size: opt_discr_size.map(|s| s.bytes()),
92 self.type_sizes.insert(info);
95 pub fn print_type_sizes(&self) {
96 let mut sorted: Vec<_> = self.type_sizes.iter().collect();
98 // Primary sort: large-to-small.
99 // Secondary sort: description (dictionary order)
100 sorted.sort_by(|info1, info2| {
101 // (reversing cmp order to get large-to-small ordering)
102 match info2.overall_size.cmp(&info1.overall_size) {
103 Ordering::Equal => info1.type_description.cmp(&info2.type_description),
108 for info in &sorted {
109 println!("print-type-size type: `{}`: {} bytes, alignment: {} bytes",
110 info.type_description, info.overall_size, info.align);
113 let discr_size = if let Some(discr_size) = info.opt_discr_size {
114 println!("print-type-size {}discriminant: {} bytes",
121 // We start this at discr_size (rather than 0) because
122 // things like C-enums do not have variants but we still
123 // want the max_variant_size at the end of the loop below
124 // to reflect the presence of the discriminant.
125 let mut max_variant_size = discr_size;
127 let struct_like = match info.kind {
128 DataTypeKind::Struct | DataTypeKind::Closure => true,
129 DataTypeKind::Enum | DataTypeKind::Union => false,
131 for (i, variant_info) in info.variants.iter().enumerate() {
132 let VariantInfo { ref name, kind: _, align: _, size, ref fields } = *variant_info;
133 let indent = if !struct_like {
134 let name = match name.as_ref() {
135 Some(name) => format!("{}", name),
136 None => format!("{}", i),
138 println!("print-type-size {}variant `{}`: {} bytes",
139 indent, name, size - discr_size);
145 max_variant_size = cmp::max(max_variant_size, size);
147 let mut min_offset = discr_size;
149 // We want to print fields by increasing offset.
150 let mut fields = fields.clone();
151 fields.sort_by_key(|f| f.offset);
153 for field in fields.iter() {
154 let FieldInfo { ref name, offset, size, align } = *field;
156 // Include field alignment in output only if it caused padding injection
157 if min_offset != offset {
158 let pad = offset - min_offset;
159 println!("print-type-size {}padding: {} bytes",
161 println!("print-type-size {}field `.{}`: {} bytes, alignment: {} bytes",
162 indent, name, size, align);
164 println!("print-type-size {}field `.{}`: {} bytes",
168 min_offset = offset + size;
172 assert!(max_variant_size <= info.overall_size,
173 "max_variant_size {} !<= {} overall_size",
174 max_variant_size, info.overall_size);
175 if max_variant_size < info.overall_size {
176 println!("print-type-size {}end padding: {} bytes",
177 indent, info.overall_size - max_variant_size);