Skip to content

Commit

Permalink
Measure selectors.
Browse files Browse the repository at this point in the history
This patch adds measurement of Selectors within StyleRule. This requires
exposing the pointer within ThinArc.

The patch also adds measurement of the several CssRule variants, in order to
measure nested CssRules (and PropertyDeclarationBlocks) within them:
DocumentRule, MediaRule, PageRule, SupportsRule.
  • Loading branch information
nnethercote committed Sep 7, 2017
1 parent 812cac7 commit 61877b7
Show file tree
Hide file tree
Showing 8 changed files with 95 additions and 15 deletions.
6 changes: 5 additions & 1 deletion components/selectors/parser.rs
Expand Up @@ -423,7 +423,6 @@ impl<Impl: SelectorImpl> Selector<Impl> {
))
}


/// Returns an iterator over this selector in matching order (right-to-left).
/// When a combinator is reached, the iterator will return None, and
/// next_sequence() may be called to continue to the next sequence.
Expand Down Expand Up @@ -494,6 +493,11 @@ impl<Impl: SelectorImpl> Selector<Impl> {
pub fn len(&self) -> usize {
self.0.slice.len()
}

/// Returns the address on the heap of the ThinArc for memory reporting.
pub fn thin_arc_heap_ptr(&self) -> *const ::std::os::raw::c_void {
self.0.heap_ptr()
}
}

#[derive(Clone)]
Expand Down
6 changes: 6 additions & 0 deletions components/servo_arc/lib.rs
Expand Up @@ -676,6 +676,12 @@ impl<H: 'static, T: 'static> ThinArc<H, T> {
// Forward the result.
result
}

/// Returns the address on the heap of the ThinArc itself -- not the T
/// within it -- for memory reporting.
pub fn heap_ptr(&self) -> *const c_void {
self.ptr as *const ArcInner<T> as *const c_void
}
}

