mirror of
https://github.com/RGBCube/serenity
synced 2025-05-31 07:08:10 +00:00
LibWeb: Implement value_id_from_string by hash lookup
The old approach was basically a linear scan, which is slower than a hash map for the currently 303 elements, as evidenced by the new benchmark in TestCSSIDSpeed. Before: Completed benchmark 'value_id_from_string' in 3238ms After: Completed benchmark 'value_id_from_string' in 193ms
This commit is contained in:
parent
b3d2fdceb1
commit
23d111a459
1 changed files with 9 additions and 5 deletions
|
@ -85,12 +85,12 @@ ErrorOr<void> generate_implementation_file(JsonArray& identifier_data, Core::Str
|
|||
|
||||
generator.append(R"~~~(
|
||||
#include <AK/Assertions.h>
|
||||
#include <AK/HashMap.h>
|
||||
#include <LibWeb/CSS/ValueID.h>
|
||||
|
||||
namespace Web::CSS {
|
||||
|
||||
ValueID value_id_from_string(StringView string)
|
||||
{
|
||||
HashMap<StringView, ValueID, AK::CaseInsensitiveStringViewTraits> g_stringview_to_value_id_map {
|
||||
)~~~");
|
||||
|
||||
identifier_data.for_each([&](auto& name) {
|
||||
|
@ -98,13 +98,17 @@ ValueID value_id_from_string(StringView string)
|
|||
member_generator.set("name", name.to_deprecated_string());
|
||||
member_generator.set("name:titlecase", title_casify(name.to_deprecated_string()));
|
||||
member_generator.append(R"~~~(
|
||||
if (string.equals_ignoring_case("@name@"sv))
|
||||
return ValueID::@name:titlecase@;
|
||||
{"@name@"sv, ValueID::@name:titlecase@},
|
||||
)~~~");
|
||||
});
|
||||
|
||||
generator.append(R"~~~(
|
||||
return ValueID::Invalid;
|
||||
};
|
||||
|
||||
ValueID value_id_from_string(StringView string)
|
||||
{
|
||||
auto maybe_value_id = g_stringview_to_value_id_map.get(string);
|
||||
return maybe_value_id.value_or(ValueID::Invalid);
|
||||
}
|
||||
|
||||
StringView string_from_value_id(ValueID value_id) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue