Skip to content

How to calculate in runtime the memory each IValue takes? #23

@gkorland

Description

@gkorland

We need to report in runtime the memory each IValue takes.
What is the best way to calculate its overhead, even ignoring the IString intern?

I tried to sum it calc it this way

fn size(v: &IValue) -> Result<usize, Error> {
    let res = size_of::<IValue>()
        + match v.type_() {
            ValueType::Null | ValueType::Bool | ValueType::Number => 0,
            ValueType::String => v.as_string().unwrap().len(),
            ValueType::Array => v
                .as_array()
                .unwrap()
                .into_iter()
                .map(|v| size(v).unwrap())
                .sum(),
            ValueType::Object => v
                .as_object()
                .unwrap()
                .into_iter()
                .map(|(s, v)|  s.len() + size(v).unwrap())
                .sum(),
        };
    Ok(res)
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions