| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250 |
- /*
- * 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 <RmlUi/Core/Context.h>
- #include <RmlUi/Core/Element.h>
- #include <RmlUi/Core/ElementDocument.h>
- #include <RmlUi/Core/Types.h>
- #include <doctest.h>
- #include <nanobench.h>
- using namespace ankerl;
- using namespace Rml;
- static constexpr const char* document_rml_template = R"(
- <rml>
- <head>
-
- <title>Benchmark Sample</title>
- <link type="text/template" href="/assets/window.rml"/>
- <style>
- body
- {
- font-family: LatoLatin;
- font-weight: normal;
- font-style: normal;
- font-size: 15dp;
- color: white;
- }
- body.window
- {
- max-width: 2000px;
- max-height: 2000px;
- left: 100px;
- top: 50px;
- width: 1300px;
- height: 600px;
- }
- #performance
- {
- width: 800px;
- height: 300px;
- }
- /* Insert generated style rules below */
- %s
- </style>
- </head>
- <body template="window">
- <div id="performance"/>
- </body>
- </rml>
- )";
- 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"(
- <div class="row">
- <div class="col col1"><button class="expand" index="%d">+</button> <a>Route %d</a></div>
- <div class="col col23"><input type="range" class="assign_range" min="0" max="%d" value="%d"/></div>
- <div class="col col4 %s">Assigned</div>
- <select>
- <option>Red</option><option>Blue</option><option selected>Green</option><option style="background-color: yellow;">Yellow</option>
- </select>
- <div class="inrow unmark_collapse %s">
- <div class="col col123 assign_text">Assign to route</div>
- <div class="col col4">
- <input type="submit" class="vehicle_depot_assign_confirm" quantity="0">Confirm</input>
- </div>
- </div>
- </div>)",
- 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();
- }
- }
|