impl<H, T> Deref for ThinArc<H, T> {
Expand Down
11 changes: 10 additions & 1 deletion components/style/stylesheets/document_rule.rs
Expand Up @@ -13,7 +13,7 @@ use servo_arc::Arc;
use shared_lock::{DeepCloneParams, DeepCloneWithLock, Locked, SharedRwLock, SharedRwLockReadGuard, ToCssWithGuard};
use std::fmt;
use style_traits::{ToCss, ParseError, StyleParseError};
use stylesheets::CssRules;
use stylesheets::{CssRules, MallocSizeOfFn, MallocSizeOfWithGuard};
use values::specified::url::SpecifiedUrl;

#[derive(Debug)]
Expand All @@ -27,6 +27,15 @@ pub struct DocumentRule {
pub source_location: SourceLocation,
}

impl DocumentRule {
/// Measure heap usage.
pub fn malloc_size_of_children(&self, guard: &SharedRwLockReadGuard,
malloc_size_of: MallocSizeOfFn) -> usize {
// Measurement of other fields may be added later.
self.rules.read_with(guard).malloc_size_of_children(guard, malloc_size_of)
}
}

impl ToCssWithGuard for DocumentRule {
fn to_css<W>(&self, guard: &SharedRwLockReadGuard, dest: &mut W) -> fmt::Result
where W: fmt::Write {
Expand Down
11 changes: 10 additions & 1 deletion components/style/stylesheets/media_rule.rs
Expand Up @@ -12,7 +12,7 @@ use servo_arc::Arc;
use shared_lock::{DeepCloneParams, DeepCloneWithLock, Locked, SharedRwLock, SharedRwLockReadGuard, ToCssWithGuard};
use std::fmt;
use style_traits::ToCss;
use stylesheets::CssRules;
use stylesheets::{CssRules, MallocSizeOfFn, MallocSizeOfWithGuard};

/// An [`@media`][media] urle.
///
Expand All @@ -27,6 +27,15 @@ pub struct MediaRule {
pub source_location: SourceLocation,
}

impl MediaRule {
/// Measure heap usage.
pub fn malloc_size_of_children(&self, guard: &SharedRwLockReadGuard,
malloc_size_of: MallocSizeOfFn) -> usize {
// Measurement of other fields may be added later.
self.rules.read_with(guard).malloc_size_of_children(guard, malloc_size_of)
}
}

impl ToCssWithGuard for MediaRule {
// Serialization of MediaRule is not specced.
// https://drafts.csswg.org/cssom/#serialize-a-css-rule CSSMediaRule
Expand Down
35 changes: 27 additions & 8 deletions components/style/stylesheets/mod.rs
Expand Up @@ -116,21 +116,40 @@ impl MallocSizeOfWithGuard for CssRule {
malloc_size_of: MallocSizeOfFn
) -> usize {
match *self {
// Not all fields are currently fully measured. Extra measurement
// may be added later.

CssRule::Namespace(_) => 0,

// We don't need to measure ImportRule::stylesheet because we measure
// it on the C++ side in the child list of the ServoStyleSheet.
CssRule::Import(_) => 0,

CssRule::Style(ref lock) => {
lock.read_with(guard).malloc_size_of_children(guard, malloc_size_of)
},
// Measurement of these fields may be added later.
CssRule::Import(_) => 0,
CssRule::Media(_) => 0,

CssRule::Media(ref lock) => {
lock.read_with(guard).malloc_size_of_children(guard, malloc_size_of)
},

CssRule::FontFace(_) => 0,
CssRule::FontFeatureValues(_) => 0,
CssRule::CounterStyle(_) => 0,
CssRule::Keyframes(_) => 0,
CssRule::Namespace(_) => 0,
CssRule::Viewport(_) => 0,
CssRule::Supports(_) => 0,
CssRule::Page(_) => 0,
CssRule::Document(_) => 0,
CssRule::Keyframes(_) => 0,

CssRule::Supports(ref lock) => {
lock.read_with(guard).malloc_size_of_children(guard, malloc_size_of)
},

CssRule::Page(ref lock) => {
lock.read_with(guard).malloc_size_of_children(guard, malloc_size_of)
},

CssRule::Document(ref lock) => {
lock.read_with(guard).malloc_size_of_children(guard, malloc_size_of)
},
}
}
}
Expand Down
10 changes: 10 additions & 0 deletions components/style/stylesheets/page_rule.rs
Expand Up @@ -12,6 +12,7 @@ use servo_arc::Arc;
use shared_lock::{DeepCloneParams, DeepCloneWithLock, Locked, SharedRwLock, SharedRwLockReadGuard, ToCssWithGuard};
use std::fmt;
use style_traits::ToCss;
use stylesheets::{MallocSizeOf, MallocSizeOfFn};

/// A [`@page`][page] rule.
///
Expand All @@ -30,6 +31,15 @@ pub struct PageRule {
pub source_location: SourceLocation,
}

impl PageRule {
/// Measure heap usage.
pub fn malloc_size_of_children(&self, guard: &SharedRwLockReadGuard,
malloc_size_of: MallocSizeOfFn) -> usize {
// Measurement of other fields may be added later.
self.block.read_with(guard).malloc_size_of_children(malloc_size_of)
}
}

impl ToCssWithGuard for PageRule {
/// Serialization of PageRule is not specced, adapted from steps for
/// StyleRule.
Expand Down
20 changes: 17 additions & 3 deletions components/style/stylesheets/style_rule.rs
Expand Up @@ -12,7 +12,7 @@ use servo_arc::Arc;
use shared_lock::{DeepCloneParams, DeepCloneWithLock, Locked, SharedRwLock, SharedRwLockReadGuard, ToCssWithGuard};
use std::fmt;
use style_traits::ToCss;
use stylesheets::{MallocSizeOf, MallocSizeOfFn, MallocSizeOfWithGuard};
use stylesheets::{MallocSizeOf, MallocSizeOfFn, MallocSizeOfVec, MallocSizeOfWithGuard};

/// A style rule, with selectors and declarations.
#[derive(Debug)]
Expand Down Expand Up @@ -47,8 +47,22 @@ impl MallocSizeOfWithGuard for StyleRule {
guard: &SharedRwLockReadGuard,
malloc_size_of: MallocSizeOfFn
) -> usize {
// Measurement of other fields may be added later.
self.block.read_with(guard).malloc_size_of_children(malloc_size_of)
let mut n = 0;

// We may add measurement of things hanging off the embedded Components
// later.
n += self.selectors.0.malloc_shallow_size_of_vec(malloc_size_of);
for selector in self.selectors.0.iter() {
// It's safe to measure this ThinArc directly because it's the
// "primary" reference. (The secondary references are on the
// Stylist.)
let ptr = selector.thin_arc_heap_ptr();
n += unsafe { (malloc_size_of.0)(ptr) };
}

n += self.block.read_with(guard).malloc_size_of_children(malloc_size_of);

n
}
}

Expand Down
11 changes: 10 additions & 1 deletion components/style/stylesheets/supports_rule.rs
Expand Up @@ -13,7 +13,7 @@ use servo_arc::Arc;
use shared_lock::{DeepCloneParams, DeepCloneWithLock, Locked, SharedRwLock, SharedRwLockReadGuard, ToCssWithGuard};
use std::fmt;
use style_traits::{ToCss, ParseError, StyleParseError};
use stylesheets::{CssRuleType, CssRules};
use stylesheets::{CssRuleType, CssRules, MallocSizeOfFn, MallocSizeOfWithGuard};

/// An [`@supports`][supports] rule.
///
Expand All @@ -30,6 +30,15 @@ pub struct SupportsRule {
pub source_location: SourceLocation,
}

impl SupportsRule {
/// Measure heap usage.
pub fn malloc_size_of_children(&self, guard: &SharedRwLockReadGuard,
malloc_size_of: MallocSizeOfFn) -> usize {
// Measurement of other fields may be added later.
self.rules.read_with(guard).malloc_size_of_children(guard, malloc_size_of)
}
}

impl ToCssWithGuard for SupportsRule {
fn to_css<W>(&self, guard: &SharedRwLockReadGuard, dest: &mut W) -> fmt::Result
where W: fmt::Write {
Expand Down

0 comments on commit 61877b7

Please sign in to comment.