diff options
author | Brian Gianforcaro <bgianf@serenityos.org> | 2021-09-17 02:12:29 -0700 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2021-09-18 21:05:13 +0200 |
commit | 952441943fd2600bc666a3b5455e7b04d87484cd (patch) | |
tree | b7055b80c12a5099430dd5de3afcf5cd294c410f /Userland/Libraries/LibDebug/Dwarf | |
parent | 19c492e976ca51f935dde865d1555e4c378ddf96 (diff) | |
download | serenity-952441943fd2600bc666a3b5455e7b04d87484cd.zip |
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.
Diffstat (limited to 'Userland/Libraries/LibDebug/Dwarf')
-rw-r--r-- | Userland/Libraries/LibDebug/Dwarf/DIE.cpp | 28 | ||||
-rw-r--r-- | Userland/Libraries/LibDebug/Dwarf/DIE.h | 1 |
2 files changed, 17 insertions, 12 deletions
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<u32> parent_offset) : m_compilation_unit(unit) - , m_offset(offset) { + rehydrate_from(offset, parent_offset); +} + +void DIE::rehydrate_from(u32 offset, Optional<u32> 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<void(DIE const& child)> callback) const if (!m_has_children) return; - NonnullOwnPtr<DIE> current_child = make<DIE>(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<DIE>(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<DIE>(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<u32> parent_offset() const { return m_parent_offset; } private: + void rehydrate_from(u32 offset, Optional<u32> parent_offset); CompilationUnit const& m_compilation_unit; u32 m_offset { 0 }; u32 m_data_offset { 0 }; |