/*
* This source file is part of RmlUi, the HTML/CSS Interface Middleware
*
* For the latest information, see http://github.com/mikke89/RmlUi
*
* Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
* Copyright (c) 2019 The RmlUi Team, and contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
*/
#include "../Common/TestsShell.h"
#include
#include
#include
#include
#include
#include
using namespace ankerl;
using namespace Rml;
static constexpr const char* document_rml_template = R"(
Benchmark Sample
)";
static int GetNumDescendentElements(Element* element)
{
const int num_children = element->GetNumChildren(true);
int result = num_children;
for (int i = 0; i < num_children; i++)
{
result += GetNumDescendentElements(element->GetChild(i));
}
return result;
}
static constexpr int num_rule_iterations = 10;
static String GenerateRCSS(bool with_tag, bool with_id, bool with_class, bool with_pseudo_class, bool with_child_div, String& out_rule_name)
{
static_assert('a' < 'z' && 'a' + 25 == 'z', "Assumes ASCII characters");
auto GenerateRule = [=](const String& name) {
String rule;
if (!with_tag && !with_id && !with_class && !with_pseudo_class)
rule += '*';
else
{
if (with_tag)
{
if (with_id || with_class || with_pseudo_class)
rule += "div";
else
rule += name;
}
if (with_id)
rule += '#' + name;
if (with_class)
rule += '.' + name;
if (with_pseudo_class)
rule += ':' + name;
}
if (with_child_div)
rule += " div";
return rule;
};
out_rule_name = GenerateRule("a");
String result;
for (int i = 0; i < num_rule_iterations; i++)
{
for (char c = 'a'; c <= 'z'; c++)
{
const String name(i, c);
result += GenerateRule(name);
// Set a property that does not require a layout change
result += CreateString(64, " { scrollbar-margin: %dpx; }\n", int(c - 'a') + 1);
}
}
return result;
}
static String GenerateRml(const int num_rows)
{
static nanobench::Rng rng;
Rml::String rml;
rml.reserve(1000 * num_rows);
for (int i = 0; i < num_rows; i++)
{
int index = rng() % 1000;
int route = rng() % 50;
int max = (rng() % 40) + 10;
int value = rng() % max;
String class_name_a = char('a' + char(rng() % 26)) + ToString(rng() % num_rule_iterations);
String class_name_b = char('a' + char(rng() % 26)) + ToString(rng() % num_rule_iterations);
Rml::String rml_row = Rml::CreateString(1000, R"(
)",
index, route, max, value, class_name_a.c_str(), class_name_b.c_str());
rml += rml_row;
}
return rml;
}
TEST_CASE("elementstyle")
{
Context* context = TestsShell::GetContext();
REQUIRE(context);
constexpr int num_rows = 50;
const String rml = GenerateRml(num_rows);
// Benchmark the lookup of applicable style rules for elements.
//
// We do this by toggling a pseudo class on an element with a lot of descendent elements. This dirties the style definition for this and all
// descendent elements, requiring a lookup for applicable nodes on each of them. We repeat this benchmark with different combinations of unique
// "dummy" style rules added to the style sheet.
nanobench::Bench bench;
bench.title("ElementStyle (rule name)");
bench.timeUnit(std::chrono::microseconds(1), "us");
bench.relative(true);
for (int i = 0; i <= 0b11111 + 1; i++)
{
const bool reference = (i == 0);
const int flags = i - 1;
const bool with_tag = flags & (1 << 0);
const bool with_id = flags & (1 << 1);
const bool with_class = flags & (1 << 2);
const bool with_pseudo_class = flags & (1 << 3);
const bool with_child_div = flags & (1 << 4);
String name = "Reference (no style rules)";
const String styles = reference ? "" : GenerateRCSS(with_tag, with_id, with_class, with_pseudo_class, with_child_div, name);
const String compiled_document_rml = Rml::CreateString(1000 + styles.size(), document_rml_template, styles.c_str());
ElementDocument* document = context->LoadDocumentFromMemory(compiled_document_rml);
document->Show();
Element* el = document->GetElementById("performance");
el->SetInnerRML(rml);
context->Update();
context->Render();
if (reference)
{
String msg = Rml::CreateString(128, "\nElement update after pseudo class change with %d descendant elements and %d unique RCSS rules.",
GetNumDescendentElements(el), num_rule_iterations * 26);
MESSAGE(msg);
bench.run("Reference (load document)", [&] {
ElementDocument* new_document = context->LoadDocumentFromMemory(compiled_document_rml);
new_document->Close();
context->Update();
});
bench.run("Reference (update unmodified)", [&] { context->Update(); });
}
bool hover_active = false;
bench.run(name, [&] {
hover_active = !hover_active;
// Toggle some arbitrary pseudo class on the element to dirty the definition on this and all descendent elements.
el->SetPseudoClass("hover", hover_active);
context->Update();
});
document->Close();
context->Update();
}
}