Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Shrink ElementData by moving pseudo count to type
`ElementStyles` holds an optional list of values for each eager pseudo-element.
However, the type was declared as a slice instead of a fixed size array, so an
extra 8 bytes were being allocated to hold the size, even though it never
changes.

Moving the constant size into the type reduces `ElementStyles` and `ElementData`
by 8 bytes.

MozReview-Commit-ID: GaO6DKFxUMo
  • Loading branch information
jryans committed Jun 22, 2017
1 parent 2b5c56e commit 87c51bd
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
22 changes: 19 additions & 3 deletions components/style/data.rs
Expand Up @@ -100,8 +100,24 @@ impl RestyleData {

/// A list of styles for eagerly-cascaded pseudo-elements.
/// Lazily-allocated.
#[derive(Clone, Debug)]
pub struct EagerPseudoStyles(pub Option<Box<[Option<Arc<ComputedValues>>]>>);
#[derive(Debug)]
pub struct EagerPseudoStyles(pub Option<Box<[Option<Arc<ComputedValues>>; EAGER_PSEUDO_COUNT]>>);

// Manually implement `Clone` here because the derived impl of `Clone` for
// array types assumes the value inside is `Copy`.
impl Clone for EagerPseudoStyles {
fn clone(&self) -> Self {
if self.0.is_none() {
return EagerPseudoStyles(None)
}
let self_values = self.0.as_ref().unwrap();
let mut values: [Option<Arc<ComputedValues>>; EAGER_PSEUDO_COUNT] = Default::default();
for i in 0..EAGER_PSEUDO_COUNT {
values[i] = self_values[i].clone();
}
EagerPseudoStyles(Some(Box::new(values)))
}
}

impl EagerPseudoStyles {
/// Returns whether there are any pseudo styles.
Expand Down Expand Up @@ -129,7 +145,7 @@ impl EagerPseudoStyles {
/// Sets the style for the eager pseudo.
pub fn set(&mut self, pseudo: &PseudoElement, value: Arc<ComputedValues>) {
if self.0.is_none() {
self.0 = Some(vec![None; EAGER_PSEUDO_COUNT].into_boxed_slice());
self.0 = Some(Box::new(Default::default()));
}
self.0.as_mut().unwrap()[pseudo.eager_index()] = Some(value);
}
Expand Down
4 changes: 2 additions & 2 deletions tests/unit/stylo/size_of.rs
Expand Up @@ -32,9 +32,9 @@ size_of_test!(test_size_of_rule, style::stylist::Rule, 32);
size_of_test!(test_size_of_option_arc_cv, Option<Arc<ComputedValues>>, 8);
size_of_test!(test_size_of_option_rule_node, Option<StrongRuleNode>, 8);

size_of_test!(test_size_of_element_styles, ElementStyles, 24);
size_of_test!(test_size_of_element_styles, ElementStyles, 16);
size_of_test!(test_size_of_restyle_data, RestyleData, 8);
size_of_test!(test_size_of_element_data, ElementData, 32);
size_of_test!(test_size_of_element_data, ElementData, 24);

size_of_test!(test_size_of_property_declaration, style::properties::PropertyDeclaration, 32);

Expand Down

0 comments on commit 87c51bd

Please sign in to comment.