Measure Arc<Locked<T>> fields properly.

Currently when we measure various Arc<Locked<T>> fields we don't measure the T
itself, but only the descendants of the T. This patch fixes this.

This fix requires introducing a new trait, MallocUnconditionalShallowSizeOf,
which is implemented for servo_arc::Arc. A similar trait,
MallocConditionalShallowSizeOf, is also introduced, though it has no uses as
yet.
This commit is contained in:
Nicholas Nethercote 2017-09-12 16:36:28 +10:00
parent 7f4cb1861b
commit 779fbda81d
8 changed files with 61 additions and 22 deletions

View file

@ -141,9 +141,10 @@ pub trait MallocSizeOf {
/// Trait for measuring the "shallow" heap usage of a container.
pub trait MallocShallowSizeOf {
/// Measure the heap usage of direct descendant structures, but not the
/// space taken up by the value itself. Anything pointed to by the
/// immediate children must be measured separately, using iteration.
/// Measure the heap usage of immediate heap-allocated descendant
/// structures, but not the space taken up by the value itself. Anything
/// beyond the immediate descendants must be measured separately, using
/// iteration.
fn shallow_size_of(&self, ops: &mut MallocSizeOfOps) -> usize;
}
@ -156,6 +157,12 @@ pub trait MallocUnconditionalSizeOf {
fn unconditional_size_of(&self, ops: &mut MallocSizeOfOps) -> usize;
}
/// `MallocUnconditionalSizeOf` combined with `MallocShallowSizeOf`.
pub trait MallocUnconditionalShallowSizeOf {
/// `unconditional_size_of` combined with `shallow_size_of`.
fn unconditional_shallow_size_of(&self, ops: &mut MallocSizeOfOps) -> usize;
}
/// Like `MallocSizeOf`, but only measures if the value hasn't already been
/// measured. For use with types like `Rc` and `Arc` when appropriate (e.g.
/// when there is no "primary" reference).
@ -166,6 +173,12 @@ pub trait MallocConditionalSizeOf {
fn conditional_size_of(&self, ops: &mut MallocSizeOfOps) -> usize;
}
/// `MallocConditionalSizeOf` combined with `MallocShallowSizeOf`.
pub trait MallocConditionalShallowSizeOf {
/// `conditional_size_of` combined with `shallow_size_of`.
fn conditional_shallow_size_of(&self, ops: &mut MallocSizeOfOps) -> usize;
}
impl<T> MallocShallowSizeOf for Box<T> {
fn shallow_size_of(&self, ops: &mut MallocSizeOfOps) -> usize {
ops.malloc_size_of(&**self)
@ -294,9 +307,25 @@ impl<K, V, S> MallocSizeOf for HashMap<K, V, S>
//impl<T> !MallocSizeOf for Arc<T> { }
//impl<T> !MallocShallowSizeOf for Arc<T> { }
impl<T> MallocUnconditionalShallowSizeOf for Arc<T> {
fn unconditional_shallow_size_of(&self, ops: &mut MallocSizeOfOps) -> usize {
ops.malloc_size_of(self.heap_ptr())
}
}
impl<T: MallocSizeOf> MallocUnconditionalSizeOf for Arc<T> {
fn unconditional_size_of(&self, ops: &mut MallocSizeOfOps) -> usize {
ops.malloc_size_of(self.heap_ptr()) + (**self).size_of(ops)
self.unconditional_shallow_size_of(ops) + (**self).size_of(ops)
}
}
impl<T> MallocConditionalShallowSizeOf for Arc<T> {
fn conditional_shallow_size_of(&self, ops: &mut MallocSizeOfOps) -> usize {
if ops.have_seen_ptr(self.heap_ptr()) {
0
} else {
self.unconditional_shallow_size_of(ops)
}
}
}