1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 19:07:35 +00:00

LibWeb: Maintain a map of child-to-parent nodes in OOPWV DOM Inspector

Currently, each time parent_index() is invoked, two depth-first searches
are incurred to find the node's parent and grandparent. This becomes
particularly expensive, for example, when trying to scroll through a
large <ul> list.

Instead, upon creation, traverse the DOM JSON and create a map of child
nodes to their parent. Then those two lookups become hash map lookups
rather than a DFS traversal.
This commit is contained in:
Timothy Flynn 2021-07-01 12:27:32 -04:00 committed by Andreas Kling
parent 97ea192e3e
commit 447ecc2155
2 changed files with 23 additions and 30 deletions

View file

@ -7,6 +7,7 @@
#pragma once
#include <AK/HashMap.h>
#include <AK/JsonObject.h>
#include <LibGUI/Model.h>
#include <LibWeb/Forward.h>
@ -36,16 +37,11 @@ public:
private:
explicit DOMTreeJSONModel(JsonObject);
ALWAYS_INLINE JsonObject const* find_parent_of_child_with_internal_id(size_t internal_id) const
ALWAYS_INLINE JsonObject const* get_parent(const JsonObject& o) const
{
return find_parent_of_child_with_internal_id(m_dom_tree, internal_id);
}
JsonObject const* find_parent_of_child_with_internal_id(JsonObject const&, size_t) const;
ALWAYS_INLINE static size_t get_internal_id(const JsonObject& o)
{
return o.get("internal_id").as_u32();
auto parent_node = m_dom_node_to_parent_map.get(&o);
VERIFY(parent_node.has_value());
return *parent_node;
}
ALWAYS_INLINE static JsonArray const* get_children(const JsonObject& o)
@ -55,10 +51,13 @@ private:
return nullptr;
}
void map_dom_nodes_to_parent(JsonObject const* parent, JsonObject const* child);
GUI::Icon m_document_icon;
GUI::Icon m_element_icon;
GUI::Icon m_text_icon;
JsonObject m_dom_tree;
HashMap<JsonObject const*, JsonObject const*> m_dom_node_to_parent_map;
};
}