2020-05-22 21:46:13 +02:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
|
2021-07-14 23:15:40 +02:00
|
|
|
* Copyright (c) 2021, Max Wipfli <max.wipfli@serenityos.org>
|
2020-05-22 21:46:13 +02:00
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-05-22 21:46:13 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2023-01-08 19:23:00 -05:00
|
|
|
#include <AK/DeprecatedFlyString.h>
|
2022-12-04 18:02:33 +00:00
|
|
|
#include <AK/DeprecatedString.h>
|
2021-07-14 23:53:11 +02:00
|
|
|
#include <AK/Function.h>
|
LibWeb: Change HTMLToken storage architecture
This completely changes how HTMLTokens store their data. Previously,
space was allocated for all token types separately. Now, the HTMLToken's
data is stored in just a String, two booleans and a Variant.
This change reduces sizeof(HTMLToken) from 68 to 32. Also, this reduces
raw tokenization time by around 20 to 50 percent, depending on the page.
Full document parsing time (with HTMLDocumentParser, on a local HTML
page without any dependency files) is reduced by between 4 and 20
percent, depending on the page.
Since tokenizing HTML pages can easily generated 50'000 tokens and more,
the storage has been designed in a way that avoids heap allocations
where possible, while trying to reduce the size of the tokens. The only
tokens which need to allocate on the heap are thus DOCTYPE tokens (max.
1 per document), and tag tokens (but only if they have attributes). This
way, only around 5 percent of all tokens generated need to allocate on
the heap (except for StringImpl allocations).
2021-07-15 18:49:39 +02:00
|
|
|
#include <AK/OwnPtr.h>
|
2020-05-22 21:46:13 +02:00
|
|
|
#include <AK/Types.h>
|
LibWeb: Change HTMLToken storage architecture
This completely changes how HTMLTokens store their data. Previously,
space was allocated for all token types separately. Now, the HTMLToken's
data is stored in just a String, two booleans and a Variant.
This change reduces sizeof(HTMLToken) from 68 to 32. Also, this reduces
raw tokenization time by around 20 to 50 percent, depending on the page.
Full document parsing time (with HTMLDocumentParser, on a local HTML
page without any dependency files) is reduced by between 4 and 20
percent, depending on the page.
Since tokenizing HTML pages can easily generated 50'000 tokens and more,
the storage has been designed in a way that avoids heap allocations
where possible, while trying to reduce the size of the tokens. The only
tokens which need to allocate on the heap are thus DOCTYPE tokens (max.
1 per document), and tag tokens (but only if they have attributes). This
way, only around 5 percent of all tokens generated need to allocate on
the heap (except for StringImpl allocations).
2021-07-15 18:49:39 +02:00
|
|
|
#include <AK/Variant.h>
|
2020-05-22 21:46:13 +02:00
|
|
|
#include <AK/Vector.h>
|
|
|
|
|
2020-07-28 18:20:36 +02:00
|
|
|
namespace Web::HTML {
|
2020-05-22 21:46:13 +02:00
|
|
|
|
2021-07-15 15:53:54 +02:00
|
|
|
class HTMLTokenizer;
|
|
|
|
|
2020-05-22 21:46:13 +02:00
|
|
|
class HTMLToken {
|
2021-07-15 22:32:36 +02:00
|
|
|
AK_MAKE_NONCOPYABLE(HTMLToken);
|
2020-05-22 21:46:13 +02:00
|
|
|
|
|
|
|
public:
|
LibWeb: Change HTMLToken storage architecture
This completely changes how HTMLTokens store their data. Previously,
space was allocated for all token types separately. Now, the HTMLToken's
data is stored in just a String, two booleans and a Variant.
This change reduces sizeof(HTMLToken) from 68 to 32. Also, this reduces
raw tokenization time by around 20 to 50 percent, depending on the page.
Full document parsing time (with HTMLDocumentParser, on a local HTML
page without any dependency files) is reduced by between 4 and 20
percent, depending on the page.
Since tokenizing HTML pages can easily generated 50'000 tokens and more,
the storage has been designed in a way that avoids heap allocations
where possible, while trying to reduce the size of the tokens. The only
tokens which need to allocate on the heap are thus DOCTYPE tokens (max.
1 per document), and tag tokens (but only if they have attributes). This
way, only around 5 percent of all tokens generated need to allocate on
the heap (except for StringImpl allocations).
2021-07-15 18:49:39 +02:00
|
|
|
enum class Type : u8 {
|
2020-05-24 20:24:43 +02:00
|
|
|
Invalid,
|
2020-05-22 21:46:13 +02:00
|
|
|
DOCTYPE,
|
|
|
|
StartTag,
|
|
|
|
EndTag,
|
|
|
|
Comment,
|
|
|
|
Character,
|
|
|
|
EndOfFile,
|
|
|
|
};
|
|
|
|
|
2021-07-14 23:15:40 +02:00
|
|
|
struct Position {
|
|
|
|
size_t line { 0 };
|
|
|
|
size_t column { 0 };
|
|
|
|
};
|
|
|
|
|
2021-07-14 23:17:35 +02:00
|
|
|
struct Attribute {
|
2022-12-04 18:02:33 +00:00
|
|
|
DeprecatedString prefix;
|
|
|
|
DeprecatedString local_name { "" };
|
|
|
|
DeprecatedString namespace_;
|
|
|
|
DeprecatedString value { "" };
|
2021-07-14 23:15:40 +02:00
|
|
|
Position name_start_position;
|
|
|
|
Position value_start_position;
|
|
|
|
Position name_end_position;
|
|
|
|
Position value_end_position;
|
|
|
|
};
|
|
|
|
|
2021-07-15 00:03:50 +02:00
|
|
|
struct DoctypeData {
|
|
|
|
// NOTE: "Missing" is a distinct state from the empty string.
|
2022-12-04 18:02:33 +00:00
|
|
|
DeprecatedString name;
|
|
|
|
DeprecatedString public_identifier;
|
|
|
|
DeprecatedString system_identifier;
|
2021-07-15 00:03:50 +02:00
|
|
|
bool missing_name { true };
|
|
|
|
bool missing_public_identifier { true };
|
|
|
|
bool missing_system_identifier { true };
|
|
|
|
bool force_quirks { false };
|
|
|
|
};
|
|
|
|
|
2020-08-05 16:31:20 -04:00
|
|
|
static HTMLToken make_character(u32 code_point)
|
2020-05-28 18:43:52 +02:00
|
|
|
{
|
2021-07-15 15:55:46 +02:00
|
|
|
HTMLToken token { Type::Character };
|
2021-07-14 23:33:12 +02:00
|
|
|
token.set_code_point(code_point);
|
2020-05-28 18:43:52 +02:00
|
|
|
return token;
|
|
|
|
}
|
|
|
|
|
2023-01-08 19:23:00 -05:00
|
|
|
static HTMLToken make_start_tag(DeprecatedFlyString const& tag_name)
|
2020-07-23 17:30:03 +02:00
|
|
|
{
|
2021-07-15 15:55:46 +02:00
|
|
|
HTMLToken token { Type::StartTag };
|
2021-07-14 23:37:48 +02:00
|
|
|
token.set_tag_name(tag_name);
|
2020-07-23 17:30:03 +02:00
|
|
|
return token;
|
|
|
|
}
|
|
|
|
|
2021-07-15 15:55:46 +02:00
|
|
|
HTMLToken() = default;
|
|
|
|
|
|
|
|
HTMLToken(Type type)
|
|
|
|
: m_type(type)
|
|
|
|
{
|
LibWeb: Change HTMLToken storage architecture
This completely changes how HTMLTokens store their data. Previously,
space was allocated for all token types separately. Now, the HTMLToken's
data is stored in just a String, two booleans and a Variant.
This change reduces sizeof(HTMLToken) from 68 to 32. Also, this reduces
raw tokenization time by around 20 to 50 percent, depending on the page.
Full document parsing time (with HTMLDocumentParser, on a local HTML
page without any dependency files) is reduced by between 4 and 20
percent, depending on the page.
Since tokenizing HTML pages can easily generated 50'000 tokens and more,
the storage has been designed in a way that avoids heap allocations
where possible, while trying to reduce the size of the tokens. The only
tokens which need to allocate on the heap are thus DOCTYPE tokens (max.
1 per document), and tag tokens (but only if they have attributes). This
way, only around 5 percent of all tokens generated need to allocate on
the heap (except for StringImpl allocations).
2021-07-15 18:49:39 +02:00
|
|
|
switch (m_type) {
|
|
|
|
case Type::Character:
|
|
|
|
m_data.set(0u);
|
|
|
|
break;
|
|
|
|
case Type::DOCTYPE:
|
|
|
|
m_data.set(OwnPtr<DoctypeData> {});
|
|
|
|
break;
|
|
|
|
case Type::StartTag:
|
|
|
|
case Type::EndTag:
|
|
|
|
m_data.set(OwnPtr<Vector<Attribute>>());
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2021-07-15 15:55:46 +02:00
|
|
|
}
|
|
|
|
|
LibWeb: Change HTMLToken storage architecture
This completely changes how HTMLTokens store their data. Previously,
space was allocated for all token types separately. Now, the HTMLToken's
data is stored in just a String, two booleans and a Variant.
This change reduces sizeof(HTMLToken) from 68 to 32. Also, this reduces
raw tokenization time by around 20 to 50 percent, depending on the page.
Full document parsing time (with HTMLDocumentParser, on a local HTML
page without any dependency files) is reduced by between 4 and 20
percent, depending on the page.
Since tokenizing HTML pages can easily generated 50'000 tokens and more,
the storage has been designed in a way that avoids heap allocations
where possible, while trying to reduce the size of the tokens. The only
tokens which need to allocate on the heap are thus DOCTYPE tokens (max.
1 per document), and tag tokens (but only if they have attributes). This
way, only around 5 percent of all tokens generated need to allocate on
the heap (except for StringImpl allocations).
2021-07-15 18:49:39 +02:00
|
|
|
HTMLToken(HTMLToken&&) = default;
|
|
|
|
HTMLToken& operator=(HTMLToken&&) = default;
|
2021-07-15 22:32:36 +02:00
|
|
|
|
2020-05-24 00:14:23 +02:00
|
|
|
bool is_doctype() const { return m_type == Type::DOCTYPE; }
|
|
|
|
bool is_start_tag() const { return m_type == Type::StartTag; }
|
|
|
|
bool is_end_tag() const { return m_type == Type::EndTag; }
|
|
|
|
bool is_comment() const { return m_type == Type::Comment; }
|
|
|
|
bool is_character() const { return m_type == Type::Character; }
|
|
|
|
bool is_end_of_file() const { return m_type == Type::EndOfFile; }
|
|
|
|
|
2020-08-05 16:31:20 -04:00
|
|
|
u32 code_point() const
|
2020-05-24 19:51:50 +02:00
|
|
|
{
|
2021-02-23 20:42:32 +01:00
|
|
|
VERIFY(is_character());
|
LibWeb: Change HTMLToken storage architecture
This completely changes how HTMLTokens store their data. Previously,
space was allocated for all token types separately. Now, the HTMLToken's
data is stored in just a String, two booleans and a Variant.
This change reduces sizeof(HTMLToken) from 68 to 32. Also, this reduces
raw tokenization time by around 20 to 50 percent, depending on the page.
Full document parsing time (with HTMLDocumentParser, on a local HTML
page without any dependency files) is reduced by between 4 and 20
percent, depending on the page.
Since tokenizing HTML pages can easily generated 50'000 tokens and more,
the storage has been designed in a way that avoids heap allocations
where possible, while trying to reduce the size of the tokens. The only
tokens which need to allocate on the heap are thus DOCTYPE tokens (max.
1 per document), and tag tokens (but only if they have attributes). This
way, only around 5 percent of all tokens generated need to allocate on
the heap (except for StringImpl allocations).
2021-07-15 18:49:39 +02:00
|
|
|
return m_data.get<u32>();
|
2020-05-24 19:51:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool is_parser_whitespace() const
|
|
|
|
{
|
|
|
|
// NOTE: The parser considers '\r' to be whitespace, while the tokenizer does not.
|
|
|
|
if (!is_character())
|
|
|
|
return false;
|
2020-08-05 16:31:20 -04:00
|
|
|
switch (code_point()) {
|
2020-05-24 19:51:50 +02:00
|
|
|
case '\t':
|
|
|
|
case '\n':
|
|
|
|
case '\f':
|
|
|
|
case '\r':
|
|
|
|
case ' ':
|
|
|
|
return true;
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-14 23:33:12 +02:00
|
|
|
void set_code_point(u32 code_point)
|
|
|
|
{
|
|
|
|
VERIFY(is_character());
|
LibWeb: Change HTMLToken storage architecture
This completely changes how HTMLTokens store their data. Previously,
space was allocated for all token types separately. Now, the HTMLToken's
data is stored in just a String, two booleans and a Variant.
This change reduces sizeof(HTMLToken) from 68 to 32. Also, this reduces
raw tokenization time by around 20 to 50 percent, depending on the page.
Full document parsing time (with HTMLDocumentParser, on a local HTML
page without any dependency files) is reduced by between 4 and 20
percent, depending on the page.
Since tokenizing HTML pages can easily generated 50'000 tokens and more,
the storage has been designed in a way that avoids heap allocations
where possible, while trying to reduce the size of the tokens. The only
tokens which need to allocate on the heap are thus DOCTYPE tokens (max.
1 per document), and tag tokens (but only if they have attributes). This
way, only around 5 percent of all tokens generated need to allocate on
the heap (except for StringImpl allocations).
2021-07-15 18:49:39 +02:00
|
|
|
m_data.get<u32>() = code_point;
|
2021-07-14 23:33:12 +02:00
|
|
|
}
|
|
|
|
|
2023-01-08 19:23:00 -05:00
|
|
|
DeprecatedFlyString const& comment() const
|
2021-07-14 23:32:18 +02:00
|
|
|
{
|
|
|
|
VERIFY(is_comment());
|
LibWeb: Change HTMLToken storage architecture
This completely changes how HTMLTokens store their data. Previously,
space was allocated for all token types separately. Now, the HTMLToken's
data is stored in just a String, two booleans and a Variant.
This change reduces sizeof(HTMLToken) from 68 to 32. Also, this reduces
raw tokenization time by around 20 to 50 percent, depending on the page.
Full document parsing time (with HTMLDocumentParser, on a local HTML
page without any dependency files) is reduced by between 4 and 20
percent, depending on the page.
Since tokenizing HTML pages can easily generated 50'000 tokens and more,
the storage has been designed in a way that avoids heap allocations
where possible, while trying to reduce the size of the tokens. The only
tokens which need to allocate on the heap are thus DOCTYPE tokens (max.
1 per document), and tag tokens (but only if they have attributes). This
way, only around 5 percent of all tokens generated need to allocate on
the heap (except for StringImpl allocations).
2021-07-15 18:49:39 +02:00
|
|
|
return m_string_data;
|
2021-07-14 23:32:18 +02:00
|
|
|
}
|
|
|
|
|
2022-12-04 18:02:33 +00:00
|
|
|
void set_comment(DeprecatedString comment)
|
2021-07-14 23:32:18 +02:00
|
|
|
{
|
|
|
|
VERIFY(is_comment());
|
LibWeb: Change HTMLToken storage architecture
This completely changes how HTMLTokens store their data. Previously,
space was allocated for all token types separately. Now, the HTMLToken's
data is stored in just a String, two booleans and a Variant.
This change reduces sizeof(HTMLToken) from 68 to 32. Also, this reduces
raw tokenization time by around 20 to 50 percent, depending on the page.
Full document parsing time (with HTMLDocumentParser, on a local HTML
page without any dependency files) is reduced by between 4 and 20
percent, depending on the page.
Since tokenizing HTML pages can easily generated 50'000 tokens and more,
the storage has been designed in a way that avoids heap allocations
where possible, while trying to reduce the size of the tokens. The only
tokens which need to allocate on the heap are thus DOCTYPE tokens (max.
1 per document), and tag tokens (but only if they have attributes). This
way, only around 5 percent of all tokens generated need to allocate on
the heap (except for StringImpl allocations).
2021-07-15 18:49:39 +02:00
|
|
|
m_string_data = move(comment);
|
2021-07-14 23:32:18 +02:00
|
|
|
}
|
|
|
|
|
2023-01-08 19:23:00 -05:00
|
|
|
DeprecatedFlyString const& tag_name() const
|
2020-05-24 00:14:23 +02:00
|
|
|
{
|
2021-02-23 20:42:32 +01:00
|
|
|
VERIFY(is_start_tag() || is_end_tag());
|
LibWeb: Change HTMLToken storage architecture
This completely changes how HTMLTokens store their data. Previously,
space was allocated for all token types separately. Now, the HTMLToken's
data is stored in just a String, two booleans and a Variant.
This change reduces sizeof(HTMLToken) from 68 to 32. Also, this reduces
raw tokenization time by around 20 to 50 percent, depending on the page.
Full document parsing time (with HTMLDocumentParser, on a local HTML
page without any dependency files) is reduced by between 4 and 20
percent, depending on the page.
Since tokenizing HTML pages can easily generated 50'000 tokens and more,
the storage has been designed in a way that avoids heap allocations
where possible, while trying to reduce the size of the tokens. The only
tokens which need to allocate on the heap are thus DOCTYPE tokens (max.
1 per document), and tag tokens (but only if they have attributes). This
way, only around 5 percent of all tokens generated need to allocate on
the heap (except for StringImpl allocations).
2021-07-15 18:49:39 +02:00
|
|
|
return m_string_data;
|
2020-05-24 00:14:23 +02:00
|
|
|
}
|
|
|
|
|
2022-12-04 18:02:33 +00:00
|
|
|
void set_tag_name(DeprecatedString name)
|
2021-07-14 23:37:48 +02:00
|
|
|
{
|
|
|
|
VERIFY(is_start_tag() || is_end_tag());
|
LibWeb: Change HTMLToken storage architecture
This completely changes how HTMLTokens store their data. Previously,
space was allocated for all token types separately. Now, the HTMLToken's
data is stored in just a String, two booleans and a Variant.
This change reduces sizeof(HTMLToken) from 68 to 32. Also, this reduces
raw tokenization time by around 20 to 50 percent, depending on the page.
Full document parsing time (with HTMLDocumentParser, on a local HTML
page without any dependency files) is reduced by between 4 and 20
percent, depending on the page.
Since tokenizing HTML pages can easily generated 50'000 tokens and more,
the storage has been designed in a way that avoids heap allocations
where possible, while trying to reduce the size of the tokens. The only
tokens which need to allocate on the heap are thus DOCTYPE tokens (max.
1 per document), and tag tokens (but only if they have attributes). This
way, only around 5 percent of all tokens generated need to allocate on
the heap (except for StringImpl allocations).
2021-07-15 18:49:39 +02:00
|
|
|
m_string_data = move(name);
|
2021-07-14 23:37:48 +02:00
|
|
|
}
|
|
|
|
|
2020-05-24 00:14:23 +02:00
|
|
|
bool is_self_closing() const
|
|
|
|
{
|
2021-02-23 20:42:32 +01:00
|
|
|
VERIFY(is_start_tag() || is_end_tag());
|
LibWeb: Change HTMLToken storage architecture
This completely changes how HTMLTokens store their data. Previously,
space was allocated for all token types separately. Now, the HTMLToken's
data is stored in just a String, two booleans and a Variant.
This change reduces sizeof(HTMLToken) from 68 to 32. Also, this reduces
raw tokenization time by around 20 to 50 percent, depending on the page.
Full document parsing time (with HTMLDocumentParser, on a local HTML
page without any dependency files) is reduced by between 4 and 20
percent, depending on the page.
Since tokenizing HTML pages can easily generated 50'000 tokens and more,
the storage has been designed in a way that avoids heap allocations
where possible, while trying to reduce the size of the tokens. The only
tokens which need to allocate on the heap are thus DOCTYPE tokens (max.
1 per document), and tag tokens (but only if they have attributes). This
way, only around 5 percent of all tokens generated need to allocate on
the heap (except for StringImpl allocations).
2021-07-15 18:49:39 +02:00
|
|
|
return m_tag_self_closing;
|
2020-05-24 00:14:23 +02:00
|
|
|
}
|
|
|
|
|
2021-07-14 23:37:48 +02:00
|
|
|
void set_self_closing(bool self_closing)
|
|
|
|
{
|
|
|
|
VERIFY(is_start_tag() || is_end_tag());
|
LibWeb: Change HTMLToken storage architecture
This completely changes how HTMLTokens store their data. Previously,
space was allocated for all token types separately. Now, the HTMLToken's
data is stored in just a String, two booleans and a Variant.
This change reduces sizeof(HTMLToken) from 68 to 32. Also, this reduces
raw tokenization time by around 20 to 50 percent, depending on the page.
Full document parsing time (with HTMLDocumentParser, on a local HTML
page without any dependency files) is reduced by between 4 and 20
percent, depending on the page.
Since tokenizing HTML pages can easily generated 50'000 tokens and more,
the storage has been designed in a way that avoids heap allocations
where possible, while trying to reduce the size of the tokens. The only
tokens which need to allocate on the heap are thus DOCTYPE tokens (max.
1 per document), and tag tokens (but only if they have attributes). This
way, only around 5 percent of all tokens generated need to allocate on
the heap (except for StringImpl allocations).
2021-07-15 18:49:39 +02:00
|
|
|
m_tag_self_closing = self_closing;
|
2021-07-14 23:37:48 +02:00
|
|
|
}
|
|
|
|
|
2020-05-25 20:16:48 +02:00
|
|
|
bool has_acknowledged_self_closing_flag() const
|
|
|
|
{
|
2021-02-23 20:42:32 +01:00
|
|
|
VERIFY(is_self_closing());
|
LibWeb: Change HTMLToken storage architecture
This completely changes how HTMLTokens store their data. Previously,
space was allocated for all token types separately. Now, the HTMLToken's
data is stored in just a String, two booleans and a Variant.
This change reduces sizeof(HTMLToken) from 68 to 32. Also, this reduces
raw tokenization time by around 20 to 50 percent, depending on the page.
Full document parsing time (with HTMLDocumentParser, on a local HTML
page without any dependency files) is reduced by between 4 and 20
percent, depending on the page.
Since tokenizing HTML pages can easily generated 50'000 tokens and more,
the storage has been designed in a way that avoids heap allocations
where possible, while trying to reduce the size of the tokens. The only
tokens which need to allocate on the heap are thus DOCTYPE tokens (max.
1 per document), and tag tokens (but only if they have attributes). This
way, only around 5 percent of all tokens generated need to allocate on
the heap (except for StringImpl allocations).
2021-07-15 18:49:39 +02:00
|
|
|
return m_tag_self_closing_acknowledged;
|
2020-05-25 20:16:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void acknowledge_self_closing_flag_if_set()
|
|
|
|
{
|
|
|
|
if (is_self_closing())
|
LibWeb: Change HTMLToken storage architecture
This completely changes how HTMLTokens store their data. Previously,
space was allocated for all token types separately. Now, the HTMLToken's
data is stored in just a String, two booleans and a Variant.
This change reduces sizeof(HTMLToken) from 68 to 32. Also, this reduces
raw tokenization time by around 20 to 50 percent, depending on the page.
Full document parsing time (with HTMLDocumentParser, on a local HTML
page without any dependency files) is reduced by between 4 and 20
percent, depending on the page.
Since tokenizing HTML pages can easily generated 50'000 tokens and more,
the storage has been designed in a way that avoids heap allocations
where possible, while trying to reduce the size of the tokens. The only
tokens which need to allocate on the heap are thus DOCTYPE tokens (max.
1 per document), and tag tokens (but only if they have attributes). This
way, only around 5 percent of all tokens generated need to allocate on
the heap (except for StringImpl allocations).
2021-07-15 18:49:39 +02:00
|
|
|
m_tag_self_closing_acknowledged = true;
|
2020-05-25 20:16:48 +02:00
|
|
|
}
|
|
|
|
|
2021-07-14 23:53:11 +02:00
|
|
|
bool has_attributes() const
|
|
|
|
{
|
|
|
|
VERIFY(is_start_tag() || is_end_tag());
|
LibWeb: Change HTMLToken storage architecture
This completely changes how HTMLTokens store their data. Previously,
space was allocated for all token types separately. Now, the HTMLToken's
data is stored in just a String, two booleans and a Variant.
This change reduces sizeof(HTMLToken) from 68 to 32. Also, this reduces
raw tokenization time by around 20 to 50 percent, depending on the page.
Full document parsing time (with HTMLDocumentParser, on a local HTML
page without any dependency files) is reduced by between 4 and 20
percent, depending on the page.
Since tokenizing HTML pages can easily generated 50'000 tokens and more,
the storage has been designed in a way that avoids heap allocations
where possible, while trying to reduce the size of the tokens. The only
tokens which need to allocate on the heap are thus DOCTYPE tokens (max.
1 per document), and tag tokens (but only if they have attributes). This
way, only around 5 percent of all tokens generated need to allocate on
the heap (except for StringImpl allocations).
2021-07-15 18:49:39 +02:00
|
|
|
auto* ptr = tag_attributes();
|
|
|
|
return ptr && !ptr->is_empty();
|
2021-07-14 23:53:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t attribute_count() const
|
|
|
|
{
|
|
|
|
VERIFY(is_start_tag() || is_end_tag());
|
LibWeb: Change HTMLToken storage architecture
This completely changes how HTMLTokens store their data. Previously,
space was allocated for all token types separately. Now, the HTMLToken's
data is stored in just a String, two booleans and a Variant.
This change reduces sizeof(HTMLToken) from 68 to 32. Also, this reduces
raw tokenization time by around 20 to 50 percent, depending on the page.
Full document parsing time (with HTMLDocumentParser, on a local HTML
page without any dependency files) is reduced by between 4 and 20
percent, depending on the page.
Since tokenizing HTML pages can easily generated 50'000 tokens and more,
the storage has been designed in a way that avoids heap allocations
where possible, while trying to reduce the size of the tokens. The only
tokens which need to allocate on the heap are thus DOCTYPE tokens (max.
1 per document), and tag tokens (but only if they have attributes). This
way, only around 5 percent of all tokens generated need to allocate on
the heap (except for StringImpl allocations).
2021-07-15 18:49:39 +02:00
|
|
|
if (auto* ptr = tag_attributes())
|
|
|
|
return ptr->size();
|
|
|
|
return 0;
|
2021-07-14 23:53:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void add_attribute(Attribute attribute)
|
|
|
|
{
|
|
|
|
VERIFY(is_start_tag() || is_end_tag());
|
LibWeb: Change HTMLToken storage architecture
This completely changes how HTMLTokens store their data. Previously,
space was allocated for all token types separately. Now, the HTMLToken's
data is stored in just a String, two booleans and a Variant.
This change reduces sizeof(HTMLToken) from 68 to 32. Also, this reduces
raw tokenization time by around 20 to 50 percent, depending on the page.
Full document parsing time (with HTMLDocumentParser, on a local HTML
page without any dependency files) is reduced by between 4 and 20
percent, depending on the page.
Since tokenizing HTML pages can easily generated 50'000 tokens and more,
the storage has been designed in a way that avoids heap allocations
where possible, while trying to reduce the size of the tokens. The only
tokens which need to allocate on the heap are thus DOCTYPE tokens (max.
1 per document), and tag tokens (but only if they have attributes). This
way, only around 5 percent of all tokens generated need to allocate on
the heap (except for StringImpl allocations).
2021-07-15 18:49:39 +02:00
|
|
|
ensure_tag_attributes().append(move(attribute));
|
2021-07-14 23:53:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Attribute const& last_attribute() const
|
|
|
|
{
|
|
|
|
VERIFY(is_start_tag() || is_end_tag());
|
LibWeb: Change HTMLToken storage architecture
This completely changes how HTMLTokens store their data. Previously,
space was allocated for all token types separately. Now, the HTMLToken's
data is stored in just a String, two booleans and a Variant.
This change reduces sizeof(HTMLToken) from 68 to 32. Also, this reduces
raw tokenization time by around 20 to 50 percent, depending on the page.
Full document parsing time (with HTMLDocumentParser, on a local HTML
page without any dependency files) is reduced by between 4 and 20
percent, depending on the page.
Since tokenizing HTML pages can easily generated 50'000 tokens and more,
the storage has been designed in a way that avoids heap allocations
where possible, while trying to reduce the size of the tokens. The only
tokens which need to allocate on the heap are thus DOCTYPE tokens (max.
1 per document), and tag tokens (but only if they have attributes). This
way, only around 5 percent of all tokens generated need to allocate on
the heap (except for StringImpl allocations).
2021-07-15 18:49:39 +02:00
|
|
|
VERIFY(has_attributes());
|
|
|
|
return tag_attributes()->last();
|
2021-07-14 23:53:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Attribute& last_attribute()
|
|
|
|
{
|
|
|
|
VERIFY(is_start_tag() || is_end_tag());
|
LibWeb: Change HTMLToken storage architecture
This completely changes how HTMLTokens store their data. Previously,
space was allocated for all token types separately. Now, the HTMLToken's
data is stored in just a String, two booleans and a Variant.
This change reduces sizeof(HTMLToken) from 68 to 32. Also, this reduces
raw tokenization time by around 20 to 50 percent, depending on the page.
Full document parsing time (with HTMLDocumentParser, on a local HTML
page without any dependency files) is reduced by between 4 and 20
percent, depending on the page.
Since tokenizing HTML pages can easily generated 50'000 tokens and more,
the storage has been designed in a way that avoids heap allocations
where possible, while trying to reduce the size of the tokens. The only
tokens which need to allocate on the heap are thus DOCTYPE tokens (max.
1 per document), and tag tokens (but only if they have attributes). This
way, only around 5 percent of all tokens generated need to allocate on
the heap (except for StringImpl allocations).
2021-07-15 18:49:39 +02:00
|
|
|
VERIFY(has_attributes());
|
|
|
|
return tag_attributes()->last();
|
2021-07-14 23:53:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void drop_attributes()
|
|
|
|
{
|
|
|
|
VERIFY(is_start_tag() || is_end_tag());
|
LibWeb: Change HTMLToken storage architecture
This completely changes how HTMLTokens store their data. Previously,
space was allocated for all token types separately. Now, the HTMLToken's
data is stored in just a String, two booleans and a Variant.
This change reduces sizeof(HTMLToken) from 68 to 32. Also, this reduces
raw tokenization time by around 20 to 50 percent, depending on the page.
Full document parsing time (with HTMLDocumentParser, on a local HTML
page without any dependency files) is reduced by between 4 and 20
percent, depending on the page.
Since tokenizing HTML pages can easily generated 50'000 tokens and more,
the storage has been designed in a way that avoids heap allocations
where possible, while trying to reduce the size of the tokens. The only
tokens which need to allocate on the heap are thus DOCTYPE tokens (max.
1 per document), and tag tokens (but only if they have attributes). This
way, only around 5 percent of all tokens generated need to allocate on
the heap (except for StringImpl allocations).
2021-07-15 18:49:39 +02:00
|
|
|
m_data.get<OwnPtr<Vector<Attribute>>>().clear();
|
2021-07-14 23:53:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void for_each_attribute(Function<IterationDecision(Attribute const&)> callback) const
|
|
|
|
{
|
|
|
|
VERIFY(is_start_tag() || is_end_tag());
|
LibWeb: Change HTMLToken storage architecture
This completely changes how HTMLTokens store their data. Previously,
space was allocated for all token types separately. Now, the HTMLToken's
data is stored in just a String, two booleans and a Variant.
This change reduces sizeof(HTMLToken) from 68 to 32. Also, this reduces
raw tokenization time by around 20 to 50 percent, depending on the page.
Full document parsing time (with HTMLDocumentParser, on a local HTML
page without any dependency files) is reduced by between 4 and 20
percent, depending on the page.
Since tokenizing HTML pages can easily generated 50'000 tokens and more,
the storage has been designed in a way that avoids heap allocations
where possible, while trying to reduce the size of the tokens. The only
tokens which need to allocate on the heap are thus DOCTYPE tokens (max.
1 per document), and tag tokens (but only if they have attributes). This
way, only around 5 percent of all tokens generated need to allocate on
the heap (except for StringImpl allocations).
2021-07-15 18:49:39 +02:00
|
|
|
auto* ptr = tag_attributes();
|
|
|
|
if (!ptr)
|
|
|
|
return;
|
|
|
|
for (auto& attribute : *ptr) {
|
2021-07-14 23:53:11 +02:00
|
|
|
if (callback(attribute) == IterationDecision::Break)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void for_each_attribute(Function<IterationDecision(Attribute&)> callback)
|
|
|
|
{
|
|
|
|
VERIFY(is_start_tag() || is_end_tag());
|
LibWeb: Change HTMLToken storage architecture
This completely changes how HTMLTokens store their data. Previously,
space was allocated for all token types separately. Now, the HTMLToken's
data is stored in just a String, two booleans and a Variant.
This change reduces sizeof(HTMLToken) from 68 to 32. Also, this reduces
raw tokenization time by around 20 to 50 percent, depending on the page.
Full document parsing time (with HTMLDocumentParser, on a local HTML
page without any dependency files) is reduced by between 4 and 20
percent, depending on the page.
Since tokenizing HTML pages can easily generated 50'000 tokens and more,
the storage has been designed in a way that avoids heap allocations
where possible, while trying to reduce the size of the tokens. The only
tokens which need to allocate on the heap are thus DOCTYPE tokens (max.
1 per document), and tag tokens (but only if they have attributes). This
way, only around 5 percent of all tokens generated need to allocate on
the heap (except for StringImpl allocations).
2021-07-15 18:49:39 +02:00
|
|
|
auto* ptr = tag_attributes();
|
|
|
|
if (!ptr)
|
|
|
|
return;
|
|
|
|
for (auto& attribute : *ptr) {
|
2021-07-14 23:53:11 +02:00
|
|
|
if (callback(attribute) == IterationDecision::Break)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-29 23:46:18 +01:00
|
|
|
StringView attribute(DeprecatedFlyString const& attribute_name) const
|
2020-05-28 12:18:46 +02:00
|
|
|
{
|
2021-02-23 20:42:32 +01:00
|
|
|
VERIFY(is_start_tag() || is_end_tag());
|
LibWeb: Change HTMLToken storage architecture
This completely changes how HTMLTokens store their data. Previously,
space was allocated for all token types separately. Now, the HTMLToken's
data is stored in just a String, two booleans and a Variant.
This change reduces sizeof(HTMLToken) from 68 to 32. Also, this reduces
raw tokenization time by around 20 to 50 percent, depending on the page.
Full document parsing time (with HTMLDocumentParser, on a local HTML
page without any dependency files) is reduced by between 4 and 20
percent, depending on the page.
Since tokenizing HTML pages can easily generated 50'000 tokens and more,
the storage has been designed in a way that avoids heap allocations
where possible, while trying to reduce the size of the tokens. The only
tokens which need to allocate on the heap are thus DOCTYPE tokens (max.
1 per document), and tag tokens (but only if they have attributes). This
way, only around 5 percent of all tokens generated need to allocate on
the heap (except for StringImpl allocations).
2021-07-15 18:49:39 +02:00
|
|
|
|
|
|
|
auto* ptr = tag_attributes();
|
|
|
|
if (!ptr)
|
|
|
|
return {};
|
|
|
|
for (auto& attribute : *ptr) {
|
2021-05-23 08:50:48 +02:00
|
|
|
if (attribute_name == attribute.local_name)
|
|
|
|
return attribute.value;
|
2020-05-28 12:18:46 +02:00
|
|
|
}
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2023-01-08 19:23:00 -05:00
|
|
|
bool has_attribute(DeprecatedFlyString const& attribute_name)
|
2020-10-12 01:51:28 +01:00
|
|
|
{
|
|
|
|
return !attribute(attribute_name).is_null();
|
|
|
|
}
|
|
|
|
|
2023-01-08 19:23:00 -05:00
|
|
|
void adjust_tag_name(DeprecatedFlyString const& old_name, DeprecatedFlyString const& new_name)
|
2020-10-12 01:51:28 +01:00
|
|
|
{
|
2021-02-23 20:42:32 +01:00
|
|
|
VERIFY(is_start_tag() || is_end_tag());
|
2021-07-14 23:37:48 +02:00
|
|
|
if (old_name == tag_name())
|
|
|
|
set_tag_name(new_name);
|
2020-10-12 01:51:28 +01:00
|
|
|
}
|
|
|
|
|
2023-01-08 19:23:00 -05:00
|
|
|
void adjust_attribute_name(DeprecatedFlyString const& old_name, DeprecatedFlyString const& new_name)
|
2020-06-21 06:58:03 +02:00
|
|
|
{
|
2021-02-23 20:42:32 +01:00
|
|
|
VERIFY(is_start_tag() || is_end_tag());
|
2021-07-14 23:53:11 +02:00
|
|
|
for_each_attribute([&](Attribute& attribute) {
|
|
|
|
if (old_name == attribute.local_name)
|
2021-05-23 08:50:48 +02:00
|
|
|
attribute.local_name = new_name;
|
2021-07-14 23:53:11 +02:00
|
|
|
return IterationDecision::Continue;
|
|
|
|
});
|
2020-06-21 06:58:03 +02:00
|
|
|
}
|
|
|
|
|
2023-01-08 19:23:00 -05:00
|
|
|
void adjust_foreign_attribute(DeprecatedFlyString const& old_name, DeprecatedFlyString const& prefix, DeprecatedFlyString const& local_name, DeprecatedFlyString const& namespace_)
|
2020-06-21 06:58:03 +02:00
|
|
|
{
|
2021-02-23 20:42:32 +01:00
|
|
|
VERIFY(is_start_tag() || is_end_tag());
|
2021-07-14 23:53:11 +02:00
|
|
|
for_each_attribute([&](Attribute& attribute) {
|
2021-05-23 08:50:48 +02:00
|
|
|
if (old_name == attribute.local_name) {
|
2021-05-23 08:08:39 +02:00
|
|
|
attribute.prefix = prefix;
|
2021-05-23 08:50:48 +02:00
|
|
|
attribute.local_name = local_name;
|
2021-05-23 08:08:39 +02:00
|
|
|
attribute.namespace_ = namespace_;
|
2020-06-21 06:58:03 +02:00
|
|
|
}
|
2021-07-14 23:53:11 +02:00
|
|
|
return IterationDecision::Continue;
|
|
|
|
});
|
2020-06-03 22:34:50 -06:00
|
|
|
}
|
|
|
|
|
2021-07-15 00:03:50 +02:00
|
|
|
DoctypeData const& doctype_data() const
|
|
|
|
{
|
|
|
|
VERIFY(is_doctype());
|
LibWeb: Change HTMLToken storage architecture
This completely changes how HTMLTokens store their data. Previously,
space was allocated for all token types separately. Now, the HTMLToken's
data is stored in just a String, two booleans and a Variant.
This change reduces sizeof(HTMLToken) from 68 to 32. Also, this reduces
raw tokenization time by around 20 to 50 percent, depending on the page.
Full document parsing time (with HTMLDocumentParser, on a local HTML
page without any dependency files) is reduced by between 4 and 20
percent, depending on the page.
Since tokenizing HTML pages can easily generated 50'000 tokens and more,
the storage has been designed in a way that avoids heap allocations
where possible, while trying to reduce the size of the tokens. The only
tokens which need to allocate on the heap are thus DOCTYPE tokens (max.
1 per document), and tag tokens (but only if they have attributes). This
way, only around 5 percent of all tokens generated need to allocate on
the heap (except for StringImpl allocations).
2021-07-15 18:49:39 +02:00
|
|
|
auto* ptr = m_data.get<OwnPtr<DoctypeData>>().ptr();
|
|
|
|
VERIFY(ptr);
|
|
|
|
return *ptr;
|
2021-07-15 00:03:50 +02:00
|
|
|
}
|
|
|
|
|
2021-07-15 18:58:11 +02:00
|
|
|
DoctypeData& ensure_doctype_data()
|
2021-07-15 00:03:50 +02:00
|
|
|
{
|
|
|
|
VERIFY(is_doctype());
|
LibWeb: Change HTMLToken storage architecture
This completely changes how HTMLTokens store their data. Previously,
space was allocated for all token types separately. Now, the HTMLToken's
data is stored in just a String, two booleans and a Variant.
This change reduces sizeof(HTMLToken) from 68 to 32. Also, this reduces
raw tokenization time by around 20 to 50 percent, depending on the page.
Full document parsing time (with HTMLDocumentParser, on a local HTML
page without any dependency files) is reduced by between 4 and 20
percent, depending on the page.
Since tokenizing HTML pages can easily generated 50'000 tokens and more,
the storage has been designed in a way that avoids heap allocations
where possible, while trying to reduce the size of the tokens. The only
tokens which need to allocate on the heap are thus DOCTYPE tokens (max.
1 per document), and tag tokens (but only if they have attributes). This
way, only around 5 percent of all tokens generated need to allocate on
the heap (except for StringImpl allocations).
2021-07-15 18:49:39 +02:00
|
|
|
auto& ptr = m_data.get<OwnPtr<DoctypeData>>();
|
|
|
|
if (!ptr)
|
|
|
|
ptr = make<DoctypeData>();
|
|
|
|
return *ptr;
|
2021-07-15 00:03:50 +02:00
|
|
|
}
|
|
|
|
|
2020-05-22 21:46:13 +02:00
|
|
|
Type type() const { return m_type; }
|
|
|
|
|
2022-12-06 01:12:49 +00:00
|
|
|
DeprecatedString to_deprecated_string() const;
|
2020-05-24 00:14:23 +02:00
|
|
|
|
2021-07-14 23:15:40 +02:00
|
|
|
Position const& start_position() const { return m_start_position; }
|
|
|
|
Position const& end_position() const { return m_end_position; }
|
2021-05-20 23:11:41 +04:30
|
|
|
|
2021-07-15 15:53:54 +02:00
|
|
|
void set_start_position(Badge<HTMLTokenizer>, Position start_position) { m_start_position = start_position; }
|
|
|
|
void set_end_position(Badge<HTMLTokenizer>, Position end_position) { m_end_position = end_position; }
|
|
|
|
|
2020-05-22 21:46:13 +02:00
|
|
|
private:
|
LibWeb: Change HTMLToken storage architecture
This completely changes how HTMLTokens store their data. Previously,
space was allocated for all token types separately. Now, the HTMLToken's
data is stored in just a String, two booleans and a Variant.
This change reduces sizeof(HTMLToken) from 68 to 32. Also, this reduces
raw tokenization time by around 20 to 50 percent, depending on the page.
Full document parsing time (with HTMLDocumentParser, on a local HTML
page without any dependency files) is reduced by between 4 and 20
percent, depending on the page.
Since tokenizing HTML pages can easily generated 50'000 tokens and more,
the storage has been designed in a way that avoids heap allocations
where possible, while trying to reduce the size of the tokens. The only
tokens which need to allocate on the heap are thus DOCTYPE tokens (max.
1 per document), and tag tokens (but only if they have attributes). This
way, only around 5 percent of all tokens generated need to allocate on
the heap (except for StringImpl allocations).
2021-07-15 18:49:39 +02:00
|
|
|
Vector<Attribute> const* tag_attributes() const
|
|
|
|
{
|
|
|
|
return m_data.get<OwnPtr<Vector<Attribute>>>().ptr();
|
|
|
|
}
|
|
|
|
|
|
|
|
Vector<Attribute>* tag_attributes()
|
|
|
|
{
|
|
|
|
return m_data.get<OwnPtr<Vector<Attribute>>>().ptr();
|
|
|
|
}
|
|
|
|
|
|
|
|
Vector<Attribute>& ensure_tag_attributes()
|
|
|
|
{
|
|
|
|
VERIFY(is_start_tag() || is_end_tag());
|
|
|
|
auto& ptr = m_data.get<OwnPtr<Vector<Attribute>>>();
|
|
|
|
if (!ptr)
|
|
|
|
ptr = make<Vector<Attribute>>();
|
|
|
|
return *ptr;
|
|
|
|
}
|
|
|
|
|
2020-05-24 20:24:43 +02:00
|
|
|
Type m_type { Type::Invalid };
|
2020-05-22 21:46:13 +02:00
|
|
|
|
LibWeb: Change HTMLToken storage architecture
This completely changes how HTMLTokens store their data. Previously,
space was allocated for all token types separately. Now, the HTMLToken's
data is stored in just a String, two booleans and a Variant.
This change reduces sizeof(HTMLToken) from 68 to 32. Also, this reduces
raw tokenization time by around 20 to 50 percent, depending on the page.
Full document parsing time (with HTMLDocumentParser, on a local HTML
page without any dependency files) is reduced by between 4 and 20
percent, depending on the page.
Since tokenizing HTML pages can easily generated 50'000 tokens and more,
the storage has been designed in a way that avoids heap allocations
where possible, while trying to reduce the size of the tokens. The only
tokens which need to allocate on the heap are thus DOCTYPE tokens (max.
1 per document), and tag tokens (but only if they have attributes). This
way, only around 5 percent of all tokens generated need to allocate on
the heap (except for StringImpl allocations).
2021-07-15 18:49:39 +02:00
|
|
|
// Type::StartTag and Type::EndTag
|
|
|
|
bool m_tag_self_closing { false };
|
|
|
|
bool m_tag_self_closing_acknowledged { false };
|
|
|
|
|
|
|
|
// Type::Comment (comment data), Type::StartTag and Type::EndTag (tag name)
|
2023-01-08 19:23:00 -05:00
|
|
|
DeprecatedFlyString m_string_data;
|
LibWeb: Change HTMLToken storage architecture
This completely changes how HTMLTokens store their data. Previously,
space was allocated for all token types separately. Now, the HTMLToken's
data is stored in just a String, two booleans and a Variant.
This change reduces sizeof(HTMLToken) from 68 to 32. Also, this reduces
raw tokenization time by around 20 to 50 percent, depending on the page.
Full document parsing time (with HTMLDocumentParser, on a local HTML
page without any dependency files) is reduced by between 4 and 20
percent, depending on the page.
Since tokenizing HTML pages can easily generated 50'000 tokens and more,
the storage has been designed in a way that avoids heap allocations
where possible, while trying to reduce the size of the tokens. The only
tokens which need to allocate on the heap are thus DOCTYPE tokens (max.
1 per document), and tag tokens (but only if they have attributes). This
way, only around 5 percent of all tokens generated need to allocate on
the heap (except for StringImpl allocations).
2021-07-15 18:49:39 +02:00
|
|
|
|
2021-09-19 23:00:45 +02:00
|
|
|
Variant<Empty, u32, OwnPtr<DoctypeData>, OwnPtr<Vector<Attribute>>> m_data {};
|
2021-05-20 23:11:41 +04:30
|
|
|
|
|
|
|
Position m_start_position;
|
|
|
|
Position m_end_position;
|
2020-05-22 21:46:13 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|