mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 15:27:35 +00:00
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.
This commit is contained in:
parent
19c492e976
commit
952441943f
2 changed files with 17 additions and 12 deletions
|
@ -14,8 +14,14 @@ namespace Debug::Dwarf {
|
||||||
|
|
||||||
DIE::DIE(CompilationUnit const& unit, u32 offset, Optional<u32> parent_offset)
|
DIE::DIE(CompilationUnit const& unit, u32 offset, Optional<u32> parent_offset)
|
||||||
: m_compilation_unit(unit)
|
: 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());
|
InputMemoryStream stream(m_compilation_unit.dwarf_info().debug_info_data());
|
||||||
stream.discard_or_error(m_offset);
|
stream.discard_or_error(m_offset);
|
||||||
stream.read_LEB128_unsigned(m_abbreviation_code);
|
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)
|
if (!m_has_children)
|
||||||
return;
|
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) {
|
while (true) {
|
||||||
callback(*current_child);
|
callback(current_child);
|
||||||
if (current_child->is_null())
|
if (current_child.is_null())
|
||||||
break;
|
break;
|
||||||
if (!current_child->has_children()) {
|
if (!current_child.has_children()) {
|
||||||
current_child = make<DIE>(m_compilation_unit, current_child->offset() + current_child->size(), m_offset);
|
current_child.rehydrate_from(current_child.offset() + current_child.size(), m_offset);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
auto sibling = current_child->get_attribute(Attribute::Sibling);
|
auto sibling = current_child.get_attribute(Attribute::Sibling);
|
||||||
u32 sibling_offset = 0;
|
u32 sibling_offset = 0;
|
||||||
if (sibling.has_value()) {
|
if (sibling.has_value()) {
|
||||||
sibling_offset = sibling.value().data.as_unsigned;
|
sibling_offset = sibling.value().data.as_unsigned;
|
||||||
}
|
} else {
|
||||||
|
|
||||||
if (!sibling.has_value()) {
|
|
||||||
// NOTE: According to the spec, the compiler doesn't have to supply the sibling information.
|
// 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
|
// 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();
|
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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -36,6 +36,7 @@ public:
|
||||||
Optional<u32> parent_offset() const { return m_parent_offset; }
|
Optional<u32> parent_offset() const { return m_parent_offset; }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
void rehydrate_from(u32 offset, Optional<u32> parent_offset);
|
||||||
CompilationUnit const& m_compilation_unit;
|
CompilationUnit const& m_compilation_unit;
|
||||||
u32 m_offset { 0 };
|
u32 m_offset { 0 };
|
||||||
u32 m_data_offset { 0 };
|
u32 m_data_offset { 0 };
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue