Skip to main content

graphannis_malloc_size_of/
lib.rs

1// Copyright 2016-2017 The Servo Project Developers.
2//
3// Licensed under the Apache License, Version 2.0
4// <http://www.apache.org/licenses/LICENSE-2.0>.
5// This file may not be copied, modified, or distributed
6// except according to those terms.
7
8//! A crate for measuring the heap usage of data structures in a way that
9//! integrates with Firefox's memory reporting, particularly the use of
10//! mozjemalloc and DMD. In particular, it has the following features.
11//! - It isn't bound to a particular heap allocator.
12//! - It provides traits for both "shallow" and "deep" measurement, which gives
13//!   flexibility in the cases where the traits can't be used.
14//! - It allows for measuring blocks even when only an interior pointer can be
15//!   obtained for heap allocations, e.g. `HashSet` and `HashMap`. (This relies
16//!   on the heap allocator having suitable support, which mozjemalloc has.)
17//! - It allows handling of types like `Rc` and `Arc` by providing traits that
18//!   are different to the ones for non-graph structures.
19//!
20//! Suggested uses are as follows.
21//! - When possible, use the `MallocSizeOf` trait. (Deriving support is
22//!   provided by the `malloc_size_of_derive` crate.)
23//! - If you need an additional synchronization argument, provide a function
24//!   that is like the standard trait method, but with the extra argument.
25//! - If you need multiple measurements for a type, provide a function named
26//!   `add_size_of` that takes a mutable reference to a struct that contains
27//!   the multiple measurement fields.
28//! - When deep measurement (via `MallocSizeOf`) cannot be implemented for a
29//!   type, shallow measurement (via `MallocShallowSizeOf`) in combination with
30//!   iteration can be a useful substitute.
31//! - `Rc` and `Arc` are always tricky, which is why `MallocSizeOf` is not (and
32//!   should not be) implemented for them.
33//! - If an `Rc` or `Arc` is known to be a "primary" reference and can always
34//!   be measured, it should be measured via the `MallocUnconditionalSizeOf`
35//!   trait.
36//! - If an `Rc` or `Arc` should be measured only if it hasn't been seen
37//!   before, it should be measured via the `MallocConditionalSizeOf` trait.
38//! - Using universal function call syntax is a good idea when measuring boxed
39//!   fields in structs, because it makes it clear that the Box is being
40//!   measured as well as the thing it points to. E.g.
41//!   `<Box<_> as MallocSizeOf>::size_of(field, ops)`.
42
43#[cfg(feature = "app_units")]
44extern crate app_units;
45#[cfg(feature = "cssparser")]
46extern crate cssparser;
47#[cfg(feature = "serde")]
48extern crate serde;
49#[cfg(feature = "serde_bytes")]
50extern crate serde_bytes;
51#[cfg(feature = "smallbitvec")]
52extern crate smallbitvec;
53#[cfg(feature = "smallvec")]
54extern crate smallvec;
55#[cfg(feature = "string_cache")]
56extern crate string_cache;
57#[cfg(feature = "thin_slice")]
58extern crate thin_slice;
59#[cfg(feature = "url")]
60extern crate url;
61#[cfg(feature = "void")]
62extern crate void;
63#[cfg(feature = "xml5ever")]
64extern crate xml5ever;
65
66#[cfg(feature = "serde_bytes")]
67use serde_bytes::ByteBuf;
68use std::hash::{BuildHasher, Hash};
69use std::mem::size_of;
70use std::ops::Range;
71use std::ops::{Deref, DerefMut};
72use std::os::raw::c_void;
73
74#[cfg(feature = "void")]
75use void::Void;
76
77/// A C function that takes a pointer to a heap allocation and returns its size.
78type VoidPtrToSizeFn = unsafe extern "C" fn(ptr: *const c_void) -> usize;
79
80/// A closure implementing a stateful predicate on pointers.
81type VoidPtrToBoolFnMut = dyn FnMut(*const c_void) -> bool;
82
83/// Operations used when measuring heap usage of data structures.
84pub struct MallocSizeOfOps {
85    /// A function that returns the size of a heap allocation.
86    size_of_op: VoidPtrToSizeFn,
87
88    /// Like `size_of_op`, but can take an interior pointer. Optional because
89    /// not all allocators support this operation. If it's not provided, some
90    /// memory measurements will actually be computed estimates rather than
91    /// real and accurate measurements.
92    enclosing_size_of_op: Option<VoidPtrToSizeFn>,
93
94    /// Check if a pointer has been seen before, and remember it for next time.
95    /// Useful when measuring `Rc`s and `Arc`s. Optional, because many places
96    /// don't need it.
97    have_seen_ptr_op: Option<Box<VoidPtrToBoolFnMut>>,
98}
99
100impl MallocSizeOfOps {
101    pub fn new(
102        size_of: VoidPtrToSizeFn,
103        malloc_enclosing_size_of: Option<VoidPtrToSizeFn>,
104        have_seen_ptr: Option<Box<VoidPtrToBoolFnMut>>,
105    ) -> Self {
106        MallocSizeOfOps {
107            size_of_op: size_of,
108            enclosing_size_of_op: malloc_enclosing_size_of,
109            have_seen_ptr_op: have_seen_ptr,
110        }
111    }
112
113    /// Check if an allocation is empty. This relies on knowledge of how Rust
114    /// handles empty allocations, which may change in the future.
115    fn is_empty<T: ?Sized>(ptr: *const T) -> bool {
116        // The correct condition is this:
117        //   `ptr as usize <= ::std::mem::align_of::<T>()`
118        // But we can't call align_of() on a ?Sized T. So we approximate it
119        // with the following. 256 is large enough that it should always be
120        // larger than the required alignment, but small enough that it is
121        // always in the first page of memory and therefore not a legitimate
122        // address.
123        return ptr as *const usize as usize <= 256;
124    }
125
126    /// Call `size_of_op` on `ptr`, first checking that the allocation isn't
127    /// empty, because some types (such as `Vec`) utilize empty allocations.
128    pub unsafe fn malloc_size_of<T: ?Sized>(&self, ptr: *const T) -> usize {
129        if MallocSizeOfOps::is_empty(ptr) {
130            0
131        } else {
132            (self.size_of_op)(ptr as *const c_void)
133        }
134    }
135
136    /// Is an `enclosing_size_of_op` available?
137    pub fn has_malloc_enclosing_size_of(&self) -> bool {
138        self.enclosing_size_of_op.is_some()
139    }
140
141    /// Call `enclosing_size_of_op`, which must be available, on `ptr`, which
142    /// must not be empty.
143    pub unsafe fn malloc_enclosing_size_of<T>(&self, ptr: *const T) -> usize {
144        assert!(!MallocSizeOfOps::is_empty(ptr));
145        (self.enclosing_size_of_op.unwrap())(ptr as *const c_void)
146    }
147
148    /// Call `have_seen_ptr_op` on `ptr`.
149    pub fn have_seen_ptr<T>(&mut self, ptr: *const T) -> bool {
150        let have_seen_ptr_op = self
151            .have_seen_ptr_op
152            .as_mut()
153            .expect("missing have_seen_ptr_op");
154        have_seen_ptr_op(ptr as *const c_void)
155    }
156}
157
158/// Trait for measuring the "deep" heap usage of a data structure. This is the
159/// most commonly-used of the traits.
160pub trait MallocSizeOf {
161    /// Measure the heap usage of all descendant heap-allocated structures, but
162    /// not the space taken up by the value itself.
163    fn size_of(&self, ops: &mut MallocSizeOfOps) -> usize;
164}
165
166/// Trait for measuring the "shallow" heap usage of a container.
167pub trait MallocShallowSizeOf {
168    /// Measure the heap usage of immediate heap-allocated descendant
169    /// structures, but not the space taken up by the value itself. Anything
170    /// beyond the immediate descendants must be measured separately, using
171    /// iteration.
172    fn shallow_size_of(&self, ops: &mut MallocSizeOfOps) -> usize;
173}
174
175/// Like `MallocSizeOf`, but with a different name so it cannot be used
176/// accidentally with derive(MallocSizeOf). For use with types like `Rc` and
177/// `Arc` when appropriate (e.g. when measuring a "primary" reference).
178pub trait MallocUnconditionalSizeOf {
179    /// Measure the heap usage of all heap-allocated descendant structures, but
180    /// not the space taken up by the value itself.
181    fn unconditional_size_of(&self, ops: &mut MallocSizeOfOps) -> usize;
182}
183
184/// `MallocUnconditionalSizeOf` combined with `MallocShallowSizeOf`.
185pub trait MallocUnconditionalShallowSizeOf {
186    /// `unconditional_size_of` combined with `shallow_size_of`.
187    fn unconditional_shallow_size_of(&self, ops: &mut MallocSizeOfOps) -> usize;
188}
189
190/// Like `MallocSizeOf`, but only measures if the value hasn't already been
191/// measured. For use with types like `Rc` and `Arc` when appropriate (e.g.
192/// when there is no "primary" reference).
193pub trait MallocConditionalSizeOf {
194    /// Measure the heap usage of all heap-allocated descendant structures, but
195    /// not the space taken up by the value itself, and only if that heap usage
196    /// hasn't already been measured.
197    fn conditional_size_of(&self, ops: &mut MallocSizeOfOps) -> usize;
198}
199
200/// `MallocConditionalSizeOf` combined with `MallocShallowSizeOf`.
201pub trait MallocConditionalShallowSizeOf {
202    /// `conditional_size_of` combined with `shallow_size_of`.
203    fn conditional_shallow_size_of(&self, ops: &mut MallocSizeOfOps) -> usize;
204}
205
206impl MallocSizeOf for String {
207    fn size_of(&self, ops: &mut MallocSizeOfOps) -> usize {
208        unsafe { ops.malloc_size_of(self.as_ptr()) }
209    }
210}
211
212#[cfg(feature = "smartstring")]
213impl MallocSizeOf for smartstring::alias::String {
214    fn size_of(&self, ops: &mut MallocSizeOfOps) -> usize {
215        if self.is_inline() {
216            return 0;
217        }
218        unsafe { ops.malloc_size_of(self.as_ptr()) }
219    }
220}
221
222impl<'a, T: ?Sized> MallocSizeOf for &'a T {
223    fn size_of(&self, _ops: &mut MallocSizeOfOps) -> usize {
224        // Zero makes sense for a non-owning reference.
225        0
226    }
227}
228
229impl<T: ?Sized> MallocShallowSizeOf for Box<T> {
230    fn shallow_size_of(&self, ops: &mut MallocSizeOfOps) -> usize {
231        unsafe { ops.malloc_size_of(&**self) }
232    }
233}
234
235impl<T: MallocSizeOf + ?Sized> MallocSizeOf for Box<T> {
236    fn size_of(&self, ops: &mut MallocSizeOfOps) -> usize {
237        self.shallow_size_of(ops) + (**self).size_of(ops)
238    }
239}
240
241#[cfg(feature = "thin_slice")]
242impl<T> MallocShallowSizeOf for thin_slice::ThinBoxedSlice<T> {
243    fn shallow_size_of(&self, ops: &mut MallocSizeOfOps) -> usize {
244        let mut n = 0;
245        unsafe {
246            n += thin_slice::ThinBoxedSlice::spilled_storage(self)
247                .map_or(0, |ptr| ops.malloc_size_of(ptr));
248            n += ops.malloc_size_of(&**self);
249        }
250        n
251    }
252}
253
254#[cfg(feature = "thin_slice")]
255impl<T: MallocSizeOf> MallocSizeOf for thin_slice::ThinBoxedSlice<T> {
256    fn size_of(&self, ops: &mut MallocSizeOfOps) -> usize {
257        self.shallow_size_of(ops) + (**self).size_of(ops)
258    }
259}
260
261impl MallocSizeOf for () {
262    fn size_of(&self, _ops: &mut MallocSizeOfOps) -> usize {
263        0
264    }
265}
266
267impl<T1, T2> MallocSizeOf for (T1, T2)
268where
269    T1: MallocSizeOf,
270    T2: MallocSizeOf,
271{
272    fn size_of(&self, ops: &mut MallocSizeOfOps) -> usize {
273        self.0.size_of(ops) + self.1.size_of(ops)
274    }
275}
276
277impl<T1, T2, T3> MallocSizeOf for (T1, T2, T3)
278where
279    T1: MallocSizeOf,
280    T2: MallocSizeOf,
281    T3: MallocSizeOf,
282{
283    fn size_of(&self, ops: &mut MallocSizeOfOps) -> usize {
284        self.0.size_of(ops) + self.1.size_of(ops) + self.2.size_of(ops)
285    }
286}
287
288impl<T1, T2, T3, T4> MallocSizeOf for (T1, T2, T3, T4)
289where
290    T1: MallocSizeOf,
291    T2: MallocSizeOf,
292    T3: MallocSizeOf,
293    T4: MallocSizeOf,
294{
295    fn size_of(&self, ops: &mut MallocSizeOfOps) -> usize {
296        self.0.size_of(ops) + self.1.size_of(ops) + self.2.size_of(ops) + self.3.size_of(ops)
297    }
298}
299
300impl<T: MallocSizeOf> MallocSizeOf for Option<T> {
301    fn size_of(&self, ops: &mut MallocSizeOfOps) -> usize {
302        if let Some(val) = self.as_ref() {
303            val.size_of(ops)
304        } else {
305            0
306        }
307    }
308}
309
310impl<T: MallocSizeOf, E: MallocSizeOf> MallocSizeOf for Result<T, E> {
311    fn size_of(&self, ops: &mut MallocSizeOfOps) -> usize {
312        match *self {
313            Ok(ref x) => x.size_of(ops),
314            Err(ref e) => e.size_of(ops),
315        }
316    }
317}
318
319impl<T: MallocSizeOf + Copy> MallocSizeOf for std::cell::Cell<T> {
320    fn size_of(&self, ops: &mut MallocSizeOfOps) -> usize {
321        self.get().size_of(ops)
322    }
323}
324
325impl<T: MallocSizeOf> MallocSizeOf for std::cell::RefCell<T> {
326    fn size_of(&self, ops: &mut MallocSizeOfOps) -> usize {
327        self.borrow().size_of(ops)
328    }
329}
330
331impl<'a, B: ?Sized + ToOwned> MallocSizeOf for std::borrow::Cow<'a, B>
332where
333    B::Owned: MallocSizeOf,
334{
335    fn size_of(&self, ops: &mut MallocSizeOfOps) -> usize {
336        match *self {
337            std::borrow::Cow::Borrowed(_) => 0,
338            std::borrow::Cow::Owned(ref b) => b.size_of(ops),
339        }
340    }
341}
342
343impl<T: MallocSizeOf> MallocSizeOf for [T] {
344    fn size_of(&self, ops: &mut MallocSizeOfOps) -> usize {
345        let mut n = 0;
346        for elem in self.iter() {
347            n += elem.size_of(ops);
348        }
349        n
350    }
351}
352
353#[cfg(feature = "serde_bytes")]
354impl MallocShallowSizeOf for ByteBuf {
355    fn shallow_size_of(&self, ops: &mut MallocSizeOfOps) -> usize {
356        unsafe { ops.malloc_size_of(self.as_ptr()) }
357    }
358}
359
360#[cfg(feature = "serde_bytes")]
361impl MallocSizeOf for ByteBuf {
362    fn size_of(&self, ops: &mut MallocSizeOfOps) -> usize {
363        let mut n = self.shallow_size_of(ops);
364        for elem in self.iter() {
365            n += elem.size_of(ops);
366        }
367        n
368    }
369}
370
371impl<T> MallocShallowSizeOf for Vec<T> {
372    fn shallow_size_of(&self, ops: &mut MallocSizeOfOps) -> usize {
373        unsafe { ops.malloc_size_of(self.as_ptr()) }
374    }
375}
376
377impl<T: MallocSizeOf> MallocSizeOf for Vec<T> {
378    fn size_of(&self, ops: &mut MallocSizeOfOps) -> usize {
379        let mut n = self.shallow_size_of(ops);
380        for elem in self.iter() {
381            n += elem.size_of(ops);
382        }
383        n
384    }
385}
386
387impl<T> MallocShallowSizeOf for std::collections::VecDeque<T> {
388    fn shallow_size_of(&self, ops: &mut MallocSizeOfOps) -> usize {
389        if ops.has_malloc_enclosing_size_of() {
390            if let Some(front) = self.front() {
391                // The front element is an interior pointer.
392                unsafe { ops.malloc_enclosing_size_of(&*front) }
393            } else {
394                // This assumes that no memory is allocated when the VecDeque is empty.
395                0
396            }
397        } else {
398            // An estimate.
399            self.capacity() * size_of::<T>()
400        }
401    }
402}
403
404impl<T: MallocSizeOf> MallocSizeOf for std::collections::VecDeque<T> {
405    fn size_of(&self, ops: &mut MallocSizeOfOps) -> usize {
406        let mut n = self.shallow_size_of(ops);
407        for elem in self.iter() {
408            n += elem.size_of(ops);
409        }
410        n
411    }
412}
413
414#[cfg(feature = "smallvec")]
415impl<A: smallvec::Array> MallocShallowSizeOf for smallvec::SmallVec<A> {
416    fn shallow_size_of(&self, ops: &mut MallocSizeOfOps) -> usize {
417        if self.spilled() {
418            unsafe { ops.malloc_size_of(self.as_ptr()) }
419        } else {
420            0
421        }
422    }
423}
424
425#[cfg(feature = "smallvec")]
426impl<A> MallocSizeOf for smallvec::SmallVec<A>
427where
428    A: smallvec::Array,
429    A::Item: MallocSizeOf,
430{
431    fn size_of(&self, ops: &mut MallocSizeOfOps) -> usize {
432        let mut n = self.shallow_size_of(ops);
433        for elem in self.iter() {
434            n += elem.size_of(ops);
435        }
436        n
437    }
438}
439
440impl<T, S> MallocShallowSizeOf for std::collections::HashSet<T, S>
441where
442    T: Eq + Hash,
443    S: BuildHasher,
444{
445    fn shallow_size_of(&self, ops: &mut MallocSizeOfOps) -> usize {
446        if ops.has_malloc_enclosing_size_of() {
447            // The first value from the iterator gives us an interior pointer.
448            // `ops.malloc_enclosing_size_of()` then gives us the storage size.
449            // This assumes that the `HashSet`'s contents (values and hashes)
450            // are all stored in a single contiguous heap allocation.
451            self.iter()
452                .next()
453                .map_or(0, |t| unsafe { ops.malloc_enclosing_size_of(t) })
454        } else {
455            // An estimate.
456            self.capacity() * (size_of::<T>() + size_of::<usize>())
457        }
458    }
459}
460
461impl<T, S> MallocSizeOf for std::collections::HashSet<T, S>
462where
463    T: Eq + Hash + MallocSizeOf,
464    S: BuildHasher,
465{
466    fn size_of(&self, ops: &mut MallocSizeOfOps) -> usize {
467        let mut n = self.shallow_size_of(ops);
468        for t in self.iter() {
469            n += t.size_of(ops);
470        }
471        n
472    }
473}
474
475impl<K, V, S> MallocShallowSizeOf for std::collections::HashMap<K, V, S>
476where
477    K: Eq + Hash,
478    S: BuildHasher,
479{
480    fn shallow_size_of(&self, ops: &mut MallocSizeOfOps) -> usize {
481        // See the implementation for std::collections::HashSet for details.
482        if ops.has_malloc_enclosing_size_of() {
483            self.values()
484                .next()
485                .map_or(0, |v| unsafe { ops.malloc_enclosing_size_of(v) })
486        } else {
487            self.capacity() * (size_of::<V>() + size_of::<K>() + size_of::<usize>())
488        }
489    }
490}
491
492impl<K, V, S> MallocSizeOf for std::collections::HashMap<K, V, S>
493where
494    K: Eq + Hash + MallocSizeOf,
495    V: MallocSizeOf,
496    S: BuildHasher,
497{
498    fn size_of(&self, ops: &mut MallocSizeOfOps) -> usize {
499        let mut n = self.shallow_size_of(ops);
500        for (k, v) in self.iter() {
501            n += k.size_of(ops);
502            n += v.size_of(ops);
503        }
504        n
505    }
506}
507
508// PhantomData is always 0.
509impl<T> MallocSizeOf for std::marker::PhantomData<T> {
510    fn size_of(&self, _ops: &mut MallocSizeOfOps) -> usize {
511        0
512    }
513}
514
515// XXX: we don't want MallocSizeOf to be defined for Rc and Arc. If negative
516// trait bounds are ever allowed, this code should be uncommented.
517// (We do have a compile-fail test for this:
518// rc_arc_must_not_derive_malloc_size_of.rs)
519//impl<T> !MallocSizeOf for Arc<T> { }
520//impl<T> !MallocShallowSizeOf for Arc<T> { }
521
522/// If a mutex is stored directly as a member of a data type that is being measured,
523/// it is the unique owner of its contents and deserves to be measured.
524///
525/// If a mutex is stored inside of an Arc value as a member of a data type that is being measured,
526/// the Arc will not be automatically measured so there is no risk of overcounting the mutex's
527/// contents.
528impl<T: MallocSizeOf> MallocSizeOf for std::sync::Mutex<T> {
529    fn size_of(&self, ops: &mut MallocSizeOfOps) -> usize {
530        (*self.lock().unwrap()).size_of(ops)
531    }
532}
533
534#[cfg(feature = "smallbitvec")]
535impl MallocSizeOf for smallbitvec::SmallBitVec {
536    fn size_of(&self, ops: &mut MallocSizeOfOps) -> usize {
537        if let Some(ptr) = self.heap_ptr() {
538            unsafe { ops.malloc_size_of(ptr) }
539        } else {
540            0
541        }
542    }
543}
544
545#[cfg(feature = "void")]
546impl MallocSizeOf for Void {
547    #[inline]
548    fn size_of(&self, _ops: &mut MallocSizeOfOps) -> usize {
549        void::unreachable(*self)
550    }
551}
552
553#[cfg(feature = "string_cache")]
554impl<Static: string_cache::StaticAtomSet> MallocSizeOf for string_cache::Atom<Static> {
555    fn size_of(&self, _ops: &mut MallocSizeOfOps) -> usize {
556        0
557    }
558}
559
560/// For use on types where size_of() returns 0.
561#[macro_export]
562macro_rules! malloc_size_of_is_0(
563    ($($ty:ty),+) => (
564        $(
565            impl $crate::MallocSizeOf for $ty {
566                #[inline(always)]
567                fn size_of(&self, _: &mut $crate::MallocSizeOfOps) -> usize {
568                    0
569                }
570            }
571        )+
572    );
573    ($($ty:ident<$($gen:ident),+>),+) => (
574        $(
575        impl<$($gen: $crate::MallocSizeOf),+> $crate::MallocSizeOf for $ty<$($gen),+> {
576            #[inline(always)]
577            fn size_of(&self, _: &mut $crate::MallocSizeOfOps) -> usize {
578                0
579            }
580        }
581        )+
582    );
583);
584
585malloc_size_of_is_0!(bool, char, str);
586malloc_size_of_is_0!(u8, u16, u32, u64, u128, usize);
587malloc_size_of_is_0!(i8, i16, i32, i64, i128, isize);
588malloc_size_of_is_0!(f32, f64);
589
590malloc_size_of_is_0!(std::sync::atomic::AtomicBool);
591malloc_size_of_is_0!(std::sync::atomic::AtomicIsize);
592malloc_size_of_is_0!(std::sync::atomic::AtomicUsize);
593
594malloc_size_of_is_0!(Range<u8>, Range<u16>, Range<u32>, Range<u64>, Range<usize>);
595malloc_size_of_is_0!(Range<i8>, Range<i16>, Range<i32>, Range<i64>, Range<isize>);
596malloc_size_of_is_0!(Range<f32>, Range<f64>);
597
598#[cfg(feature = "app_units")]
599malloc_size_of_is_0!(app_units::Au);
600
601#[cfg(feature = "cssparser")]
602malloc_size_of_is_0!(cssparser::RGBA, cssparser::TokenSerializationType);
603
604#[cfg(feature = "url")]
605impl MallocSizeOf for url::Host {
606    fn size_of(&self, ops: &mut MallocSizeOfOps) -> usize {
607        match *self {
608            url::Host::Domain(ref s) => s.size_of(ops),
609            _ => 0,
610        }
611    }
612}
613
614#[cfg(feature = "xml5ever")]
615impl MallocSizeOf for xml5ever::QualName {
616    fn size_of(&self, ops: &mut MallocSizeOfOps) -> usize {
617        self.prefix.size_of(ops) + self.ns.size_of(ops) + self.local.size_of(ops)
618    }
619}
620
621/// Measurable that defers to inner value and used to verify MallocSizeOf implementation in a
622/// struct.
623#[derive(Clone)]
624pub struct Measurable<T: MallocSizeOf>(pub T);
625
626impl<T: MallocSizeOf> Deref for Measurable<T> {
627    type Target = T;
628
629    fn deref(&self) -> &T {
630        &self.0
631    }
632}
633
634impl<T: MallocSizeOf> DerefMut for Measurable<T> {
635    fn deref_mut(&mut self) -> &mut T {
636        &mut self.0
637    }
638}