Skip to content

Commit

Permalink
LibWeb: Store JSON pointers in the OOPWV DOM Inspector model indices
Browse files Browse the repository at this point in the history
Currently, the DOM Inspector stores a numeric ID for each DOM node. This
is used to look up the data for that node in the JSON representation of
the DOM. The method to do this search performs a depth-first search
through the JSON value, and is invoked quite frequently.

Instead, we can just store a pointer to the JSON value in the index, and
avoid this search altogether. This is similar to how the IPWV stores a
pointer to the DOM node.
  • Loading branch information
trflynn89 authored and awesomekling committed Jun 30, 2021
1 parent 7604c2f commit 067f7ae
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 52 deletions.
56 changes: 9 additions & 47 deletions Userland/Libraries/LibWeb/DOMTreeJSONModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,16 @@ DOMTreeJSONModel::~DOMTreeJSONModel()
GUI::ModelIndex DOMTreeJSONModel::index(int row, int column, const GUI::ModelIndex& parent) const
{
if (!parent.is_valid()) {
return create_index(row, column, (void*)get_internal_id(m_dom_tree));
return create_index(row, column, &m_dom_tree);
}

auto const& parent_node = find_node(parent);
auto const& parent_node = *static_cast<JsonObject const*>(parent.internal_data());
auto const* children = get_children(parent_node);
if (!children)
return create_index(row, column, (void*)get_internal_id(m_dom_tree));
return create_index(row, column, &m_dom_tree);

auto const& child_node = children->at(row).as_object();
auto child_internal_id = (void*)get_internal_id(child_node);
return create_index(row, column, child_internal_id);
return create_index(row, column, &child_node);
}

GUI::ModelIndex DOMTreeJSONModel::parent_index(const GUI::ModelIndex& index) const
Expand All @@ -47,7 +46,7 @@ GUI::ModelIndex DOMTreeJSONModel::parent_index(const GUI::ModelIndex& index) con
if (!index.is_valid())
return {};

auto const& node = find_node(index);
auto const& node = *static_cast<JsonObject const*>(index.internal_data());
auto node_internal_id = get_internal_id(node);

auto const* parent_node = find_parent_of_child_with_internal_id(node_internal_id);
Expand All @@ -57,7 +56,7 @@ GUI::ModelIndex DOMTreeJSONModel::parent_index(const GUI::ModelIndex& index) con
// If the parent is the root document, we know it has index 0, 0
auto parent_node_internal_id = get_internal_id(*parent_node);
if (parent_node_internal_id == get_internal_id(m_dom_tree)) {
return create_index(0, 0, (void*)parent_node_internal_id);
return create_index(0, 0, parent_node);
}

// Otherwise, we need to find the grandparent, to find the index of parent within that
Expand All @@ -71,7 +70,7 @@ GUI::ModelIndex DOMTreeJSONModel::parent_index(const GUI::ModelIndex& index) con
for (size_t grandparent_child_index = 0; grandparent_child_index < grandparent_children->size(); ++grandparent_child_index) {
auto const& child = grandparent_children->at(grandparent_child_index).as_object();
if (get_internal_id(child) == parent_node_internal_id)
return create_index(grandparent_child_index, 0, (void*)(parent_node_internal_id));
return create_index(grandparent_child_index, 0, parent_node);
}

return {};
Expand All @@ -82,7 +81,7 @@ int DOMTreeJSONModel::row_count(const GUI::ModelIndex& index) const
if (!index.is_valid())
return 1;

auto const& node = find_node(index);
auto const& node = *static_cast<JsonObject const*>(index.internal_data());
auto const* children = get_children(node);
return children ? children->size() : 0;
}
Expand Down Expand Up @@ -115,7 +114,7 @@ static String with_whitespace_collapsed(const StringView& string)

GUI::Variant DOMTreeJSONModel::data(const GUI::ModelIndex& index, GUI::ModelRole role) const
{
auto const& node = find_node(index);
auto const& node = *static_cast<JsonObject const*>(index.internal_data());
auto node_name = node.get("name").as_string();
auto type = node.get("type").as_string_or("unknown");

Expand Down Expand Up @@ -183,32 +182,6 @@ JsonObject const* DOMTreeJSONModel::find_parent_of_child_with_internal_id(JsonOb
return nullptr;
}

JsonObject const* DOMTreeJSONModel::find_child_with_internal_id(size_t internal_id) const
{
return find_child_with_internal_id(m_dom_tree, internal_id);
}

JsonObject const* DOMTreeJSONModel::find_child_with_internal_id(JsonObject const& node, size_t internal_id) const
{
auto node_internal_id = get_internal_id(node);
if (node_internal_id == internal_id) {
return &node;
}

auto const* children = get_children(node);
if (!children)
return nullptr;

for (size_t i = 0; i < children->size(); ++i) {
auto const& child = children->at(i).as_object();

if (auto const* maybe_node = find_child_with_internal_id(child, internal_id); maybe_node)
return maybe_node;
}

return nullptr;
}

size_t DOMTreeJSONModel::get_internal_id(JsonObject const& o)
{
return o.get("internal_id").as_u32();
Expand All @@ -221,15 +194,4 @@ JsonArray const* DOMTreeJSONModel::get_children(JsonObject const& o)
return nullptr;
}

JsonObject const& DOMTreeJSONModel::find_node(GUI::ModelIndex const& index) const
{
auto internal_id = (size_t)(index.internal_data());

if (auto const* maybe_node = find_child_with_internal_id(internal_id); maybe_node)
return *maybe_node;

dbgln("Failed to find node with internal_id={}", internal_id);
VERIFY_NOT_REACHED();
}

}
5 changes: 0 additions & 5 deletions Userland/Libraries/LibWeb/DOMTreeJSONModel.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,6 @@ class DOMTreeJSONModel final : public GUI::Model {
JsonObject const* find_parent_of_child_with_internal_id(size_t) const;
JsonObject const* find_parent_of_child_with_internal_id(JsonObject const&, size_t) const;

JsonObject const* find_child_with_internal_id(size_t) const;
JsonObject const* find_child_with_internal_id(JsonObject const&, size_t) const;

JsonObject const& find_node(GUI::ModelIndex const&) const;

static size_t get_internal_id(const JsonObject& o);
static JsonArray const* get_children(const JsonObject& o);

Expand Down

0 comments on commit 067f7ae

Please sign in to comment.