benchmark.cpp 33 KB

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