Replace all uses of the heapsize crate with malloc_size_of.

Servo currently uses `heapsize`, but Stylo/Gecko use `malloc_size_of`.
`malloc_size_of` is better -- it handles various cases that `heapsize` does not
-- so this patch changes Servo to use `malloc_size_of`.

This patch makes the following changes to the `malloc_size_of` crate.

- Adds `MallocSizeOf` trait implementations for numerous types, some built-in
  (e.g. `VecDeque`), some external and Servo-only (e.g. `string_cache`).

- Makes `enclosing_size_of_op` optional, because vanilla jemalloc doesn't
  support that operation.

- For `HashSet`/`HashMap`, falls back to a computed estimate when
  `enclosing_size_of_op` isn't available.

- Adds an extern "C" `malloc_size_of` function that does the actual heap
  measurement; this is based on the same functions from the `heapsize` crate.

This patch makes the following changes elsewhere.

- Converts all the uses of `heapsize` to instead use `malloc_size_of`.

- Disables the "heapsize"/"heap_size" feature for the external crates that
  provide it.

- Removes the `HeapSizeOf` implementation from `hashglobe`.

- Adds `ignore` annotations to a few `Rc`/`Arc`, because `malloc_size_of`
  doesn't derive those types, unlike `heapsize`.
This commit is contained in:
Nicholas Nethercote 2017-10-18 10:42:01 +11:00
parent 421baa854e
commit 4506f0d30c
269 changed files with 1418 additions and 1521 deletions

View file

@ -21,12 +21,12 @@ fnv = "1.0"
fontsan = {git = "https://github.com/servo/fontsan"}
gfx_traits = {path = "../gfx_traits"}
harfbuzz-sys = "0.1"
heapsize = "0.4"
heapsize_derive = "0.1"
ipc-channel = "0.9"
lazy_static = "0.2"
libc = "0.2"
log = "0.3.5"
malloc_size_of = { path = "../malloc_size_of" }
malloc_size_of_derive = { path = "../malloc_size_of_derive" }
msg = {path = "../msg"}
net_traits = {path = "../net_traits"}
ordered-float = "0.4"

View file

