Skip to content

Instantly share code, notes, and snippets.

@cart
Created August 22, 2024 01:50
Show Gist options
  • Save cart/722756ba3da0e983d207633e0a48a8ab to your computer and use it in GitHub Desktop.
Save cart/722756ba3da0e983d207633e0a48a8ab to your computer and use it in GitHub Desktop.
use serde::{Deserialize, Serialize};
use std::{collections::HashMap, fs::read_to_string};
#[derive(Serialize, Deserialize, Debug)]
struct BloatOutput {
#[serde(alias = "file-size")]
file_size: usize,
#[serde(alias = "text-section-size")]
text_section_size: usize,
functions: Vec<Function>,
}
#[derive(Serialize, Deserialize, Debug)]
struct Function {
#[serde(alias = "crate")]
crate_name: Option<String>,
name: String,
size: usize,
}
fn main() {
let output = read_to_string("out.json").unwrap();
let p: BloatOutput = serde_json::from_str(&output).unwrap();
let mut combined = HashMap::<String, usize>::default();
for func in p.functions {
let count = combined.entry(func.name.clone()).or_default();
*count += func.size;
}
let mut values = combined.into_iter().collect::<Vec<_>>();
values.sort_by_key(|(_, size)| *size);
let values = values
.into_iter()
.map(|(name, size)| (name, size, 100.0 * size as f32 / p.text_section_size as f32))
.collect::<Vec<_>>();
println!("{:#?}", values);
}
@kitlith
Copy link

kitlith commented Sep 10, 2024

It might be useful to turn this into a script using the nightly/experimental cargo-script functionality.

#!/usr/bin/env -S cargo +nightly -Zscript
---cargo
[dependencies]
serde = { version = "1", features = ["derive"]}
serde_json = "1"
---
// rest of the program follows here

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment