Selectors.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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. enum SelectorFlags {
  86. NO_SELECTOR,
  87. TAG = 1 << 0,
  88. ID = 1 << 1,
  89. CLASS = 1 << 2,
  90. PSEUDO_CLASS = 1 << 3,
  91. NUM_COMBINATIONS = 1 << 4,
  92. };
  93. static String GenerateRCSS(SelectorFlags selectors, const String& complex_selector, String& out_rule_name)
  94. {
  95. static_assert('a' < 'z' && 'a' + 25 == 'z', "Assumes ASCII characters");
  96. auto GenerateRule = [=](const String& name) {
  97. String rule;
  98. if (!complex_selector.empty())
  99. {
  100. rule = complex_selector;
  101. }
  102. else if (selectors == NO_SELECTOR || name.empty())
  103. {
  104. rule += '*';
  105. }
  106. else
  107. {
  108. if (selectors & TAG)
  109. {
  110. if (selectors == TAG)
  111. rule += name;
  112. else
  113. rule += "div";
  114. }
  115. if (selectors & ID)
  116. rule += '#' + name;
  117. if (selectors & CLASS)
  118. rule += '.' + name;
  119. if (selectors & PSEUDO_CLASS)
  120. rule += ':' + name;
  121. }
  122. return rule;
  123. };
  124. out_rule_name = GenerateRule("a");
  125. String result;
  126. for (int i = 0; i < num_rule_iterations; i++)
  127. {
  128. for (char c = 'a'; c <= 'z'; c++)
  129. {
  130. const String name(i, c);
  131. result += GenerateRule(name);
  132. // Set a property that does not require a layout change
  133. result += CreateString(64, " { scrollbar-margin: %dpx; }\n", int(c - 'a') + 1);
  134. #if 1
  135. // This conditions ensures that only a single version of the complex selector is included. This can be disabled to test how well the rules
  136. // are de-duplicated, since then a lot more selectors will be tested per update call. Rules that contain sub-selectors are currently not
  137. // de-duplicated, such as :not().
  138. if (!complex_selector.empty())
  139. return result;
  140. #endif
  141. }
  142. }
  143. return result;
  144. }
  145. static String GenerateRml(const int num_rows)
  146. {
  147. static nanobench::Rng rng;
  148. Rml::String rml;
  149. rml.reserve(1000 * num_rows);
  150. for (int i = 0; i < num_rows; i++)
  151. {
  152. int index = rng() % 1000;
  153. int route = rng() % 50;
  154. int max = (rng() % 40) + 10;
  155. int value = rng() % max;
  156. String class_name_a = char('a' + char(rng() % 26)) + ToString(rng() % num_rule_iterations);
  157. String class_name_b = char('a' + char(rng() % 26)) + ToString(rng() % num_rule_iterations);
  158. Rml::String rml_row = Rml::CreateString(1000, R"(
  159. <div class="row">
  160. <div class="col col1"><button class="expand" index="%d">+</button>&nbsp;<a>Route %d</a></div>
  161. <div class="col col23"><input type="range" class="assign_range" min="0" max="%d" value="%d"/></div>
  162. <div class="col col4 %s">Assigned</div>
  163. <select>
  164. <option>Red</option><option>Blue</option><option selected>Green</option><option style="background-color: yellow;">Yellow</option>
  165. </select>
  166. <div class="inrow unmark_collapse %s">
  167. <div class="col col123 assign_text">Assign to route</div>
  168. <div class="col col4">
  169. <input type="submit" class="vehicle_depot_assign_confirm" quantity="0">Confirm</input>
  170. </div>
  171. </div>
  172. </div>)",
  173. index, route, max, value, class_name_a.c_str(), class_name_b.c_str());
  174. rml += rml_row;
  175. }
  176. return rml;
  177. }
  178. TEST_CASE("Selectors")
  179. {
  180. Context* context = TestsShell::GetContext();
  181. REQUIRE(context);
  182. constexpr int num_rows = 50;
  183. const String rml = GenerateRml(num_rows);
  184. // Benchmark the lookup of applicable style rules for elements.
  185. //
  186. // 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
  187. // descendent elements, requiring a lookup for applicable nodes on each of them. We repeat this benchmark with different combinations of unique
  188. // "dummy" style rules added to the style sheet.
  189. nanobench::Bench bench;
  190. bench.title("Selector (rule name)");
  191. bench.timeUnit(std::chrono::microseconds(1), "us");
  192. bench.relative(true);
  193. const Vector<String> complex_selectors = {
  194. "div",
  195. "div div",
  196. "div > div",
  197. "div + div",
  198. "div ~ div",
  199. ":empty div",
  200. ":only-child div",
  201. ":first-child div",
  202. ":nth-child(2n+3) div",
  203. ":nth-of-type(2n+3) div",
  204. ":not(div) div",
  205. };
  206. for (int i = 0; i < NUM_COMBINATIONS + (int)complex_selectors.size(); i++)
  207. {
  208. const bool reference = (i == 0);
  209. const SelectorFlags selector_flags = SelectorFlags(i < NUM_COMBINATIONS ? i : NO_SELECTOR);
  210. const String& complex_selector = (i < NUM_COMBINATIONS ? String() : complex_selectors[i - NUM_COMBINATIONS]);
  211. String name, styles;
  212. if (reference)
  213. name = "Reference (no style rules)";
  214. else
  215. styles = GenerateRCSS(selector_flags, complex_selector, name);
  216. const String compiled_document_rml = Rml::CreateString(1000 + styles.size(), document_rml_template, styles.c_str());
  217. ElementDocument* document = context->LoadDocumentFromMemory(compiled_document_rml);
  218. document->Show();
  219. Element* el = document->GetElementById("performance");
  220. el->SetInnerRML(rml);
  221. context->Update();
  222. context->Render();
  223. if (reference)
  224. {
  225. String msg = Rml::CreateString(128, "\nElement update after pseudo class change with %d descendant elements and %d unique RCSS rules.",
  226. GetNumDescendentElements(el), num_rule_iterations * 26);
  227. MESSAGE(msg);
  228. bench.run("Reference (load document)", [&] {
  229. ElementDocument* new_document = context->LoadDocumentFromMemory(compiled_document_rml);
  230. new_document->Close();
  231. context->Update();
  232. });
  233. bench.run("Reference (update unmodified)", [&] { context->Update(); });
  234. }
  235. bool hover_active = false;
  236. bench.run(name, [&] {
  237. hover_active = !hover_active;
  238. // Toggle some arbitrary pseudo class on the element to dirty the definition on this and all descendent elements.
  239. el->SetPseudoClass("hover", hover_active);
  240. context->Update();
  241. });
  242. document->Close();
  243. context->Update();
  244. }
  245. }