benchmark.cpp 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087
  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<int>();
  115. std::cout << "Erasing 1000000 components from their entities" << std::endl;
  116. registry.create(entities.begin(), entities.end());
  117. registry.insert<int>(entities.begin(), entities.end());
  118. timer timer;
  119. for(auto entity: view) {
  120. registry.erase<int>(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<int>();
  128. std::cout << "Erasing 1000000 components from their entities at once" << std::endl;
  129. registry.create(entities.begin(), entities.end());
  130. registry.insert<int>(entities.begin(), entities.end());
  131. timer timer;
  132. registry.erase<int>(view.begin(), view.end());
  133. timer.elapsed();
  134. }
  135. TEST(Benchmark, Remove) {
  136. entt::registry registry;
  137. std::vector<entt::entity> entities(1000000);
  138. auto view = registry.view<int>();
  139. std::cout << "Removing 1000000 components from their entities" << std::endl;
  140. registry.create(entities.begin(), entities.end());
  141. registry.insert<int>(entities.begin(), entities.end());
  142. timer timer;
  143. for(auto entity: view) {
  144. registry.remove<int>(entity);
  145. }
  146. timer.elapsed();
  147. }
  148. TEST(Benchmark, RemoveMany) {
  149. entt::registry registry;
  150. std::vector<entt::entity> entities(1000000);
  151. auto view = registry.view<int>();
  152. std::cout << "Removing 1000000 components from their entities at once" << std::endl;
  153. registry.create(entities.begin(), entities.end());
  154. registry.insert<int>(entities.begin(), entities.end());
  155. timer timer;
  156. registry.remove<int>(view.begin(), view.end());
  157. timer.elapsed();
  158. }
  159. TEST(Benchmark, Clear) {
  160. entt::registry registry;
  161. std::vector<entt::entity> entities(1000000);
  162. std::cout << "Clearing 1000000 components from their entities" << std::endl;
  163. registry.create(entities.begin(), entities.end());
  164. registry.insert<int>(entities.begin(), entities.end());
  165. timer timer;
  166. registry.clear<int>();
  167. timer.elapsed();
  168. }
  169. TEST(Benchmark, Recycle) {
  170. entt::registry registry;
  171. std::vector<entt::entity> entities(1000000);
  172. std::cout << "Recycling 1000000 entities" << std::endl;
  173. registry.create(entities.begin(), entities.end());
  174. registry.destroy(entities.begin(), entities.end());
  175. timer timer;
  176. for(auto next = entities.size(); next; --next) {
  177. entities[next] = registry.create();
  178. }
  179. timer.elapsed();
  180. }
  181. TEST(Benchmark, RecycleMany) {
  182. entt::registry registry;
  183. std::vector<entt::entity> entities(1000000);
  184. std::cout << "Recycling 1000000 entities" << std::endl;
  185. registry.create(entities.begin(), entities.end());
  186. registry.destroy(entities.begin(), entities.end());
  187. timer timer;
  188. registry.create(entities.begin(), entities.end());
  189. timer.elapsed();
  190. }
  191. TEST(Benchmark, Destroy) {
  192. entt::registry registry;
  193. std::vector<entt::entity> entities(1000000);
  194. auto view = registry.view<int>();
  195. std::cout << "Destroying 1000000 entities" << std::endl;
  196. registry.create(entities.begin(), entities.end());
  197. registry.insert<int>(entities.begin(), entities.end());
  198. timer timer;
  199. for(auto entity: view) {
  200. registry.destroy(entity);
  201. }
  202. timer.elapsed();
  203. }
  204. TEST(Benchmark, DestroyMany) {
  205. entt::registry registry;
  206. std::vector<entt::entity> entities(1000000);
  207. auto view = registry.view<int>();
  208. std::cout << "Destroying 1000000 entities at once" << std::endl;
  209. registry.create(entities.begin(), entities.end());
  210. registry.insert<int>(entities.begin(), entities.end());
  211. timer timer;
  212. registry.destroy(view.begin(), view.end());
  213. timer.elapsed();
  214. }
  215. TEST(Benchmark, IterateSingleComponent1M) {
  216. entt::registry registry;
  217. std::cout << "Iterating over 1000000 entities, one component" << std::endl;
  218. for(std::uint64_t i = 0; i < 1000000L; i++) {
  219. const auto entity = registry.create();
  220. registry.emplace<position>(entity);
  221. }
  222. generic(registry.view<position>(), [](auto &...comp) {
  223. ((comp.x = {}), ...);
  224. });
  225. }
  226. TEST(Benchmark, IterateSingleComponentTombstonePolicy1M) {
  227. entt::registry registry;
  228. std::cout << "Iterating over 1000000 entities, one component, tombstone policy" << std::endl;
  229. for(std::uint64_t i = 0; i < 1000000L; i++) {
  230. const auto entity = registry.create();
  231. registry.emplace<stable_position>(entity);
  232. }
  233. generic(registry.view<stable_position>(), [](auto &...comp) {
  234. ((comp.x = {}), ...);
  235. });
  236. }
  237. TEST(Benchmark, IterateSingleComponentRuntime1M) {
  238. entt::registry registry;
  239. std::cout << "Iterating over 1000000 entities, one component, runtime view" << std::endl;
  240. for(std::uint64_t i = 0; i < 1000000L; i++) {
  241. const auto entity = registry.create();
  242. registry.emplace<position>(entity);
  243. }
  244. entt::runtime_view view{};
  245. view.iterate(registry.storage<position>());
  246. generic(view, [&registry](auto entity) {
  247. registry.get<position>(entity).x = {};
  248. });
  249. }
  250. TEST(Benchmark, IterateTwoComponents1M) {
  251. entt::registry registry;
  252. std::cout << "Iterating over 1000000 entities, two components" << std::endl;
  253. for(std::uint64_t i = 0; i < 1000000L; i++) {
  254. const auto entity = registry.create();
  255. registry.emplace<position>(entity);
  256. registry.emplace<velocity>(entity);
  257. }
  258. generic(registry.view<position, velocity>(), [](auto &...comp) {
  259. ((comp.x = {}), ...);
  260. });
  261. }
  262. TEST(Benchmark, IterateTombstonePolicyTwoComponentsTombstonePolicy1M) {
  263. entt::registry registry;
  264. std::cout << "Iterating over 1000000 entities, two components, tombstone policy" << std::endl;
  265. for(std::uint64_t i = 0; i < 1000000L; i++) {
  266. const auto entity = registry.create();
  267. registry.emplace<stable_position>(entity);
  268. registry.emplace<velocity>(entity);
  269. }
  270. generic(registry.view<stable_position, velocity>(), [](auto &...comp) {
  271. ((comp.x = {}), ...);
  272. });
  273. }
  274. TEST(Benchmark, IterateTwoComponents1MHalf) {
  275. entt::registry registry;
  276. std::cout << "Iterating over 1000000 entities, two components, half of the entities have all the components" << std::endl;
  277. for(std::uint64_t i = 0; i < 1000000L; i++) {
  278. const auto entity = registry.create();
  279. registry.emplace<velocity>(entity);
  280. if(i % 2) {
  281. registry.emplace<position>(entity);
  282. }
  283. }
  284. generic(registry.view<position, velocity>(), [](auto &...comp) {
  285. ((comp.x = {}), ...);
  286. });
  287. }
  288. TEST(Benchmark, IterateTwoComponents1MOne) {
  289. entt::registry registry;
  290. std::cout << "Iterating over 1000000 entities, two components, only one entity has all the components" << std::endl;
  291. for(std::uint64_t i = 0; i < 1000000L; i++) {
  292. const auto entity = registry.create();
  293. registry.emplace<velocity>(entity);
  294. if(i == 500000L) {
  295. registry.emplace<position>(entity);
  296. }
  297. }
  298. generic(registry.view<position, velocity>(), [](auto &...comp) {
  299. ((comp.x = {}), ...);
  300. });
  301. }
  302. TEST(Benchmark, IterateTwoComponentsNonOwningGroup1M) {
  303. entt::registry registry;
  304. std::cout << "Iterating over 1000000 entities, two components, non owning group" << std::endl;
  305. for(std::uint64_t i = 0; i < 1000000L; i++) {
  306. const auto entity = registry.create();
  307. registry.emplace<position>(entity);
  308. registry.emplace<velocity>(entity);
  309. }
  310. generic(registry.group<>(entt::get<position, velocity>), [](auto &...comp) {
  311. ((comp.x = {}), ...);
  312. });
  313. }
  314. TEST(Benchmark, IterateTwoComponentsFullOwningGroup1M) {
  315. entt::registry registry;
  316. std::cout << "Iterating over 1000000 entities, two components, full owning group" << std::endl;
  317. for(std::uint64_t i = 0; i < 1000000L; i++) {
  318. const auto entity = registry.create();
  319. registry.emplace<position>(entity);
  320. registry.emplace<velocity>(entity);
  321. }
  322. generic(registry.group<position, velocity>(), [](auto &...comp) {
  323. ((comp.x = {}), ...);
  324. });
  325. }
  326. TEST(Benchmark, IterateTwoComponentsPartialOwningGroup1M) {
  327. entt::registry registry;
  328. std::cout << "Iterating over 1000000 entities, two components, partial owning group" << std::endl;
  329. for(std::uint64_t i = 0; i < 1000000L; i++) {
  330. const auto entity = registry.create();
  331. registry.emplace<position>(entity);
  332. registry.emplace<velocity>(entity);
  333. }
  334. generic(registry.group<position>(entt::get<velocity>), [](auto &...comp) {
  335. ((comp.x = {}), ...);
  336. });
  337. }
  338. TEST(Benchmark, IterateTwoComponentsRuntime1M) {
  339. entt::registry registry;
  340. std::cout << "Iterating over 1000000 entities, two components, runtime view" << std::endl;
  341. for(std::uint64_t i = 0; i < 1000000L; i++) {
  342. const auto entity = registry.create();
  343. registry.emplace<position>(entity);
  344. registry.emplace<velocity>(entity);
  345. }
  346. entt::runtime_view view{};
  347. view.iterate(registry.storage<position>())
  348. .iterate(registry.storage<velocity>());
  349. generic(view, [&registry](auto entity) {
  350. registry.get<position>(entity).x = {};
  351. registry.get<velocity>(entity).x = {};
  352. });
  353. }
  354. TEST(Benchmark, IterateTwoComponentsRuntime1MHalf) {
  355. entt::registry registry;
  356. std::cout << "Iterating over 1000000 entities, two components, half of the entities have all the components, runtime view" << std::endl;
  357. for(std::uint64_t i = 0; i < 1000000L; i++) {
  358. const auto entity = registry.create();
  359. registry.emplace<velocity>(entity);
  360. if(i % 2) {
  361. registry.emplace<position>(entity);
  362. }
  363. }
  364. entt::runtime_view view{};
  365. view.iterate(registry.storage<position>())
  366. .iterate(registry.storage<velocity>());
  367. generic(view, [&registry](auto entity) {
  368. registry.get<position>(entity).x = {};
  369. registry.get<velocity>(entity).x = {};
  370. });
  371. }
  372. TEST(Benchmark, IterateTwoComponentsRuntime1MOne) {
  373. entt::registry registry;
  374. std::cout << "Iterating over 1000000 entities, two components, only one entity has all the components, runtime view" << std::endl;
  375. for(std::uint64_t i = 0; i < 1000000L; i++) {
  376. const auto entity = registry.create();
  377. registry.emplace<velocity>(entity);
  378. if(i == 500000L) {
  379. registry.emplace<position>(entity);
  380. }
  381. }
  382. entt::runtime_view view{};
  383. view.iterate(registry.storage<position>())
  384. .iterate(registry.storage<velocity>());
  385. generic(view, [&registry](auto entity) {
  386. registry.get<position>(entity).x = {};
  387. registry.get<velocity>(entity).x = {};
  388. });
  389. }
  390. TEST(Benchmark, IterateThreeComponents1M) {
  391. entt::registry registry;
  392. std::cout << "Iterating over 1000000 entities, three components" << std::endl;
  393. for(std::uint64_t i = 0; i < 1000000L; i++) {
  394. const auto entity = registry.create();
  395. registry.emplace<position>(entity);
  396. registry.emplace<velocity>(entity);
  397. registry.emplace<comp<0>>(entity);
  398. }
  399. generic(registry.view<position, velocity, comp<0>>(), [](auto &...comp) {
  400. ((comp.x = {}), ...);
  401. });
  402. }
  403. TEST(Benchmark, IterateThreeComponentsTombstonePolicy1M) {
  404. entt::registry registry;
  405. std::cout << "Iterating over 1000000 entities, three components, tombstone policy" << std::endl;
  406. for(std::uint64_t i = 0; i < 1000000L; i++) {
  407. const auto entity = registry.create();
  408. registry.emplace<stable_position>(entity);
  409. registry.emplace<velocity>(entity);
  410. registry.emplace<comp<0>>(entity);
  411. }
  412. generic(registry.view<stable_position, velocity, comp<0>>(), [](auto &...comp) {
  413. ((comp.x = {}), ...);
  414. });
  415. }
  416. TEST(Benchmark, IterateThreeComponents1MHalf) {
  417. entt::registry registry;
  418. std::cout << "Iterating over 1000000 entities, three components, half of the entities have all the components" << std::endl;
  419. for(std::uint64_t i = 0; i < 1000000L; i++) {
  420. const auto entity = registry.create();
  421. registry.emplace<velocity>(entity);
  422. registry.emplace<comp<0>>(entity);
  423. if(i % 2) {
  424. registry.emplace<position>(entity);
  425. }
  426. }
  427. generic(registry.view<position, velocity, comp<0>>(), [](auto &...comp) {
  428. ((comp.x = {}), ...);
  429. });
  430. }
  431. TEST(Benchmark, IterateThreeComponents1MOne) {
  432. entt::registry registry;
  433. std::cout << "Iterating over 1000000 entities, three components, only one entity has all the components" << std::endl;
  434. for(std::uint64_t i = 0; i < 1000000L; i++) {
  435. const auto entity = registry.create();
  436. registry.emplace<velocity>(entity);
  437. registry.emplace<comp<0>>(entity);
  438. if(i == 500000L) {
  439. registry.emplace<position>(entity);
  440. }
  441. }
  442. generic(registry.view<position, velocity, comp<0>>(), [](auto &...comp) {
  443. ((comp.x = {}), ...);
  444. });
  445. }
  446. TEST(Benchmark, IterateThreeComponentsNonOwningGroup1M) {
  447. entt::registry registry;
  448. std::cout << "Iterating over 1000000 entities, three components, non owning group" << std::endl;
  449. for(std::uint64_t i = 0; i < 1000000L; i++) {
  450. const auto entity = registry.create();
  451. registry.emplace<position>(entity);
  452. registry.emplace<velocity>(entity);
  453. registry.emplace<comp<0>>(entity);
  454. }
  455. generic(registry.group<>(entt::get<position, velocity, comp<0>>), [](auto &...comp) {
  456. ((comp.x = {}), ...);
  457. });
  458. }
  459. TEST(Benchmark, IterateThreeComponentsFullOwningGroup1M) {
  460. entt::registry registry;
  461. std::cout << "Iterating over 1000000 entities, three components, full owning group" << std::endl;
  462. for(std::uint64_t i = 0; i < 1000000L; i++) {
  463. const auto entity = registry.create();
  464. registry.emplace<position>(entity);
  465. registry.emplace<velocity>(entity);
  466. registry.emplace<comp<0>>(entity);
  467. }
  468. generic(registry.group<position, velocity, comp<0>>(), [](auto &...comp) {
  469. ((comp.x = {}), ...);
  470. });
  471. }
  472. TEST(Benchmark, IterateThreeComponentsPartialOwningGroup1M) {
  473. entt::registry registry;
  474. std::cout << "Iterating over 1000000 entities, three components, partial owning group" << std::endl;
  475. for(std::uint64_t i = 0; i < 1000000L; i++) {
  476. const auto entity = registry.create();
  477. registry.emplace<position>(entity);
  478. registry.emplace<velocity>(entity);
  479. registry.emplace<comp<0>>(entity);
  480. }
  481. generic(registry.group<position, velocity>(entt::get<comp<0>>), [](auto &...comp) {
  482. ((comp.x = {}), ...);
  483. });
  484. }
  485. TEST(Benchmark, IterateThreeComponentsRuntime1M) {
  486. entt::registry registry;
  487. std::cout << "Iterating over 1000000 entities, three components, runtime view" << std::endl;
  488. for(std::uint64_t i = 0; i < 1000000L; i++) {
  489. const auto entity = registry.create();
  490. registry.emplace<position>(entity);
  491. registry.emplace<velocity>(entity);
  492. registry.emplace<comp<0>>(entity);
  493. }
  494. entt::runtime_view view{};
  495. view.iterate(registry.storage<position>())
  496. .iterate(registry.storage<velocity>())
  497. .iterate(registry.storage<comp<0>>());
  498. generic(view, [&registry](auto entity) {
  499. registry.get<position>(entity).x = {};
  500. registry.get<velocity>(entity).x = {};
  501. registry.get<comp<0>>(entity).x = {};
  502. });
  503. }
  504. TEST(Benchmark, IterateThreeComponentsRuntime1MHalf) {
  505. entt::registry registry;
  506. std::cout << "Iterating over 1000000 entities, three components, half of the entities have all the components, runtime view" << std::endl;
  507. for(std::uint64_t i = 0; i < 1000000L; i++) {
  508. const auto entity = registry.create();
  509. registry.emplace<velocity>(entity);
  510. registry.emplace<comp<0>>(entity);
  511. if(i % 2) {
  512. registry.emplace<position>(entity);
  513. }
  514. }
  515. entt::runtime_view view{};
  516. view.iterate(registry.storage<position>())
  517. .iterate(registry.storage<velocity>())
  518. .iterate(registry.storage<comp<0>>());
  519. generic(view, [&registry](auto entity) {
  520. registry.get<position>(entity).x = {};
  521. registry.get<velocity>(entity).x = {};
  522. registry.get<comp<0>>(entity).x = {};
  523. });
  524. }
  525. TEST(Benchmark, IterateThreeComponentsRuntime1MOne) {
  526. entt::registry registry;
  527. std::cout << "Iterating over 1000000 entities, three components, only one entity has all the components, runtime view" << std::endl;
  528. for(std::uint64_t i = 0; i < 1000000L; i++) {
  529. const auto entity = registry.create();
  530. registry.emplace<velocity>(entity);
  531. registry.emplace<comp<0>>(entity);
  532. if(i == 500000L) {
  533. registry.emplace<position>(entity);
  534. }
  535. }
  536. entt::runtime_view view{};
  537. view.iterate(registry.storage<position>())
  538. .iterate(registry.storage<velocity>())
  539. .iterate(registry.storage<comp<0>>());
  540. generic(view, [&registry](auto entity) {
  541. registry.get<position>(entity).x = {};
  542. registry.get<velocity>(entity).x = {};
  543. registry.get<comp<0>>(entity).x = {};
  544. });
  545. }
  546. TEST(Benchmark, IterateFiveComponents1M) {
  547. entt::registry registry;
  548. std::cout << "Iterating over 1000000 entities, five components" << std::endl;
  549. for(std::uint64_t i = 0; i < 1000000L; i++) {
  550. const auto entity = registry.create();
  551. registry.emplace<position>(entity);
  552. registry.emplace<velocity>(entity);
  553. registry.emplace<comp<0>>(entity);
  554. registry.emplace<comp<1>>(entity);
  555. registry.emplace<comp<2>>(entity);
  556. }
  557. generic(registry.view<position, velocity, comp<0>, comp<1>, comp<2>>(), [](auto &...comp) {
  558. ((comp.x = {}), ...);
  559. });
  560. }
  561. TEST(Benchmark, IterateFiveComponentsTombstonePolicy1M) {
  562. entt::registry registry;
  563. std::cout << "Iterating over 1000000 entities, five components, tombstone policy" << std::endl;
  564. for(std::uint64_t i = 0; i < 1000000L; i++) {
  565. const auto entity = registry.create();
  566. registry.emplace<stable_position>(entity);
  567. registry.emplace<velocity>(entity);
  568. registry.emplace<comp<0>>(entity);
  569. registry.emplace<comp<1>>(entity);
  570. registry.emplace<comp<2>>(entity);
  571. }
  572. generic(registry.view<stable_position, velocity, comp<0>, comp<1>, comp<2>>(), [](auto &...comp) {
  573. ((comp.x = {}), ...);
  574. });
  575. }
  576. TEST(Benchmark, IterateFiveComponents1MHalf) {
  577. entt::registry registry;
  578. std::cout << "Iterating over 1000000 entities, five components, half of the entities have all the components" << std::endl;
  579. for(std::uint64_t i = 0; i < 1000000L; i++) {
  580. const auto entity = registry.create();
  581. registry.emplace<velocity>(entity);
  582. registry.emplace<comp<0>>(entity);
  583. registry.emplace<comp<1>>(entity);
  584. registry.emplace<comp<2>>(entity);
  585. if(i % 2) {
  586. registry.emplace<position>(entity);
  587. }
  588. }
  589. generic(registry.view<position, velocity, comp<0>, comp<1>, comp<2>>(), [](auto &...comp) {
  590. ((comp.x = {}), ...);
  591. });
  592. }
  593. TEST(Benchmark, IterateFiveComponents1MOne) {
  594. entt::registry registry;
  595. std::cout << "Iterating over 1000000 entities, five components, only one entity has all the components" << std::endl;
  596. for(std::uint64_t i = 0; i < 1000000L; i++) {
  597. const auto entity = registry.create();
  598. registry.emplace<velocity>(entity);
  599. registry.emplace<comp<0>>(entity);
  600. registry.emplace<comp<1>>(entity);
  601. registry.emplace<comp<2>>(entity);
  602. if(i == 500000L) {
  603. registry.emplace<position>(entity);
  604. }
  605. }
  606. generic(registry.view<position, velocity, comp<0>, comp<1>, comp<2>>(), [](auto &...comp) {
  607. ((comp.x = {}), ...);
  608. });
  609. }
  610. TEST(Benchmark, IterateFiveComponentsNonOwningGroup1M) {
  611. entt::registry registry;
  612. std::cout << "Iterating over 1000000 entities, five components, non owning group" << std::endl;
  613. for(std::uint64_t i = 0; i < 1000000L; i++) {
  614. const auto entity = registry.create();
  615. registry.emplace<position>(entity);
  616. registry.emplace<velocity>(entity);
  617. registry.emplace<comp<0>>(entity);
  618. registry.emplace<comp<1>>(entity);
  619. registry.emplace<comp<2>>(entity);
  620. }
  621. generic(registry.group<>(entt::get<position, velocity, comp<0>, comp<1>, comp<2>>), [](auto &...comp) {
  622. ((comp.x = {}), ...);
  623. });
  624. }
  625. TEST(Benchmark, IterateFiveComponentsFullOwningGroup1M) {
  626. entt::registry registry;
  627. std::cout << "Iterating over 1000000 entities, five components, full owning group" << std::endl;
  628. for(std::uint64_t i = 0; i < 1000000L; i++) {
  629. const auto entity = registry.create();
  630. registry.emplace<position>(entity);
  631. registry.emplace<velocity>(entity);
  632. registry.emplace<comp<0>>(entity);
  633. registry.emplace<comp<1>>(entity);
  634. registry.emplace<comp<2>>(entity);
  635. }
  636. generic(registry.group<position, velocity, comp<0>, comp<1>, comp<2>>(), [](auto &...comp) {
  637. ((comp.x = {}), ...);
  638. });
  639. }
  640. TEST(Benchmark, IterateFiveComponentsPartialFourOfFiveOwningGroup1M) {
  641. entt::registry registry;
  642. std::cout << "Iterating over 1000000 entities, five components, partial (4 of 5) owning group" << std::endl;
  643. for(std::uint64_t i = 0; i < 1000000L; i++) {
  644. const auto entity = registry.create();
  645. registry.emplace<position>(entity);
  646. registry.emplace<velocity>(entity);
  647. registry.emplace<comp<0>>(entity);
  648. registry.emplace<comp<1>>(entity);
  649. registry.emplace<comp<2>>(entity);
  650. }
  651. generic(registry.group<position, velocity, comp<0>, comp<1>>(entt::get<comp<2>>), [](auto &...comp) {
  652. ((comp.x = {}), ...);
  653. });
  654. }
  655. TEST(Benchmark, IterateFiveComponentsPartialThreeOfFiveOwningGroup1M) {
  656. entt::registry registry;
  657. std::cout << "Iterating over 1000000 entities, five components, partial (3 of 5) 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<position, velocity, comp<0>>(entt::get<comp<1>, comp<2>>), [](auto &...comp) {
  667. ((comp.x = {}), ...);
  668. });
  669. }
  670. TEST(Benchmark, IterateFiveComponentsRuntime1M) {
  671. entt::registry registry;
  672. std::cout << "Iterating over 1000000 entities, five components, runtime view" << 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. entt::runtime_view view{};
  682. view.iterate(registry.storage<position>())
  683. .iterate(registry.storage<velocity>())
  684. .iterate(registry.storage<comp<0>>())
  685. .iterate(registry.storage<comp<1>>())
  686. .iterate(registry.storage<comp<2>>());
  687. generic(view, [&registry](auto entity) {
  688. registry.get<position>(entity).x = {};
  689. registry.get<velocity>(entity).x = {};
  690. registry.get<comp<0>>(entity).x = {};
  691. registry.get<comp<1>>(entity).x = {};
  692. registry.get<comp<2>>(entity).x = {};
  693. });
  694. }
  695. TEST(Benchmark, IterateFiveComponentsRuntime1MHalf) {
  696. entt::registry registry;
  697. std::cout << "Iterating over 1000000 entities, five components, half of the entities have all the components, runtime view" << std::endl;
  698. for(std::uint64_t i = 0; i < 1000000L; i++) {
  699. const auto entity = registry.create();
  700. registry.emplace<velocity>(entity);
  701. registry.emplace<comp<0>>(entity);
  702. registry.emplace<comp<1>>(entity);
  703. registry.emplace<comp<2>>(entity);
  704. if(i % 2) {
  705. registry.emplace<position>(entity);
  706. }
  707. }
  708. entt::runtime_view view{};
  709. view.iterate(registry.storage<position>())
  710. .iterate(registry.storage<velocity>())
  711. .iterate(registry.storage<comp<0>>())
  712. .iterate(registry.storage<comp<1>>())
  713. .iterate(registry.storage<comp<2>>());
  714. generic(view, [&registry](auto entity) {
  715. registry.get<position>(entity).x = {};
  716. registry.get<velocity>(entity).x = {};
  717. registry.get<comp<0>>(entity).x = {};
  718. registry.get<comp<1>>(entity).x = {};
  719. registry.get<comp<2>>(entity).x = {};
  720. });
  721. }
  722. TEST(Benchmark, IterateFiveComponentsRuntime1MOne) {
  723. entt::registry registry;
  724. std::cout << "Iterating over 1000000 entities, five components, only one entity has all the components, runtime view" << std::endl;
  725. for(std::uint64_t i = 0; i < 1000000L; i++) {
  726. const auto entity = registry.create();
  727. registry.emplace<velocity>(entity);
  728. registry.emplace<comp<0>>(entity);
  729. registry.emplace<comp<1>>(entity);
  730. registry.emplace<comp<2>>(entity);
  731. if(i == 500000L) {
  732. registry.emplace<position>(entity);
  733. }
  734. }
  735. entt::runtime_view view{};
  736. view.iterate(registry.storage<position>())
  737. .iterate(registry.storage<velocity>())
  738. .iterate(registry.storage<comp<0>>())
  739. .iterate(registry.storage<comp<1>>())
  740. .iterate(registry.storage<comp<2>>());
  741. generic(view, [&registry](auto entity) {
  742. registry.get<position>(entity).x = {};
  743. registry.get<velocity>(entity).x = {};
  744. registry.get<comp<0>>(entity).x = {};
  745. registry.get<comp<1>>(entity).x = {};
  746. registry.get<comp<2>>(entity).x = {};
  747. });
  748. }
  749. TEST(Benchmark, IteratePathological) {
  750. std::cout << "Pathological case" << std::endl;
  751. pathological([](auto &registry) { return registry.template view<position, velocity, comp<0>>(); });
  752. }
  753. TEST(Benchmark, IteratePathologicalNonOwningGroup) {
  754. std::cout << "Pathological case (non-owning group)" << std::endl;
  755. pathological([](auto &registry) { return registry.template group<>(entt::get<position, velocity, comp<0>>); });
  756. }
  757. TEST(Benchmark, IteratePathologicalFullOwningGroup) {
  758. std::cout << "Pathological case (full-owning group)" << std::endl;
  759. pathological([](auto &registry) { return registry.template group<position, velocity, comp<0>>(); });
  760. }
  761. TEST(Benchmark, IteratePathologicalPartialOwningGroup) {
  762. std::cout << "Pathological case (partial-owning group)" << std::endl;
  763. pathological([](auto &registry) { return registry.template group<position, velocity>(entt::get<comp<0>>); });
  764. }
  765. TEST(Benchmark, SortSingle) {
  766. entt::registry registry;
  767. std::cout << "Sort 150000 entities, one component" << std::endl;
  768. for(std::uint64_t i = 0; i < 150000L; i++) {
  769. const auto entity = registry.create();
  770. registry.emplace<position>(entity, i, i);
  771. }
  772. timer timer;
  773. registry.sort<position>([](const auto &lhs, const auto &rhs) { return lhs.x < rhs.x && lhs.y < rhs.y; });
  774. timer.elapsed();
  775. }
  776. TEST(Benchmark, SortMulti) {
  777. entt::registry registry;
  778. std::cout << "Sort 150000 entities, two components" << std::endl;
  779. for(std::uint64_t i = 0; i < 150000L; i++) {
  780. const auto entity = registry.create();
  781. registry.emplace<position>(entity, i, i);
  782. registry.emplace<velocity>(entity, i, i);
  783. }
  784. registry.sort<position>([](const auto &lhs, const auto &rhs) { return lhs.x < rhs.x && lhs.y < rhs.y; });
  785. timer timer;
  786. registry.sort<velocity, position>();
  787. timer.elapsed();
  788. }
  789. TEST(Benchmark, AlmostSortedStdSort) {
  790. entt::registry registry;
  791. entt::entity entities[3]{};
  792. std::cout << "Sort 150000 entities, almost sorted, std::sort" << std::endl;
  793. for(std::uint64_t i = 0; i < 150000L; i++) {
  794. const auto entity = registry.create();
  795. registry.emplace<position>(entity, i, i);
  796. if(!(i % 50000)) {
  797. entities[i / 50000] = entity;
  798. }
  799. }
  800. for(std::uint64_t i = 0; i < 3; ++i) {
  801. registry.destroy(entities[i]);
  802. const auto entity = registry.create();
  803. registry.emplace<position>(entity, 50000 * i, 50000 * i);
  804. }
  805. timer timer;
  806. registry.sort<position>([](const auto &lhs, const auto &rhs) { return lhs.x > rhs.x && lhs.y > rhs.y; });
  807. timer.elapsed();
  808. }
  809. TEST(Benchmark, AlmostSortedInsertionSort) {
  810. entt::registry registry;
  811. entt::entity entities[3]{};
  812. std::cout << "Sort 150000 entities, almost sorted, insertion sort" << 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. if(!(i % 50000)) {
  817. entities[i / 50000] = entity;
  818. }
  819. }
  820. for(std::uint64_t i = 0; i < 3; ++i) {
  821. registry.destroy(entities[i]);
  822. const auto entity = registry.create();
  823. registry.emplace<position>(entity, 50000 * i, 50000 * i);
  824. }
  825. timer timer;
  826. registry.sort<position>([](const auto &lhs, const auto &rhs) { return lhs.x > rhs.x && lhs.y > rhs.y; }, entt::insertion_sort{});
  827. timer.elapsed();
  828. }