Skip to content

Commit

Permalink
style: Shrink selectors::Component to 24 bytes.
Browse files Browse the repository at this point in the history
This saves about 37 KiB of memory across the UA style sheets.

Bug: 1475197
Reviewed-by: emilio
  • Loading branch information
heycam authored and emilio committed Aug 7, 2018
1 parent b05ace3 commit 62419ad
Show file tree
Hide file tree
Showing 12 changed files with 96 additions and 50 deletions.
1 change: 1 addition & 0 deletions components/malloc_size_of/Cargo.toml
Expand Up @@ -37,6 +37,7 @@ servo_arc = { path = "../servo_arc" }
smallbitvec = "2.1.0"
smallvec = "0.6"
string_cache = { version = "0.7", optional = true }
thin-slice = "0.1.0"
time = { version = "0.1.17", optional = true }
url = { version = "1.2", optional = true }
webrender_api = { git = "https://github.com/servo/webrender", features = ["ipc"], optional = true }
Expand Down
21 changes: 20 additions & 1 deletion components/malloc_size_of/lib.rs
Expand Up @@ -63,6 +63,7 @@ extern crate smallbitvec;
extern crate smallvec;
#[cfg(feature = "servo")]
extern crate string_cache;
extern crate thin_slice;
#[cfg(feature = "servo")]
extern crate time;
#[cfg(feature = "url")]
Expand Down Expand Up @@ -231,6 +232,24 @@ impl<T: MallocSizeOf + ?Sized> MallocSizeOf for Box<T> {
}
}

impl<T> MallocShallowSizeOf for thin_slice::ThinBoxedSlice<T> {
fn shallow_size_of(&self, ops: &mut MallocSizeOfOps) -> usize {
let mut n = 0;
unsafe {
n += thin_slice::ThinBoxedSlice::spilled_storage(self)
.map_or(0, |ptr| ops.malloc_size_of(ptr));
n += ops.malloc_size_of(&**self);
}
n
}
}

impl<T: MallocSizeOf> MallocSizeOf for thin_slice::ThinBoxedSlice<T> {
fn size_of(&self, ops: &mut MallocSizeOfOps) -> usize {
self.shallow_size_of(ops) + (**self).size_of(ops)
}
}

