Skip to content
This repository was archived by the owner on Jul 9, 2023. It is now read-only.
This repository was archived by the owner on Jul 9, 2023. It is now read-only.

Custom derive for deep_size_of #22

@matklad

Description

@matklad

It might be useful to understand how many bytes in memory a particular datastructure occupies.
If doesn't have any heap-allocated storage, figuring out the answer is easy: it's just std::mem::size_of.

However for data structures which include vectors, maps, boxes, etc, size_of will account only for (comparatively insignificant) stack part of the storage.

One approach to the problem is servo/heapsize, which communicates with the allocator to learn about which blocks of memory are allocated. This is a relatively precise approach, because it accounts for allocator's own overheads, but it requires the use of jemalloc.

A potentially more robust but less precise approach is to walk the datastructure recursively (that is, iterate all the vectors, hash maps, etc) and sum size_of. Looks like this can be done with a proc macro which would custom-derive the following trait:

trait DeepSizeOf {
    /// Returns an estimation of a total size of memory owned by the object,
    /// including heap-managed storage.
    /// This is an estimation and not a precise result, because it 
    /// doesn't account for allocator's overhead. 
    fn deep_size_of(&self) -> usize;
}

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