Skip to content

Truly independent web browser

LibWeb: Make interned style values immortal

Andreas Kling243370af4007
diff --git a/Libraries/LibWeb/CSS/StyleValues/IntegerStyleValue.h b/Libraries/LibWeb/CSS/StyleValues/IntegerStyleValue.h
index 99066231ca6..6b02afac92e 100644
--- a/Libraries/LibWeb/CSS/StyleValues/IntegerStyleValue.h
+++ b/Libraries/LibWeb/CSS/StyleValues/IntegerStyleValue.h
@@ -24,7 +24,7 @@ public:
auto* instances = new (nothrow) Vector<NonnullRefPtr<IntegerStyleValue const>>();
instances->ensure_capacity(last_interned_value - first_interned_value + 1);
for (i32 interned_value = first_interned_value; interned_value <= last_interned_value; ++interned_value)
- instances->unchecked_append(adopt_ref(*new (nothrow) IntegerStyleValue(interned_value)));
+ instances->unchecked_append(*make_immortal(new (nothrow) IntegerStyleValue(interned_value)));
return instances;
}();
return instances[value - first_interned_value];
diff --git a/Libraries/LibWeb/CSS/StyleValues/KeywordStyleValue.h b/Libraries/LibWeb/CSS/StyleValues/KeywordStyleValue.h
index 754118fe39d..d0df67ae96e 100644
--- a/Libraries/LibWeb/CSS/StyleValues/KeywordStyleValue.h
+++ b/Libraries/LibWeb/CSS/StyleValues/KeywordStyleValue.h
@@ -20,13 +20,14 @@ public:
static ValueComparingNonnullRefPtr<KeywordStyleValue const> create(Keyword keyword)
{
// Keyword values are immutable and the keyword set is small, so every keyword is
- // interned: one instance per keyword for the lifetime of the process. Repeated
- // creations are then allocation-free, and identical keywords are pointer-identical.
+ // interned: one instance per keyword for the lifetime of the process, for all of its
+ // threads. Repeated creations are then allocation-free, and identical keywords are
+ // pointer-identical.
static auto const& instances = *[] {
auto* instances = new (nothrow) Vector<NonnullRefPtr<KeywordStyleValue const>>();
instances->ensure_capacity(number_of_keywords);
for (size_t i = 0; i < number_of_keywords; ++i)
- instances->unchecked_append(adopt_ref(*new (nothrow) KeywordStyleValue(static_cast<Keyword>(i))));
+ instances->unchecked_append(*make_immortal(new (nothrow) KeywordStyleValue(static_cast<Keyword>(i))));
return instances;
}();
return instances[to_underlying(keyword)];
diff --git a/Libraries/LibWeb/CSS/StyleValues/LengthStyleValue.cpp b/Libraries/LibWeb/CSS/StyleValues/LengthStyleValue.cpp
index 2b587d247bf..80b92c2449e 100644
--- a/Libraries/LibWeb/CSS/StyleValues/LengthStyleValue.cpp
+++ b/Libraries/LibWeb/CSS/StyleValues/LengthStyleValue.cpp
@@ -26,7 +26,7 @@ ValueComparingNonnullRefPtr<LengthStyleValue const> LengthStyleValue::create(Len
auto* instances = new (nothrow) Vector<NonnullRefPtr<LengthStyleValue const>>();
instances->ensure_capacity(last_interned_px_value + 1);
for (i32 px = 0; px <= last_interned_px_value; ++px)
- instances->unchecked_append(adopt_ref(*new (nothrow) LengthStyleValue(Length::make_px(px))));
+ instances->unchecked_append(*make_immortal(new (nothrow) LengthStyleValue(Length::make_px(px))));
return instances;
}();
return instances[static_cast<i32>(raw_value)];
diff --git a/Libraries/LibWeb/CSS/StyleValues/NumberStyleValue.h b/Libraries/LibWeb/CSS/StyleValues/NumberStyleValue.h
index 85bcfd93bad..868aa6ac36a 100644
--- a/Libraries/LibWeb/CSS/StyleValues/NumberStyleValue.h
+++ b/Libraries/LibWeb/CSS/StyleValues/NumberStyleValue.h
@@ -21,11 +21,11 @@ public:
// Zero and one dominate real-world number values (opacity, alpha, flex factors), so
// those two are interned.
if (value == 0 && !signbit(value)) {
- static auto const& zero_instance = adopt_ref(*new (nothrow) NumberStyleValue(0.0)).leak_ref();
+ static auto const& zero_instance = *make_immortal(new (nothrow) NumberStyleValue(0.0));
return zero_instance;
}
if (value == 1) {
- static auto const& one_instance = adopt_ref(*new (nothrow) NumberStyleValue(1)).leak_ref();
+ static auto const& one_instance = *make_immortal(new (nothrow) NumberStyleValue(1));
return one_instance;
}
return adopt_ref(*new (nothrow) NumberStyleValue(value));
diff --git a/Libraries/LibWeb/CSS/StyleValues/PercentageStyleValue.h b/Libraries/LibWeb/CSS/StyleValues/PercentageStyleValue.h
index 8a0beea43db..8294213586a 100644
--- a/Libraries/LibWeb/CSS/StyleValues/PercentageStyleValue.h
+++ b/Libraries/LibWeb/CSS/StyleValues/PercentageStyleValue.h
@@ -20,15 +20,15 @@ public:
{
// The 0%, 50% and 100% values dominate real-world percentages, so they are interned.
if (percentage.value() == 0) {
- static auto const& zero_instance = adopt_ref(*new (nothrow) PercentageStyleValue(Percentage(0))).leak_ref();
+ static auto const& zero_instance = *make_immortal(new (nothrow) PercentageStyleValue(Percentage(0)));
return zero_instance;
}
if (percentage.value() == 50) {
- static auto const& fifty_instance = adopt_ref(*new (nothrow) PercentageStyleValue(Percentage(50))).leak_ref();
+ static auto const& fifty_instance = *make_immortal(new (nothrow) PercentageStyleValue(Percentage(50)));
return fifty_instance;
}
if (percentage.value() == 100) {
- static auto const& hundred_instance = adopt_ref(*new (nothrow) PercentageStyleValue(Percentage(100))).leak_ref();
+ static auto const& hundred_instance = *make_immortal(new (nothrow) PercentageStyleValue(Percentage(100)));
return hundred_instance;
}
return adopt_ref(*new (nothrow) PercentageStyleValue(move(percentage)));
diff --git a/Libraries/LibWeb/CSS/StyleValues/StyleValue.cpp b/Libraries/LibWeb/CSS/StyleValues/StyleValue.cpp
index d31f95c6200..644edc5a4ca 100644
--- a/Libraries/LibWeb/CSS/StyleValues/StyleValue.cpp
+++ b/Libraries/LibWeb/CSS/StyleValues/StyleValue.cpp
@@ -279,10 +279,20 @@ ValueComparingNonnullRefPtr<StyleValue const> StyleValue::adopt_rust_style_value
ValueComparingNonnullRefPtr<StyleValue const> StyleValue::create_guaranteed_invalid()
{
- static auto& instance = adopt_ref(*new (nothrow) StyleValue(Type::GuaranteedInvalid, StyleValueFFI::rust_style_value_create_guaranteed_invalid())).leak_ref();
+ static auto& instance = *make_immortal(new (nothrow) StyleValue(Type::GuaranteedInvalid, StyleValueFFI::rust_style_value_create_guaranteed_invalid()));
return instance;
}
+NonnullRefPtr<StyleValue const> StyleValue::intern(Atomic<StyleValue const*>& slot, NonnullRefPtr<StyleValue const> value)
+{
+ // NB: Two threads can get here for the same slot. The one that loses has built a value that nothing will free.
+ auto const* candidate = make_immortal(value.ptr());
+ StyleValue const* winner = nullptr;
+ if (slot.compare_exchange_strong(winner, candidate, AK::MemoryOrder::memory_order_acq_rel))
+ return *candidate;
+ return *winner;
+}
+
void StyleValue::set_style_sheet(StyleSheetState* style_sheet)
{
// Only the types holding nested values with document-associated state care.
diff --git a/Libraries/LibWeb/CSS/StyleValues/StyleValue.h b/Libraries/LibWeb/CSS/StyleValues/StyleValue.h
index 38256ab327e..cc549b9855e 100644
--- a/Libraries/LibWeb/CSS/StyleValues/StyleValue.h
+++ b/Libraries/LibWeb/CSS/StyleValues/StyleValue.h
@@ -9,8 +9,10 @@
#pragma once
+#include <AK/Atomic.h>
#include <AK/GenericShorthands.h>
#include <AK/NonnullOwnPtr.h>
+#include <AK/NumericLimits.h>
#include <AK/RefCounted.h>
#include <AK/RefPtr.h>
#include <AK/String.h>
@@ -139,6 +141,25 @@ public:
virtual ~StyleValue() = default;
+ // An interned value belongs to every thread of the process and is never freed. These leave its count alone, which
+ // spares all other values an atomic one.
+ ALWAYS_INLINE void ref() const
+ {
+ VERIFY(m_ref_count > 0);
+ if (m_ref_count != IMMORTAL_REF_COUNT)
+ ++m_ref_count;
+ }
+ ALWAYS_INLINE bool unref() const
+ {
+ if (m_ref_count == IMMORTAL_REF_COUNT)
+ return false;
+ return RefCounted::unref();
+ }
+
+ // Puts value into slot, unless another value got there first, and returns the one that is there. This is how a table
+ // of values that fills on demand can be shared between threads.
+ static NonnullRefPtr<StyleValue const> intern(Atomic<StyleValue const*>& slot, NonnullRefPtr<StyleValue const> value);
+
enum class Type {
#define __ENUMERATE_CSS_STYLE_VALUE_TYPE(title_case, snake_case, style_value_class_name) title_case,
ENUMERATE_CSS_STYLE_VALUE_TYPES
@@ -233,10 +254,20 @@ public:
protected:
StyleValue(Type, StyleValueFFI::StyleValueData const*);
+ // For an interned value that nothing else has seen yet.
+ template<typename T>
+ static T const* make_immortal(T const* value)
+ {
+ value->m_ref_count = IMMORTAL_REF_COUNT;
+ return value;
+ }
+
// The shared Rust-owned allocation holding this value's data.
RustStyleValueHandle m_value;
private:
+ static constexpr RefCountType IMMORTAL_REF_COUNT = NumericLimits<RefCountType>::max();
+
Type m_type;
};
diff --git a/Meta/Generators/generate_libweb_css_descriptors.py b/Meta/Generators/generate_libweb_css_descriptors.py
index b64991c62b1..458a8740f6b 100644
--- a/Meta/Generators/generate_libweb_css_descriptors.py
+++ b/Meta/Generators/generate_libweb_css_descriptors.py
@@ -83,7 +83,7 @@ def write_implementation_file(out: TextIO, at_rules_data: dict, all_descriptors:
descriptor_count = len(all_descriptors)
out.write("""
-#include <AK/NeverDestroyed.h>
+#include <AK/Array.h>
#include <LibWeb/CSS/DescriptorID.h>
#include <LibWeb/CSS/StyleValues/StyleValue.h>
#include <LibWeb/CSS/Parser/Parser.h>
@@ -201,9 +201,10 @@ RefPtr<StyleValue const> descriptor_initial_value(AtRuleID at_rule_id, Descripto
if (!at_rule_supports_descriptor(at_rule_id, descriptor_id))
return nullptr;
- static NeverDestroyed<Array<Array<RefPtr<StyleValue const>, {descriptor_count}>, {at_rule_count}>> initial_values;
- if (auto initial_value = (*initial_values)[to_underlying(at_rule_id)][to_underlying(descriptor_id)])
- return initial_value.release_nonnull();
+ static Array<Array<Atomic<StyleValue const*>, {descriptor_count}>, {at_rule_count}> initial_values;
+ auto& slot = initial_values[to_underlying(at_rule_id)][to_underlying(descriptor_id)];
+ if (auto const* initial_value = slot.load(AK::MemoryOrder::memory_order_acquire))
+ return *initial_value;
// Lazily parse initial values as needed.
@@ -229,9 +230,7 @@ RefPtr<StyleValue const> descriptor_initial_value(AtRuleID at_rule_id, Descripto
case DescriptorID::{descriptor_titlecase}: {{
auto parsed_value = parse_css_descriptor(parsing_params, AtRuleID::{at_rule_titlecase}, DescriptorNameAndID::from_id(DescriptorID::{descriptor_titlecase}), "{initial_value}"sv);
VERIFY(!parsed_value.is_null());
- auto initial_value = parsed_value.release_nonnull();
- (*initial_values)[to_underlying(at_rule_id)][to_underlying(descriptor_id)] = initial_value;
- return initial_value;
+ return StyleValue::intern(slot, parsed_value.release_nonnull());
}}
""")
else:
diff --git a/Meta/Generators/generate_libweb_css_property_id.py b/Meta/Generators/generate_libweb_css_property_id.py
index 9a397675347..d7232c5da9f 100755
--- a/Meta/Generators/generate_libweb_css_property_id.py
+++ b/Meta/Generators/generate_libweb_css_property_id.py
@@ -494,16 +494,15 @@ bool property_needs_layout_node_for_resolved_value(PropertyID property_id)
NonnullRefPtr<StyleValue const> property_initial_value(PropertyID property_id)
{
- static NeverDestroyed<Array<RefPtr<StyleValue const>, to_underlying(last_property_id) + 1>> initial_values;
- if (auto initial_value = (*initial_values)[to_underlying(property_id)])
- return initial_value.release_nonnull();
+ static Array<Atomic<StyleValue const*>, to_underlying(last_property_id) + 1> initial_values;
+ auto& slot = initial_values[to_underlying(property_id)];
+ if (auto const* initial_value = slot.load(AK::MemoryOrder::memory_order_acquire))
+ return *initial_value;
// A longhand's initial value is the one style computation selects, which Rust parses.
if (property_id >= first_longhand_property_id && property_id <= last_longhand_property_id) {
auto const* data = static_cast<StyleValueFFI::StyleValueData const*>(ComputedValuesFFI::rust_style_metadata_initial_value(to_underlying(property_id)));
- auto initial_value = StyleValue::adopt_rust_style_value_data(StyleValueFFI::rust_style_value_retain(data));
- (*initial_values)[to_underlying(property_id)] = initial_value;
- return initial_value;
+ return StyleValue::intern(slot, StyleValue::adopt_rust_style_value_data(StyleValueFFI::rust_style_value_retain(data)));
}
// Lazily parse shorthand initial values as needed.
@@ -525,9 +524,7 @@ NonnullRefPtr<StyleValue const> property_initial_value(PropertyID property_id)
{{
auto parsed_value = parse_css_value(parsing_params, "{initial_value_string}"sv, PropertyID::{title});
VERIFY(!parsed_value.is_null());
- auto initial_value = parsed_value.release_nonnull();
- (*initial_values)[to_underlying(PropertyID::{title})] = initial_value;
- return initial_value;
+ return StyleValue::intern(slot, parsed_value.release_nonnull());
}}
""")