impl MallocSizeOf for () {
fn size_of(&self, _ops: &mut MallocSizeOfOps) -> usize {
0
Expand Down Expand Up @@ -770,7 +789,7 @@ where
}

impl<Impl: selectors::parser::SelectorImpl> MallocSizeOf
for selectors::attr::AttrSelectorWithNamespace<Impl>
for selectors::attr::AttrSelectorWithOptionalNamespace<Impl>
{
fn size_of(&self, _ops: &mut MallocSizeOfOps) -> usize {
0
Expand Down
1 change: 1 addition & 0 deletions components/selectors/Cargo.toml
Expand Up @@ -30,6 +30,7 @@ phf = "0.7.18"
precomputed-hash = "0.1"
servo_arc = { version = "0.1", path = "../servo_arc" }
smallvec = "0.6.2"
thin-slice = "0.1.0"

[build-dependencies]
phf_codegen = "0.7.18"
12 changes: 6 additions & 6 deletions components/selectors/attr.rs
Expand Up @@ -7,20 +7,20 @@ use parser::SelectorImpl;
use std::fmt;

#[derive(Clone, Eq, PartialEq)]
pub struct AttrSelectorWithNamespace<Impl: SelectorImpl> {
pub namespace: NamespaceConstraint<(Impl::NamespacePrefix, Impl::NamespaceUrl)>,
pub struct AttrSelectorWithOptionalNamespace<Impl: SelectorImpl> {
pub namespace: Option<NamespaceConstraint<(Impl::NamespacePrefix, Impl::NamespaceUrl)>>,
pub local_name: Impl::LocalName,
pub local_name_lower: Impl::LocalName,
pub operation: ParsedAttrSelectorOperation<Impl::AttrValue>,
pub never_matches: bool,
}

impl<Impl: SelectorImpl> AttrSelectorWithNamespace<Impl> {
pub fn namespace(&self) -> NamespaceConstraint<&Impl::NamespaceUrl> {
match self.namespace {
impl<Impl: SelectorImpl> AttrSelectorWithOptionalNamespace<Impl> {
pub fn namespace(&self) -> Option<NamespaceConstraint<&Impl::NamespaceUrl>> {
self.namespace.as_ref().map(|ns| match ns {
NamespaceConstraint::Any => NamespaceConstraint::Any,
NamespaceConstraint::Specific((_, ref url)) => NamespaceConstraint::Specific(url),
}
})
}
}

Expand Down
1 change: 1 addition & 0 deletions components/selectors/lib.rs
Expand Up @@ -19,6 +19,7 @@ extern crate phf;
extern crate precomputed_hash;
extern crate servo_arc;
extern crate smallvec;
extern crate thin_slice;

pub mod attr;
pub mod bloom;
Expand Down
13 changes: 10 additions & 3 deletions components/selectors/matching.rs
Expand Up @@ -699,7 +699,6 @@ where
},
Component::AttributeInNoNamespace {
ref local_name,
ref local_name_lower,
ref value,
operator,
case_sensitivity,
Expand All @@ -711,7 +710,7 @@ where
let is_html = element.is_html_element_in_html_document();
element.attr_matches(
&NamespaceConstraint::Specific(&::parser::namespace_empty_string::<E::Impl>()),
select_name(is_html, local_name, local_name_lower),
local_name,
&AttrSelectorOperation::WithValue {
operator: operator,
case_sensitivity: case_sensitivity.to_unconditional(is_html),
Expand All @@ -724,8 +723,16 @@ where
return false;
}
let is_html = element.is_html_element_in_html_document();
let empty_string;
let namespace = match attr_sel.namespace() {
Some(ns) => ns,
None => {
empty_string = ::parser::namespace_empty_string::<E::Impl>();
NamespaceConstraint::Specific(&empty_string)
}
};
element.attr_matches(
&attr_sel.namespace(),
&namespace,
select_name(is_html, &attr_sel.local_name, &attr_sel.local_name_lower),
&match attr_sel.operation {
ParsedAttrSelectorOperation::Exists => AttrSelectorOperation::Exists,
Expand Down
76 changes: 44 additions & 32 deletions components/selectors/parser.rs
Expand Up @@ -2,8 +2,9 @@
* 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 attr::{AttrSelectorOperator, AttrSelectorWithNamespace, ParsedAttrSelectorOperation};
use attr::{NamespaceConstraint, ParsedCaseSensitivity, SELECTOR_WHITESPACE};
use attr::{AttrSelectorOperator, AttrSelectorWithOptionalNamespace};
use attr::{NamespaceConstraint, ParsedAttrSelectorOperation};
use attr::{ParsedCaseSensitivity, SELECTOR_WHITESPACE};
use bloom::BLOOM_HASH_MASK;
use builder::{SelectorBuilder, SpecificityAndFlags};
use context::QuirksMode;
Expand All @@ -19,6 +20,7 @@ use std::borrow::{Borrow, Cow};
use std::fmt::{self, Debug, Display, Write};
use std::iter::Rev;
use std::slice;
use thin_slice::ThinBoxedSlice;
pub use visitor::{SelectorVisitor, Visit};

/// A trait that represents a pseudo-element.
Expand All @@ -45,6 +47,8 @@ pub trait NonTSPseudoClass: Sized + ToCss {
fn is_active_or_hover(&self) -> bool;
}

/// Returns a Cow::Borrowed if `s` is already ASCII lowercase, and a
/// Cow::Owned if `s` had to be converted into ASCII lowercase.
fn to_ascii_lowercase(s: &str) -> Cow<str> {
if let Some(first_uppercase) = s.bytes().position(|byte| byte >= b'A' && byte <= b'Z') {
let mut string = s.to_owned();
Expand Down Expand Up @@ -428,22 +432,29 @@ where
},
AttributeInNoNamespace {
ref local_name,
ref local_name_lower,
never_matches,
..
} if !never_matches =>
{
if !visitor.visit_attribute_selector(
&NamespaceConstraint::Specific(&namespace_empty_string::<Impl>()),
local_name,
local_name_lower,
local_name,
) {
return false;
}
},
AttributeOther(ref attr_selector) if !attr_selector.never_matches => {
let empty_string;
let namespace = match attr_selector.namespace() {
Some(ns) => ns,
None => {
empty_string = ::parser::namespace_empty_string::<Impl>();
NamespaceConstraint::Specific(&empty_string)
}
};
if !visitor.visit_attribute_selector(
&attr_selector.namespace(),
&namespace,
&attr_selector.local_name,
&attr_selector.local_name_lower,
) {
Expand Down Expand Up @@ -815,16 +826,16 @@ pub enum Component<Impl: SelectorImpl> {
local_name: Impl::LocalName,
local_name_lower: Impl::LocalName,
},
// Used only when local_name is already lowercase.
AttributeInNoNamespace {
local_name: Impl::LocalName,
local_name_lower: Impl::LocalName,
operator: AttrSelectorOperator,
value: Impl::AttrValue,
case_sensitivity: ParsedCaseSensitivity,
never_matches: bool,
},
// Use a Box in the less common cases with more data to keep size_of::<Component>() small.
AttributeOther(Box<AttrSelectorWithNamespace<Impl>>),
AttributeOther(Box<AttrSelectorWithOptionalNamespace<Impl>>),

/// Pseudo-classes
///
Expand All @@ -836,7 +847,7 @@ pub enum Component<Impl: SelectorImpl> {
/// need to think about how this should interact with
/// visit_complex_selector, and what the consumers of those APIs should do
/// about the presence of combinators in negation.
Negation(Box<[Component<Impl>]>),
Negation(ThinBoxedSlice<Component<Impl>>),
FirstChild,
LastChild,
OnlyChild,
Expand Down Expand Up @@ -948,7 +959,7 @@ impl<Impl: SelectorImpl> Debug for Component<Impl> {
self.to_css(f)
}
}
impl<Impl: SelectorImpl> Debug for AttrSelectorWithNamespace<Impl> {
impl<Impl: SelectorImpl> Debug for AttrSelectorWithOptionalNamespace<Impl> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.to_css(f)
}
Expand Down Expand Up @@ -1238,18 +1249,19 @@ impl<Impl: SelectorImpl> ToCss for Component<Impl> {
}
}

impl<Impl: SelectorImpl> ToCss for AttrSelectorWithNamespace<Impl> {
impl<Impl: SelectorImpl> ToCss for AttrSelectorWithOptionalNamespace<Impl> {
fn to_css<W>(&self, dest: &mut W) -> fmt::Result
where
W: fmt::Write,
{
dest.write_char('[')?;
match self.namespace {
NamespaceConstraint::Specific((ref prefix, _)) => {
Some(NamespaceConstraint::Specific((ref prefix, _))) => {
display_to_css_identifier(prefix, dest)?;
dest.write_char('|')?
},
NamespaceConstraint::Any => dest.write_str("*|")?,
Some(NamespaceConstraint::Any) => dest.write_str("*|")?,
None => {}
}
display_to_css_identifier(&self.local_name, dest)?;
match self.operation {
Expand Down Expand Up @@ -1628,8 +1640,8 @@ where
let local_name = local_name.as_ref().into();
if let Some(namespace) = namespace {
return Ok(Component::AttributeOther(Box::new(
AttrSelectorWithNamespace {
namespace: namespace,
AttrSelectorWithOptionalNamespace {
namespace: Some(namespace),
local_name: local_name,
local_name_lower: local_name_lower,
operation: ParsedAttrSelectorOperation::Exists,
Expand Down Expand Up @@ -1685,6 +1697,7 @@ where

let value = value.as_ref().into();
let local_name_lower;
let local_name_is_ascii_lowercase;
{
let local_name_lower_cow = to_ascii_lowercase(&local_name);
if let ParsedCaseSensitivity::CaseSensitive = case_sensitivity {
Expand All @@ -1699,15 +1712,16 @@ where
}
}
local_name_lower = local_name_lower_cow.as_ref().into();
local_name_is_ascii_lowercase = matches!(local_name_lower_cow, Cow::Borrowed(..));
}
let local_name = local_name.as_ref().into();
if let Some(namespace) = namespace {
if namespace.is_some() || !local_name_is_ascii_lowercase {
Ok(Component::AttributeOther(Box::new(
AttrSelectorWithNamespace {
namespace: namespace,
local_name: local_name,
local_name_lower: local_name_lower,
never_matches: never_matches,
AttrSelectorWithOptionalNamespace {
namespace,
local_name,
local_name_lower,
never_matches,
operation: ParsedAttrSelectorOperation::WithValue {
operator: operator,
case_sensitivity: case_sensitivity,
Expand All @@ -1718,7 +1732,6 @@ where
} else {
Ok(Component::AttributeInNoNamespace {
local_name: local_name,
local_name_lower: local_name_lower,
operator: operator,
value: value,
case_sensitivity: case_sensitivity,
Expand Down Expand Up @@ -1785,7 +1798,7 @@ where
}

// Success.
Ok(Component::Negation(sequence.into_vec().into_boxed_slice()))
Ok(Component::Negation(sequence.into_vec().into_boxed_slice().into()))
}

/// simple_selector_sequence
Expand Down Expand Up @@ -2625,7 +2638,7 @@ pub mod tests {
vec![
Component::DefaultNamespace(MATHML.into()),
Component::Negation(
vec![Component::Class(DummyAtom::from("cl"))].into_boxed_slice(),
vec![Component::Class(DummyAtom::from("cl"))].into_boxed_slice().into(),
),
],
specificity(0, 1, 0),
Expand All @@ -2642,7 +2655,7 @@ pub mod tests {
vec![
Component::DefaultNamespace(MATHML.into()),
Component::ExplicitUniversalType,
].into_boxed_slice(),
].into_boxed_slice().into(),
),
],
specificity(0, 0, 0),
Expand All @@ -2662,7 +2675,7 @@ pub mod tests {
name: DummyAtom::from("e"),
lower_name: DummyAtom::from("e"),
}),
].into_boxed_slice(),
].into_boxed_slice().into(),
),
],
specificity(0, 0, 1),
Expand All @@ -2676,7 +2689,6 @@ pub mod tests {
vec![
Component::AttributeInNoNamespace {
local_name: DummyAtom::from("attr"),
local_name_lower: DummyAtom::from("attr"),
operator: AttrSelectorOperator::DashMatch,
value: DummyAtom::from("foo"),
never_matches: false,
Expand Down Expand Up @@ -2770,7 +2782,7 @@ pub mod tests {
Selector::from_vec(
vec![
Component::Negation(
vec![Component::ID(DummyAtom::from("provel"))].into_boxed_slice(),
vec![Component::ID(DummyAtom::from("provel"))].into_boxed_slice().into(),
),
],
specificity(1, 0, 0),
Expand All @@ -2789,7 +2801,7 @@ pub mod tests {
name: DummyAtom::from("circle"),
lower_name: DummyAtom::from("circle"),
}),
].into_boxed_slice(),
].into_boxed_slice().into(),
),
],
specificity(0, 0, 1),
Expand All @@ -2803,7 +2815,7 @@ pub mod tests {
Selector::from_vec(
vec![
Component::Negation(
vec![Component::ExplicitUniversalType].into_boxed_slice(),
vec![Component::ExplicitUniversalType].into_boxed_slice().into(),
),
],
specificity(0, 0, 0),
Expand All @@ -2819,7 +2831,7 @@ pub mod tests {
vec![
Component::ExplicitNoNamespace,
Component::ExplicitUniversalType,
].into_boxed_slice(),
].into_boxed_slice().into(),
),
],
specificity(0, 0, 0),
Expand All @@ -2834,7 +2846,7 @@ pub mod tests {
Selector::from_vec(
vec![
Component::Negation(
vec![Component::ExplicitUniversalType].into_boxed_slice(),
vec![Component::ExplicitUniversalType].into_boxed_slice().into(),
),
],
specificity(0, 0, 0),
Expand All @@ -2850,7 +2862,7 @@ pub mod tests {
vec![
Component::Namespace(DummyAtom("svg".into()), SVG.into()),
Component::ExplicitUniversalType,
].into_boxed_slice(),
].into_boxed_slice().into(),
),
],
specificity(0, 0, 0),
Expand Down

0 comments on commit 62419ad

Please sign in to comment.