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_with_stack_only_item_heap_size_helper<T>(v: &Vec<T>) -> usize {
39    v.capacity() * std::mem::size_of::<T>()
40}
41
42pub fn vec_heap_size_helper<T: GetSize>(v: &Vec<T>) -> usize {
43    impl_vec_alike_heap_size_helper!(v, T)
44}
45
46pub fn vec_heap_size_with_fn_helper<T>(v: &Vec<T>, get_heap_size: impl Fn(&T) -> usize) -> usize {
47    impl_vec_alike_heap_size_with_fn_helper!(v, T, std::mem::size_of::<T>, get_heap_size)
48}
49
50pub fn nunny_vec_heap_size_helper<T: GetSize>(v: &nunny::Vec<T>) -> usize {
51    impl_vec_alike_heap_size_helper!(v, T)
52}
53
54// This is a rough estimation. Use `b.allocation_size()`
55// once https://github.com/rust-num/num-bigint/pull/333 is accepted and released.
56pub fn big_int_heap_size_helper(b: &BigInt) -> usize {
57    b.bits().div_ceil(8) as usize
58}
59
60#[cfg(test)]
61mod tests {
62    use super::*;
63    use crate::utils::multihash::MultihashCode;
64    use fvm_ipld_encoding::DAG_CBOR;
65    use multihash_derive::MultihashDigest as _;
66
67    #[test]
68    fn test_cid() {
69        let cid = Cid::new_v1(DAG_CBOR, MultihashCode::Blake2b256.digest(&[0, 1, 2, 3]));
70        let wrapper = CidWrapper(cid);
71        assert_eq!(std::mem::size_of_val(&cid), wrapper.get_size());
72    }
73
74    #[test]
75    fn test_heap_size_helper() {
76        let keys: nunny::Vec<CidWrapper> = nunny::vec![Cid::default().into(); 3];
77        // It's likely > 3 (4 on my laptop)
78        println!("keys.capacity() = {}", keys.capacity());
79        assert_eq!(
80            nunny_vec_heap_size_helper(&keys),
81            CidWrapper::get_stack_size() * usize::from(keys.capacity())
82        );
83    }
84}