Selectors.cpp 8.1 KB

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