Repository: servo/servo
File: components/malloc_size_of/lib.rs (Rust)

Code before:
        sum += masks.iter().map(|x| x.size_of(ops)).sum::<usize>();
        sum += filters.iter().map(|x| x.size_of(ops)).sum::<usize>();
        sum += fontdb.size_of(ops);

        if ops.has_malloc_enclosing_size_of() {
            unsafe {
                sum += ops.malloc_enclosing_size_of(root);
                if !linear_gradients.is_empty() {
                    sum += ops.malloc_enclosing_size_of(linear_gradients.as_ptr());
                }
                if !radial_gradients.is_empty() {
                    sum += ops.malloc_enclosing_size_of(radial_gradients.as_ptr());
                }
                if !patterns.is_empty() {
                    sum += ops.malloc_enclosing_size_of(patterns.as_ptr());
                }
                if !clip_paths.is_empty() {
                    sum += ops.malloc_enclosing_size_of(clip_paths.as_ptr());
                }
                if !masks.is_empty() {
                    sum += ops.malloc_enclosing_size_of(masks.as_ptr());
                }
                if !filters.is_empty() {
                    sum += ops.malloc_enclosing_size_of(filters.as_ptr());
                }
                sum += ops.malloc_enclosing_size_of(fontdb);
            }
        }
        sum
    }
}

impl MallocSizeOf for usvg::Group {
    fn size_of(&self, ops: &mut MallocSizeOfOps) -> usize {
        let id = self.id();
        let children = self.children();
        let filters = self.filters();
        let clip_path = self.clip_path();

        let mut sum = 0;
        sum += id.size_of(ops);
        sum += children.iter().map(|x| x.size_of(ops)).sum::<usize>();
        sum += filters.iter().map(|x| x.size_of(ops)).sum::<usize>();
        sum += clip_path.size_of(ops);

        if ops.has_malloc_enclosing_size_of() {
            unsafe {
                if !id.is_empty() {
                    sum += ops.malloc_enclosing_size_of(id.as_ptr());
                }
                if let Some(c) = clip_path {

Review comment (suggestion):
We don't want to use enclosing_size_of on fontdb, since we already have the owning pointer to it via the `Arc`.

Code after:
                sum += ops.malloc_enclosing_size_of(root);
                if !linear_gradients.is_empty() {
                    sum += ops.malloc_enclosing_size_of(linear_gradients.as_ptr());
                }
                if !radial_gradients.is_empty() {
                    sum += ops.malloc_enclosing_size_of(radial_gradients.as_ptr());
                }
                if !patterns.is_empty() {
                    sum += ops.malloc_enclosing_size_of(patterns.as_ptr());
                }
                if !clip_paths.is_empty() {
                    sum += ops.malloc_enclosing_size_of(clip_paths.as_ptr());
                }
                if !masks.is_empty() {
                    sum += ops.malloc_enclosing_size_of(masks.as_ptr());
                }
                if !filters.is_empty() {
                    sum += ops.malloc_enclosing_size_of(filters.as_ptr());
                }
            }
        }
        sum
    }
}

impl MallocSizeOf for usvg::Group {
    fn size_of(&self, ops: &mut MallocSizeOfOps) -> usize {
        let id = self.id();
        let children = self.children();
        let filters = self.filters();
        let clip_path = self.clip_path();

        let mut sum =
            id.size_of(ops) + children.size_of(ops) + filters.size_of(ops) + clip_path.size_of(ops);

        if ops.has_malloc_enclosing_size_of() {
            unsafe {
                if !id.is_empty() {
                    sum += ops.malloc_enclosing_size_of(id.as_ptr());
                }
                if let Some(c) = clip_path {
                    sum += ops.malloc_enclosing_size_of(c)
                }
                if !children.is_empty() {
                    sum += ops.malloc_enclosing_size_of(children.as_ptr());
                }
                if !filters.is_empty() {
                    sum += ops.malloc_enclosing_size_of(filters.as_ptr());
                }
            }
        }