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.
This commit is contained in:
Andreas Kling 2020-05-26 23:27:22 +02:00
parent 5069d380a8
commit 82444048de
Notes: sideshowbarker 2024-07-19 06:05:55 +09:00
7 changed files with 102 additions and 5 deletions

View file

@ -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);
}
}

View file

@ -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

View file

@ -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:

View file

@ -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;
}
}
}
}

View file

@ -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_;
}
}
}

View file

@ -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();
});

View file

@ -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() { }
};
}