diff --git a/Kernel/PerformanceEventBuffer.cpp b/Kernel/PerformanceEventBuffer.cpp index 8ef21aad18..98c155fd58 100644 --- a/Kernel/PerformanceEventBuffer.cpp +++ b/Kernel/PerformanceEventBuffer.cpp @@ -199,8 +199,17 @@ ErrorOr PerformanceEventBuffer::to_json_impl(Serializer& object) const { { auto strings = TRY(object.add_array("strings")); - for (auto const& it : m_strings) - TRY(strings.add(it->view())); + Vector strings_sorted_by_index; + TRY(strings_sorted_by_index.try_resize(m_strings.size())); + + for (auto& entry : m_strings) { + strings_sorted_by_index[entry.value] = const_cast(entry.key.ptr()); + } + + for (size_t i = 0; i < m_strings.size(); i++) { + TRY(strings.add(strings_sorted_by_index[i]->view())); + } + TRY(strings.finish()); } @@ -361,9 +370,14 @@ ErrorOr PerformanceEventBuffer::add_process(const Process& process, Proces ErrorOr PerformanceEventBuffer::register_string(NonnullOwnPtr string) { - FlatPtr string_id = m_strings.size(); - TRY(m_strings.try_set(move(string))); - return string_id; + auto it = m_strings.find(string); + if (it != m_strings.end()) { + return it->value; + } + + auto new_index = m_strings.size(); + TRY(m_strings.try_set(move(string), move(new_index))); + return new_index; } } diff --git a/Kernel/PerformanceEventBuffer.h b/Kernel/PerformanceEventBuffer.h index 41d36a16cf..7570dd136b 100644 --- a/Kernel/PerformanceEventBuffer.h +++ b/Kernel/PerformanceEventBuffer.h @@ -145,7 +145,7 @@ private: size_t m_count { 0 }; NonnullOwnPtr m_buffer; - HashTable> m_strings; + HashMap, size_t> m_strings; }; extern bool g_profiling_all_threads;