Skip to content

Commit

Permalink
Bug 1870676 - Other minor clean-ups to PropertyId-related code. r=fir…
Browse files Browse the repository at this point in the history
…efox-style-system-reviewers,zrhoffman

Make NonCustomPropertyId an u16 (because it can, and it's more compact),
and use arrays for ShorthandId::longhands and LonghandId::shorthands.

Differential Revision: https://phabricator.services.mozilla.com/D196759
  • Loading branch information
emilio committed Dec 19, 2023
1 parent 933d556 commit 2ced0c5
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 38 deletions.
32 changes: 12 additions & 20 deletions servo/components/style/properties/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ impl fmt::Debug for PropertyDeclaration {

/// A longhand or shorthand property.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct NonCustomPropertyId(u32);
pub struct NonCustomPropertyId(u16);

impl NonCustomPropertyId {
/// Returns the underlying index, used for use counter.
Expand All @@ -217,7 +217,7 @@ impl NonCustomPropertyId {
return None;
}
// guaranteed by static_assert_nscsspropertyid above.
Some(NonCustomPropertyId(prop as u32))
Some(NonCustomPropertyId(prop as u16))
}

/// Resolves the alias of a given property if needed.
Expand All @@ -237,7 +237,7 @@ impl NonCustomPropertyId {
/// Returns a longhand id, if this property is one.
#[inline]
pub fn as_longhand(self) -> Option<LonghandId> {
if self.0 < property_counts::LONGHANDS as u32 {
if self.0 < property_counts::LONGHANDS as u16 {
return Some(unsafe { mem::transmute(self.0 as u16) });
}
None
Expand All @@ -246,12 +246,10 @@ impl NonCustomPropertyId {
/// Returns a shorthand id, if this property is one.
#[inline]
pub fn as_shorthand(self) -> Option<ShorthandId> {
if self.0 >= property_counts::LONGHANDS as u32 &&
self.0 < property_counts::LONGHANDS_AND_SHORTHANDS as u32
if self.0 >= property_counts::LONGHANDS as u16 &&
self.0 < property_counts::LONGHANDS_AND_SHORTHANDS as u16
{
return Some(unsafe {
mem::transmute((self.0 - (property_counts::LONGHANDS as u32)) as u16)
});
return Some(unsafe { mem::transmute(self.0 - (property_counts::LONGHANDS as u16)) });
}
None
}
Expand All @@ -260,9 +258,9 @@ impl NonCustomPropertyId {
#[inline]
pub fn as_alias(self) -> Option<AliasId> {
debug_assert!((self.0 as usize) < property_counts::NON_CUSTOM);
if self.0 >= property_counts::LONGHANDS_AND_SHORTHANDS as u32 {
if self.0 >= property_counts::LONGHANDS_AND_SHORTHANDS as u16 {
return Some(unsafe {
mem::transmute((self.0 - (property_counts::LONGHANDS_AND_SHORTHANDS as u32)) as u16)
mem::transmute(self.0 - (property_counts::LONGHANDS_AND_SHORTHANDS as u16))
});
}
None
Expand All @@ -281,19 +279,19 @@ impl NonCustomPropertyId {
/// Converts a longhand id into a non-custom property id.
#[inline]
pub const fn from_longhand(id: LonghandId) -> Self {
Self(id as u32)
Self(id as u16)
}

/// Converts a shorthand id into a non-custom property id.
#[inline]
pub const fn from_shorthand(id: ShorthandId) -> Self {
Self((id as u32) + (property_counts::LONGHANDS as u32))
Self((id as u16) + (property_counts::LONGHANDS as u16))
}

/// Converts an alias id into a non-custom property id.
#[inline]
pub const fn from_alias(id: AliasId) -> Self {
Self((id as u32) + (property_counts::LONGHANDS_AND_SHORTHANDS as u32))
Self((id as u16) + (property_counts::LONGHANDS_AND_SHORTHANDS as u16))
}
}

Expand All @@ -320,20 +318,14 @@ impl From<AliasId> for NonCustomPropertyId {

/// Representation of a CSS property, that is, either a longhand, a shorthand, or a custom
/// property.
#[derive(Clone, Eq, PartialEq)]
#[derive(Clone, Eq, PartialEq, Debug)]
pub enum PropertyId {
/// An alias for a shorthand property.
NonCustom(NonCustomPropertyId),
/// A custom property.
Custom(custom_properties::Name),
}

impl fmt::Debug for PropertyId {
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
self.to_css(&mut CssWriter::new(formatter))
}
}

impl ToCss for PropertyId {
fn to_css<W>(&self, dest: &mut CssWriter<W>) -> fmt::Result
where
Expand Down
32 changes: 14 additions & 18 deletions servo/components/style/properties/properties.mako.rs
Original file line number Diff line number Diff line change
Expand Up @@ -929,7 +929,7 @@ impl LogicalMappingData {

impl LonghandId {
/// Returns an iterator over all the shorthands that include this longhand.
pub fn shorthands(&self) -> NonCustomPropertyIterator<ShorthandId> {
pub fn shorthands(self) -> NonCustomPropertyIterator<ShorthandId> {
// first generate longhand to shorthands lookup map
//
// NOTE(emilio): This currently doesn't exclude the "all" shorthand. It
Expand Down Expand Up @@ -966,21 +966,19 @@ impl LonghandId {
%>

// based on lookup results for each longhand, create result arrays
static MAP: [&'static [ShorthandId]; property_counts::LONGHANDS] = [
% 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
];

NonCustomPropertyIterator {
filter: NonCustomPropertyId::from(*self).enabled_for_all_content(),
iter: match *self {
% for property in data.longhands:
LonghandId::${property.camel_case} => ${property.ident.upper()},
% endfor
}.iter(),
filter: NonCustomPropertyId::from(self).enabled_for_all_content(),
iter: MAP[self as usize].iter(),
}
}

Expand Down Expand Up @@ -1069,21 +1067,19 @@ pub enum ShorthandId {

impl ShorthandId {
/// Get the longhand ids that form this shorthand.
pub fn longhands(&self) -> NonCustomPropertyIterator<LonghandId> {
pub fn longhands(self) -> NonCustomPropertyIterator<LonghandId> {
static MAP: [&'static [LonghandId]; property_counts::SHORTHANDS] = [
% for property in data.shorthands:
static ${property.ident.upper()}: &'static [LonghandId] = &[
&[
% for sub in property.sub_properties:
LonghandId::${sub.camel_case},
% endfor
];
],
% endfor
];
NonCustomPropertyIterator {
filter: NonCustomPropertyId::from(*self).enabled_for_all_content(),
iter: match *self {
% for property in data.shorthands:
ShorthandId::${property.camel_case} => ${property.ident.upper()},
% endfor
}.iter()
filter: NonCustomPropertyId::from(self).enabled_for_all_content(),
iter: MAP[self as usize].iter(),
}
}

Expand Down

0 comments on commit 2ced0c5

Please sign in to comment.