benchmark.cpp 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148
  1. #include <chrono>
  2. #include <cstdint>
  3. #include <iostream>
  4. #include <utility>
  5. #include <vector>
  6. #include <gtest/gtest.h>
  7. #include <entt/entity/registry.hpp>
  8. #include <entt/entity/runtime_view.hpp>
  9. struct position {
  10. std::uint64_t x;
  11. std::uint64_t y;
  12. };
  13. struct velocity: position {};
  14. struct stable_position: position {
  15. static constexpr auto in_place_delete = true;
  16. };
  17. template<auto>
  18. struct comp {
  19. int x;
  20. };
  21. struct timer final {
  22. timer()
  23. : start{std::chrono::system_clock::now()} {}
  24. void elapsed() {
  25. auto now = std::chrono::system_clock::now();
  26. std::cout << std::chrono::duration<double>(now - start).count() << " seconds" << std::endl;
  27. }
  28. private:
  29. std::chrono::time_point<std::chrono::system_clock> start;
  30. };
  31. template<typename Iterable, typename Func>
  32. void generic(Iterable &&iterable, Func func) {
  33. timer timer;
  34. std::forward<Iterable>(iterable).each(func);
  35. timer.elapsed();
  36. }
  37. template<typename Func>
  38. void pathological(Func func) {
  39. entt::registry registry;
  40. for(std::uint64_t i = 0; i < 500000L; i++) {
  41. const auto entity = registry.create();
  42. registry.emplace<position>(entity);
  43. registry.emplace<velocity>(entity);
  44. registry.emplace<comp<0>>(entity);
  45. }
  46. for(auto i = 0; i < 10; ++i) {
  47. registry.each([i = 0, &registry](const auto entity) mutable {
  48. if(!(++i % 7)) {
  49. registry.remove<position>(entity);
  50. }
  51. if(!(++i % 11)) {
  52. registry.remove<velocity>(entity);
  53. }
  54. if(!(++i % 13)) {
  55. registry.remove<comp<0>>(entity);
  56. }
  57. if(!(++i % 17)) {
  58. registry.destroy(entity);
  59. }
  60. });
  61. for(std::uint64_t j = 0; j < 50000L; j++) {
  62. const auto entity = registry.create();
  63. registry.emplace<position>(entity);
  64. registry.emplace<velocity>(entity);
  65. registry.emplace<comp<0>>(entity);
  66. }
  67. }
  68. timer timer;
  69. func(registry).each([](auto &...comp) { ((comp.x = {}), ...); });
  70. timer.elapsed();
  71. }
  72. TEST(Benchmark, Create) {
  73. entt::registry registry;
  74. std::cout << "Creating 1000000 entities" << std::endl;
  75. timer timer;
  76. for(std::uint64_t i = 0; i < 1000000L; i++) {
  77. static_cast<void>(registry.create());
  78. }
  79. timer.elapsed();
  80. }
  81. TEST(Benchmark, CreateMany) {
  82. entt::registry registry;
  83. std::vector<entt::entity> entities(1000000);
  84. std::cout << "Creating 1000000 entities at once" << std::endl;
  85. timer timer;
  86. registry.create(entities.begin(), entities.end());
  87. timer.elapsed();
  88. }
  89. TEST(Benchmark, CreateManyAndEmplaceComponents) {
  90. entt::registry registry;
  91. std::vector<entt::entity> entities(1000000);
  92. std::cout << "Creating 1000000 entities at once and emplace components" << std::endl;
  93. timer timer;
  94. registry.create(entities.begin(), entities.end());
  95. for(const auto entity: entities) {
  96. registry.emplace<position>(entity);
  97. registry.emplace<velocity>(entity);
  98. }
  99. timer.elapsed();
  100. }
  101. TEST(Benchmark, CreateManyWithComponents) {
  102. entt::registry registry;
  103. std::vector<entt::entity> entities(1000000);
  104. std::cout << "Creating 1000000 entities at once with components" << std::endl;
  105. timer timer;
  106. registry.create(entities.begin(), entities.end());
  107. registry.insert<position>(entities.begin(), entities.end());
  108. registry.insert<velocity>(entities.begin(), entities.end());
  109. timer.elapsed();
  110. }
  111. TEST(Benchmark, Erase) {
  112. entt::registry registry;
  113. std::vector<entt::entity> entities(1000000);
  114. auto view = registry.view<position>();
  115. std::cout << "Erasing 1000000 components from their entities" << std::endl;
  116. registry.create(entities.begin(), entities.end());
  117. registry.insert<position>(entities.begin(), entities.end());
  118. timer timer;
  119. for(auto entity: view) {
  120. registry.erase<position>(entity);
  121. }
  122. timer.elapsed();
  123. }
  124. TEST(Benchmark, EraseMany) {
  125. entt::registry registry;
  126. std::vector<entt::entity> entities(1000000);
  127. auto view = registry.view<position>();
  128. std::cout << "Erasing 1000000 components from their entities at once" << std::endl;
  129. registry.create(entities.begin(), entities.end());
  130. registry.insert<position>(entities.begin(), entities.end());
  131. timer timer;
  132. registry.erase<position>(view.begin(), view.end());
  133. timer.elapsed();
  134. }
  135. TEST(Benchmark, EraseManyMulti) {
  136. entt::registry registry;
  137. std::vector<entt::entity> entities(1000000);
  138. auto view = registry.view<position>();
  139. std::cout << "Erasing 1000000 components from their entities at once" << std::endl;
  140. registry.create(entities.begin(), entities.end());
  141. registry.insert<position>(entities.begin(), entities.end());
  142. registry.insert<velocity>(entities.begin(), entities.end());
  143. timer timer;
  144. registry.erase<position, velocity>(view.begin(), view.end());
  145. timer.elapsed();
  146. }
  147. TEST(Benchmark, Remove) {
  148. entt::registry registry;
  149. std::vector<entt::entity> entities(1000000);
  150. auto view = registry.view<position>();
  151. std::cout << "Removing 1000000 components from their entities" << std::endl;
  152. registry.create(entities.begin(), entities.end());
  153. registry.insert<position>(entities.begin(), entities.end());
  154. timer timer;
  155. for(auto entity: view) {
  156. registry.remove<position>(entity);
  157. }
  158. timer.elapsed();
  159. }
  160. TEST(Benchmark, RemoveMany) {
  161. entt::registry registry;
  162. std::vector<entt::entity> entities(1000000);
  163. auto view = registry.view<position>();
  164. std::cout << "Removing 1000000 components from their entities at once" << std::endl;
  165. registry.create(entities.begin(), entities.end());
  166. registry.insert<position>(entities.begin(), entities.end());
  167. timer timer;
  168. registry.remove<position>(view.begin(), view.end());
  169. timer.elapsed();
  170. }
  171. TEST(Benchmark, RemoveManyMulti) {
  172. entt::registry registry;
  173. std::vector<entt::entity> entities(1000000);
  174. auto view = registry.view<position>();
  175. std::cout << "Removing 1000000 components from their entities at once" << std::endl;
  176. registry.create(entities.begin(), entities.end());
  177. registry.insert<position>(entities.begin(), entities.end());
  178. registry.insert<velocity>(entities.begin(), entities.end());
  179. timer timer;
  180. registry.remove<position, velocity>(view.begin(), view.end());
  181. timer.elapsed();
  182. }
  183. TEST(Benchmark, Clear) {
  184. entt::registry registry;
  185. std::vector<entt::entity> entities(1000000);
  186. std::cout << "Clearing 1000000 components from their entities" << std::endl;
  187. registry.create(entities.begin(), entities.end());
  188. registry.insert<position>(entities.begin(), entities.end());
  189. timer timer;
  190. registry.clear<position>();
  191. timer.elapsed();
  192. }
  193. TEST(Benchmark, ClearMulti) {
  194. entt::registry registry;
  195. std::vector<entt::entity> entities(1000000);
  196. std::cout << "Clearing 1000000 components from their entities" << std::endl;
  197. registry.create(entities.begin(), entities.end());
  198. registry.insert<position>(entities.begin(), entities.end());
  199. registry.insert<velocity>(entities.begin(), entities.end());
  200. timer timer;
  201. registry.clear<position, velocity>();
  202. timer.elapsed();
  203. }
  204. TEST(Benchmark, ClearStable) {
  205. entt::registry registry;
  206. std::vector<entt::entity> entities(1000000);
  207. std::cout << "Clearing 1000000 stable components from their entities" << std::endl;
  208. registry.create(entities.begin(), entities.end());
  209. registry.insert<stable_position>(entities.begin(), entities.end());
  210. timer timer;
  211. registry.clear<stable_position>();
  212. timer.elapsed();
  213. }
  214. TEST(Benchmark, Recycle) {
  215. entt::registry registry;
  216. std::vector<entt::entity> entities(1000000);
  217. std::cout << "Recycling 1000000 entities" << std::endl;
  218. registry.create(entities.begin(), entities.end());
  219. registry.destroy(entities.begin(), entities.end());
  220. timer timer;
  221. for(auto next = entities.size(); next; --next) {
  222. entities[next] = registry.create();
  223. }
  224. timer.elapsed();
  225. }
  226. TEST(Benchmark, RecycleMany) {
  227. entt::registry registry;
  228. std::vector<entt::entity> entities(1000000);
  229. std::cout << "Recycling 1000000 entities" << std::endl;
  230. registry.create(entities.begin(), entities.end());
  231. registry.destroy(entities.begin(), entities.end());
  232. timer timer;
  233. registry.create(entities.begin(), entities.end());
  234. timer.elapsed();
  235. }
  236. TEST(Benchmark, Destroy) {
  237. entt::registry registry;
  238. std::vector<entt::entity> entities(1000000);
  239. auto view = registry.view<position>();
  240. std::cout << "Destroying 1000000 entities" << std::endl;
  241. registry.create(entities.begin(), entities.end());
  242. registry.insert<position>(entities.begin(), entities.end());
  243. timer timer;
  244. for(auto entity: view) {
  245. registry.destroy(entity);
  246. }
  247. timer.elapsed();
  248. }
  249. TEST(Benchmark, DestroyMany) {
  250. entt::registry registry;
  251. std::vector<entt::entity> entities(1000000);
  252. auto view = registry.view<position>();
  253. std::cout << "Destroying 1000000 entities at once" << std::endl;
  254. registry.create(entities.begin(), entities.end());
  255. registry.insert<position>(entities.begin(), entities.end());
  256. timer timer;
  257. registry.destroy(view.begin(), view.end());
  258. timer.elapsed();
  259. }
  260. TEST(Benchmark, IterateSingleComponent1M) {
  261. entt::registry registry;
  262. std::cout << "Iterating over 1000000 entities, one component" << std::endl;
  263. for(std::uint64_t i = 0; i < 1000000L; i++) {
  264. const auto entity = registry.create();
  265. registry.emplace<position>(entity);
  266. }
  267. generic(registry.view<position>(), [](auto &...comp) {
  268. ((comp.x = {}), ...);
  269. });
  270. }
  271. TEST(Benchmark, IterateSingleStableComponent1M) {
  272. entt::registry registry;
  273. std::cout << "Iterating over 1000000 entities, one stable component" << std::endl;
  274. for(std::uint64_t i = 0; i < 1000000L; i++) {
  275. const auto entity = registry.create();
  276. registry.emplace<stable_position>(entity);
  277. }
  278. generic(registry.view<stable_position>(), [](auto &...comp) {
  279. ((comp.x = {}), ...);
  280. });
  281. }
  282. TEST(Benchmark, IterateSingleComponentRuntime1M) {
  283. entt::registry registry;
  284. std::cout << "Iterating over 1000000 entities, one component, runtime view" << std::endl;
  285. for(std::uint64_t i = 0; i < 1000000L; i++) {
  286. const auto entity = registry.create();
  287. registry.emplace<position>(entity);
  288. }
  289. entt::runtime_view view{};
  290. view.iterate(registry.storage<position>());
  291. generic(view, [&registry](auto entity) {
  292. registry.get<position>(entity).x = {};
  293. });
  294. }
  295. TEST(Benchmark, IterateTwoComponents1M) {
  296. entt::registry registry;
  297. std::cout << "Iterating over 1000000 entities, two components" << std::endl;
  298. for(std::uint64_t i = 0; i < 1000000L; i++) {
  299. const auto entity = registry.create();
  300. registry.emplace<position>(entity);
  301. registry.emplace<velocity>(entity);
  302. }
  303. generic(registry.view<position, velocity>(), [](auto &...comp) {
  304. ((comp.x = {}), ...);
  305. });
  306. }
  307. TEST(Benchmark, IterateTwoStableComponents1M) {
  308. entt::registry registry;
  309. std::cout << "Iterating over 1000000 entities, two stable components" << std::endl;
  310. for(std::uint64_t i = 0; i < 1000000L; i++) {
  311. const auto entity = registry.create();
  312. registry.emplace<stable_position>(entity);
  313. registry.emplace<velocity>(entity);
  314. }
  315. generic(registry.view<stable_position, velocity>(), [](auto &...comp) {
  316. ((comp.x = {}), ...);
  317. });
  318. }
  319. TEST(Benchmark, IterateTwoComponents1MHalf) {
  320. entt::registry registry;
  321. std::cout << "Iterating over 1000000 entities, two components, half of the entities have all the components" << std::endl;
  322. for(std::uint64_t i = 0; i < 1000000L; i++) {
  323. const auto entity = registry.create();
  324. registry.emplace<velocity>(entity);
  325. if(i % 2) {
  326. registry.emplace<position>(entity);
  327. }
  328. }
  329. generic(registry.view<position, velocity>(), [](auto &...comp) {
  330. ((comp.x = {}), ...);
  331. });
  332. }
  333. TEST(Benchmark, IterateTwoComponents1MOne) {
  334. entt::registry registry;
  335. std::cout << "Iterating over 1000000 entities, two components, only one entity has all the components" << std::endl;
  336. for(std::uint64_t i = 0; i < 1000000L; i++) {
  337. const auto entity = registry.create();
  338. registry.emplace<velocity>(entity);
  339. if(i == 500000L) {
  340. registry.emplace<position>(entity);
  341. }
  342. }
  343. generic(registry.view<position, velocity>(), [](auto &...comp) {
  344. ((comp.x = {}), ...);
  345. });
  346. }
  347. TEST(Benchmark, IterateTwoComponentsNonOwningGroup1M) {
  348. entt::registry registry;
  349. std::cout << "Iterating over 1000000 entities, two components, non owning group" << std::endl;
  350. for(std::uint64_t i = 0; i < 1000000L; i++) {
  351. const auto entity = registry.create();
  352. registry.emplace<position>(entity);
  353. registry.emplace<velocity>(entity);
  354. }
  355. generic(registry.group<>(entt::get<position, velocity>), [](auto &...comp) {
  356. ((comp.x = {}), ...);
  357. });
  358. }
  359. TEST(Benchmark, IterateTwoComponentsFullOwningGroup1M) {
  360. entt::registry registry;
  361. std::cout << "Iterating over 1000000 entities, two components, full owning group" << std::endl;
  362. for(std::uint64_t i = 0; i < 1000000L; i++) {
  363. const auto entity = registry.create();
  364. registry.emplace<position>(entity);
  365. registry.emplace<velocity>(entity);
  366. }
  367. generic(registry.group<position, velocity>(), [](auto &...comp) {
  368. ((comp.x = {}), ...);
  369. });
  370. }
  371. TEST(Benchmark, IterateTwoComponentsPartialOwningGroup1M) {
  372. entt::registry registry;
  373. std::cout << "Iterating over 1000000 entities, two components, partial owning group" << std::endl;
  374. for(std::uint64_t i = 0; i < 1000000L; i++) {
  375. const auto entity = registry.create();
  376. registry.emplace<position>(entity);
  377. registry.emplace<velocity>(entity);
  378. }
  379. generic(registry.group<position>(entt::get<velocity>), [](auto &...comp) {
  380. ((comp.x = {}), ...);
  381. });
  382. }
  383. TEST(Benchmark, IterateTwoComponentsRuntime1M) {
  384. entt::registry registry;
  385. std::cout << "Iterating over 1000000 entities, two components, runtime view" << std::endl;
  386. for(std::uint64_t i = 0; i < 1000000L; i++) {
  387. const auto entity = registry.create();
  388. registry.emplace<position>(entity);
  389. registry.emplace<velocity>(entity);
  390. }
  391. entt::runtime_view view{};
  392. view.iterate(registry.storage<position>())
  393. .iterate(registry.storage<velocity>());
  394. generic(view, [&registry](auto entity) {
  395. registry.get<position>(entity).x = {};
  396. registry.get<velocity>(entity).x = {};
  397. });
  398. }
  399. TEST(Benchmark, IterateTwoComponentsRuntime1MHalf) {
  400. entt::registry registry;
  401. std::cout << "Iterating over 1000000 entities, two components, half of the entities have all the components, runtime view" << std::endl;
  402. for(std::uint64_t i = 0; i < 1000000L; i++) {
  403. const auto entity = registry.create();
  404. registry.emplace<velocity>(entity);
  405. if(i % 2) {
  406. registry.emplace<position>(entity);
  407. }
  408. }
  409. entt::runtime_view view{};
  410. view.iterate(registry.storage<position>())
  411. .iterate(registry.storage<velocity>());
  412. generic(view, [&registry](auto entity) {
  413. registry.get<position>(entity).x = {};
  414. registry.get<velocity>(entity).x = {};
  415. });
  416. }
  417. TEST(Benchmark, IterateTwoComponentsRuntime1MOne) {
  418. entt::registry registry;
  419. std::cout << "Iterating over 1000000 entities, two components, only one entity has all the components, runtime view" << std::endl;
  420. for(std::uint64_t i = 0; i < 1000000L; i++) {
  421. const auto entity = registry.create();
  422. registry.emplace<velocity>(entity);
  423. if(i == 500000L) {
  424. registry.emplace<position>(entity);
  425. }
  426. }
  427. entt::runtime_view view{};
  428. view.iterate(registry.storage<position>())
  429. .iterate(registry.storage<velocity>());
  430. generic(view, [&registry](auto entity) {
  431. registry.get<position>(entity).x = {};
  432. registry.get<velocity>(entity).x = {};
  433. });
  434. }
  435. TEST(Benchmark, IterateThreeComponents1M) {
  436. entt::registry registry;
  437. std::cout << "Iterating over 1000000 entities, three components" << std::endl;
  438. for(std::uint64_t i = 0; i < 1000000L; i++) {
  439. const auto entity = registry.create();
  440. registry.emplace<position>(entity);
  441. registry.emplace<velocity>(entity);
  442. registry.emplace<comp<0>>(entity);
  443. }
  444. generic(registry.view<position, velocity, comp<0>>(), [](auto &...comp) {
  445. ((comp.x = {}), ...);
  446. });
  447. }
  448. TEST(Benchmark, IterateThreeStableComponents1M) {
  449. entt::registry registry;
  450. std::cout << "Iterating over 1000000 entities, three stable components" << std::endl;
  451. for(std::uint64_t i = 0; i < 1000000L; i++) {
  452. const auto entity = registry.create();
  453. registry.emplace<stable_position>(entity);
  454. registry.emplace<velocity>(entity);
  455. registry.emplace<comp<0>>(entity);
  456. }
  457. generic(registry.view<stable_position, velocity, comp<0>>(), [](auto &...comp) {
  458. ((comp.x = {}), ...);
  459. });
  460. }
  461. TEST(Benchmark, IterateThreeComponents1MHalf) {
  462. entt::registry registry;
  463. std::cout << "Iterating over 1000000 entities, three components, half of the entities have all the components" << std::endl;
  464. for(std::uint64_t i = 0; i < 1000000L; i++) {
  465. const auto entity = registry.create();
  466. registry.emplace<velocity>(entity);
  467. registry.emplace<comp<0>>(entity);
  468. if(i % 2) {
  469. registry.emplace<position>(entity);
  470. }
  471. }
  472. generic(registry.view<position, velocity, comp<0>>(), [](auto &...comp) {
  473. ((comp.x = {}), ...);
  474. });
  475. }
  476. TEST(Benchmark, IterateThreeComponents1MOne) {
  477. entt::registry registry;
  478. std::cout << "Iterating over 1000000 entities, three components, only one entity has all the components" << std::endl;
  479. for(std::uint64_t i = 0; i < 1000000L; i++) {
  480. const auto entity = registry.create();
  481. registry.emplace<velocity>(entity);
  482. registry.emplace<comp<0>>(entity);
  483. if(i == 500000L) {
  484. registry.emplace<position>(entity);
  485. }
  486. }
  487. generic(registry.view<position, velocity, comp<0>>(), [](auto &...comp) {
  488. ((comp.x = {}), ...);
  489. });
  490. }
  491. TEST(Benchmark, IterateThreeComponentsNonOwningGroup1M) {
  492. entt::registry registry;
  493. std::cout << "Iterating over 1000000 entities, three components, non owning group" << std::endl;
  494. for(std::uint64_t i = 0; i < 1000000L; i++) {
  495. const auto entity = registry.create();
  496. registry.emplace<position>(entity);
  497. registry.emplace<velocity>(entity);
  498. registry.emplace<comp<0>>(entity);
  499. }
  500. generic(registry.group<>(entt::get<position, velocity, comp<0>>), [](auto &...comp) {
  501. ((comp.x = {}), ...);
  502. });
  503. }
  504. TEST(Benchmark, IterateThreeComponentsFullOwningGroup1M) {
  505. entt::registry registry;
  506. std::cout << "Iterating over 1000000 entities, three components, full owning group" << std::endl;
  507. for(std::uint64_t i = 0; i < 1000000L; i++) {
  508. const auto entity = registry.create();
  509. registry.emplace<position>(entity);
  510. registry.emplace<velocity>(entity);
  511. registry.emplace<comp<0>>(entity);
  512. }
  513. generic(registry.group<position, velocity, comp<0>>(), [](auto &...comp) {
  514. ((comp.x = {}), ...);
  515. });
  516. }
  517. TEST(Benchmark, IterateThreeComponentsPartialOwningGroup1M) {
  518. entt::registry registry;
  519. std::cout << "Iterating over 1000000 entities, three components, partial owning group" << std::endl;
  520. for(std::uint64_t i = 0; i < 1000000L; i++) {
  521. const auto entity = registry.create();
  522. registry.emplace<position>(entity);
  523. registry.emplace<velocity>(entity);
  524. registry.emplace<comp<0>>(entity);
  525. }
  526. generic(registry.group<position, velocity>(entt::get<comp<0>>), [](auto &...comp) {
  527. ((comp.x = {}), ...);
  528. });
  529. }
  530. TEST(Benchmark, IterateThreeComponentsRuntime1M) {
  531. entt::registry registry;
  532. std::cout << "Iterating over 1000000 entities, three components, runtime view" << std::endl;
  533. for(std::uint64_t i = 0; i < 1000000L; i++) {
  534. const auto entity = registry.create();
  535. registry.emplace<position>(entity);
  536. registry.emplace<velocity>(entity);
  537. registry.emplace<comp<0>>(entity);
  538. }
  539. entt::runtime_view view{};
  540. view.iterate(registry.storage<position>())
  541. .iterate(registry.storage<velocity>())
  542. .iterate(registry.storage<comp<0>>());
  543. generic(view, [&registry](auto entity) {
  544. registry.get<position>(entity).x = {};
  545. registry.get<velocity>(entity).x = {};
  546. registry.get<comp<0>>(entity).x = {};
  547. });
  548. }
  549. TEST(Benchmark, IterateThreeComponentsRuntime1MHalf) {
  550. entt::registry registry;
  551. std::cout << "Iterating over 1000000 entities, three components, half of the entities have all the components, runtime view" << std::endl;
  552. for(std::uint64_t i = 0; i < 1000000L; i++) {
  553. const auto entity = registry.create();
  554. registry.emplace<velocity>(entity);
  555. registry.emplace<comp<0>>(entity);
  556. if(i % 2) {
  557. registry.emplace<position>(entity);
  558. }
  559. }
  560. entt::runtime_view view{};
  561. view.iterate(registry.storage<position>())
  562. .iterate(registry.storage<velocity>())
  563. .iterate(registry.storage<comp<0>>());
  564. generic(view, [&registry](auto entity) {
  565. registry.get<position>(entity).x = {};
  566. registry.get<velocity>(entity).x = {};
  567. registry.get<comp<0>>(entity).x = {};
  568. });
  569. }
  570. TEST(Benchmark, IterateThreeComponentsRuntime1MOne) {
  571. entt::registry registry;
  572. std::cout << "Iterating over 1000000 entities, three components, only one entity has all the components, runtime view" << std::endl;
  573. for(std::uint64_t i = 0; i < 1000000L; i++) {
  574. const auto entity = registry.create();
  575. registry.emplace<velocity>(entity);
  576. registry.emplace<comp<0>>(entity);
  577. if(i == 500000L) {
  578. registry.emplace<position>(entity);
  579. }
  580. }
  581. entt::runtime_view view{};
  582. view.iterate(registry.storage<position>())
  583. .iterate(registry.storage<velocity>())
  584. .iterate(registry.storage<comp<0>>());
  585. generic(view, [&registry](auto entity) {
  586. registry.get<position>(entity).x = {};
  587. registry.get<velocity>(entity).x = {};
  588. registry.get<comp<0>>(entity).x = {};
  589. });
  590. }
  591. TEST(Benchmark, IterateFiveComponents1M) {
  592. entt::registry registry;
  593. std::cout << "Iterating over 1000000 entities, five components" << std::endl;
  594. for(std::uint64_t i = 0; i < 1000000L; i++) {
  595. const auto entity = registry.create();
  596. registry.emplace<position>(entity);
  597. registry.emplace<velocity>(entity);
  598. registry.emplace<comp<0>>(entity);
  599. registry.emplace<comp<1>>(entity);
  600. registry.emplace<comp<2>>(entity);
  601. }
  602. generic(registry.view<position, velocity, comp<0>, comp<1>, comp<2>>(), [](auto &...comp) {
  603. ((comp.x = {}), ...);
  604. });
  605. }
  606. TEST(Benchmark, IterateFiveStableComponents1M) {
  607. entt::registry registry;
  608. std::cout << "Iterating over 1000000 entities, five stable components" << std::endl;
  609. for(std::uint64_t i = 0; i < 1000000L; i++) {
  610. const auto entity = registry.create();
  611. registry.emplace<stable_position>(entity);
  612. registry.emplace<velocity>(entity);
  613. registry.emplace<comp<0>>(entity);
  614. registry.emplace<comp<1>>(entity);
  615. registry.emplace<comp<2>>(entity);
  616. }
  617. generic(registry.view<stable_position, velocity, comp<0>, comp<1>, comp<2>>(), [](auto &...comp) {
  618. ((comp.x = {}), ...);
  619. });
  620. }
  621. TEST(Benchmark, IterateFiveComponents1MHalf) {
  622. entt::registry registry;
  623. std::cout << "Iterating over 1000000 entities, five components, half of the entities have all the components" << std::endl;
  624. for(std::uint64_t i = 0; i < 1000000L; i++) {
  625. const auto entity = registry.create();
  626. registry.emplace<velocity>(entity);
  627. registry.emplace<comp<0>>(entity);
  628. registry.emplace<comp<1>>(entity);
  629. registry.emplace<comp<2>>(entity);
  630. if(i % 2) {
  631. registry.emplace<position>(entity);
  632. }
  633. }
  634. generic(registry.view<position, velocity, comp<0>, comp<1>, comp<2>>(), [](auto &...comp) {
  635. ((comp.x = {}), ...);
  636. });
  637. }
  638. TEST(Benchmark, IterateFiveComponents1MOne) {
  639. entt::registry registry;
  640. std::cout << "Iterating over 1000000 entities, five components, only one entity has all the components" << std::endl;
  641. for(std::uint64_t i = 0; i < 1000000L; i++) {
  642. const auto entity = registry.create();
  643. registry.emplace<velocity>(entity);
  644. registry.emplace<comp<0>>(entity);
  645. registry.emplace<comp<1>>(entity);
  646. registry.emplace<comp<2>>(entity);
  647. if(i == 500000L) {
  648. registry.emplace<position>(entity);
  649. }
  650. }
  651. generic(registry.view<position, velocity, comp<0>, comp<1>, comp<2>>(), [](auto &...comp) {
  652. ((comp.x = {}), ...);
  653. });
  654. }
  655. TEST(Benchmark, IterateFiveComponentsNonOwningGroup1M) {
  656. entt::registry registry;
  657. std::cout << "Iterating over 1000000 entities, five components, non owning group" << std::endl;
  658. for(std::uint64_t i = 0; i < 1000000L; i++) {
  659. const auto entity = registry.create();
  660. registry.emplace<position>(entity);
  661. registry.emplace<velocity>(entity);
  662. registry.emplace<comp<0>>(entity);
  663. registry.emplace<comp<1>>(entity);
  664. registry.emplace<comp<2>>(entity);
  665. }
  666. generic(registry.group<>(entt::get<position, velocity, comp<0>, comp<1>, comp<2>>), [](auto &...comp) {
  667. ((comp.x = {}), ...);
  668. });
  669. }
  670. TEST(Benchmark, IterateFiveComponentsFullOwningGroup1M) {
  671. entt::registry registry;
  672. std::cout << "Iterating over 1000000 entities, five components, full owning group" << std::endl;
  673. for(std::uint64_t i = 0; i < 1000000L; i++) {
  674. const auto entity = registry.create();
  675. registry.emplace<position>(entity);
  676. registry.emplace<velocity>(entity);
  677. registry.emplace<comp<0>>(entity);
  678. registry.emplace<comp<1>>(entity);
  679. registry.emplace<comp<2>>(entity);
  680. }
  681. generic(registry.group<position, velocity, comp<0>, comp<1>, comp<2>>(), [](auto &...comp) {
  682. ((comp.x = {}), ...);
  683. });
  684. }
  685. TEST(Benchmark, IterateFiveComponentsPartialFourOfFiveOwningGroup1M) {
  686. entt::registry registry;
  687. std::cout << "Iterating over 1000000 entities, five components, partial (4 of 5) owning group" << std::endl;
  688. for(std::uint64_t i = 0; i < 1000000L; i++) {
  689. const auto entity = registry.create();
  690. registry.emplace<position>(entity);
  691. registry.emplace<velocity>(entity);
  692. registry.emplace<comp<0>>(entity);
  693. registry.emplace<comp<1>>(entity);
  694. registry.emplace<comp<2>>(entity);
  695. }
  696. generic(registry.group<position, velocity, comp<0>, comp<1>>(entt::get<comp<2>>), [](auto &...comp) {
  697. ((comp.x = {}), ...);
  698. });
  699. }
  700. TEST(Benchmark, IterateFiveComponentsPartialThreeOfFiveOwningGroup1M) {
  701. entt::registry registry;
  702. std::cout << "Iterating over 1000000 entities, five components, partial (3 of 5) owning group" << std::endl;
  703. for(std::uint64_t i = 0; i < 1000000L; i++) {
  704. const auto entity = registry.create();
  705. registry.emplace<position>(entity);
  706. registry.emplace<velocity>(entity);
  707. registry.emplace<comp<0>>(entity);
  708. registry.emplace<comp<1>>(entity);
  709. registry.emplace<comp<2>>(entity);
  710. }
  711. generic(registry.group<position, velocity, comp<0>>(entt::get<comp<1>, comp<2>>), [](auto &...comp) {
  712. ((comp.x = {}), ...);
  713. });
  714. }
  715. TEST(Benchmark, IterateFiveComponentsRuntime1M) {
  716. entt::registry registry;
  717. std::cout << "Iterating over 1000000 entities, five components, runtime view" << std::endl;
  718. for(std::uint64_t i = 0; i < 1000000L; i++) {
  719. const auto entity = registry.create();
  720. registry.emplace<position>(entity);
  721. registry.emplace<velocity>(entity);
  722. registry.emplace<comp<0>>(entity);
  723. registry.emplace<comp<1>>(entity);
  724. registry.emplace<comp<2>>(entity);
  725. }
  726. entt::runtime_view view{};
  727. view.iterate(registry.storage<position>())
  728. .iterate(registry.storage<velocity>())
  729. .iterate(registry.storage<comp<0>>())
  730. .iterate(registry.storage<comp<1>>())
  731. .iterate(registry.storage<comp<2>>());
  732. generic(view, [&registry](auto entity) {
  733. registry.get<position>(entity).x = {};
  734. registry.get<velocity>(entity).x = {};
  735. registry.get<comp<0>>(entity).x = {};
  736. registry.get<comp<1>>(entity).x = {};
  737. registry.get<comp<2>>(entity).x = {};
  738. });
  739. }
  740. TEST(Benchmark, IterateFiveComponentsRuntime1MHalf) {
  741. entt::registry registry;
  742. std::cout << "Iterating over 1000000 entities, five components, half of the entities have all the components, runtime view" << std::endl;
  743. for(std::uint64_t i = 0; i < 1000000L; i++) {
  744. const auto entity = registry.create();
  745. registry.emplace<velocity>(entity);
  746. registry.emplace<comp<0>>(entity);
  747. registry.emplace<comp<1>>(entity);
  748. registry.emplace<comp<2>>(entity);
  749. if(i % 2) {
  750. registry.emplace<position>(entity);
  751. }
  752. }
  753. entt::runtime_view view{};
  754. view.iterate(registry.storage<position>())
  755. .iterate(registry.storage<velocity>())
  756. .iterate(registry.storage<comp<0>>())
  757. .iterate(registry.storage<comp<1>>())
  758. .iterate(registry.storage<comp<2>>());
  759. generic(view, [&registry](auto entity) {
  760. registry.get<position>(entity).x = {};
  761. registry.get<velocity>(entity).x = {};
  762. registry.get<comp<0>>(entity).x = {};
  763. registry.get<comp<1>>(entity).x = {};
  764. registry.get<comp<2>>(entity).x = {};
  765. });
  766. }
  767. TEST(Benchmark, IterateFiveComponentsRuntime1MOne) {
  768. entt::registry registry;
  769. std::cout << "Iterating over 1000000 entities, five components, only one entity has all the components, runtime view" << std::endl;
  770. for(std::uint64_t i = 0; i < 1000000L; i++) {
  771. const auto entity = registry.create();
  772. registry.emplace<velocity>(entity);
  773. registry.emplace<comp<0>>(entity);
  774. registry.emplace<comp<1>>(entity);
  775. registry.emplace<comp<2>>(entity);
  776. if(i == 500000L) {
  777. registry.emplace<position>(entity);
  778. }
  779. }
  780. entt::runtime_view view{};
  781. view.iterate(registry.storage<position>())
  782. .iterate(registry.storage<velocity>())
  783. .iterate(registry.storage<comp<0>>())
  784. .iterate(registry.storage<comp<1>>())
  785. .iterate(registry.storage<comp<2>>());
  786. generic(view, [&registry](auto entity) {
  787. registry.get<position>(entity).x = {};
  788. registry.get<velocity>(entity).x = {};
  789. registry.get<comp<0>>(entity).x = {};
  790. registry.get<comp<1>>(entity).x = {};
  791. registry.get<comp<2>>(entity).x = {};
  792. });
  793. }
  794. TEST(Benchmark, IteratePathological) {
  795. std::cout << "Pathological case" << std::endl;
  796. pathological([](auto &registry) { return registry.template view<position, velocity, comp<0>>(); });
  797. }
  798. TEST(Benchmark, IteratePathologicalNonOwningGroup) {
  799. std::cout << "Pathological case (non-owning group)" << std::endl;
  800. pathological([](auto &registry) { return registry.template group<>(entt::get<position, velocity, comp<0>>); });
  801. }
  802. TEST(Benchmark, IteratePathologicalFullOwningGroup) {
  803. std::cout << "Pathological case (full-owning group)" << std::endl;
  804. pathological([](auto &registry) { return registry.template group<position, velocity, comp<0>>(); });
  805. }
  806. TEST(Benchmark, IteratePathologicalPartialOwningGroup) {
  807. std::cout << "Pathological case (partial-owning group)" << std::endl;
  808. pathological([](auto &registry) { return registry.template group<position, velocity>(entt::get<comp<0>>); });
  809. }
  810. TEST(Benchmark, SortSingle) {
  811. entt::registry registry;
  812. std::cout << "Sort 150000 entities, one component" << std::endl;
  813. for(std::uint64_t i = 0; i < 150000L; i++) {
  814. const auto entity = registry.create();
  815. registry.emplace<position>(entity, i, i);
  816. }
  817. timer timer;
  818. registry.sort<position>([](const auto &lhs, const auto &rhs) { return lhs.x < rhs.x && lhs.y < rhs.y; });
  819. timer.elapsed();
  820. }
  821. TEST(Benchmark, SortMulti) {
  822. entt::registry registry;
  823. std::cout << "Sort 150000 entities, two components" << std::endl;
  824. for(std::uint64_t i = 0; i < 150000L; i++) {
  825. const auto entity = registry.create();
  826. registry.emplace<position>(entity, i, i);
  827. registry.emplace<velocity>(entity, i, i);
  828. }
  829. registry.sort<position>([](const auto &lhs, const auto &rhs) { return lhs.x < rhs.x && lhs.y < rhs.y; });
  830. timer timer;
  831. registry.sort<velocity, position>();
  832. timer.elapsed();
  833. }
  834. TEST(Benchmark, AlmostSortedStdSort) {
  835. entt::registry registry;
  836. entt::entity entities[3]{};
  837. std::cout << "Sort 150000 entities, almost sorted, std::sort" << std::endl;
  838. for(std::uint64_t i = 0; i < 150000L; i++) {
  839. const auto entity = registry.create();
  840. registry.emplace<position>(entity, i, i);
  841. if(!(i % 50000)) {
  842. entities[i / 50000] = entity;
  843. }
  844. }
  845. for(std::uint64_t i = 0; i < 3; ++i) {
  846. registry.destroy(entities[i]);
  847. const auto entity = registry.create();
  848. registry.emplace<position>(entity, 50000 * i, 50000 * i);
  849. }
  850. timer timer;
  851. registry.sort<position>([](const auto &lhs, const auto &rhs) { return lhs.x > rhs.x && lhs.y > rhs.y; });
  852. timer.elapsed();
  853. }
  854. TEST(Benchmark, AlmostSortedInsertionSort) {
  855. entt::registry registry;
  856. entt::entity entities[3]{};
  857. std::cout << "Sort 150000 entities, almost sorted, insertion sort" << std::endl;
  858. for(std::uint64_t i = 0; i < 150000L; i++) {
  859. const auto entity = registry.create();
  860. registry.emplace<position>(entity, i, i);
  861. if(!(i % 50000)) {
  862. entities[i / 50000] = entity;
  863. }
  864. }
  865. for(std::uint64_t i = 0; i < 3; ++i) {
  866. registry.destroy(entities[i]);
  867. const auto entity = registry.create();
  868. registry.emplace<position>(entity, 50000 * i, 50000 * i);
  869. }
  870. timer timer;
  871. registry.sort<position>([](const auto &lhs, const auto &rhs) { return lhs.x > rhs.x && lhs.y > rhs.y; }, entt::insertion_sort{});
  872. timer.elapsed();
  873. }