mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 05:37:35 +00:00
LibWeb: Add fast_is<T>() for some DOM and layout node subclasses
The generic is<T>() uses dynamic_cast which is fine in the majority of cases, but when one of them shows up in profiles, we can make it faster by answering the is-a question manually.
This commit is contained in:
parent
65fa0c2774
commit
fd441b954d
7 changed files with 24 additions and 17 deletions
|
@ -115,13 +115,7 @@ private:
|
||||||
Vector<FlyString> m_classes;
|
Vector<FlyString> m_classes;
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
namespace AK {
|
|
||||||
template<>
|
template<>
|
||||||
inline bool is<Web::DOM::Element>(const Web::DOM::Node& input)
|
inline bool Node::fast_is<Element>() const { return is_element(); }
|
||||||
{
|
|
||||||
return input.is_element();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -143,6 +143,9 @@ public:
|
||||||
|
|
||||||
virtual EventTarget* get_parent(const Event&) override;
|
virtual EventTarget* get_parent(const Event&) override;
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
bool fast_is() const = delete;
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
Node(Document&, NodeType);
|
Node(Document&, NodeType);
|
||||||
|
|
||||||
|
|
|
@ -52,8 +52,14 @@ public:
|
||||||
void for_each_fragment(Callback) const;
|
void for_each_fragment(Callback) const;
|
||||||
|
|
||||||
virtual void split_into_lines(InlineFormattingContext&, LayoutMode) override;
|
virtual void split_into_lines(InlineFormattingContext&, LayoutMode) override;
|
||||||
|
|
||||||
|
private:
|
||||||
|
virtual bool is_block_box() const final { return true; }
|
||||||
};
|
};
|
||||||
|
|
||||||
|
template<>
|
||||||
|
inline bool Node::fast_is<BlockBox>() const { return is_block_box(); }
|
||||||
|
|
||||||
template<typename Callback>
|
template<typename Callback>
|
||||||
void BlockBox::for_each_fragment(Callback callback)
|
void BlockBox::for_each_fragment(Callback callback)
|
||||||
{
|
{
|
||||||
|
|
|
@ -134,13 +134,7 @@ private:
|
||||||
OwnPtr<StackingContext> m_stacking_context;
|
OwnPtr<StackingContext> m_stacking_context;
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
namespace AK {
|
|
||||||
template<>
|
template<>
|
||||||
inline bool is<Web::Layout::Box>(const Web::Layout::Node& input)
|
inline bool Node::fast_is<Box>() const { return is_box(); }
|
||||||
{
|
|
||||||
return input.is_box();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -67,7 +67,7 @@ const BlockBox* Node::containing_block() const
|
||||||
auto* ancestor = parent();
|
auto* ancestor = parent();
|
||||||
while (ancestor && !is<BlockBox>(*ancestor))
|
while (ancestor && !is<BlockBox>(*ancestor))
|
||||||
ancestor = ancestor->parent();
|
ancestor = ancestor->parent();
|
||||||
return downcast<BlockBox>(ancestor);
|
return static_cast<const BlockBox*>(ancestor);
|
||||||
};
|
};
|
||||||
|
|
||||||
if (is<TextNode>(*this))
|
if (is<TextNode>(*this))
|
||||||
|
@ -79,9 +79,9 @@ const BlockBox* Node::containing_block() const
|
||||||
auto* ancestor = parent();
|
auto* ancestor = parent();
|
||||||
while (ancestor && !ancestor->can_contain_boxes_with_position_absolute())
|
while (ancestor && !ancestor->can_contain_boxes_with_position_absolute())
|
||||||
ancestor = ancestor->parent();
|
ancestor = ancestor->parent();
|
||||||
while (ancestor && (!is<BlockBox>(ancestor) || ancestor->is_anonymous()))
|
while (ancestor && (!is<BlockBox>(*ancestor) || ancestor->is_anonymous()))
|
||||||
ancestor = ancestor->containing_block();
|
ancestor = ancestor->containing_block();
|
||||||
return downcast<BlockBox>(ancestor);
|
return static_cast<const BlockBox*>(ancestor);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (position == CSS::Position::Fixed)
|
if (position == CSS::Position::Fixed)
|
||||||
|
|
|
@ -115,7 +115,13 @@ public:
|
||||||
virtual void paint_fragment(PaintContext&, const LineBoxFragment&, PaintPhase) const { }
|
virtual void paint_fragment(PaintContext&, const LineBoxFragment&, PaintPhase) const { }
|
||||||
virtual void after_children_paint(PaintContext&, PaintPhase) {};
|
virtual void after_children_paint(PaintContext&, PaintPhase) {};
|
||||||
|
|
||||||
|
// These are used to optimize hot is<T> variants for some classes where dynamic_cast is too slow.
|
||||||
virtual bool is_box() const { return false; }
|
virtual bool is_box() const { return false; }
|
||||||
|
virtual bool is_block_box() const { return false; }
|
||||||
|
virtual bool is_text_node() const { return false; }
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
bool fast_is() const = delete;
|
||||||
|
|
||||||
bool is_floating() const;
|
bool is_floating() const;
|
||||||
bool is_positioned() const;
|
bool is_positioned() const;
|
||||||
|
|
|
@ -47,6 +47,7 @@ public:
|
||||||
virtual void split_into_lines(InlineFormattingContext&, LayoutMode) override;
|
virtual void split_into_lines(InlineFormattingContext&, LayoutMode) override;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
virtual bool is_text_node() const final { return true; }
|
||||||
void split_into_lines_by_rules(InlineFormattingContext&, LayoutMode, bool do_collapse, bool do_wrap_lines, bool do_wrap_breaks);
|
void split_into_lines_by_rules(InlineFormattingContext&, LayoutMode, bool do_collapse, bool do_wrap_lines, bool do_wrap_breaks);
|
||||||
void paint_cursor_if_needed(PaintContext&, const LineBoxFragment&) const;
|
void paint_cursor_if_needed(PaintContext&, const LineBoxFragment&) const;
|
||||||
|
|
||||||
|
@ -56,4 +57,7 @@ private:
|
||||||
String m_text_for_rendering;
|
String m_text_for_rendering;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
template<>
|
||||||
|
inline bool Node::fast_is<TextNode>() const { return is_text_node(); }
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue