Last active
December 2, 2021 10:38
-
-
Save justanotherdot/0b13f8d118e1c48c98a778a5c5423058 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#![feature(allocator_api)] | |
use std::alloc::Allocator; | |
use std::alloc::Global; | |
use std::marker::PhantomData; | |
// Basicaly a Vec. | |
pub struct V<T, A: Allocator = Global> { | |
pub buf: Rv<T, A>, | |
pub len: usize, | |
} | |
// Basically a RawVec. | |
pub struct Rv<T, A: Allocator = Global> { | |
pub ptr: Unique<T>, | |
pub cap: usize, | |
pub alloc: A, | |
} | |
// Basically a Unique. | |
pub struct Unique<T: ?Sized> { | |
pub pointer: *const T, | |
pub _marker: PhantomData<T>, | |
} | |
fn main() { | |
// Builtin. | |
//RUSTFLAGS="$RUSTFLAGS -Zprint-type-sizes" cargo +nightly build --release | |
//print-type-size type: `std::vec::Vec<i32>`: 24 bytes, alignment: 8 bytes | |
//print-type-size field `.buf`: 16 bytes | |
//print-type-size field `.len`: 8 bytes | |
//print-type-size type: `alloc::raw_vec::RawVec<i32>`: 16 bytes, alignment: 8 bytes | |
//print-type-size field `.alloc`: 0 bytes | |
//print-type-size field `.ptr`: 8 bytes | |
//print-type-size field `.cap`: 8 bytes | |
//RUSTFLAGS="$RUSTFLAGS --emit llvm-ir" cargo +nightly build --release | |
//cat target/release/deps/type_sizes-dbd3f07f95882e7a.ll | rustfilt | less | |
//%"alloc::vec::Vec<i32>" = type { { i32*, i64 }, i64 } | |
let xs = vec![1, 2, 3]; | |
println!("{:?} takes up {} bytes", xs, std::mem::size_of_val(&xs)); | |
// Homespun. | |
let ys = V { | |
buf: Rv { | |
ptr: Unique { | |
pointer: std::mem::align_of::<i32>() as *mut i32, | |
_marker: PhantomData, | |
}, | |
cap: 0, | |
alloc: Global, | |
}, | |
len: 0, | |
}; | |
//print-type-size type: `V<i32>`: 24 bytes, alignment: 8 bytes | |
//print-type-size field `.buf`: 16 bytes | |
//print-type-size field `.len`: 8 bytes | |
// ... | |
//print-type-size type: `Rv<i32>`: 16 bytes, alignment: 8 bytes | |
//print-type-size field `.alloc`: 0 bytes | |
//print-type-size field `.ptr`: 8 bytes | |
//print-type-size field `.cap`: 8 bytes | |
// !499 = !DICompositeType(tag: DW_TAG_structure_type, name: "V<i32, alloc::alloc::Global>", scope: | |
// !500, file: !2, size: 192, align: 64, elements: !501, templateParams: !320, identifier: "a134b15c57d7976d79d922cfe8e48ad2") | |
// ... | |
// | |
// !503 = !DICompositeType(tag: DW_TAG_structure_type, name: "Rv<i32, alloc::alloc::Global>", scope: | |
// !500, file: !2, size: 128, align: 64, elements: !504, templateParams: !320, identifier: "5377eda6ce5cb55c8f9ffedb4de51a6d") | |
println!("V takes up {} bytes", std::mem::size_of_val(&ys)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment