#![allow(incomplete_features)]
#![feature(generic_const_exprs)]
use std::alloc;
use std::env;
use cap::Cap;
use multi_array_list::MultiArrayList;
#[global_allocator]
static ALLOCATOR: Cap<alloc::System> = Cap::new(alloc::System, usize::max_value());
#[derive(Clone, PartialEq, Eq, Debug)]
struct Pizza {
radius: i32,
toppings: Vec<Topping>,
}
#[derive(Clone, PartialEq, Eq, Debug)]
#[repr(u8)]
#[allow(dead_code)]
enum Topping {
Tomato,
Mozzarella,
Anchovies,
}
fn main() {
let n: usize = env::args()
.skip(1)
.next()
.unwrap_or_else(String::new)
.parse()
.unwrap_or(500);
let alloc_start = ALLOCATOR.allocated();
let _vec: Vec<_> = (0..n)
.map(|i| Pizza {
radius: i as i32,
toppings: vec![],
})
.collect();
let alloc_after_vec = ALLOCATOR.allocated();
let vec_diff = alloc_after_vec - alloc_start;
let mut list = MultiArrayList::<Pizza>::new();
for i in 0..n {
list.push(Pizza {
radius: i as i32,
toppings: vec![],
});
}
let alloc_after_list = ALLOCATOR.allocated();
let diff = alloc_after_list - alloc_after_vec;
let ratio = -(100.0 - (diff as f64) / (vec_diff as f64) * 100.0);
println!("Allocating {n} pizzas.");
println!("Vec allocated: {vec_diff} bytes");
println!("List allocated: {diff} bytes ({ratio:.2}%)");
}