From 952441943fd2600bc666a3b5455e7b04d87484cd Mon Sep 17 00:00:00 2001 From: Brian Gianforcaro Date: Fri, 17 Sep 2021 02:12:29 -0700 Subject: LibDebug: Avoid short lived allocations in DIE::for_each_child This algorithm is both iterative and recursive, so allocating on every recursion, or when iterating each child is extremely costly. Instead allow the on stack DIE to be re-initialized so it can be reused. --- Userland/Libraries/LibDebug/Dwarf/DIE.cpp | 28 ++++++++++++++++------------ Userland/Libraries/LibDebug/Dwarf/DIE.h | 1 + 2 files changed, 17 insertions(+), 12 deletions(-) (limited to 'Userland/Libraries/LibDebug/Dwarf') diff --git a/Userland/Libraries/LibDebug/Dwarf/DIE.cpp b/Userland/Libraries/LibDebug/Dwarf/DIE.cpp index 66e15e56ab..1adabbd21c 100644 --- a/Userland/Libraries/LibDebug/Dwarf/DIE.cpp +++ b/Userland/Libraries/LibDebug/Dwarf/DIE.cpp @@ -14,8 +14,14 @@ namespace Debug::Dwarf { DIE::DIE(CompilationUnit const& unit, u32 offset, Optional parent_offset) : m_compilation_unit(unit) - , m_offset(offset) { + rehydrate_from(offset, parent_offset); +} + +void DIE::rehydrate_from(u32 offset, Optional parent_offset) +{ + m_offset = offset; + InputMemoryStream stream(m_compilation_unit.dwarf_info().debug_info_data()); stream.discard_or_error(m_offset); stream.read_LEB128_unsigned(m_abbreviation_code); @@ -62,30 +68,28 @@ void DIE::for_each_child(Function callback) const if (!m_has_children) return; - NonnullOwnPtr current_child = make(m_compilation_unit, m_offset + m_size, m_offset); + auto current_child = DIE(m_compilation_unit, m_offset + m_size, m_offset); while (true) { - callback(*current_child); - if (current_child->is_null()) + callback(current_child); + if (current_child.is_null()) break; - if (!current_child->has_children()) { - current_child = make(m_compilation_unit, current_child->offset() + current_child->size(), m_offset); + if (!current_child.has_children()) { + current_child.rehydrate_from(current_child.offset() + current_child.size(), m_offset); continue; } - auto sibling = current_child->get_attribute(Attribute::Sibling); + auto sibling = current_child.get_attribute(Attribute::Sibling); u32 sibling_offset = 0; if (sibling.has_value()) { sibling_offset = sibling.value().data.as_unsigned; - } - - if (!sibling.has_value()) { + } else { // NOTE: According to the spec, the compiler doesn't have to supply the sibling information. // When it doesn't, we have to recursively iterate the current child's children to find where they end - current_child->for_each_child([&](DIE const& sub_child) { + current_child.for_each_child([&](DIE const& sub_child) { sibling_offset = sub_child.offset() + sub_child.size(); }); } - current_child = make(m_compilation_unit, sibling_offset, m_offset); + current_child.rehydrate_from(sibling_offset, m_offset); } } diff --git a/Userland/Libraries/LibDebug/Dwarf/DIE.h b/Userland/Libraries/LibDebug/Dwarf/DIE.h index 7062b6f3d5..2f0ef61b74 100644 --- a/Userland/Libraries/LibDebug/Dwarf/DIE.h +++ b/Userland/Libraries/LibDebug/Dwarf/DIE.h @@ -36,6 +36,7 @@ public: Optional parent_offset() const { return m_parent_offset; } private: + void rehydrate_from(u32 offset, Optional parent_offset); CompilationUnit const& m_compilation_unit; u32 m_offset { 0 }; u32 m_data_offset { 0 }; -- cgit v1.2.3