From 76af9fae63f56cb6309e5f2929869ed9a8cff239 Mon Sep 17 00:00:00 2001 From: Timothy Flynn Date: Fri, 10 Dec 2021 15:53:20 -0500 Subject: 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. --- .../CodeGenerators/LibUnicode/GeneratorUtil.h | 63 +++++++++++++++++++++- 1 file changed, 62 insertions(+), 1 deletion(-) 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 #include +template +inline constexpr bool StorageTypeIsList = false; + +template +inline constexpr bool StorageTypeIsList> = true; + template 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) { 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) + { + 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, @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 m_storage; HashMap m_storage_indices; + // clang-format on }; template -- cgit v1.2.3