ic_wasm/
shrink.rs

1use crate::utils::*;
2use walrus::*;
3
4pub fn shrink(m: &mut Module) {
5    if is_motoko_canister(m) {
6        let ids = get_motoko_wasm_data_sections(m);
7        for (id, mut module) in ids.into_iter() {
8            shrink(&mut module);
9            let blob = encode_module_as_data_section(module);
10            let original_len = m.data.get(id).value.len();
11            if blob.len() < original_len {
12                m.data.get_mut(id).value = blob;
13            }
14        }
15    }
16    let to_remove: Vec<_> = m
17        .customs
18        .iter()
19        .filter(|(_, section)| !section.name().starts_with("icp:"))
20        .map(|(id, _)| id)
21        .collect();
22    for s in to_remove {
23        m.customs.delete(s);
24    }
25    passes::gc::run(m);
26}