Skip to content

Truly independent web browser

LibWeb: Intern CSS property accessor names only once

Andreas Kling80f26c13144a
diff --git a/Libraries/LibWeb/Bindings/Rust/src/css/css_style_declaration.rs b/Libraries/LibWeb/Bindings/Rust/src/css/css_style_declaration.rs
index 54c98fcada5..e5d5c91fca7 100644
--- a/Libraries/LibWeb/Bindings/Rust/src/css/css_style_declaration.rs
+++ b/Libraries/LibWeb/Bindings/Rust/src/css/css_style_declaration.rs
@@ -87,7 +87,7 @@ impl CSSStyleDeclaration {
}
/// The value of the property or descriptor, whose name is one the bindings know.
- pub(crate) fn get(&self, name: &str) -> Utf16String {
+ pub(crate) fn get(&self, name: &'static str) -> Utf16String {
// SAFETY: The cell is live, the name is lent for the call, and LibWeb hands over the string.
unsafe {
Utf16String::from_raw_owned(web_css_declaration_get_by_name(
@@ -99,7 +99,7 @@ impl CSSStyleDeclaration {
}
/// Sets the property or descriptor, whose name is one the bindings know.
- pub(crate) fn set(&self, name: &str, value: Utf16String) -> WebIdlResult<()> {
+ pub(crate) fn set(&self, name: &'static str, value: Utf16String) -> WebIdlResult<()> {
let mut error_message = 0;
// SAFETY: The cell is live, the name is lent for the call, and the value is handed over.
let succeeded = unsafe {
diff --git a/Libraries/LibWeb/CSS/CSSStyleDeclaration.cpp b/Libraries/LibWeb/CSS/CSSStyleDeclaration.cpp
index c231463a9c1..62e441649e4 100644
--- a/Libraries/LibWeb/CSS/CSSStyleDeclaration.cpp
+++ b/Libraries/LibWeb/CSS/CSSStyleDeclaration.cpp
@@ -5,6 +5,8 @@
* SPDX-License-Identifier: BSD-2-Clause
*/
+#include <AK/HashMap.h>
+#include <AK/NeverDestroyed.h>
#include <LibJS/HostClassBuilder.h>
#include <LibWeb/CSS/CSSStyleDeclaration.h>
#include <LibWeb/CSS/StyleComputer.h>
@@ -146,9 +148,12 @@ static CSSStyleDeclaration& declaration_from_cell(void* cell)
return *static_cast<CSSStyleDeclaration*>(static_cast<GC::Cell*>(cell));
}
-static Utf16FlyString property_name_from_utf8(u8 const* name, size_t name_length)
+// NB: The bindings only pass static names, so each one is interned once.
+static Utf16FlyString property_name_from_static_utf8(u8 const* name, size_t name_length)
{
- return Utf16FlyString::from_utf8_without_validation(StringView { name, name_length });
+ static NeverDestroyed<HashMap<StringView, Utf16FlyString>> names;
+ StringView key { name, name_length };
+ return names->ensure(key, [&] { return Utf16FlyString::from_utf8_without_validation(key); });
}
static Utf16FlyString property_name_from_raw(FlatPtr property)
@@ -178,13 +183,13 @@ void* web_css_declaration_owner(void* declaration)
FlatPtr web_css_declaration_get_by_name(void* declaration, u8 const* name, size_t name_length)
{
- return declaration_from_cell(declaration).get_property_value(property_name_from_utf8(name, name_length)).into_raw();
+ return declaration_from_cell(declaration).get_property_value(property_name_from_static_utf8(name, name_length)).into_raw();
}
bool web_css_declaration_set_by_name(void* declaration, u8 const* name, size_t name_length, FlatPtr value, FlatPtr* error_message)
{
auto text = Utf16String::adopt_raw(value);
- return succeeded_or_store_message(declaration_from_cell(declaration).set_property(property_name_from_utf8(name, name_length), text.utf16_view(), u""sv), error_message);
+ return succeeded_or_store_message(declaration_from_cell(declaration).set_property(property_name_from_static_utf8(name, name_length), text.utf16_view(), u""sv), error_message);
}
FlatPtr web_css_declaration_css_text(void* declaration)