iota_util_mem/
external_impls.rs

1// Copyright (c) Mysten Labs, Inc.
2// Modifications Copyright (c) 2024 IOTA Stiftung
3// SPDX-License-Identifier: Apache-2.0
4
5use crate::{MallocShallowSizeOf, MallocSizeOf};
6
7// ed25519_consensus
8malloc_size_of_is_0!(ed25519_consensus::Signature);
9
10// fastcrypto
11malloc_size_of_is_0!(fastcrypto::bls12381::min_sig::BLS12381PublicKey);
12malloc_size_of_is_0!(fastcrypto::bls12381::min_sig::BLS12381PublicKeyAsBytes);
13malloc_size_of_is_0!(fastcrypto::bls12381::min_sig::BLS12381Signature);
14malloc_size_of_is_0!(fastcrypto::bls12381::min_sig::BLS12381AggregateSignature);
15malloc_size_of_is_0!(fastcrypto::bls12381::min_sig::BLS12381AggregateSignatureAsBytes);
16malloc_size_of_is_0!(fastcrypto::bls12381::min_pk::BLS12381PublicKey);
17malloc_size_of_is_0!(fastcrypto::bls12381::min_pk::BLS12381Signature);
18malloc_size_of_is_0!(fastcrypto::bls12381::min_pk::BLS12381AggregateSignature);
19malloc_size_of_is_0!(fastcrypto::ed25519::Ed25519PublicKey);
20malloc_size_of_is_0!(fastcrypto::ed25519::Ed25519Signature);
21impl MallocSizeOf for fastcrypto::ed25519::Ed25519AggregateSignature {
22    fn size_of(&self, ops: &mut crate::MallocSizeOfOps) -> usize {
23        self.sigs.size_of(ops)
24    }
25}
26malloc_size_of_is_0!(fastcrypto::groups::ristretto255::RistrettoPoint);
27impl<G> MallocSizeOf for fastcrypto_tbls::dkg::Complaint<G>
28where
29    G: fastcrypto::groups::GroupElement,
30{
31    fn size_of(&self, _ops: &mut crate::MallocSizeOfOps) -> usize {
32        0
33    }
34}
35impl<G> MallocSizeOf for fastcrypto_tbls::dkg::Confirmation<G>
36where
37    G: fastcrypto::groups::GroupElement,
38{
39    fn size_of(&self, ops: &mut crate::MallocSizeOfOps) -> usize {
40        self.complaints.size_of(ops)
41    }
42}
43impl<G, EG> MallocSizeOf for fastcrypto_tbls::dkg_v0::Message<G, EG>
44where
45    G: fastcrypto::groups::GroupElement,
46    EG: fastcrypto::groups::GroupElement,
47{
48    fn size_of(&self, ops: &mut crate::MallocSizeOfOps) -> usize {
49        self.encrypted_shares.size_of(ops)
50    }
51}
52impl<G, EG> MallocSizeOf for fastcrypto_tbls::dkg_v1::Message<G, EG>
53where
54    G: fastcrypto::groups::GroupElement,
55    EG: fastcrypto::groups::GroupElement,
56{
57    fn size_of(&self, ops: &mut crate::MallocSizeOfOps) -> usize {
58        self.encrypted_shares.size_of(ops)
59    }
60}
61impl<G> MallocSizeOf for fastcrypto_tbls::ecies_v0::Encryption<G>
62where
63    G: fastcrypto::groups::GroupElement,
64{
65    fn size_of(&self, _ops: &mut crate::MallocSizeOfOps) -> usize {
66        // Can't measure size of internal Vec<u8> here because it's private.
67        0
68    }
69}
70impl<G> MallocSizeOf for fastcrypto_tbls::ecies_v0::MultiRecipientEncryption<G>
71where
72    G: fastcrypto::groups::GroupElement,
73{
74    fn size_of(&self, _ops: &mut crate::MallocSizeOfOps) -> usize {
75        // Can't measure size of internal Vec<Vec<u8>> here because it's private.
76        0
77    }
78}
79impl<G> MallocSizeOf for fastcrypto_tbls::ecies_v1::MultiRecipientEncryption<G>
80where
81    G: fastcrypto::groups::GroupElement,
82{
83    fn size_of(&self, _ops: &mut crate::MallocSizeOfOps) -> usize {
84        // Can't measure size of internal Vec<Vec<u8>> here because it's private.
85        0
86    }
87}
88impl MallocSizeOf for fastcrypto_tbls::polynomial::Poly<fastcrypto::groups::bls12381::G2Element> {
89    fn size_of(&self, _ops: &mut crate::MallocSizeOfOps) -> usize {
90        (self.degree() as usize + 1)
91            * core::mem::size_of::<fastcrypto::groups::bls12381::G2Element>()
92    }
93}
94malloc_size_of_is_0!(fastcrypto::groups::bls12381::G1Element);
95
96// hash_map
97#[cfg(feature = "std")]
98malloc_size_of_is_0!(std::collections::hash_map::RandomState);
99
100// indexmap
101impl<K: MallocSizeOf, V: MallocSizeOf, S> MallocShallowSizeOf for indexmap::IndexMap<K, V, S> {
102    fn shallow_size_of(&self, _ops: &mut crate::MallocSizeOfOps) -> usize {
103        self.capacity()
104            * (core::mem::size_of::<K>()
105                + core::mem::size_of::<V>()
106                + (2 * core::mem::size_of::<usize>()))
107    }
108}
109impl<K: MallocSizeOf, V: MallocSizeOf, S> MallocSizeOf for indexmap::IndexMap<K, V, S> {
110    // This only produces a rough estimate of IndexMap size, because we cannot
111    // access private fields to measure them precisely.
112    fn size_of(&self, ops: &mut crate::MallocSizeOfOps) -> usize {
113        let mut n = self.shallow_size_of(ops);
114        if let (Some(k), Some(v)) = (K::constant_size(), V::constant_size()) {
115            n += self.len() * (k + v)
116        } else {
117            n += self
118                .iter()
119                .fold(n, |acc, (k, v)| acc + k.size_of(ops) + v.size_of(ops))
120        }
121        n
122    }
123}
124
125// roaring
126impl MallocSizeOf for roaring::RoaringBitmap {
127    // This only produces a rough estimate of RoaringBitmap size, because we cannot
128    // access private fields to measure them precisely.
129    fn size_of(&self, _ops: &mut crate::MallocSizeOfOps) -> usize {
130        self.serialized_size()
131    }
132}