Browse Source

Convert a dynamic_cast to static_cast for a measurable performance gain.

Michael Ragazzon 5 years ago
parent
commit
c82f60854c
2 changed files with 10 additions and 6 deletions
  1. 6 4
      Source/Core/LayoutInlineBoxText.cpp
  2. 4 2
      Source/Core/LayoutLineBox.cpp

+ 6 - 4
Source/Core/LayoutInlineBoxText.cpp

@@ -39,7 +39,7 @@
 
 namespace Rml {
 
-LayoutInlineBoxText::LayoutInlineBoxText(Element* element, int _line_begin) : LayoutInlineBox(element, Box())
+LayoutInlineBoxText::LayoutInlineBoxText(ElementText* element, int _line_begin) : LayoutInlineBox(static_cast<Element*>(element), Box())
 {
 	line_begin = _line_begin;
 
@@ -76,7 +76,7 @@ UniquePtr<LayoutInlineBox> LayoutInlineBoxText::FlowContent(bool first_box, floa
 	LayoutInlineBox::FlowContent(first_box, available_width, right_spacing_width);
 
 	if (overflow)
-		return MakeUnique<LayoutInlineBoxText>(element, line_begin + line_length);
+		return MakeUnique<LayoutInlineBoxText>(GetTextElement(), line_begin + line_length);
 
 	return nullptr;
 }
@@ -140,7 +140,9 @@ void LayoutInlineBoxText::operator delete(void* chunk)
 // Returns the box's element as a text element.
 ElementText* LayoutInlineBoxText::GetTextElement()
 {
-	return rmlui_dynamic_cast< ElementText* >(element);
+	RMLUI_ASSERT(rmlui_dynamic_cast<ElementText*>(element));
+
+	return static_cast< ElementText* >(element);
 }
 
 // Builds a box for the first word of the element.
@@ -162,7 +164,7 @@ void LayoutInlineBoxText::BuildWordBox()
 
 	Vector2f content_area;
 	line_segmented = !text_element->GenerateToken(content_area.x, line_begin);
-	content_area.y = element->GetLineHeight();
+	content_area.y = text_element->GetLineHeight();
 	box.SetContent(content_area);
 }
 

+ 4 - 2
Source/Core/LayoutLineBox.cpp

@@ -194,8 +194,10 @@ LayoutInlineBox* LayoutLineBox::AddElement(Element* element, const Box& box)
 {
 	RMLUI_ZoneScoped;
 
-	if (rmlui_dynamic_cast< ElementText* >(element) != nullptr)
-		return AddBox(MakeUnique<LayoutInlineBoxText>(element));
+	ElementText* element_text = rmlui_dynamic_cast<ElementText*>(element);
+
+	if (element_text)
+		return AddBox(MakeUnique<LayoutInlineBoxText>(element_text));
 	else
 		return AddBox(MakeUnique<LayoutInlineBox>(element, box));
 }