Ver código fonte

LibWeb: Add cached global attribute name FlyStrings

Instead of creating extremely common FlyStrings like "id" and "class"
on demand every time they are needed, we now have AttributeNames.h,
which provides Web::HTML::AttributeNames::{id,class_}

This avoids a bunch of string allocations during selector matching.
Andreas Kling 5 anos atrás
pai
commit
82444048de

+ 3 - 2
Libraries/LibWeb/Bindings/ElementWrapper.cpp

@@ -31,6 +31,7 @@
 #include <LibJS/Runtime/PrimitiveString.h>
 #include <LibJS/Runtime/Value.h>
 #include <LibWeb/Bindings/ElementWrapper.h>
+#include <LibWeb/DOM/AttributeNames.h>
 #include <LibWeb/DOM/Element.h>
 
 namespace Web {
@@ -131,7 +132,7 @@ void ElementWrapper::inner_html_setter(JS::Interpreter& interpreter, JS::Value v
 JS::Value ElementWrapper::id_getter(JS::Interpreter& interpreter)
 {
     if (auto* impl = impl_from(interpreter))
-        return JS::js_string(interpreter, impl->attribute("id"));
+        return JS::js_string(interpreter, impl->attribute(HTML::AttributeNames::id));
     return {};
 }
 
@@ -141,7 +142,7 @@ void ElementWrapper::id_setter(JS::Interpreter& interpreter, JS::Value value)
         auto string = value.to_string(interpreter);
         if (interpreter.exception())
             return;
-        impl->set_attribute("id", string);
+        impl->set_attribute(HTML::AttributeNames::id, string);
     }
 }
 

+ 1 - 0
Libraries/LibWeb/CMakeLists.txt

@@ -25,6 +25,7 @@ set(SOURCES
     CSS/StyleRule.cpp
     CSS/StyleSheet.cpp
     CSS/StyleValue.cpp
+    DOM/AttributeNames.cpp
     DOM/CanvasRenderingContext2D.cpp
     DOM/CharacterData.cpp
     DOM/Comment.cpp

+ 2 - 1
Libraries/LibWeb/CSS/SelectorEngine.cpp

@@ -25,6 +25,7 @@
  */
 
 #include <LibWeb/CSS/SelectorEngine.h>
+#include <LibWeb/DOM/AttributeNames.h>
 #include <LibWeb/DOM/Document.h>
 #include <LibWeb/DOM/Element.h>
 #include <LibWeb/DOM/Text.h>
@@ -98,7 +99,7 @@ bool matches(const Selector::SimpleSelector& component, const Element& element)
     case Selector::SimpleSelector::Type::Universal:
         return true;
     case Selector::SimpleSelector::Type::Id:
-        return component.value == element.attribute("id");
+        return component.value == element.attribute(HTML::AttributeNames::id);
     case Selector::SimpleSelector::Type::Class:
         return element.has_class(component.value);
     case Selector::SimpleSelector::Type::TagName:

+ 48 - 0
Libraries/LibWeb/DOM/AttributeNames.cpp

@@ -0,0 +1,48 @@
+/*
+ * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright notice, this
+ *    list of conditions and the following disclaimer.
+ *
+ * 2. Redistributions in binary form must reproduce the above copyright notice,
+ *    this list of conditions and the following disclaimer in the documentation
+ *    and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include <LibWeb/DOM/AttributeNames.h>
+
+namespace Web {
+namespace HTML {
+namespace AttributeNames {
+
+FlyString id;
+FlyString class_;
+
+void initialize()
+{
+    bool s_initialized = false;
+    if (s_initialized)
+        return;
+    id = "id";
+    class_ = "class";
+    s_initialized = true;
+}
+
+}
+}
+}

+ 42 - 0
Libraries/LibWeb/DOM/AttributeNames.h

@@ -0,0 +1,42 @@
+/*
+ * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright notice, this
+ *    list of conditions and the following disclaimer.
+ *
+ * 2. Redistributions in binary form must reproduce the above copyright notice,
+ *    this list of conditions and the following disclaimer in the documentation
+ *    and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#pragma once
+
+#include <AK/FlyString.h>
+
+namespace Web {
+namespace HTML {
+namespace AttributeNames {
+
+void initialize();
+
+extern FlyString id;
+extern FlyString class_;
+
+}
+}
+}

+ 3 - 0
Libraries/LibWeb/DOM/Document.cpp

@@ -37,6 +37,7 @@
 #include <LibWeb/Bindings/WindowObject.h>
 #include <LibWeb/CSS/SelectorEngine.h>
 #include <LibWeb/CSS/StyleResolver.h>
+#include <LibWeb/DOM/AttributeNames.h>
 #include <LibWeb/DOM/Document.h>
 #include <LibWeb/DOM/DocumentType.h>
 #include <LibWeb/DOM/Element.h>
@@ -65,6 +66,8 @@ Document::Document(const URL& url)
     , m_url(url)
     , m_window(Window::create_with_document(*this))
 {
+    HTML::AttributeNames::initialize();
+
     m_style_update_timer = Core::Timer::create_single_shot(0, [this] {
         update_style();
     });

+ 3 - 2
Libraries/LibWeb/DOM/NonElementParentNode.h

@@ -27,6 +27,7 @@
 #pragma once
 
 #include <AK/Forward.h>
+#include <LibWeb/DOM/AttributeNames.h>
 #include <LibWeb/Forward.h>
 #include <LibWeb/TreeNode.h>
 
@@ -39,7 +40,7 @@ public:
     {
         const Element* found_element = nullptr;
         static_cast<const NodeType*>(this)->template for_each_in_subtree_of_type<Element>([&](auto& element) {
-            if (element.attribute("id") == id) {
+            if (element.attribute(HTML::AttributeNames::id) == id) {
                 found_element = &element;
                 return IterationDecision::Break;
             }
@@ -53,7 +54,7 @@ public:
     }
 
 protected:
-    NonElementParentNode() {}
+    NonElementParentNode() { }
 };
 
 }