@ -43,7 +43,7 @@ pub use style::dom::OpaqueNode;
/// items that involve a blur. This ensures that the display item boundaries include all the ink.
pub static BLUR_INFLATION_FACTOR: i32 = 3;
#[derive(Deserialize, HeapSizeOf, Serialize)]
#[derive(Deserialize, MallocSizeOf, Serialize)]
pub struct DisplayList {
pub list: Vec<DisplayItem>,
}
@ -314,7 +314,7 @@ impl<'a> Iterator for DisplayListTraversal<'a> {
/// Display list sections that make up a stacking context. Each section here refers
/// to the steps in CSS 2.1 Appendix E.
///
#[derive(Clone, Copy, Debug, Deserialize, Eq, HeapSizeOf, Ord, PartialEq, PartialOrd, Serialize)]
#[derive(Clone, Copy, Debug, Deserialize, Eq, MallocSizeOf, Ord, PartialEq, PartialOrd, Serialize)]
pub enum DisplayListSection {
BackgroundAndBorders,
BlockBackgroundsAndBorders,
@ -322,14 +322,14 @@ pub enum DisplayListSection {
Outlines,
}
#[derive(Clone, Copy, Debug, Deserialize, Eq, HeapSizeOf, Ord, PartialEq, PartialOrd, Serialize)]
#[derive(Clone, Copy, Debug, Deserialize, Eq, MallocSizeOf, Ord, PartialEq, PartialOrd, Serialize)]
pub enum StackingContextType {
Real,
PseudoPositioned,
PseudoFloat,
}
#[derive(Clone, Deserialize, HeapSizeOf, Serialize)]
#[derive(Clone, Deserialize, MallocSizeOf, Serialize)]
/// Represents one CSS stacking context, which may or may not have a hardware layer.
pub struct StackingContext {
/// The ID of this StackingContext for uniquely identifying it.
@ -484,7 +484,7 @@ impl fmt::Debug for StackingContext {
}
}
#[derive(Clone, Debug, Deserialize, HeapSizeOf, Serialize)]
#[derive(Clone, Debug, Deserialize, MallocSizeOf, Serialize)]
pub enum ClipScrollNodeType {
ScrollFrame(ScrollSensitivity),
StickyFrame(StickyFrameInfo),
@ -492,7 +492,7 @@ pub enum ClipScrollNodeType {
}
/// Defines a clip scroll node.
#[derive(Clone, Debug, Deserialize, HeapSizeOf, Serialize)]
#[derive(Clone, Debug, Deserialize, MallocSizeOf, Serialize)]
pub struct ClipScrollNode {
/// The WebRender clip id of this scroll root based on the source of this clip
/// and information about the fragment.
@ -522,7 +522,7 @@ impl ClipScrollNode {
/// One drawing command in the list.
#[derive(Clone, Deserialize, HeapSizeOf, Serialize)]
#[derive(Clone, Deserialize, MallocSizeOf, Serialize)]
pub enum DisplayItem {
SolidColor(Box<SolidColorDisplayItem>),
Text(Box<TextDisplayItem>),
@ -541,7 +541,7 @@ pub enum DisplayItem {
}
/// Information common to all display items.
#[derive(Clone, Deserialize, HeapSizeOf, Serialize)]
#[derive(Clone, Deserialize, MallocSizeOf, Serialize)]
pub struct BaseDisplayItem {
/// The boundaries of the display item, in layer coordinates.
pub bounds: Rect<Au>,
@ -600,7 +600,7 @@ impl BaseDisplayItem {
/// A clipping region for a display item. Currently, this can describe rectangles, rounded
/// rectangles (for `border-radius`), or arbitrary intersections of the two. Arbitrary transforms
/// are not supported because those are handled by the higher-level `StackingContext` abstraction.
#[derive(Clone, Deserialize, HeapSizeOf, PartialEq, Serialize)]
#[derive(Clone, Deserialize, MallocSizeOf, PartialEq, Serialize)]
pub struct ClippingRegion {
/// The main rectangular region. This does not include any corners.
pub main: Rect<Au>,
@ -614,7 +614,7 @@ pub struct ClippingRegion {
/// A complex clipping region. These don't as easily admit arbitrary intersection operations, so
/// they're stored in a list over to the side. Currently a complex clipping region is just a
/// rounded rectangle, but the CSS WGs will probably make us throw more stuff in here eventually.
#[derive(Clone, Debug, Deserialize, HeapSizeOf, PartialEq, Serialize)]
#[derive(Clone, Debug, Deserialize, MallocSizeOf, PartialEq, Serialize)]
pub struct ComplexClippingRegion {
/// The boundaries of the rectangle.
pub rect: Rect<Au>,
@ -784,7 +784,7 @@ impl ComplexClippingRegion {
/// Metadata attached to each display item. This is useful for performing auxiliary threads with
/// the display list involving hit testing: finding the originating DOM node and determining the
/// cursor to use when the element is hovered over.
#[derive(Clone, Copy, Deserialize, HeapSizeOf, Serialize)]
#[derive(Clone, Copy, Deserialize, MallocSizeOf, Serialize)]
pub struct DisplayItemMetadata {
/// The DOM node from which this display item originated.
pub node: OpaqueNode,
@ -794,7 +794,7 @@ pub struct DisplayItemMetadata {
}
/// Paints a solid color.
#[derive(Clone, Deserialize, HeapSizeOf, Serialize)]
#[derive(Clone, Deserialize, MallocSizeOf, Serialize)]
pub struct SolidColorDisplayItem {
/// Fields common to all display items.
pub base: BaseDisplayItem,
@ -804,13 +804,13 @@ pub struct SolidColorDisplayItem {
}
/// Paints text.
#[derive(Clone, Deserialize, HeapSizeOf, Serialize)]
#[derive(Clone, Deserialize, MallocSizeOf, Serialize)]
pub struct TextDisplayItem {
/// Fields common to all display items.
pub base: BaseDisplayItem,
/// The text run.
#[ignore_heap_size_of = "Because it is non-owning"]
#[ignore_malloc_size_of = "Because it is non-owning"]
pub text_run: Arc<TextRun>,
/// The range of text within the text run.
@ -826,7 +826,7 @@ pub struct TextDisplayItem {
pub orientation: TextOrientation,
}
#[derive(Clone, Deserialize, Eq, HeapSizeOf, PartialEq, Serialize)]
#[derive(Clone, Deserialize, Eq, MallocSizeOf, PartialEq, Serialize)]
pub enum TextOrientation {
Upright,
SidewaysLeft,
@ -834,13 +834,13 @@ pub enum TextOrientation {
}
/// Paints an image.
#[derive(Clone, Deserialize, HeapSizeOf, Serialize)]
#[derive(Clone, Deserialize, MallocSizeOf, Serialize)]
pub struct ImageDisplayItem {
pub base: BaseDisplayItem,
pub webrender_image: WebRenderImageInfo,
#[ignore_heap_size_of = "Because it is non-owning"]
#[ignore_malloc_size_of = "Because it is non-owning"]
pub image_data: Option<Arc<IpcSharedMemory>>,
/// The dimensions to which the image display item should be stretched. If this is smaller than
@ -857,14 +857,14 @@ pub struct ImageDisplayItem {
pub image_rendering: image_rendering::T,
}
/// Paints an iframe.
#[derive(Clone, Deserialize, HeapSizeOf, Serialize)]
#[derive(Clone, Deserialize, MallocSizeOf, Serialize)]
pub struct IframeDisplayItem {
pub base: BaseDisplayItem,
pub iframe: PipelineId,
}
/// Paints a gradient.
#[derive(Clone, Deserialize, HeapSizeOf, Serialize)]
#[derive(Clone, Deserialize, MallocSizeOf, Serialize)]
pub struct Gradient {
/// The start point of the gradient (computed during display list construction).
pub start_point: Point2D<Au>,
@ -879,7 +879,7 @@ pub struct Gradient {
pub repeating: bool,
}
#[derive(Clone, Deserialize, HeapSizeOf, Serialize)]
#[derive(Clone, Deserialize, MallocSizeOf, Serialize)]
pub struct GradientDisplayItem {
/// Fields common to all display item.
pub base: BaseDisplayItem,
@ -889,7 +889,7 @@ pub struct GradientDisplayItem {
}
/// Paints a radial gradient.
#[derive(Clone, Deserialize, HeapSizeOf, Serialize)]
#[derive(Clone, Deserialize, MallocSizeOf, Serialize)]
pub struct RadialGradient {
/// The center point of the gradient.
pub center: Point2D<Au>,
@ -904,7 +904,7 @@ pub struct RadialGradient {
pub repeating: bool,
}
#[derive(Clone, Deserialize, HeapSizeOf, Serialize)]
#[derive(Clone, Deserialize, MallocSizeOf, Serialize)]
pub struct RadialGradientDisplayItem {
/// Fields common to all display item.
pub base: BaseDisplayItem,
@ -914,7 +914,7 @@ pub struct RadialGradientDisplayItem {
}
/// A normal border, supporting CSS border styles.
#[derive(Clone, Deserialize, HeapSizeOf, Serialize)]
#[derive(Clone, Deserialize, MallocSizeOf, Serialize)]
pub struct NormalBorder {
/// Border colors.
pub color: SideOffsets2D<ColorF>,
@ -929,7 +929,7 @@ pub struct NormalBorder {
}
/// A border that is made of image segments.
#[derive(Clone, Deserialize, HeapSizeOf, Serialize)]
#[derive(Clone, Deserialize, MallocSizeOf, Serialize)]
pub struct ImageBorder {
/// The image this border uses, border-image-source.
pub image: WebRenderImageInfo,
@ -951,7 +951,7 @@ pub struct ImageBorder {
}
/// A border that is made of linear gradient
#[derive(Clone, Deserialize, HeapSizeOf, Serialize)]
#[derive(Clone, Deserialize, MallocSizeOf, Serialize)]
pub struct GradientBorder {
/// The gradient info that this border uses, border-image-source.
pub gradient: Gradient,
@ -961,7 +961,7 @@ pub struct GradientBorder {
}
/// A border that is made of radial gradient
#[derive(Clone, Deserialize, HeapSizeOf, Serialize)]
#[derive(Clone, Deserialize, MallocSizeOf, Serialize)]
pub struct RadialGradientBorder {
/// The gradient info that this border uses, border-image-source.
pub gradient: RadialGradient,
@ -971,7 +971,7 @@ pub struct RadialGradientBorder {
}
/// Specifies the type of border
#[derive(Clone, Deserialize, HeapSizeOf, Serialize)]
#[derive(Clone, Deserialize, MallocSizeOf, Serialize)]
pub enum BorderDetails {
Normal(NormalBorder),
Image(ImageBorder),
@ -980,7 +980,7 @@ pub enum BorderDetails {
}
/// Paints a border.
#[derive(Clone, Deserialize, HeapSizeOf, Serialize)]
#[derive(Clone, Deserialize, MallocSizeOf, Serialize)]
pub struct BorderDisplayItem {
/// Fields common to all display items.
pub base: BaseDisplayItem,
@ -995,7 +995,7 @@ pub struct BorderDisplayItem {
/// Information about the border radii.
///
/// TODO(pcwalton): Elliptical radii.
#[derive(Clone, Copy, Debug, Deserialize, HeapSizeOf, PartialEq, Serialize)]
#[derive(Clone, Copy, Debug, Deserialize, MallocSizeOf, PartialEq, Serialize)]
pub struct BorderRadii<T> {
pub top_left: Size2D<T>,
pub top_right: Size2D<T>,
@ -1057,7 +1057,7 @@ impl<T> BorderRadii<T> where T: PartialEq + Zero + Clone {
}
/// Paints a line segment.
#[derive(Clone, Deserialize, HeapSizeOf, Serialize)]
#[derive(Clone, Deserialize, MallocSizeOf, Serialize)]
pub struct LineDisplayItem {
pub base: BaseDisplayItem,
@ -1065,12 +1065,12 @@ pub struct LineDisplayItem {
pub color: ColorF,
/// The line segment style.
#[ignore_heap_size_of = "enum type in webrender"]
#[ignore_malloc_size_of = "enum type in webrender"]
pub style: webrender_api::LineStyle,
}
/// Paints a box shadow per CSS-BACKGROUNDS.
#[derive(Clone, Deserialize, HeapSizeOf, Serialize)]
#[derive(Clone, Deserialize, MallocSizeOf, Serialize)]
pub struct BoxShadowDisplayItem {
/// Fields common to all display items.
pub base: BaseDisplayItem,
@ -1100,7 +1100,7 @@ pub struct BoxShadowDisplayItem {
}
/// Defines a text shadow that affects all items until the paired PopTextShadow.
#[derive(Clone, Deserialize, HeapSizeOf, Serialize)]
#[derive(Clone, Deserialize, MallocSizeOf, Serialize)]
pub struct PushTextShadowDisplayItem {
/// Fields common to all display items.
pub base: BaseDisplayItem,
@ -1116,14 +1116,14 @@ pub struct PushTextShadowDisplayItem {
}
/// Defines a text shadow that affects all items until the next PopTextShadow.
#[derive(Clone, Deserialize, HeapSizeOf, Serialize)]
#[derive(Clone, Deserialize, MallocSizeOf, Serialize)]
pub struct PopAllTextShadowsDisplayItem {
/// Fields common to all display items.
pub base: BaseDisplayItem,
}
/// Defines a stacking context.
#[derive(Clone, Deserialize, HeapSizeOf, Serialize)]
#[derive(Clone, Deserialize, MallocSizeOf, Serialize)]
pub struct PushStackingContextItem {
/// Fields common to all display items.
pub base: BaseDisplayItem,
@ -1132,7 +1132,7 @@ pub struct PushStackingContextItem {
}
/// Defines a stacking context.
#[derive(Clone, Deserialize, HeapSizeOf, Serialize)]
#[derive(Clone, Deserialize, MallocSizeOf, Serialize)]
pub struct PopStackingContextItem {
/// Fields common to all display items.
pub base: BaseDisplayItem,
@ -1141,7 +1141,7 @@ pub struct PopStackingContextItem {
}
/// Starts a group of items inside a particular scroll root.
#[derive(Clone, Deserialize, HeapSizeOf, Serialize)]
#[derive(Clone, Deserialize, MallocSizeOf, Serialize)]
pub struct DefineClipScrollNodeItem {
/// Fields common to all display items.
pub base: BaseDisplayItem,
@ -1151,7 +1151,7 @@ pub struct DefineClipScrollNodeItem {
}
/// How a box shadow should be clipped.
#[derive(Clone, Copy, Debug, Deserialize, HeapSizeOf, PartialEq, Serialize)]
#[derive(Clone, Copy, Debug, Deserialize, MallocSizeOf, PartialEq, Serialize)]
pub enum BoxShadowClipMode {
/// No special clipping should occur. This is used for (shadowed) text decorations.
None,
@ -1258,7 +1258,7 @@ impl fmt::Debug for DisplayItem {
}
}
#[derive(Clone, Copy, Deserialize, HeapSizeOf, Serialize)]
#[derive(Clone, Copy, Deserialize, MallocSizeOf, Serialize)]
pub struct WebRenderImageInfo {
pub width: u32,
pub height: u32,

View file

@ -7,7 +7,7 @@ use fnv::FnvHasher;
use font::{Font, FontGroup, FontHandleMethods};
use font_cache_thread::FontCacheThread;
use font_template::FontTemplateDescriptor;
use heapsize::HeapSizeOf;
use malloc_size_of::{MallocSizeOf, MallocSizeOfOps};
use platform::font::FontHandle;
use platform::font_context::FontContextHandle;
use platform::font_template::FontTemplateData;
@ -232,10 +232,10 @@ impl FontContext {
}
}
impl HeapSizeOf for FontContext {
fn heap_size_of_children(&self) -> usize {
impl MallocSizeOf for FontContext {
fn size_of(&self, ops: &mut MallocSizeOfOps) -> usize {
// FIXME(njn): Measure other fields eventually.
self.platform_handle.heap_size_of_children()
self.platform_handle.size_of(ops)
}
}

View file

@ -35,14 +35,15 @@ extern crate gfx_traits;
// shapers. For now, however, this is a hard dependency.
extern crate harfbuzz_sys as harfbuzz;
extern crate heapsize;
#[macro_use] extern crate heapsize_derive;
extern crate ipc_channel;
#[macro_use]
extern crate lazy_static;
extern crate libc;
#[macro_use]
extern crate log;
#[cfg_attr(target_os = "windows", macro_use)]
extern crate malloc_size_of;
#[macro_use] extern crate malloc_size_of_derive;
extern crate msg;
extern crate net_traits;
extern crate ordered_float;

View file

@ -8,7 +8,7 @@ use freetype::freetype::FT_Library;
use freetype::freetype::FT_Memory;
use freetype::freetype::FT_MemoryRec_;
use freetype::freetype::FT_New_Library;
use heapsize::{HeapSizeOf, heap_size_of};
use malloc_size_of::{malloc_size_of, MallocSizeOf, MallocSizeOfOps};
use std::mem;
use std::os::raw::{c_long, c_void};
use std::ptr;
@ -31,7 +31,7 @@ extern fn ft_alloc(mem: FT_Memory, req_size: c_long) -> *mut c_void {
mem::forget(vec);
unsafe {
let actual_size = heap_size_of(ptr as *const _);
let actual_size = malloc_size_of(ptr as *const _);
let user = (*mem).user as *mut User;
(*user).size += actual_size;
}
@ -41,7 +41,7 @@ extern fn ft_alloc(mem: FT_Memory, req_size: c_long) -> *mut c_void {
extern fn ft_free(mem: FT_Memory, ptr: *mut c_void) {
unsafe {
let actual_size = heap_size_of(ptr as *const _);
let actual_size = malloc_size_of(ptr as *const _);
let user = (*mem).user as *mut User;
(*user).size -= actual_size;
@ -55,7 +55,7 @@ extern fn ft_realloc(mem: FT_Memory, _cur_size: c_long, new_req_size: c_long,
let old_actual_size;
let mut vec;
unsafe {
old_actual_size = heap_size_of(old_ptr as *const _);
old_actual_size = malloc_size_of(old_ptr as *const _);
vec = Vec::<u8>::from_raw_parts(old_ptr as *mut u8, old_actual_size, old_actual_size);
};
@ -71,7 +71,7 @@ extern fn ft_realloc(mem: FT_Memory, _cur_size: c_long, new_req_size: c_long,
mem::forget(vec);
unsafe {
let new_actual_size = heap_size_of(new_ptr as *const _);
let new_actual_size = malloc_size_of(new_ptr as *const _);
let user = (*mem).user as *mut User;
(*user).size += new_actual_size;
(*user).size -= old_actual_size;
@ -104,13 +104,13 @@ impl Drop for FreeTypeLibraryHandle {
}
}
impl HeapSizeOf for FreeTypeLibraryHandle {
fn heap_size_of_children(&self) -> usize {
impl MallocSizeOf for FreeTypeLibraryHandle {
fn size_of(&self, ops: &mut MallocSizeOfOps) -> usize {
unsafe {
(*self.user).size +
heap_size_of(self.ctx as *const _) +
heap_size_of(self.mem as *const _) +
heap_size_of(self.user as *const _)
ops.malloc_size_of(self.ctx as *const _) +
ops.malloc_size_of(self.mem as *const _) +
ops.malloc_size_of(self.user as *const _)
}
}
}
@ -123,9 +123,9 @@ pub struct FontContextHandle {
pub ctx: Rc<FreeTypeLibraryHandle>,
}
impl HeapSizeOf for FontContextHandle {
fn heap_size_of_children(&self) -> usize {
self.ctx.heap_size_of_children()
impl MallocSizeOf for FontContextHandle {
fn size_of(&self, ops: &mut MallocSizeOfOps) -> usize {
self.ctx.size_of(ops)
}
}

View file

@ -2,9 +2,7 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
use heapsize::HeapSizeOf;
#[derive(Clone, Debug)]
#[derive(Clone, Debug, MallocSizeOf)]
pub struct FontContextHandle {
ctx: ()
}
@ -15,9 +13,3 @@ impl FontContextHandle {
FontContextHandle { ctx: () }
}
}
impl HeapSizeOf for FontContextHandle {
fn heap_size_of_children(&self) -> usize {
0
}
}

View file

@ -2,8 +2,6 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
use heapsize::HeapSizeOf;
#[derive(Clone, Debug)]
pub struct FontContextHandle;
@ -14,8 +12,4 @@ impl FontContextHandle {
}
}
impl HeapSizeOf for FontContextHandle {
fn heap_size_of_children(&self) -> usize {
0
}
}
malloc_size_of_is_0!(FontContextHandle);