Skip to main content

forest/utils/get_size/
mod.rs

1// Copyright 2019-2026 ChainSafe Systems
2// SPDX-License-Identifier: Apache-2.0, MIT
3
4use cid::Cid;
5use derive_more::{From, Into};
6// re-exports the trait
7pub use get_size2::GetSize;
8use num_bigint::BigInt;
9
10#[derive(Debug, Clone, Hash, Eq, PartialEq, Ord, PartialOrd, From, Into)]
11pub struct CidWrapper(pub Cid);
12impl GetSize for CidWrapper {}
13
14macro_rules! impl_vec_alike_heap_size_with_fn_helper {
15    ($name:ident, $t:ty, $get_stack_size: expr, $get_heap_size: expr) => {{
16        let mut heap_size = 0;
17        // use `____v` to avoid naming conflict
18        for ____v in $name.iter() {
19            heap_size += $get_stack_size() + $get_heap_size(____v);
20        }
21        let additional = usize::from($name.capacity()) - usize::from($name.len());
22        heap_size += additional * $get_stack_size();
23        heap_size
24    }};
25}
26
27macro_rules! impl_vec_alike_heap_size_helper {
28    ($name:ident, $t:ty) => {
29        impl_vec_alike_heap_size_with_fn_helper!(
30            $name,
31            $t,
32            <$t>::get_stack_size,
33            GetSize::get_heap_size
34        )
35    };
36}
37
38pub fn vec_heap_size_helper<T: GetSize>(v: &Vec<T>) -> usize {
39    impl_vec_alike_heap_size_helper!(v, T)
40}
41
42pub fn vec_heap_size_with_fn_helper<T>(v: &Vec<T>, get_heap_size: impl Fn(&T) -> usize) -> usize {
43    impl_vec_alike_heap_size_with_fn_helper!(v, T, std::mem::size_of::<T>, get_heap_size)
44}
45
46pub fn nunny_vec_heap_size_helper<T: GetSize>(v: &nunny::Vec<T>) -> usize {
47    impl_vec_alike_heap_size_helper!(v, T)
48}
49
50// This is a rough estimation. Use `b.allocation_size()`
51// once https://github.com/rust-num/num-bigint/pull/333 is accepted and released.
52pub fn big_int_heap_size_helper(b: &BigInt) -> usize {
53    b.bits().div_ceil(8) as usize
54}
55
56#[cfg(test)]
57mod tests {
58    use super::*;
59    use crate::utils::multihash::MultihashCode;
60    use fvm_ipld_encoding::DAG_CBOR;
61    use multihash_derive::MultihashDigest as _;
62
63    #[test]
64    fn test_cid() {
65        let cid = Cid::new_v1(DAG_CBOR, MultihashCode::Blake2b256.digest(&[0, 1, 2, 3]));
66        let wrapper = CidWrapper(cid);
67        assert_eq!(std::mem::size_of_val(&cid), wrapper.get_size());
68    }
69
70    #[test]
71    fn test_heap_size_helper() {
72        let keys: nunny::Vec<CidWrapper> = nunny::vec![Cid::default().into(); 3];
73        // It's likely > 3 (4 on my laptop)
74        println!("keys.capacity() = {}", keys.capacity());
75        assert_eq!(
76            nunny_vec_heap_size_helper(&keys),
77            CidWrapper::get_stack_size() * usize::from(keys.capacity())
78        );
79    }
80}