Overhaul MallocSizeOf and related things.

This patch makes the MallocSizeOf stuff in Stylo work more like the HeapSizeOf
stuff already in Servo, except better. In particular, it adds deriving support
for MallocSizeOf, which will make it easier to improve coverage.

The patch does the following.

- Combines servo/components/style/stylesheets/memory.rs and the heapsize crate
  into a new crate, malloc_size_of.

- Forks the heapsize_derive crate, calling it malloc_size_of, so that
  MallocSizeOf can be derived.

- Both the new crates have MIT/Apache licenses, like heapsize, in case they are
  incorporated into heapsize in the future.

- Renames the methods within MallocSizeOf and the related traits so they are
  more concise.

- Removes MallocSizeOfWithGuard.

- Adds `derive(MallocSizeOf)` to a lot of types, in some cases replacing an
  equivalent or almost-equivalent hand-written implementation.

- Adds stuff so that Rc/Arc can be handled properly.
This commit is contained in:
Nicholas Nethercote 2017-09-12 12:03:01 +10:00
parent 24b2d8d9cf
commit 32548e5312
44 changed files with 1188 additions and 486 deletions

View file

@ -21,8 +21,6 @@ use selectors::matching::{matches_selector, MatchingContext, ElementSelectorFlag
use selectors::parser::{Component, Combinator, SelectorIter};
use smallvec::{SmallVec, VecLike};
use std::hash::{BuildHasherDefault, Hash, Hasher};
#[cfg(feature = "gecko")]
use stylesheets::{MallocEnclosingSizeOfFn, MallocSizeOfFn, MallocSizeOfHash, MallocSizeOfVec};
use stylist::Rule;
/// A hasher implementation that doesn't hash anything, because it expects its
@ -96,6 +94,7 @@ pub trait SelectorMapEntry : Sized + Clone {
///
/// TODO: Tune the initial capacity of the HashMap
#[derive(Debug)]
#[cfg_attr(feature = "gecko", derive(MallocSizeOf))]
#[cfg_attr(feature = "servo", derive(HeapSizeOf))]
pub struct SelectorMap<T: 'static> {
/// A hash from an ID to rules which contain that ID selector.
@ -148,37 +147,6 @@ impl<T: 'static> SelectorMap<T> {
pub fn len(&self) -> usize {
self.count
}
/// Measures heap usage.
#[cfg(feature = "gecko")]
pub fn malloc_size_of_children(&self, malloc_size_of: MallocSizeOfFn,
malloc_enclosing_size_of: MallocEnclosingSizeOfFn)
-> usize {
// Currently we measure the storage used by the HashMaps, and any
// heap-allocated SmallVec values, but not things pointed to by the T
// elements within the SmallVec values.
let mut n = 0;
n += self.id_hash.malloc_shallow_size_of_hash(malloc_enclosing_size_of);
for (_, val) in self.id_hash.iter() {
n += val.malloc_shallow_size_of_vec(malloc_size_of);
}
n += self.class_hash.malloc_shallow_size_of_hash(malloc_enclosing_size_of);
for (_, val) in self.class_hash.iter() {
n += val.malloc_shallow_size_of_vec(malloc_size_of);
}
n += self.local_name_hash.malloc_shallow_size_of_hash(malloc_enclosing_size_of);
for (_, val) in self.local_name_hash.iter() {
n += val.malloc_shallow_size_of_vec(malloc_size_of);
}
n += self.other.malloc_shallow_size_of_vec(malloc_size_of);
n
}
}
impl SelectorMap<Rule> {
@ -493,6 +461,7 @@ fn find_bucket<'a>(mut iter: SelectorIter<'a, SelectorImpl>) -> Bucket<'a> {
/// Wrapper for PrecomputedHashMap that does ASCII-case-insensitive lookup in quirks mode.
#[derive(Debug)]
#[cfg_attr(feature = "gecko", derive(MallocSizeOf))]
#[cfg_attr(feature = "servo", derive(HeapSizeOf))]
pub struct MaybeCaseInsensitiveHashMap<K: PrecomputedHash + Hash + Eq, V: 'static>(PrecomputedHashMap<K, V>);
@ -545,13 +514,3 @@ impl<V: 'static> MaybeCaseInsensitiveHashMap<Atom, V> {
}
}
#[cfg(feature = "gecko")]
impl<K, V> MallocSizeOfHash for MaybeCaseInsensitiveHashMap<K, V>
where K: PrecomputedHash + Eq + Hash
{
fn malloc_shallow_size_of_hash(&self, malloc_enclosing_size_of: MallocEnclosingSizeOfFn)
-> usize {
self.0.malloc_shallow_size_of_hash(malloc_enclosing_size_of)
}
}