diff options
author | Timothy Flynn <trflynn89@pm.me> | 2021-12-10 15:53:20 -0500 |
---|---|---|
committer | Linus Groh <mail@linusgroh.de> | 2021-12-11 14:17:47 +0000 |
commit | 76af9fae63f56cb6309e5f2929869ed9a8cff239 (patch) | |
tree | b8411dc42c6e5df71d2e64cef3f6e81e43835138 /Meta | |
parent | 8a4b9c092665508858f48d6c7b584ce4e21ef894 (diff) | |
download | serenity-76af9fae63f56cb6309e5f2929869ed9a8cff239.zip |
LibUnicode: Support storing lists in UniqueStorage for code generators
The evolution of UniqueStorage has been as follows:
1. It was created as UniqueStringStorage to ensure only one copy of each
unique string is generated. Interested parties stored an index into
a unique string list, rather than the string itself.
Commits: f9e605397c and 04e6b43f05
2. It became apparent that non-string structures could also be de-
duplicated to reduce the size of libunicode.so. UniqueStringStorage
was generalized to UniqueStorage for this purpose.
Commit: d8e6beb14f
It's now also apparent that there's heavy duplication of lists of
structures. For example, the NumberFormat generator stores 4 lists of
NumberFormat objects. In total, we currently generate nearly 2,000 lists
of these objects, of which 275 are unique.
This change updates UniqueStorage to support storing lists. The only
change is how the storage is generated - we generate each stored list
individually, then an array storing spans of those lists.
Diffstat (limited to 'Meta')
-rw-r--r-- | Meta/Lagom/Tools/CodeGenerators/LibUnicode/GeneratorUtil.h | 63 |
1 files changed, 62 insertions, 1 deletions
diff --git a/Meta/Lagom/Tools/CodeGenerators/LibUnicode/GeneratorUtil.h b/Meta/Lagom/Tools/CodeGenerators/LibUnicode/GeneratorUtil.h index ec4fe94a90..aff59fe3f1 100644 --- a/Meta/Lagom/Tools/CodeGenerators/LibUnicode/GeneratorUtil.h +++ b/Meta/Lagom/Tools/CodeGenerators/LibUnicode/GeneratorUtil.h @@ -19,6 +19,12 @@ #include <LibCore/File.h> #include <LibUnicode/Locale.h> +template<class T> +inline constexpr bool StorageTypeIsList = false; + +template<class T> +inline constexpr bool StorageTypeIsList<Vector<T>> = true; + template<typename StorageType, typename IndexType> class UniqueStorage { public: @@ -57,7 +63,7 @@ public: return m_storage.at(index - 1); } - void generate(SourceGenerator& generator, StringView type, StringView name, size_t max_values_per_row) + void generate(SourceGenerator& generator, StringView type, StringView name, size_t max_values_per_row) requires(!StorageTypeIsList<StorageType>) { generator.set("type"sv, type); generator.set("name"sv, name); @@ -89,9 +95,64 @@ static constexpr Array<@type@, @size@ + 1> @name@ { { )~~~"); } + void generate(SourceGenerator& generator, StringView type, StringView name) requires(StorageTypeIsList<StorageType>) + { + generator.set("type"sv, type); + generator.set("name"sv, name); + + for (size_t i = 0; i < m_storage.size(); ++i) { + auto const& list = m_storage[i]; + + generator.set("index"sv, String::number(i)); + generator.set("size"sv, String::number(list.size())); + + generator.append(R"~~~( +static constexpr Array<@type@, @size@> @name@@index@ { {)~~~"); + + bool first = true; + for (auto const& value : list) { + generator.append(first ? " " : ", "); + generator.append(String::formatted("{}", value)); + first = false; + } + + generator.append(" } };"); + } + + generator.set("size"sv, String::number(m_storage.size())); + + generator.append(R"~~~( + +static constexpr Array<Span<@type@ const>, @size@ + 1> @name@ { { + {})~~~"); + + constexpr size_t max_values_per_row = 10; + size_t values_in_current_row = 1; + + for (size_t i = 0; i < m_storage.size(); ++i) { + if (values_in_current_row++ > 0) + generator.append(", "); + + generator.set("index"sv, String::number(i)); + generator.append("@name@@index@.span()"); + + if (values_in_current_row == max_values_per_row) { + values_in_current_row = 0; + generator.append(",\n "); + } + } + + generator.append(R"~~~( +} }; +)~~~"); + } + + // clang-format off + // clang-format gets confused by the requires() clauses above, and formats this section very weirdly. private: Vector<StorageType> m_storage; HashMap<StorageType, IndexType> m_storage_indices; + // clang-format on }; template<typename StringIndexType> |