mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 00:57:45 +00:00
LibWeb: Fix O(n^2) traversal in hit testing
We already walk the entire paint tree within each stacking context in the main hit testing function (StackingContext::hit_test()), so there's no need for each individual paintable to walk its own children again. By not doing that, we remove a source of O(n^2) traversal which made hit testing on deeply nested web pages unbearably slow.
This commit is contained in:
parent
f7cfd47b48
commit
996f3228a2
2 changed files with 8 additions and 18 deletions
|
@ -535,16 +535,9 @@ Optional<HitTestResult> PaintableBox::hit_test(Gfx::FloatPoint const& position,
|
||||||
return stacking_context()->hit_test(position, type);
|
return stacking_context()->hit_test(position, type);
|
||||||
}
|
}
|
||||||
|
|
||||||
Optional<HitTestResult> result;
|
|
||||||
if (absolute_border_box_rect().contains(position.x(), position.y()))
|
if (absolute_border_box_rect().contains(position.x(), position.y()))
|
||||||
result = HitTestResult { *this };
|
return HitTestResult { *this };
|
||||||
for_each_child_in_paint_order([&](auto& child) {
|
return {};
|
||||||
if (child.paintable()) {
|
|
||||||
if (auto child_result = child.paintable()->hit_test(position, type); child_result.has_value())
|
|
||||||
result = move(child_result);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
return result;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Optional<HitTestResult> PaintableWithLines::hit_test(const Gfx::FloatPoint& position, HitTestType type) const
|
Optional<HitTestResult> PaintableWithLines::hit_test(const Gfx::FloatPoint& position, HitTestType type) const
|
||||||
|
|
|
@ -297,9 +297,8 @@ Optional<HitTestResult> StackingContext::hit_test(Gfx::FloatPoint const& positio
|
||||||
// 6. the child stacking contexts with stack level 0 and the positioned descendants with stack level 0.
|
// 6. the child stacking contexts with stack level 0 and the positioned descendants with stack level 0.
|
||||||
m_box.for_each_in_subtree_of_type<Layout::Box>([&](Layout::Box const& box) {
|
m_box.for_each_in_subtree_of_type<Layout::Box>([&](Layout::Box const& box) {
|
||||||
if (box.is_positioned() && !box.paint_box()->stacking_context()) {
|
if (box.is_positioned() && !box.paint_box()->stacking_context()) {
|
||||||
result = box.paint_box()->hit_test(transformed_position, type);
|
if (auto candidate = box.paint_box()->hit_test(transformed_position, type); candidate.has_value())
|
||||||
if (result.has_value())
|
result = move(candidate);
|
||||||
return IterationDecision::Break;
|
|
||||||
}
|
}
|
||||||
return IterationDecision::Continue;
|
return IterationDecision::Continue;
|
||||||
});
|
});
|
||||||
|
@ -316,9 +315,8 @@ Optional<HitTestResult> StackingContext::hit_test(Gfx::FloatPoint const& positio
|
||||||
// 4. the non-positioned floats.
|
// 4. the non-positioned floats.
|
||||||
m_box.for_each_in_subtree_of_type<Layout::Box>([&](Layout::Box const& box) {
|
m_box.for_each_in_subtree_of_type<Layout::Box>([&](Layout::Box const& box) {
|
||||||
if (box.is_floating()) {
|
if (box.is_floating()) {
|
||||||
result = box.paint_box()->hit_test(transformed_position, type);
|
if (auto candidate = box.paint_box()->hit_test(transformed_position, type); candidate.has_value())
|
||||||
if (result.has_value())
|
result = move(candidate);
|
||||||
return IterationDecision::Break;
|
|
||||||
}
|
}
|
||||||
return IterationDecision::Continue;
|
return IterationDecision::Continue;
|
||||||
});
|
});
|
||||||
|
@ -329,9 +327,8 @@ Optional<HitTestResult> StackingContext::hit_test(Gfx::FloatPoint const& positio
|
||||||
if (!m_box.children_are_inline()) {
|
if (!m_box.children_are_inline()) {
|
||||||
m_box.for_each_in_subtree_of_type<Layout::Box>([&](Layout::Box const& box) {
|
m_box.for_each_in_subtree_of_type<Layout::Box>([&](Layout::Box const& box) {
|
||||||
if (!box.is_absolutely_positioned() && !box.is_floating()) {
|
if (!box.is_absolutely_positioned() && !box.is_floating()) {
|
||||||
result = box.paint_box()->hit_test(transformed_position, type);
|
if (auto candidate = box.paint_box()->hit_test(transformed_position, type); candidate.has_value())
|
||||||
if (result.has_value())
|
result = move(candidate);
|
||||||
return IterationDecision::Break;
|
|
||||||
}
|
}
|
||||||
return IterationDecision::Continue;
|
return IterationDecision::Continue;
|
||||||
});
|
});
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue