Skip to content

Instantly share code, notes, and snippets.

@antiguru
Last active November 18, 2021 14:43
Show Gist options
  • Save antiguru/e3d165d7ad3ed082a633d6b0540ebd53 to your computer and use it in GitHub Desktop.
Save antiguru/e3d165d7ad3ed082a633d6b0540ebd53 to your computer and use it in GitHub Desktop.
Materialize type size analysis

Run:

RUSTFLAGS="-Zprint-type-sizes" cargo +toolchain check 2>&1 | tee rust_sizes.txt
grep " type.*bytes" rust_sizes.txt | sed 's/:\(.*\):\(.*\):\(.*\)/\2 \3 \1/' | sort -u | sort -nk3 | less

This will include output like the following:

print-type-size type 1160 bytes, alignment  8 bytes  `plan::Sink`
print-type-size type 1160 bytes, alignment  8 bytes  `sql::plan::Sink`
print-type-size type 1168 bytes, alignment  8 bytes  `catalog::Sink`
print-type-size type 1176 bytes, alignment  8 bytes  `coord::SinkConnectorReady`
print-type-size type 1216 bytes, alignment  32 bytes  `compile::Compiler`
print-type-size type 1224 bytes, alignment  8 bytes  `plan::Source`
print-type-size type 1224 bytes, alignment  8 bytes  `sql::plan::Source`
print-type-size type 1240 bytes, alignment  8 bytes  `plan::CreateSinkPlan`
print-type-size type 1240 bytes, alignment  8 bytes  `sql::plan::CreateSinkPlan`
print-type-size type 1272 bytes, alignment  8 bytes  `catalog::Source`
print-type-size type 1280 bytes, alignment  8 bytes  `catalog::CatalogItem`
print-type-size type 1304 bytes, alignment  8 bytes  `plan::CreateSourcePlan`
print-type-size type 1304 bytes, alignment  8 bytes  `sql::plan::CreateSourcePlan`
print-type-size type 1312 bytes, alignment  8 bytes  `plan::Plan`
print-type-size type 1312 bytes, alignment  8 bytes  `sql::plan::Plan`
print-type-size type 1328 bytes, alignment  8 bytes  `action::State`
print-type-size type 1328 bytes, alignment  8 bytes  `internals::attr::Container`
print-type-size type 1376 bytes, alignment  8 bytes  `catalog::Catalog::transact::Action`
print-type-size type 1376 bytes, alignment  8 bytes  `catalog::Op`
print-type-size type 1400 bytes, alignment  8 bytes  `catalog::CatalogEntry`
print-type-size type 1400 bytes, alignment  8 bytes  `std::option::Option<catalog::CatalogEntry>`
print-type-size type 1416 bytes, alignment  8 bytes  `internals::ast::Container`
print-type-size type 1416 bytes, alignment  8 bytes  `std::option::Option<internals::ast::Container>`
print-type-size type 1536 bytes, alignment  8 bytes  `command::Command`
print-type-size type 1552 bytes, alignment  8 bytes  `coord::StatementReady`
print-type-size type 1560 bytes, alignment  8 bytes  `coord::Message`
print-type-size type 1560 bytes, alignment  8 bytes  `std::option::Option<coord::Message>`
print-type-size type 1560 bytes, alignment  8 bytes  `std::task::Poll<std::option::Option<coord::Message>>`

I had to patch rustc to make this work:

diff --git a/compiler/rustc_target/src/abi/mod.rs b/compiler/rustc_target/src/abi/mod.rs
index 61607159208..4679402cc64 100644
--- a/compiler/rustc_target/src/abi/mod.rs
+++ b/compiler/rustc_target/src/abi/mod.rs
@@ -892,7 +892,8 @@ pub fn count(&self) -> usize {
     pub fn offset(&self, i: usize) -> Size {
         match *self {
             FieldsShape::Primitive => {
-                unreachable!("FieldsShape::offset: `Primitive`s have no fields")
+                warn!("FieldsShape::offset: `Primitive`s have no fields");
+                Size::ZERO
             }
             FieldsShape::Union(count) => {
                 assert!(
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment