loro_internal/
estimated_size.rs

1use loro_common::LoroValue;
2
3pub trait EstimatedSize {
4    /// Estimate the storage size of the object in bytes
5    fn estimate_storage_size(&self) -> usize;
6}
7
8impl EstimatedSize for LoroValue {
9    fn estimate_storage_size(&self) -> usize {
10        match self {
11            LoroValue::Null => 1,
12            LoroValue::Bool(_) => 1,
13            LoroValue::Double(_) => 8,
14            LoroValue::I64(i) => 1 + (64 - i.leading_zeros()) as usize / 7,
15            LoroValue::Binary(b) => b.len() + 1,
16            LoroValue::String(s) => s.len() + 1,
17            LoroValue::List(l) => 3 + l.iter().map(|x| x.estimate_storage_size()).sum::<usize>(),
18            LoroValue::Map(m) => {
19                3 + m
20                    .iter()
21                    .map(|(k, v)| k.len() + 3 + v.estimate_storage_size())
22                    .sum::<usize>()
23            }
24            LoroValue::Container(_) => 6,
25        }
26    }
27}