mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 03:37:34 +00:00
LibWeb: Do not create copies of JSON values in OOPWV DOM Inspector
To improve the performance of the DOM Inspector when the Browser is run in multi-process mode, do not create copies of the JSON values sent via IPC when searching for a model index. Methods that are guaranteed to return a value now return a reference. Methods that do not have such a guarantee return a pointer (rather than an Optional, because Optional cannot hold references). The DOM Inspector performs well at first, but will start lagging again once the tree is expanded a few nodes deep and/or with many nodes visible in the tree.
This commit is contained in:
parent
7d055dd039
commit
7604c2f38e
2 changed files with 64 additions and 54 deletions
|
@ -13,7 +13,7 @@
|
||||||
namespace Web {
|
namespace Web {
|
||||||
|
|
||||||
DOMTreeJSONModel::DOMTreeJSONModel(JsonObject dom_tree)
|
DOMTreeJSONModel::DOMTreeJSONModel(JsonObject dom_tree)
|
||||||
: m_dom_tree(dom_tree)
|
: m_dom_tree(move(dom_tree))
|
||||||
{
|
{
|
||||||
m_document_icon.set_bitmap_for_size(16, Gfx::Bitmap::load_from_file("/res/icons/16x16/filetype-html.png"));
|
m_document_icon.set_bitmap_for_size(16, Gfx::Bitmap::load_from_file("/res/icons/16x16/filetype-html.png"));
|
||||||
m_element_icon.set_bitmap_for_size(16, Gfx::Bitmap::load_from_file("/res/icons/16x16/inspector-object.png"));
|
m_element_icon.set_bitmap_for_size(16, Gfx::Bitmap::load_from_file("/res/icons/16x16/inspector-object.png"));
|
||||||
|
@ -30,10 +30,12 @@ GUI::ModelIndex DOMTreeJSONModel::index(int row, int column, const GUI::ModelInd
|
||||||
return create_index(row, column, (void*)get_internal_id(m_dom_tree));
|
return create_index(row, column, (void*)get_internal_id(m_dom_tree));
|
||||||
}
|
}
|
||||||
|
|
||||||
auto parent_node = find_node(parent);
|
auto const& parent_node = find_node(parent);
|
||||||
auto children = get_children(parent_node);
|
auto const* children = get_children(parent_node);
|
||||||
auto child_node = children[row].as_object();
|
if (!children)
|
||||||
|
return create_index(row, column, (void*)get_internal_id(m_dom_tree));
|
||||||
|
|
||||||
|
auto const& child_node = children->at(row).as_object();
|
||||||
auto child_internal_id = (void*)get_internal_id(child_node);
|
auto child_internal_id = (void*)get_internal_id(child_node);
|
||||||
return create_index(row, column, child_internal_id);
|
return create_index(row, column, child_internal_id);
|
||||||
}
|
}
|
||||||
|
@ -45,32 +47,33 @@ GUI::ModelIndex DOMTreeJSONModel::parent_index(const GUI::ModelIndex& index) con
|
||||||
if (!index.is_valid())
|
if (!index.is_valid())
|
||||||
return {};
|
return {};
|
||||||
|
|
||||||
auto node = find_node(index);
|
auto const& node = find_node(index);
|
||||||
|
|
||||||
auto node_internal_id = get_internal_id(node);
|
auto node_internal_id = get_internal_id(node);
|
||||||
auto parent_node = find_parent_of_child_with_internal_id(node_internal_id);
|
|
||||||
if (!parent_node.has_value())
|
auto const* parent_node = find_parent_of_child_with_internal_id(node_internal_id);
|
||||||
|
if (!parent_node)
|
||||||
return {};
|
return {};
|
||||||
auto parent_node_internal_id = get_internal_id(parent_node.value());
|
|
||||||
|
|
||||||
// If the parent is the root document, we know it has index 0, 0
|
// 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)) {
|
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, (void*)parent_node_internal_id);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Otherwise, we need to find the grandparent, to find the index of parent within that
|
// Otherwise, we need to find the grandparent, to find the index of parent within that
|
||||||
auto grandparent_node = find_parent_of_child_with_internal_id(parent_node_internal_id);
|
auto const* grandparent_node = find_parent_of_child_with_internal_id(parent_node_internal_id);
|
||||||
VERIFY(grandparent_node.has_value());
|
VERIFY(grandparent_node);
|
||||||
|
|
||||||
auto grandparent_children = get_children(*grandparent_node);
|
auto const* grandparent_children = get_children(*grandparent_node);
|
||||||
if (grandparent_children.is_empty())
|
if (!grandparent_children)
|
||||||
return {};
|
return {};
|
||||||
|
|
||||||
for (size_t grandparent_child_index = 0; grandparent_child_index < grandparent_children.size(); ++grandparent_child_index) {
|
for (size_t grandparent_child_index = 0; grandparent_child_index < grandparent_children->size(); ++grandparent_child_index) {
|
||||||
auto child = grandparent_children[grandparent_child_index].as_object();
|
auto const& child = grandparent_children->at(grandparent_child_index).as_object();
|
||||||
if (get_internal_id(child) == parent_node_internal_id)
|
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, (void*)(parent_node_internal_id));
|
||||||
}
|
}
|
||||||
|
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -79,8 +82,9 @@ int DOMTreeJSONModel::row_count(const GUI::ModelIndex& index) const
|
||||||
if (!index.is_valid())
|
if (!index.is_valid())
|
||||||
return 1;
|
return 1;
|
||||||
|
|
||||||
auto child = find_node(index);
|
auto const& node = find_node(index);
|
||||||
return get_children(child).size();
|
auto const* children = get_children(node);
|
||||||
|
return children ? children->size() : 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int DOMTreeJSONModel::column_count(const GUI::ModelIndex&) const
|
int DOMTreeJSONModel::column_count(const GUI::ModelIndex&) const
|
||||||
|
@ -111,7 +115,7 @@ static String with_whitespace_collapsed(const StringView& string)
|
||||||
|
|
||||||
GUI::Variant DOMTreeJSONModel::data(const GUI::ModelIndex& index, GUI::ModelRole role) const
|
GUI::Variant DOMTreeJSONModel::data(const GUI::ModelIndex& index, GUI::ModelRole role) const
|
||||||
{
|
{
|
||||||
auto node = find_node(index);
|
auto const& node = find_node(index);
|
||||||
auto node_name = node.get("name").as_string();
|
auto node_name = node.get("name").as_string();
|
||||||
auto type = node.get("type").as_string_or("unknown");
|
auto type = node.get("type").as_string_or("unknown");
|
||||||
|
|
||||||
|
@ -154,47 +158,55 @@ void DOMTreeJSONModel::update()
|
||||||
did_update();
|
did_update();
|
||||||
}
|
}
|
||||||
|
|
||||||
Optional<JsonObject> DOMTreeJSONModel::find_parent_of_child_with_internal_id(size_t internal_id) const
|
JsonObject const* DOMTreeJSONModel::find_parent_of_child_with_internal_id(size_t internal_id) const
|
||||||
{
|
{
|
||||||
return find_parent_of_child_with_internal_id(m_dom_tree, internal_id);
|
return find_parent_of_child_with_internal_id(m_dom_tree, internal_id);
|
||||||
}
|
}
|
||||||
|
|
||||||
Optional<JsonObject> DOMTreeJSONModel::find_parent_of_child_with_internal_id(JsonObject node, size_t internal_id) const
|
JsonObject const* DOMTreeJSONModel::find_parent_of_child_with_internal_id(JsonObject const& node, size_t internal_id) const
|
||||||
{
|
{
|
||||||
auto children = get_children(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();
|
||||||
|
|
||||||
for (size_t i = 0; i < children.size(); ++i) {
|
|
||||||
auto child = children[i].as_object();
|
|
||||||
auto child_internal_id = get_internal_id(child);
|
auto child_internal_id = get_internal_id(child);
|
||||||
if (child_internal_id == internal_id)
|
if (child_internal_id == internal_id)
|
||||||
return node;
|
return &node;
|
||||||
auto maybe_node = find_parent_of_child_with_internal_id(child, internal_id);
|
|
||||||
if (maybe_node.has_value())
|
if (auto const* maybe_node = find_parent_of_child_with_internal_id(child, internal_id); maybe_node)
|
||||||
return maybe_node;
|
return maybe_node;
|
||||||
}
|
}
|
||||||
return {};
|
|
||||||
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
Optional<JsonObject> DOMTreeJSONModel::find_child_with_internal_id(size_t internal_id) const
|
JsonObject const* DOMTreeJSONModel::find_child_with_internal_id(size_t internal_id) const
|
||||||
{
|
{
|
||||||
return find_child_with_internal_id(m_dom_tree, internal_id);
|
return find_child_with_internal_id(m_dom_tree, internal_id);
|
||||||
}
|
}
|
||||||
|
|
||||||
Optional<JsonObject> DOMTreeJSONModel::find_child_with_internal_id(JsonObject node, size_t internal_id) const
|
JsonObject const* DOMTreeJSONModel::find_child_with_internal_id(JsonObject const& node, size_t internal_id) const
|
||||||
{
|
{
|
||||||
auto node_internal_id = get_internal_id(node);
|
auto node_internal_id = get_internal_id(node);
|
||||||
if (node_internal_id == internal_id) {
|
if (node_internal_id == internal_id) {
|
||||||
return node;
|
return &node;
|
||||||
}
|
}
|
||||||
auto children = get_children(node);
|
|
||||||
|
|
||||||
for (size_t i = 0; i < children.size(); ++i) {
|
auto const* children = get_children(node);
|
||||||
auto child = children[i].as_object();
|
if (!children)
|
||||||
auto maybe_node = find_child_with_internal_id(child, internal_id);
|
return nullptr;
|
||||||
if (maybe_node.has_value())
|
|
||||||
|
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 maybe_node;
|
||||||
}
|
}
|
||||||
return {};
|
|
||||||
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t DOMTreeJSONModel::get_internal_id(JsonObject const& o)
|
size_t DOMTreeJSONModel::get_internal_id(JsonObject const& o)
|
||||||
|
@ -202,24 +214,22 @@ size_t DOMTreeJSONModel::get_internal_id(JsonObject const& o)
|
||||||
return o.get("internal_id").as_u32();
|
return o.get("internal_id").as_u32();
|
||||||
}
|
}
|
||||||
|
|
||||||
JsonArray DOMTreeJSONModel::get_children(JsonObject const& o)
|
JsonArray const* DOMTreeJSONModel::get_children(JsonObject const& o)
|
||||||
{
|
{
|
||||||
auto maybe_children = o.get("children");
|
if (auto const* maybe_children = o.get_ptr("children"); maybe_children)
|
||||||
if (maybe_children.is_null())
|
return &maybe_children->as_array();
|
||||||
return {};
|
return nullptr;
|
||||||
return maybe_children.as_array();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
JsonObject DOMTreeJSONModel::find_node(GUI::ModelIndex index) const
|
JsonObject const& DOMTreeJSONModel::find_node(GUI::ModelIndex const& index) const
|
||||||
{
|
{
|
||||||
auto internal_id = (size_t)(index.internal_data());
|
auto internal_id = (size_t)(index.internal_data());
|
||||||
|
|
||||||
auto maybe_node = find_child_with_internal_id(internal_id);
|
if (auto const* maybe_node = find_child_with_internal_id(internal_id); maybe_node)
|
||||||
if (!maybe_node.has_value()) {
|
return *maybe_node;
|
||||||
dbgln("Failed to find node with internal_id={}", internal_id);
|
|
||||||
VERIFY_NOT_REACHED();
|
dbgln("Failed to find node with internal_id={}", internal_id);
|
||||||
}
|
VERIFY_NOT_REACHED();
|
||||||
return maybe_node.value();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -36,16 +36,16 @@ public:
|
||||||
private:
|
private:
|
||||||
explicit DOMTreeJSONModel(JsonObject);
|
explicit DOMTreeJSONModel(JsonObject);
|
||||||
|
|
||||||
Optional<JsonObject> find_parent_of_child_with_internal_id(size_t) const;
|
JsonObject const* find_parent_of_child_with_internal_id(size_t) const;
|
||||||
Optional<JsonObject> find_parent_of_child_with_internal_id(JsonObject, size_t) const;
|
JsonObject const* find_parent_of_child_with_internal_id(JsonObject const&, size_t) const;
|
||||||
|
|
||||||
Optional<JsonObject> find_child_with_internal_id(size_t) const;
|
JsonObject const* find_child_with_internal_id(size_t) const;
|
||||||
Optional<JsonObject> find_child_with_internal_id(JsonObject, size_t) const;
|
JsonObject const* find_child_with_internal_id(JsonObject const&, size_t) const;
|
||||||
|
|
||||||
JsonObject find_node(GUI::ModelIndex) const;
|
JsonObject const& find_node(GUI::ModelIndex const&) const;
|
||||||
|
|
||||||
static size_t get_internal_id(const JsonObject& o);
|
static size_t get_internal_id(const JsonObject& o);
|
||||||
static JsonArray get_children(const JsonObject& o);
|
static JsonArray const* get_children(const JsonObject& o);
|
||||||
|
|
||||||
GUI::Icon m_document_icon;
|
GUI::Icon m_document_icon;
|
||||||
GUI::Icon m_element_icon;
|
GUI::Icon m_element_icon;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue