From fdbb4d01e64bda363bed64ea520c21555aed0903 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emilio=20Cobos=20=C3=81lvarez?= Date: Tue, 30 May 2023 20:50:03 +0200 Subject: [PATCH] style: Merge branches in some ComputedValues functions This should mitigate the code size impact. Also make get_resolved_value non-generic to avoid monomorphizing it multiple times. Differential Revision: https://phabricator.services.mozilla.com/D130354 --- .../style/properties/properties.mako.rs | 61 +++++++++++++------ 1 file changed, 41 insertions(+), 20 deletions(-) diff --git a/components/style/properties/properties.mako.rs b/components/style/properties/properties.mako.rs index 3bed01019ea..1ea03d96667 100644 --- a/components/style/properties/properties.mako.rs +++ b/components/style/properties/properties.mako.rs @@ -3089,27 +3089,28 @@ impl ComputedValues { /// /// TODO(emilio): We should move all the special resolution from /// nsComputedDOMStyle to ToResolvedValue instead. - pub fn get_resolved_value( + pub fn get_resolved_value( &self, property_id: LonghandId, - dest: &mut CssWriter - ) -> fmt::Result - where - W: Write, - { + dest: &mut CssStringWriter, + ) -> fmt::Result { use crate::values::resolved::ToResolvedValue; + let mut dest = CssWriter::new(dest); let context = resolved::Context { style: self, }; - - // TODO(emilio): Is it worth to merge branches here just like - // PropertyDeclaration::to_css does? match property_id { - % for prop in data.longhands: - LonghandId::${prop.camel_case} => { - let value = self.clone_${prop.ident}(); - value.to_resolved_value(&context).to_css(dest) + % for specified_type, props in groupby(data.longhands, key=lambda x: x.specified_type()): + <% props = list(props) %> + ${" |\n".join("LonghandId::{}".format(p.camel_case) for p in props)} => { + let value = match property_id { + % for prop in props: + LonghandId::${prop.camel_case} => self.clone_${prop.ident}(), + % endfor + _ => unsafe { debug_unreachable!() }, + }; + value.to_resolved_value(&context).to_css(&mut dest) } % endfor } @@ -3125,16 +3126,36 @@ impl ComputedValues { }; match property_id { - % for prop in data.longhands: - LonghandId::${prop.camel_case} => { - let value = self.clone_${prop.ident}(); + % for specified_type, props in groupby(data.longhands, key=lambda x: x.specified_type()): + <% props = list(props) %> + ${" |\n".join("LonghandId::{}".format(p.camel_case) for p in props)} => { + let value = match property_id { + % for prop in props: + LonghandId::${prop.camel_case} => self.clone_${prop.ident}(), + % endfor + _ => unsafe { debug_unreachable!() }, + }; let resolved = value.to_resolved_value(&context); - %if prop.boxed: - let resolved = Box::new(resolved); - %endif let computed = ToResolvedValue::from_resolved_value(resolved); let specified = ToComputedValue::from_computed_value(&computed); - PropertyDeclaration::${prop.camel_case}(specified) + % if props[0].boxed: + let specified = Box::new(specified); + % endif + % if len(props) == 1: + PropertyDeclaration::${props[0].camel_case}(specified) + % else: + unsafe { + let mut out = mem::MaybeUninit::uninit(); + ptr::write( + out.as_mut_ptr() as *mut PropertyDeclarationVariantRepr<${specified_type}>, + PropertyDeclarationVariantRepr { + tag: property_id as u16, + value: specified, + }, + ); + out.assume_init() + } + % endif } % endfor }