Skip to content

Commit eb1d6ee

Browse files
committed
Add flexbox chat benchmark (see #498)
And additional profiling zone markers.
1 parent 69479d6 commit eb1d6ee

File tree

5 files changed

+94
-2
lines changed

5 files changed

+94
-2
lines changed

Backends/RmlUi_Backend_SDL_GL3.cpp

+4
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
#include <RmlUi/Core/Context.h>
3333
#include <RmlUi/Core/Core.h>
3434
#include <RmlUi/Core/FileInterface.h>
35+
#include <RmlUi/Core/Profiling.h>
3536
#include <SDL.h>
3637
#include <SDL_image.h>
3738

@@ -321,4 +322,7 @@ void Backend::PresentFrame()
321322

322323
data->render_interface.EndFrame();
323324
SDL_GL_SwapWindow(data->window);
325+
326+
// Optional, used to mark frames during performance profiling.
327+
RMLUI_FrameMark;
324328
}

Source/Core/FontEngineDefault/FontFaceHandleDefault.cpp

+3
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
*/
2828

2929
#include "FontFaceHandleDefault.h"
30+
#include "../../../Include/RmlUi/Core/Profiling.h"
3031
#include "../../../Include/RmlUi/Core/StringUtilities.h"
3132
#include "../TextureLayout.h"
3233
#include "FontFaceLayer.h"
@@ -83,6 +84,8 @@ const FontGlyphMap& FontFaceHandleDefault::GetGlyphs() const
8384

