From 7cc718f1c1317ff4f8b2be52d4098172b5533fd3 Mon Sep 17 00:00:00 2001 From: Callum Law Date: Mon, 14 Jul 2025 15:58:29 +1200 Subject: [PATCH] LibWeb: Avoid vector creation getting {short,long}hands for CSS property The `shorthands_for_longhand`, `longhands_for_shorthand`, and `expanded_longhands_for_shorthand` methods can be pretty hot in profiles where we serialize a lot of CSS properties. By returning a const reference to a static vector instead of allocating and returning a new vector every time we can avoid a decent amount of work. Overall runtime for the particularly serialization heavy wpt.live/css/cssom/cssom-getPropertyValue-common-checks.html decreased by ~20% comparing before and after this change. --- .../LibWeb/GenerateCSSPropertyID.cpp | 48 +++++++++++-------- 1 file changed, 28 insertions(+), 20 deletions(-) diff --git a/Meta/Lagom/Tools/CodeGenerators/LibWeb/GenerateCSSPropertyID.cpp b/Meta/Lagom/Tools/CodeGenerators/LibWeb/GenerateCSSPropertyID.cpp index 214714ffd3b..885808eb6d9 100644 --- a/Meta/Lagom/Tools/CodeGenerators/LibWeb/GenerateCSSPropertyID.cpp +++ b/Meta/Lagom/Tools/CodeGenerators/LibWeb/GenerateCSSPropertyID.cpp @@ -319,10 +319,10 @@ bool property_accepts_resolution(PropertyID, Resolution const&); bool property_accepts_time(PropertyID, Time const&); bool property_is_shorthand(PropertyID); -Vector longhands_for_shorthand(PropertyID); -Vector expanded_longhands_for_shorthand(PropertyID); +Vector const& longhands_for_shorthand(PropertyID); +Vector const& expanded_longhands_for_shorthand(PropertyID); bool property_maps_to_shorthand(PropertyID); -Vector shorthands_for_longhand(PropertyID); +Vector const& shorthands_for_longhand(PropertyID); size_t property_maximum_value_count(PropertyID); @@ -1126,7 +1126,7 @@ bool property_is_shorthand(PropertyID property_id) )~~~"); generator.append(R"~~~( -Vector longhands_for_shorthand(PropertyID property_id) +Vector const& longhands_for_shorthand(PropertyID property_id) { switch (property_id) { )~~~"); @@ -1161,21 +1161,23 @@ Vector longhands_for_shorthand(PropertyID property_id) } property_generator.set("longhands", builder.to_byte_string()); property_generator.append(R"~~~( - case PropertyID::@name:titlecase@: - return { @longhands@ }; -)~~~"); + case PropertyID::@name:titlecase@: { + static Vector longhands = { @longhands@ }; + return longhands; + })~~~"); } }); generator.append(R"~~~( default: - return { }; + static Vector empty_longhands; + return empty_longhands; } } )~~~"); generator.append(R"~~~( -Vector expanded_longhands_for_shorthand(PropertyID property_id) +Vector const& expanded_longhands_for_shorthand(PropertyID property_id) { switch (property_id) { )~~~"); @@ -1213,15 +1215,18 @@ Vector expanded_longhands_for_shorthand(PropertyID property_id) } property_generator.set("longhands", builder.to_byte_string()); property_generator.append(R"~~~( - case PropertyID::@name:titlecase@: - return { @longhands@ }; -)~~~"); + case PropertyID::@name:titlecase@: { + static Vector longhands = { @longhands@ }; + return longhands; + })~~~"); } }); generator.append(R"~~~( - default: - return { }; + default: { + static Vector empty_longhands; + return empty_longhands; + } } } )~~~"); @@ -1266,7 +1271,7 @@ bool property_maps_to_shorthand(PropertyID property_id) )~~~"); generator.append(R"~~~( -Vector shorthands_for_longhand(PropertyID property_id) +Vector const& shorthands_for_longhand(PropertyID property_id) { switch (property_id) { )~~~"); @@ -1317,14 +1322,17 @@ Vector shorthands_for_longhand(PropertyID property_id) } property_generator.set("shorthands", builder.to_byte_string()); property_generator.append(R"~~~( - case PropertyID::@name:titlecase@: - return { @shorthands@ }; -)~~~"); + case PropertyID::@name:titlecase@: { + static Vector shorthands = { @shorthands@ }; + return shorthands; + })~~~"); } generator.append(R"~~~( - default: - return { }; + default: { + static Vector empty_shorthands; + return empty_shorthands; + } } } )~~~");