ElementStyle.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. /*
  2. * This source file is part of RmlUi, the HTML/CSS Interface Middleware
  3. *
  4. * For the latest information, see http://github.com/mikke89/RmlUi
  5. *
  6. * Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
  7. * Copyright (c) 2019 The RmlUi Team, and contributors
  8. *
  9. * Permission is hereby granted, free of charge, to any person obtaining a copy
  10. * of this software and associated documentation files (the "Software"), to deal
  11. * in the Software without restriction, including without limitation the rights
  12. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. * copies of the Software, and to permit persons to whom the Software is
  14. * furnished to do so, subject to the following conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be included in
  17. * all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. * THE SOFTWARE.
  26. *
  27. */
  28. #include "../Common/TestsShell.h"
  29. #include <RmlUi/Core/Context.h>
  30. #include <RmlUi/Core/Element.h>
  31. #include <RmlUi/Core/ElementDocument.h>
  32. #include <RmlUi/Core/Types.h>
  33. #include <doctest.h>
  34. #include <nanobench.h>
  35. using namespace ankerl;
  36. using namespace Rml;
  37. static constexpr const char* document_rml_template = R"(
  38. <rml>
  39. <head>
  40. <title>Benchmark Sample</title>
  41. <link type="text/template" href="/assets/window.rml"/>
  42. <style>
  43. body
  44. {
  45. font-family: LatoLatin;
  46. font-weight: normal;
  47. font-style: normal;
  48. font-size: 15dp;
  49. color: white;
  50. }
  51. body.window
  52. {
  53. max-width: 2000px;
  54. max-height: 2000px;
  55. left: 100px;
  56. top: 50px;
  57. width: 1300px;
  58. height: 600px;
  59. }
  60. #performance
  61. {
  62. width: 800px;
  63. height: 300px;
  64. }
  65. /* Insert generated style rules below */
  66. %s
  67. </style>
  68. </head>
  69. <body template="window">
  70. <div id="performance"/>
  71. </body>
  72. </rml>
  73. )";
  74. static int GetNumDescendentElements(Element* element)
  75. {
  76. const int num_children = element->GetNumChildren(true);
  77. int result = num_children;
  78. for (int i = 0; i < num_children; i++)
  79. {
  80. result += GetNumDescendentElements(element->GetChild(i));
  81. }
  82. return result;
  83. }
  84. static constexpr int num_rule_iterations = 10;
  85. static String GenerateRCSS(bool with_tag, bool with_id, bool with_class, bool with_pseudo_class, bool with_child_div, String& out_rule_name)
  86. {
  87. static_assert('a' < 'z' && 'a' + 25 == 'z', "Assumes ASCII characters");
  88. auto GenerateRule = [=](const String& name) {
  89. String rule;
  90. if (!with_tag && !with_id && !with_class && !with_pseudo_class)
  91. rule += '*';
  92. else
  93. {
  94. if (with_tag)
  95. {
  96. if (with_id || with_class || with_pseudo_class)
  97. rule += "div";
  98. else
  99. rule += name;
  100. }
  101. if (with_id)
  102. rule += '#' + name;
  103. if (with_class)
  104. rule += '.' + name;
  105. if (with_pseudo_class)
  106. rule += ':' + name;
  107. }
  108. if (with_child_div)
  109. rule += " div";
  110. return rule;
  111. };
  112. out_rule_name = GenerateRule("a");
  113. String result;
  114. for (int i = 0; i < num_rule_iterations; i++)
  115. {
  116. for (char c = 'a'; c <= 'z'; c++)
  117. {
  118. const String name(i, c);
  119. result += GenerateRule(name);
  120. // Set a property that does not require a layout change
  121. result += CreateString(64, " { scrollbar-margin: %dpx; }\n", int(c - 'a') + 1);
  122. }
  123. }
  124. return result;
  125. }
  126. static String GenerateRml(const int num_rows)
  127. {
  128. static nanobench::Rng rng;
  129. Rml::String rml;
  130. rml.reserve(1000 * num_rows);
  131. for (int i = 0; i < num_rows; i++)
  132. {
  133. int index = rng() % 1000;
  134. int route = rng() % 50;
  135. int max = (rng() % 40) + 10;
  136. int value = rng() % max;
  137. String class_name_a = char('a' + char(rng() % 26)) + ToString(rng() % num_rule_iterations);
  138. String class_name_b = char('a' + char(rng() % 26)) + ToString(rng() % num_rule_iterations);
  139. Rml::String rml_row = Rml::CreateString(1000, R"(
  140. <div class="row">
  141. <div class="col col1"><button class="expand" index="%d">+</button>&nbsp;<a>Route %d</a></div>
  142. <div class="col col23"><input type="range" class="assign_range" min="0" max="%d" value="%d"/></div>
  143. <div class="col col4 %s">Assigned</div>
  144. <select>
  145. <option>Red</option><option>Blue</option><option selected>Green</option><option style="background-color: yellow;">Yellow</option>
  146. </select>
  147. <div class="inrow unmark_collapse %s">
  148. <div class="col col123 assign_text">Assign to route</div>
  149. <div class="col col4">
  150. <input type="submit" class="vehicle_depot_assign_confirm" quantity="0">Confirm</input>
  151. </div>
  152. </div>
  153. </div>)",
  154. index, route, max, value, class_name_a.c_str(), class_name_b.c_str());
  155. rml += rml_row;
  156. }
  157. return rml;
  158. }
  159. TEST_CASE("elementstyle")
  160. {
  161. Context* context = TestsShell::GetContext();
  162. REQUIRE(context);
  163. constexpr int num_rows = 50;
  164. const String rml = GenerateRml(num_rows);
  165. // Benchmark the lookup of applicable style rules for elements.
  166. //
  167. // 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
  168. // descendent elements, requiring a lookup for applicable nodes on each of them. We repeat this benchmark with different combinations of unique
  169. // "dummy" style rules added to the style sheet.
  170. nanobench::Bench bench;
  171. bench.title("ElementStyle (rule name)");
  172. bench.timeUnit(std::chrono::microseconds(1), "us");
  173. bench.relative(true);
  174. for (int i = 0; i <= 0b11111 + 1; i++)
  175. {
  176. const bool reference = (i == 0);
  177. const int flags = i - 1;
  178. const bool with_tag = flags & (1 << 0);
  179. const bool with_id = flags & (1 << 1);
  180. const bool with_class = flags & (1 << 2);
  181. const bool with_pseudo_class = flags & (1 << 3);
  182. const bool with_child_div = flags & (1 << 4);
  183. String name = "Reference (no style rules)";
  184. const String styles = reference ? "" : GenerateRCSS(with_tag, with_id, with_class, with_pseudo_class, with_child_div, name);
  185. const String compiled_document_rml = Rml::CreateString(1000 + styles.size(), document_rml_template, styles.c_str());
  186. ElementDocument* document = context->LoadDocumentFromMemory(compiled_document_rml);
  187. document->Show();
  188. Element* el = document->GetElementById("performance");
  189. el->SetInnerRML(rml);
  190. context->Update();
  191. context->Render();
  192. if (reference)
  193. {
  194. String msg = Rml::CreateString(128, "\nElement update after pseudo class change with %d descendant elements and %d unique RCSS rules.",
  195. GetNumDescendentElements(el), num_rule_iterations * 26);
  196. MESSAGE(msg);
  197. bench.run("Reference (load document)", [&] {
  198. ElementDocument* new_document = context->LoadDocumentFromMemory(compiled_document_rml);
  199. new_document->Close();
  200. context->Update();
  201. });
  202. bench.run("Reference (update unmodified)", [&] { context->Update(); });
  203. }
  204. bool hover_active = false;
  205. bench.run(name, [&] {
  206. hover_active = !hover_active;
  207. // Toggle some arbitrary pseudo class on the element to dirty the definition on this and all descendent elements.
  208. el->SetPseudoClass("hover", hover_active);
  209. context->Update();
  210. });
  211. document->Close();
  212. context->Update();
  213. }
  214. }