8485
int FontFaceHandleDefault::GetStringWidth(const String& string, float letter_spacing, Character prior_character)
8586
{
87+
RMLUI_ZoneScoped;
88+
8689
int width = 0;
8790
for (auto it_string = StringIteratorU8(string); it_string; ++it_string)
8891
{

Source/Core/Layout/FlexFormattingContext.cpp

+5-2
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#include "../../../Include/RmlUi/Core/ComputedValues.h"
3131
#include "../../../Include/RmlUi/Core/Element.h"
3232
#include "../../../Include/RmlUi/Core/ElementScroll.h"
33+
#include "../../../Include/RmlUi/Core/Profiling.h"
3334
#include "../../../Include/RmlUi/Core/Types.h"
3435
#include "ContainerBox.h"
3536
#include "LayoutDetails.h"
@@ -42,6 +43,7 @@ namespace Rml {
4243

4344
UniquePtr<LayoutBox> FlexFormattingContext::Format(ContainerBox* parent_container, Element* element, const Box* override_initial_box)
4445
{
46+
RMLUI_ZoneScopedC(0xAFAF4F);
4547
auto flex_container_box = MakeUnique<FlexContainer>(element, parent_container);
4648

4749
ElementScroll* element_scroll = element->GetElementScroll();
@@ -135,7 +137,7 @@ struct FlexItem {
135137
Size cross;
136138
float flex_shrink_factor;
137139
float flex_grow_factor;
138-
Style::AlignSelf align_self; // 'Auto' is replaced by container's 'align-items' value
140+
Style::AlignSelf align_self; // 'Auto' is replaced by container's 'align-items' value
139141

140142
float inner_flex_base_size; // Inner size
141143
float flex_base_size; // Outer size
@@ -234,9 +236,10 @@ void FlexFormattingContext::Format(Vector2f& flex_resulting_content_size, Vector
234236
const float cross_size_base_value = (cross_available_size < 0.0f ? 0.0f : cross_available_size);
235237

236238
// -- Build a list of all flex items with base size information --
239+
const int num_flex_children = element_flex->GetNumChildren();
237240
Vector<FlexItem> items;
241+
items.reserve(num_flex_children);
238242

239-
const int num_flex_children = element_flex->GetNumChildren();
240243
for (int i = 0; i < num_flex_children; i++)
241244
{
242245
Element* element = element_flex->GetChild(i);

Source/Core/Layout/FormattingContext.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include "FormattingContext.h"
3030
#include "../../../Include/RmlUi/Core/ComputedValues.h"
3131
#include "../../../Include/RmlUi/Core/Element.h"
32+
#include "../../../Include/RmlUi/Core/Profiling.h"
3233
#include "BlockFormattingContext.h"
3334
#include "FlexFormattingContext.h"
3435
#include "LayoutBox.h"
@@ -40,6 +41,7 @@ namespace Rml {
4041
UniquePtr<LayoutBox> FormattingContext::FormatIndependent(ContainerBox* parent_container, Element* element, const Box* override_initial_box,
4142
FormattingContextType backup_context)
4243
{
44+
RMLUI_ZoneScopedC(0xAFAFAF);
4345
using namespace Style;
4446

4547
if (element->IsReplaced())

Tests/Source/Benchmarks/Flexbox.cpp

+80
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#include <RmlUi/Core/Context.h>
3131
#include <RmlUi/Core/Element.h>
3232
#include <RmlUi/Core/ElementDocument.h>
33+
#include <RmlUi/Core/Profiling.h>
3334
#include <RmlUi/Core/Types.h>
3435
#include <doctest.h>
3536
#include <nanobench.h>
@@ -491,3 +492,82 @@ TEST_CASE("flexbox")
491492
document->Close();
492493
}
493494
}
495+
496+
static const String rml_flexbox_chatbox = R"(
497+
<rml>
498+
<head>
499+
<title>Chat</title>
500+
<link type="text/rcss" href="/../Tests/Data/style.rcss"/>
501+
<style>
502+
body {
503+
font-size: 16px;
504+
overflow: auto;
505+
height: 300px;
506+
width: 300px;
507+
}
508+
#chat {
509+
display: flex;
510+
flex-direction: column;
511+
border: 2px #caa;
512+
}
513+
#chat > div {
514+
word-break: break-word;
515+
border: 2px #aac;
516+
}
517+
</style>
518+
</head>
519+
520+
<body>
521+
<div id="chat"/>
522+
</body>
523+
</rml>
524+
)";
525+
526+
TEST_CASE("flexbox.chat")
527+
{
528+
Context* context = TestsShell::GetContext();
529+
REQUIRE(context);
530+
531+
nanobench::Bench bench;
532+
bench.title("Flexbox chat");
533+
bench.relative(true);
534+
// bench.epochs(100);
535+
536+
auto MakeFlexItemsRml = [](int number_items, const String& item_text) {
537+
String rml;
538+
for (int i = 0; i < number_items; i++)
539+
rml += "<div>" + item_text + "</div>";
540+
return rml;
541+
};
542+
543+
const String short_words =
544+
MakeFlexItemsRml(10, "aaaaaaaaaaaaaaa aaaaaaaaaaaaaa aaaaaaaaa aaaaaaaaaaaa aaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa");
545+
const String long_words =
546+
MakeFlexItemsRml(10, "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
547+
548+
// The flex items will essentially be formatted four times each:
549+
// - Two times during flex formatting, first to get their height, then to do their actual formatting.
550+
// - Then flex formatting is itself done twice, since the body adds a scrollbar, thereby modifying the available width.
551+
// The long words take longer to format, since we do a naive approach to breaking up words in ElementText::GenerateLine, making us calculate
552+
// string widths repeatedly. Removing the 'word-break' property should make the long word-case much faster.
553+
ElementDocument* document = context->LoadDocumentFromMemory(rml_flexbox_chatbox);
554+
Element* chat = document->GetElementById("chat");
555+
chat->SetInnerRML(short_words + long_words);
556+
document->Show();
557+
TestsShell::RenderLoop();
558+
559+
bench.run("Short words", [&] {
560+
chat->SetInnerRML(short_words);
561+
context->Update();
562+
context->Render();
563+
RMLUI_FrameMark;
564+
});
565+
bench.run("Long words", [&] {
566+
chat->SetInnerRML(long_words);
567+
context->Update();
568+
context->Render();
569+
RMLUI_FrameMark;
570+
});
571+
572+
document->Close();
573+
}

0 commit comments

Comments
 (0)