LibWeb+LibJS: Move some code around to make CSS/Parser parse faster

This makes it possible to include fewer full definitions of things,
which makes the file about 30% faster to compile.
This commit is contained in:
Ali Mohammad Pur 2023-07-09 11:40:17 +03:30 committed by Ali Mohammad Pur
parent 392b5c3b19
commit 06c6c40df9
Notes: sideshowbarker 2024-07-18 22:57:59 +09:00
28 changed files with 117 additions and 64 deletions

View file

@ -23,6 +23,7 @@ set(SOURCES
Contrib/Test262/IsHTMLDDA.cpp Contrib/Test262/IsHTMLDDA.cpp
CyclicModule.cpp CyclicModule.cpp
Heap/BlockAllocator.cpp Heap/BlockAllocator.cpp
Heap/Cell.cpp
Heap/CellAllocator.cpp Heap/CellAllocator.cpp
Heap/Handle.cpp Heap/Handle.cpp
Heap/Heap.cpp Heap/Heap.cpp

View file

@ -0,0 +1,25 @@
/*
* Copyright (c) 2020-2022, Andreas Kling <kling@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <LibJS/Heap/Cell.h>
#include <LibJS/Heap/Heap.h>
#include <LibJS/Runtime/Completion.h>
#include <LibJS/Runtime/Value.h>
namespace JS {
ThrowCompletionOr<void> JS::Cell::initialize(JS::Realm&)
{
return {};
}
void JS::Cell::Visitor::visit(JS::Value value)
{
if (value.is_cell())
visit_impl(value.as_cell());
}
}

View file

@ -14,8 +14,6 @@
#include <LibJS/Forward.h> #include <LibJS/Forward.h>
#include <LibJS/Heap/GCPtr.h> #include <LibJS/Heap/GCPtr.h>
#include <LibJS/Heap/Internals.h> #include <LibJS/Heap/Internals.h>
#include <LibJS/Runtime/Completion.h>
#include <LibJS/Runtime/Value.h>
namespace JS { namespace JS {
@ -33,7 +31,7 @@ class Cell {
AK_MAKE_NONMOVABLE(Cell); AK_MAKE_NONMOVABLE(Cell);
public: public:
virtual ThrowCompletionOr<void> initialize(Realm&) { return {}; } virtual ThrowCompletionOr<void> initialize(Realm&);
virtual ~Cell() = default; virtual ~Cell() = default;
bool is_marked() const { return m_mark; } bool is_marked() const { return m_mark; }
@ -75,11 +73,7 @@ public:
visit_impl(const_cast<RemoveConst<T>&>(*cell.ptr())); visit_impl(const_cast<RemoveConst<T>&>(*cell.ptr()));
} }
void visit(Value value) void visit(Value value);
{
if (value.is_cell())
visit_impl(value.as_cell());
}
// Allow explicitly ignoring a GC-allocated member in a visit_edges implementation instead // Allow explicitly ignoring a GC-allocated member in a visit_edges implementation instead
// of just not using it. // of just not using it.

View file

@ -21,6 +21,7 @@
#include <LibJS/Heap/Handle.h> #include <LibJS/Heap/Handle.h>
#include <LibJS/Heap/Internals.h> #include <LibJS/Heap/Internals.h>
#include <LibJS/Heap/MarkedVector.h> #include <LibJS/Heap/MarkedVector.h>
#include <LibJS/Runtime/Completion.h>
#include <LibJS/Runtime/WeakContainer.h> #include <LibJS/Runtime/WeakContainer.h>
namespace JS { namespace JS {

View file

@ -7,7 +7,6 @@
#pragma once #pragma once
#include <AK/Weakable.h> #include <AK/Weakable.h>
#include <LibJS/Heap/GCPtr.h>
#include <LibJS/Runtime/Object.h> #include <LibJS/Runtime/Object.h>
#include <LibWeb/Forward.h> #include <LibWeb/Forward.h>

View file

@ -54,6 +54,7 @@ set(SOURCES
CSS/Parser/DeclarationOrAtRule.cpp CSS/Parser/DeclarationOrAtRule.cpp
CSS/Parser/Function.cpp CSS/Parser/Function.cpp
CSS/Parser/Parser.cpp CSS/Parser/Parser.cpp
CSS/Parser/ParsingContext.cpp
CSS/Parser/Rule.cpp CSS/Parser/Rule.cpp
CSS/Parser/Token.cpp CSS/Parser/Token.cpp
CSS/Parser/Tokenizer.cpp CSS/Parser/Tokenizer.cpp

View file

@ -12,6 +12,7 @@
#include <LibWeb/CSS/CSSRule.h> #include <LibWeb/CSS/CSSRule.h>
#include <LibWeb/CSS/CSSStyleSheet.h> #include <LibWeb/CSS/CSSStyleSheet.h>
#include <LibWeb/DOM/DocumentLoadEventDelayer.h> #include <LibWeb/DOM/DocumentLoadEventDelayer.h>
#include <LibWeb/Loader/Resource.h>
namespace Web::CSS { namespace Web::CSS {

View file

@ -11,6 +11,11 @@
namespace Web::CSS { namespace Web::CSS {
WebIDL::ExceptionOr<JS::NonnullGCPtr<CSSKeyframeRule>> CSSKeyframeRule::create(JS::Realm& realm, CSS::Percentage key, Web::CSS::CSSStyleDeclaration& declarations)
{
return MUST_OR_THROW_OOM(realm.heap().allocate<CSSKeyframeRule>(realm, realm, key, declarations));
}
void CSSKeyframeRule::visit_edges(Visitor& visitor) void CSSKeyframeRule::visit_edges(Visitor& visitor)
{ {
Base::visit_edges(visitor); Base::visit_edges(visitor);

View file

@ -20,10 +20,7 @@ class CSSKeyframeRule final : public CSSRule {
WEB_PLATFORM_OBJECT(CSSKeyframeRule, CSSRule); WEB_PLATFORM_OBJECT(CSSKeyframeRule, CSSRule);
public: public:
static WebIDL::ExceptionOr<JS::NonnullGCPtr<CSSKeyframeRule>> create(JS::Realm& realm, CSS::Percentage key, CSSStyleDeclaration& declarations) static WebIDL::ExceptionOr<JS::NonnullGCPtr<CSSKeyframeRule>> create(JS::Realm& realm, CSS::Percentage key, CSSStyleDeclaration& declarations);
{
return MUST_OR_THROW_OOM(realm.heap().allocate<CSSKeyframeRule>(realm, realm, key, declarations));
}
virtual ~CSSKeyframeRule() = default; virtual ~CSSKeyframeRule() = default;

View file

@ -10,6 +10,11 @@
namespace Web::CSS { namespace Web::CSS {
WebIDL::ExceptionOr<JS::NonnullGCPtr<CSSKeyframesRule>> CSSKeyframesRule::create(JS::Realm& realm, AK::FlyString name, Vector<JS::NonnullGCPtr<CSSKeyframeRule>> keyframes)
{
return MUST_OR_THROW_OOM(realm.heap().allocate<CSSKeyframesRule>(realm, realm, move(name), move(keyframes)));
}
void CSSKeyframesRule::visit_edges(Visitor& visitor) void CSSKeyframesRule::visit_edges(Visitor& visitor)
{ {
Base::visit_edges(visitor); Base::visit_edges(visitor);

View file

@ -21,10 +21,7 @@ class CSSKeyframesRule final : public CSSRule {
WEB_PLATFORM_OBJECT(CSSKeyframesRule, CSSRule); WEB_PLATFORM_OBJECT(CSSKeyframesRule, CSSRule);
public: public:
static WebIDL::ExceptionOr<JS::NonnullGCPtr<CSSKeyframesRule>> create(JS::Realm& realm, FlyString name, Vector<JS::NonnullGCPtr<CSSKeyframeRule>> keyframes) static WebIDL::ExceptionOr<JS::NonnullGCPtr<CSSKeyframesRule>> create(JS::Realm& realm, FlyString name, Vector<JS::NonnullGCPtr<CSSKeyframeRule>> keyframes);
{
return MUST_OR_THROW_OOM(realm.heap().allocate<CSSKeyframesRule>(realm, realm, move(name), move(keyframes)));
}
virtual ~CSSKeyframesRule() = default; virtual ~CSSKeyframesRule() = default;

View file

@ -11,7 +11,6 @@
#include <LibWeb/CSS/CSSRuleList.h> #include <LibWeb/CSS/CSSRuleList.h>
#include <LibWeb/CSS/CSSStyleRule.h> #include <LibWeb/CSS/CSSStyleRule.h>
#include <LibWeb/CSS/StyleSheet.h> #include <LibWeb/CSS/StyleSheet.h>
#include <LibWeb/Loader/Resource.h>
namespace Web::CSS { namespace Web::CSS {

View file

@ -7,6 +7,7 @@
#pragma once #pragma once
#include <AK/Optional.h> #include <AK/Optional.h>
#include <LibGfx/Painter.h>
#include <LibWeb/CSS/BackdropFilter.h> #include <LibWeb/CSS/BackdropFilter.h>
#include <LibWeb/CSS/CalculatedOr.h> #include <LibWeb/CSS/CalculatedOr.h>
#include <LibWeb/CSS/Clip.h> #include <LibWeb/CSS/Clip.h>

View file

@ -12,7 +12,6 @@
#include <AK/Debug.h> #include <AK/Debug.h>
#include <AK/GenericLexer.h> #include <AK/GenericLexer.h>
#include <AK/SourceLocation.h> #include <AK/SourceLocation.h>
#include <LibWeb/Bindings/MainThreadVM.h>
#include <LibWeb/CSS/CSSFontFaceRule.h> #include <LibWeb/CSS/CSSFontFaceRule.h>
#include <LibWeb/CSS/CSSImportRule.h> #include <LibWeb/CSS/CSSImportRule.h>
#include <LibWeb/CSS/CSSKeyframeRule.h> #include <LibWeb/CSS/CSSKeyframeRule.h>
@ -84,7 +83,6 @@
#include <LibWeb/CSS/StyleValues/URLStyleValue.h> #include <LibWeb/CSS/StyleValues/URLStyleValue.h>
#include <LibWeb/CSS/StyleValues/UnresolvedStyleValue.h> #include <LibWeb/CSS/StyleValues/UnresolvedStyleValue.h>
#include <LibWeb/CSS/StyleValues/UnsetStyleValue.h> #include <LibWeb/CSS/StyleValues/UnsetStyleValue.h>
#include <LibWeb/DOM/Document.h>
#include <LibWeb/Dump.h> #include <LibWeb/Dump.h>
#include <LibWeb/Infra/Strings.h> #include <LibWeb/Infra/Strings.h>
@ -95,43 +93,6 @@ static void log_parse_error(SourceLocation const& location = SourceLocation::cur
namespace Web::CSS::Parser { namespace Web::CSS::Parser {
ParsingContext::ParsingContext(JS::Realm& realm)
: m_realm(realm)
{
}
ParsingContext::ParsingContext(DOM::Document const& document, AK::URL url)
: m_realm(const_cast<JS::Realm&>(document.realm()))
, m_document(&document)
, m_url(move(url))
{
}
ParsingContext::ParsingContext(DOM::Document const& document)
: m_realm(const_cast<JS::Realm&>(document.realm()))
, m_document(&document)
, m_url(document.url())
{
}
ParsingContext::ParsingContext(DOM::ParentNode& parent_node)
: m_realm(parent_node.realm())
, m_document(&parent_node.document())
, m_url(parent_node.document().url())
{
}
bool ParsingContext::in_quirks_mode() const
{
return m_document ? m_document->in_quirks_mode() : false;
}
// https://www.w3.org/TR/css-values-4/#relative-urls
AK::URL ParsingContext::complete_url(StringView relative_url) const
{
return m_url.complete_url(relative_url);
}
ErrorOr<Parser> Parser::create(ParsingContext const& context, StringView input, StringView encoding) ErrorOr<Parser> Parser::create(ParsingContext const& context, StringView input, StringView encoding)
{ {
auto tokens = TRY(Tokenizer::tokenize(input, encoding)); auto tokens = TRY(Tokenizer::tokenize(input, encoding));
@ -9279,7 +9240,7 @@ CSS::Length CSS::Parser::Parser::parse_as_sizes_attribute()
// If it does not parse correctly, or it does parse correctly but the <media-condition> evaluates to false, continue. // If it does not parse correctly, or it does parse correctly but the <media-condition> evaluates to false, continue.
TokenStream<ComponentValue> token_stream { unparsed_size }; TokenStream<ComponentValue> token_stream { unparsed_size };
auto media_condition = parse_media_condition(token_stream, MediaCondition::AllowOr::Yes); auto media_condition = parse_media_condition(token_stream, MediaCondition::AllowOr::Yes);
if (media_condition && media_condition->evaluate(m_context.document()->window()) == CSS::MatchResult::True) { if (media_condition && media_condition->evaluate(*m_context.window()) == CSS::MatchResult::True) {
return size.value(); return size.value();
} else { } else {
continue; continue;

View file

@ -44,6 +44,7 @@ public:
bool in_quirks_mode() const; bool in_quirks_mode() const;
DOM::Document const* document() const { return m_document; } DOM::Document const* document() const { return m_document; }
HTML::Window const* window() const;
AK::URL complete_url(StringView) const; AK::URL complete_url(StringView) const;
PropertyID current_property_id() const { return m_current_property_id; } PropertyID current_property_id() const { return m_current_property_id; }

View file

@ -0,0 +1,58 @@
/*
* Copyright (c) 2018-2022, Andreas Kling <kling@serenityos.org>
* Copyright (c) 2020-2021, the SerenityOS developers.
* Copyright (c) 2021-2023, Sam Atkins <atkinssj@serenityos.org>
* Copyright (c) 2021, Tobias Christiansen <tobyase@serenityos.org>
* Copyright (c) 2022, MacDue <macdue@dueutil.tech>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <LibWeb/CSS/Parser/Parser.h>
#include <LibWeb/DOM/Document.h>
namespace Web::CSS::Parser {
ParsingContext::ParsingContext(JS::Realm& realm)
: m_realm(realm)
{
}
ParsingContext::ParsingContext(DOM::Document const& document, AK::URL url)
: m_realm(const_cast<JS::Realm&>(document.realm()))
, m_document(&document)
, m_url(move(url))
{
}
ParsingContext::ParsingContext(DOM::Document const& document)
: m_realm(const_cast<JS::Realm&>(document.realm()))
, m_document(&document)
, m_url(document.url())
{
}
ParsingContext::ParsingContext(DOM::ParentNode& parent_node)
: m_realm(parent_node.realm())
, m_document(&parent_node.document())
, m_url(parent_node.document().url())
{
}
bool ParsingContext::in_quirks_mode() const
{
return m_document ? m_document->in_quirks_mode() : false;
}
// https://www.w3.org/TR/css-values-4/#relative-urls
AK::URL ParsingContext::complete_url(StringView relative_url) const
{
return m_url.complete_url(relative_url);
}
HTML::Window const* ParsingContext::window() const
{
return m_document ? &m_document->window() : nullptr;
}
}

View file

@ -7,6 +7,7 @@
* SPDX-License-Identifier: BSD-2-Clause * SPDX-License-Identifier: BSD-2-Clause
*/ */
#include <LibGfx/Font/FontDatabase.h>
#include <LibGfx/Font/FontStyleMapping.h> #include <LibGfx/Font/FontStyleMapping.h>
#include <LibWeb/CSS/StyleValue.h> #include <LibWeb/CSS/StyleValue.h>
#include <LibWeb/CSS/StyleValues/AbstractImageStyleValue.h> #include <LibWeb/CSS/StyleValues/AbstractImageStyleValue.h>

View file

@ -20,7 +20,7 @@
#include <AK/Variant.h> #include <AK/Variant.h>
#include <AK/Vector.h> #include <AK/Vector.h>
#include <AK/WeakPtr.h> #include <AK/WeakPtr.h>
#include <LibGfx/Painter.h> #include <LibGfx/Color.h>
#include <LibWeb/CSS/Enums.h> #include <LibWeb/CSS/Enums.h>
#include <LibWeb/CSS/Length.h> #include <LibWeb/CSS/Length.h>
#include <LibWeb/CSS/ValueID.h> #include <LibWeb/CSS/ValueID.h>

View file

@ -9,6 +9,7 @@
#pragma once #pragma once
#include <AK/Function.h>
#include <LibWeb/CSS/Angle.h> #include <LibWeb/CSS/Angle.h>
#include <LibWeb/CSS/CSSNumericType.h> #include <LibWeb/CSS/CSSNumericType.h>
#include <LibWeb/CSS/Frequency.h> #include <LibWeb/CSS/Frequency.h>

View file

@ -4,6 +4,8 @@
* SPDX-License-Identifier: BSD-2-Clause * SPDX-License-Identifier: BSD-2-Clause
*/ */
#include <LibJS/Heap/Heap.h>
#include <LibJS/Runtime/Error.h>
#include <LibWeb/DOM/IDLEventListener.h> #include <LibWeb/DOM/IDLEventListener.h>
namespace Web::DOM { namespace Web::DOM {

View file

@ -5,6 +5,8 @@
* SPDX-License-Identifier: BSD-2-Clause * SPDX-License-Identifier: BSD-2-Clause
*/ */
#include <LibJS/Heap/Heap.h>
#include <LibJS/Runtime/Error.h>
#include <LibWeb/DOM/LiveNodeList.h> #include <LibWeb/DOM/LiveNodeList.h>
#include <LibWeb/DOM/Node.h> #include <LibWeb/DOM/Node.h>

View file

@ -5,6 +5,7 @@
*/ */
#include <LibJS/Heap/Heap.h> #include <LibJS/Heap/Heap.h>
#include <LibJS/Runtime/Error.h>
#include <LibWeb/DOM/StaticNodeList.h> #include <LibWeb/DOM/StaticNodeList.h>
namespace Web::DOM { namespace Web::DOM {

View file

@ -8,10 +8,7 @@
#pragma once #pragma once
#include <AK/URL.h> #include <AK/URL.h>
#include <LibJS/Runtime/ExecutionContext.h> #include <LibJS/Forward.h>
#include <LibJS/Runtime/GlobalObject.h>
#include <LibJS/Runtime/Object.h>
#include <LibJS/Runtime/Realm.h>
#include <LibWeb/HTML/EventLoop/EventLoop.h> #include <LibWeb/HTML/EventLoop/EventLoop.h>
#include <LibWeb/HTML/Origin.h> #include <LibWeb/HTML/Origin.h>
#include <LibWeb/HTML/Scripting/ModuleMap.h> #include <LibWeb/HTML/Scripting/ModuleMap.h>

View file

@ -8,6 +8,7 @@
#include <AK/HashTable.h> #include <AK/HashTable.h>
#include <AK/Vector.h> #include <AK/Vector.h>
#include <LibJS/Forward.h> #include <LibJS/Forward.h>
#include <LibJS/Runtime/VM.h>
#include <LibWeb/HTML/StructuredSerialize.h> #include <LibWeb/HTML/StructuredSerialize.h>
#include <LibWeb/WebIDL/ExceptionOr.h> #include <LibWeb/WebIDL/ExceptionOr.h>

View file

@ -4,6 +4,7 @@
* SPDX-License-Identifier: BSD-2-Clause * SPDX-License-Identifier: BSD-2-Clause
*/ */
#include <AK/QuickSort.h>
#include <LibWeb/Bindings/MainThreadVM.h> #include <LibWeb/Bindings/MainThreadVM.h>
#include <LibWeb/DOM/Document.h> #include <LibWeb/DOM/Document.h>
#include <LibWeb/HTML/BrowsingContextGroup.h> #include <LibWeb/HTML/BrowsingContextGroup.h>

View file

@ -6,6 +6,7 @@
#include <AK/Math.h> #include <AK/Math.h>
#include <LibGfx/Gradients.h> #include <LibGfx/Gradients.h>
#include <LibGfx/Painter.h>
#include <LibWeb/CSS/StyleValues/ConicGradientStyleValue.h> #include <LibWeb/CSS/StyleValues/ConicGradientStyleValue.h>
#include <LibWeb/CSS/StyleValues/LinearGradientStyleValue.h> #include <LibWeb/CSS/StyleValues/LinearGradientStyleValue.h>
#include <LibWeb/CSS/StyleValues/RadialGradientStyleValue.h> #include <LibWeb/CSS/StyleValues/RadialGradientStyleValue.h>

View file

@ -6,6 +6,7 @@
#include <LibGfx/AntiAliasingPainter.h> #include <LibGfx/AntiAliasingPainter.h>
#include <LibGfx/StylePainter.h> #include <LibGfx/StylePainter.h>
#include <LibJS/Heap/Heap.h>
#include <LibWeb/Layout/ListItemMarkerBox.h> #include <LibWeb/Layout/ListItemMarkerBox.h>
#include <LibWeb/Painting/MarkerPaintable.h> #include <LibWeb/Painting/MarkerPaintable.h>

View file

@ -8,7 +8,6 @@
#include <AK/DeprecatedFlyString.h> #include <AK/DeprecatedFlyString.h>
#include <AK/Diagnostics.h> #include <AK/Diagnostics.h>
#include <LibJS/Runtime/VM.h>
#include <LibWeb/Bindings/PlatformObject.h> #include <LibWeb/Bindings/PlatformObject.h>
#include <LibWeb/HTML/Scripting/Environments.h> #include <LibWeb/HTML/Scripting/Environments.h>