Remove all Arc-less ignores, force reasons, ignore_heap_size_of

This commit is contained in:
Manish Goregaokar 2015-05-27 22:53:01 +05:30
parent 5447d2af3d
commit 13b4bcfbb7
7 changed files with 97 additions and 82 deletions

View file

@ -219,7 +219,6 @@ impl DisplayList {
} }
} }
// FIXME(njn): other fields may be measured later, esp. `layer`
#[derive(HeapSizeOf)] #[derive(HeapSizeOf)]
/// Represents one CSS stacking context, which may or may not have a hardware layer. /// Represents one CSS stacking context, which may or may not have a hardware layer.
pub struct StackingContext { pub struct StackingContext {
@ -227,7 +226,7 @@ pub struct StackingContext {
pub display_list: Box<DisplayList>, pub display_list: Box<DisplayList>,
/// The layer for this stacking context, if there is one. /// The layer for this stacking context, if there is one.
#[ignore_heap_size] #[ignore_heap_size_of = "FIXME(njn): should measure this at some point"]
pub layer: Option<Arc<PaintLayer>>, pub layer: Option<Arc<PaintLayer>>,
/// The position and size of this stacking context. /// The position and size of this stacking context.
@ -240,11 +239,9 @@ pub struct StackingContext {
pub z_index: i32, pub z_index: i32,
/// CSS filters to be applied to this stacking context (including opacity). /// CSS filters to be applied to this stacking context (including opacity).
#[ignore_heap_size]
pub filters: filter::T, pub filters: filter::T,
/// The blend mode with which this stacking context blends with its backdrop. /// The blend mode with which this stacking context blends with its backdrop.
#[ignore_heap_size]
pub blend_mode: mix_blend_mode::T, pub blend_mode: mix_blend_mode::T,
/// A transform to be applied to this stacking context. /// A transform to be applied to this stacking context.
@ -768,7 +765,6 @@ pub struct DisplayItemMetadata {
pub node: OpaqueNode, pub node: OpaqueNode,
/// The value of the `cursor` property when the mouse hovers over this display item. If `None`, /// The value of the `cursor` property when the mouse hovers over this display item. If `None`,
/// this display item is ineligible for pointer events (`pointer-events: none`). /// this display item is ineligible for pointer events (`pointer-events: none`).
#[ignore_heap_size]
pub pointing: Option<Cursor>, pub pointing: Option<Cursor>,
} }
@ -808,7 +804,7 @@ pub struct TextDisplayItem {
pub base: BaseDisplayItem, pub base: BaseDisplayItem,
/// The text run. /// The text run.
#[ignore_heap_size] // We exclude `text_run` because it is non-owning. #[ignore_heap_size_of = "Because it is non-owning"]
pub text_run: Arc<Box<TextRun>>, pub text_run: Arc<Box<TextRun>>,
/// The range of text within the text run. /// The range of text within the text run.
@ -838,7 +834,7 @@ pub enum TextOrientation {
#[derive(Clone, HeapSizeOf)] #[derive(Clone, HeapSizeOf)]
pub struct ImageDisplayItem { pub struct ImageDisplayItem {
pub base: BaseDisplayItem, pub base: BaseDisplayItem,
#[ignore_heap_size] // We exclude `image` here because it is non-owning. #[ignore_heap_size_of = "Because it is non-owning"]
pub image: Arc<Image>, pub image: Arc<Image>,
/// The dimensions to which the image display item should be stretched. If this is smaller than /// The dimensions to which the image display item should be stretched. If this is smaller than
@ -848,7 +844,6 @@ pub struct ImageDisplayItem {
/// The algorithm we should use to stretch the image. See `image_rendering` in CSS-IMAGES-3 § /// The algorithm we should use to stretch the image. See `image_rendering` in CSS-IMAGES-3 §
/// 5.3. /// 5.3.
#[ignore_heap_size]
pub image_rendering: image_rendering::T, pub image_rendering: image_rendering::T,
} }

View file

@ -0,0 +1,81 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* 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/. */
//! Handles the auto-deriving for `#[derive(HeapSizeOf)]`
//!
//! This provides the `#[derive(HeapSizeOf)]` decorator, which
//! generates a `HeapSizeOf` implementation that adds up
//! calls to heap_size_of_children() for all the fields
//! of a struct or enum variant.
//!
//! Fields marked `#[ignore_heap_size_of = "reason"]` will
//! be ignored in this calculation. Providing a reason is compulsory.
use syntax::ext::base::{Annotatable, ExtCtxt};
use syntax::codemap::Span;
use syntax::ptr::P;
use syntax::ast::*;
use syntax::attr::AttrMetaMethods;
use syntax::ext::build::AstBuilder;
use syntax::ext::deriving::generic::*;
pub fn expand_heap_size(cx: &mut ExtCtxt, span: Span, mitem: &MetaItem,
item: Annotatable, push: &mut FnMut(Annotatable)) {
let trait_def = TraitDef {
span: span,
attributes: Vec::new(),
path: ty::Path::new(vec!("util", "mem", "HeapSizeOf")),
additional_bounds: Vec::new(),
generics: ty::LifetimeBounds::empty(),
methods: vec![
MethodDef {
name: "heap_size_of_children",
generics: ty::LifetimeBounds::empty(),
explicit_self: ty::borrowed_explicit_self(),
args: vec!(),
ret_ty: ty::Literal(ty::Path::new_local("usize")),
attributes: vec!(),
is_unsafe: false,
combine_substructure: combine_substructure(Box::new(heap_size_substructure))
}
],
associated_types: vec![],
};
trait_def.expand(cx, mitem, &item, push)
}
/// Defines how the implementation for `heap_size_of_children()` is to be generated.
fn heap_size_substructure(cx: &mut ExtCtxt, trait_span: Span, substr: &Substructure) -> P<Expr> {
let fields = match *substr.fields {
Struct(ref fs) | EnumMatching(_, _, ref fs) => fs,
_ => cx.span_bug(trait_span, "impossible substructure in `#[derive(HeapSizeOf)]`")
};
fields.iter().fold(cx.expr_usize(trait_span, 0), |acc, ref item| {
if item.attrs.iter()
.find(|ref a| {
if a.check_name("ignore_heap_size_of") {
match a.node.value.node {
MetaNameValue(..) => (),
_ => cx.span_err(a.span, "#[ignore_heap_size_of] \
should have an explanation, \
e.g. #[ignore_heap_size_of = \"foo\"]")
}
true
} else {
false
}
})
.is_some() {
acc
} else {
cx.expr_binary(item.span, BiAdd, acc,
cx.expr_method_call(item.span,
item.self_.clone(),
substr.method_ident,
Vec::new()))
}
})
}

View file

@ -1,58 +0,0 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* 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 syntax::ext::base::{Annotatable, ExtCtxt};
use syntax::codemap::Span;
use syntax::ptr::P;
use syntax::ast::*;
use syntax::attr::AttrMetaMethods;
use syntax::ext::build::AstBuilder;
use syntax::ext::deriving::generic::*;
pub fn expand_heapsize(cx: &mut ExtCtxt, span: Span, mitem: &MetaItem,
item: Annotatable, push: &mut FnMut(Annotatable)) {
let trait_def = TraitDef {
span: span,
attributes: Vec::new(),
path: ty::Path::new(vec!("util", "mem", "HeapSizeOf")),
additional_bounds: Vec::new(),
generics: ty::LifetimeBounds::empty(),
methods: vec![
MethodDef {
name: "heap_size_of_children",
generics: ty::LifetimeBounds::empty(),
explicit_self: ty::borrowed_explicit_self(),
args: vec!(),
ret_ty: ty::Literal(ty::Path::new_local("usize")),
attributes: vec!(),
is_unsafe: false,
combine_substructure: combine_substructure(Box::new(heapsize_substructure))
}
],
associated_types: vec![],
};
trait_def.expand(cx, mitem, &item, push)
}
fn heapsize_substructure(cx: &mut ExtCtxt, trait_span: Span, substr: &Substructure) -> P<Expr> {
let fields = match *substr.fields {
Struct(ref fs) | EnumMatching(_, _, ref fs) => fs,
_ => cx.span_bug(trait_span, "impossible substructure in `heapsize`")
};
fields.iter().fold(cx.expr_usize(trait_span, 0),
|acc, ref item| {
if item.attrs.iter()
.find(|ref a| a.check_name("ignore_heap_size"))
.is_some() {
acc
} else {
cx.expr_binary(item.span, BiAdd, acc,
cx.expr_method_call(item.span,
item.self_.clone(),
substr.method_ident,
Vec::new()))
}
})
}

View file

@ -31,8 +31,8 @@ use syntax::parse::token::intern;
// Public for documentation to show up // Public for documentation to show up
/// Handles the auto-deriving for `#[jstraceable]` /// Handles the auto-deriving for `#[jstraceable]`
pub mod jstraceable; pub mod jstraceable;
/// Handles the auto-deriving for `#[heapsize]` /// Handles the auto-deriving for `#[derive(HeapSizeOf)]`
pub mod heapsize; pub mod heap_size;
/// Autogenerates implementations of Reflectable on DOM structs /// Autogenerates implementations of Reflectable on DOM structs
pub mod reflector; pub mod reflector;
pub mod lints; pub mod lints;
@ -45,7 +45,7 @@ pub fn plugin_registrar(reg: &mut Registry) {
reg.register_syntax_extension(intern("dom_struct"), MultiModifier(box jstraceable::expand_dom_struct)); reg.register_syntax_extension(intern("dom_struct"), MultiModifier(box jstraceable::expand_dom_struct));
reg.register_syntax_extension(intern("jstraceable"), MultiDecorator(box jstraceable::expand_jstraceable)); reg.register_syntax_extension(intern("jstraceable"), MultiDecorator(box jstraceable::expand_jstraceable));
reg.register_syntax_extension(intern("_generate_reflector"), MultiDecorator(box reflector::expand_reflector)); reg.register_syntax_extension(intern("_generate_reflector"), MultiDecorator(box reflector::expand_reflector));
reg.register_syntax_extension(intern("derive_HeapSizeOf"), MultiDecorator(box heapsize::expand_heapsize)); reg.register_syntax_extension(intern("derive_HeapSizeOf"), MultiDecorator(box heap_size::expand_heap_size));
reg.register_macro("to_lower", casing::expand_lower); reg.register_macro("to_lower", casing::expand_lower);
reg.register_macro("to_upper", casing::expand_upper); reg.register_macro("to_upper", casing::expand_upper);
reg.register_lint_pass(box lints::transmute_type::TransmutePass as LintPassObject); reg.register_lint_pass(box lints::transmute_type::TransmutePass as LintPassObject);

View file

@ -2935,7 +2935,7 @@ pub mod longhands {
use values::CSSFloat; use values::CSSFloat;
use values::specified::{Angle}; use values::specified::{Angle};
#[derive(Clone, PartialEq, Debug)] #[derive(Clone, PartialEq, Debug, HeapSizeOf)]
pub enum Filter { pub enum Filter {
Blur(Au), Blur(Au),
Brightness(CSSFloat), Brightness(CSSFloat),
@ -2948,7 +2948,7 @@ pub mod longhands {
Sepia(CSSFloat), Sepia(CSSFloat),
} }
#[derive(Clone, PartialEq, Debug)] #[derive(Clone, PartialEq, Debug, HeapSizeOf)]
pub struct T { pub filters: Vec<Filter> } pub struct T { pub filters: Vec<Filter> }
impl T { impl T {
@ -3631,7 +3631,7 @@ pub mod longhands {
use cssparser::ToCss; use cssparser::ToCss;
use std::fmt; use std::fmt;
#[derive(Copy, Clone, Debug, PartialEq)] #[derive(Copy, Clone, Debug, PartialEq, HeapSizeOf)]
pub enum T { pub enum T {
Auto, Auto,
CrispEdges, CrispEdges,

View file

@ -12,7 +12,7 @@ macro_rules! define_css_keyword_enum {
}; };
($name: ident: $( $css: expr => $variant: ident ),+) => { ($name: ident: $( $css: expr => $variant: ident ),+) => {
#[allow(non_camel_case_types)] #[allow(non_camel_case_types)]
#[derive(Clone, Eq, PartialEq, Copy, Hash, RustcEncodable, Debug)] #[derive(Clone, Eq, PartialEq, Copy, Hash, RustcEncodable, Debug, HeapSizeOf)]
pub enum $name { pub enum $name {
$( $variant ),+ $( $variant ),+
} }
@ -68,7 +68,6 @@ macro_rules! define_numbered_css_keyword_enum {
} }
} }
pub type CSSFloat = f32; pub type CSSFloat = f32;
@ -607,7 +606,7 @@ pub mod specified {
} }
} }
#[derive(Clone, PartialEq, PartialOrd, Copy, Debug)] #[derive(Clone, PartialEq, PartialOrd, Copy, Debug, HeapSizeOf)]
pub struct Angle(pub CSSFloat); pub struct Angle(pub CSSFloat);
impl ToCss for Angle { impl ToCss for Angle {

View file

@ -9,11 +9,12 @@ use std::collections::LinkedList;
use std::mem::transmute; use std::mem::transmute;
use std::sync::Arc; use std::sync::Arc;
use geom::{Point2D, Rect, SideOffsets2D, Size2D, Matrix2D};
use azure::azure_hl::Color;
use cursor::Cursor;
use geom::{Point2D, Rect, SideOffsets2D, Size2D, Matrix2D};
use geometry::Au; use geometry::Au;
use range::Range; use range::Range;
use azure::azure_hl::Color;
extern { extern {
// Get the size of a heap block. // Get the size of a heap block.
@ -41,9 +42,6 @@ pub fn heap_size_of(ptr: *const c_void) -> usize {
// return multiple measurements -- e.g. measure text separately from images -- are also possible, // return multiple measurements -- e.g. measure text separately from images -- are also possible,
// and should be used when appropriate. // and should be used when appropriate.
// //
// FIXME(njn): it would be nice to be able to derive this trait automatically, given that
// implementations are mostly repetitive and mechanical.
//
pub trait HeapSizeOf { pub trait HeapSizeOf {
/// Measure the size of any heap-allocated structures that hang off this value, but not the /// Measure the size of any heap-allocated structures that hang off this value, but not the
/// space taken up by the value itself (i.e. what size_of::<T> measures, more or less); that /// space taken up by the value itself (i.e. what size_of::<T> measures, more or less); that
@ -165,7 +163,7 @@ impl<T> Drop for LinkedList2<T> {
} }
/// For use on types defined in external crates /// For use on types defined in external crates
/// with known heap sizes /// with known heap sizes.
#[macro_export] #[macro_export]
macro_rules! known_heap_size( macro_rules! known_heap_size(
($size:expr, $($ty:ident),+) => ( ($size:expr, $($ty:ident),+) => (
@ -197,5 +195,5 @@ known_heap_size!(0, bool, f32, f64);
known_heap_size!(0, Rect<T>, Point2D<T>, Size2D<T>, Matrix2D<T>, SideOffsets2D<T>); known_heap_size!(0, Rect<T>, Point2D<T>, Size2D<T>, Matrix2D<T>, SideOffsets2D<T>);
known_heap_size!(0, Au, Color); known_heap_size!(0, Au, Color, Cursor);
known_heap_size!(0, Range<T>); known_heap_size!(0, Range<T>);