Skip to content

Commit

Permalink
style: Speed up LonghandId::is_longhand_of.
Browse files Browse the repository at this point in the history
By looking at the shorthands, not the other way around, given a longhand uses to
not appear in multiple longhands.

This makes the testcase go to 430ms on my machine.

This could be even faster having a static LonghandIdSet per shorthand, I guess,
but this is not done in this PR.
  • Loading branch information
emilio committed Jun 14, 2017
1 parent 6e85601 commit 5fbaf6d
Showing 1 changed file with 41 additions and 36 deletions.
77 changes: 41 additions & 36 deletions components/style/properties/properties.mako.rs
Expand Up @@ -551,6 +551,42 @@ impl LonghandId {
}
}

fn shorthands(&self) -> &'static [ShorthandId] {
// first generate longhand to shorthands lookup map
//
// NOTE(emilio): This currently doesn't exclude the "all" shorthand. It
// could potentially do so, which would speed up serialization
// algorithms and what not, I guess.
<%
longhand_to_shorthand_map = {}
for shorthand in data.shorthands:
for sub_property in shorthand.sub_properties:
if sub_property.ident not in longhand_to_shorthand_map:
longhand_to_shorthand_map[sub_property.ident] = []

longhand_to_shorthand_map[sub_property.ident].append(shorthand.camel_case)

for shorthand_list in longhand_to_shorthand_map.itervalues():
shorthand_list.sort()
%>

// based on lookup results for each longhand, create result arrays
% for property in data.longhands:
static ${property.ident.upper()}: &'static [ShorthandId] = &[
% for shorthand in longhand_to_shorthand_map.get(property.ident, []):
ShorthandId::${shorthand},
% endfor
];
% endfor

match *self {
% for property in data.longhands:
LonghandId::${property.camel_case} => ${property.ident.upper()},
% endfor
}
}


/// If this is a logical property, return the corresponding physical one in the given writing mode.
/// Otherwise, return unchanged.
pub fn to_physical(&self, wm: WritingMode) -> Self {
Expand Down Expand Up @@ -885,7 +921,7 @@ impl<'a> PropertyDeclarationId<'a> {
PropertyDeclarationId::Longhand(id) => {
match *other {
PropertyId::Longhand(other_id) => id == other_id,
PropertyId::Shorthand(shorthand) => shorthand.longhands().contains(&id),
PropertyId::Shorthand(shorthand) => self.is_longhand_of(shorthand),
PropertyId::Custom(_) => false,
}
}
Expand All @@ -899,7 +935,7 @@ impl<'a> PropertyDeclarationId<'a> {
/// shorthand.
pub fn is_longhand_of(&self, shorthand: ShorthandId) -> bool {
match *self {
PropertyDeclarationId::Longhand(ref id) => shorthand.longhands().contains(id),
PropertyDeclarationId::Longhand(ref id) => id.shorthands().contains(&shorthand),
_ => false,
}
}
Expand Down Expand Up @@ -1308,40 +1344,9 @@ impl PropertyDeclaration {

/// The shorthands that this longhand is part of.
pub fn shorthands(&self) -> &'static [ShorthandId] {
// first generate longhand to shorthands lookup map
<%
longhand_to_shorthand_map = {}
for shorthand in data.shorthands:
for sub_property in shorthand.sub_properties:
if sub_property.ident not in longhand_to_shorthand_map:
longhand_to_shorthand_map[sub_property.ident] = []

longhand_to_shorthand_map[sub_property.ident].append(shorthand.camel_case)

for shorthand_list in longhand_to_shorthand_map.itervalues():
shorthand_list.sort()
%>

// based on lookup results for each longhand, create result arrays
% for property in data.longhands:
static ${property.ident.upper()}: &'static [ShorthandId] = &[
% for shorthand in longhand_to_shorthand_map.get(property.ident, []):
ShorthandId::${shorthand},
% endfor
];
% endfor

match *self {
% for property in data.longhands:
PropertyDeclaration::${property.camel_case}(_) => ${property.ident.upper()},
% endfor
PropertyDeclaration::CSSWideKeyword(id, _) |
PropertyDeclaration::WithVariables(id, _) => match id {
% for property in data.longhands:
LonghandId::${property.camel_case} => ${property.ident.upper()},
% endfor
},
PropertyDeclaration::Custom(_, _) => &[]
match self.id() {
PropertyDeclarationId::Longhand(id) => id.shorthands(),
PropertyDeclarationId::Custom(..) => &[],
}
}

Expand Down

0 comments on commit 5fbaf6d

Please sign in to comment.