SparseArray.cpp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  1. // Copyright (C) 2009-2021, Panagiotis Christopoulos Charitos and contributors.
  2. // All rights reserved.
  3. // Code licensed under the BSD License.
  4. // http://www.anki3d.org/LICENSE
  5. #include <Tests/Framework/Framework.h>
  6. #include <AnKi/Util/SparseArray.h>
  7. #include <AnKi/Util/HighRezTimer.h>
  8. #include <unordered_map>
  9. #include <ctime>
  10. #include <algorithm>
  11. #include <malloc.h>
  12. namespace anki {
  13. namespace {
  14. static I64 constructor0Count = 0;
  15. static I64 constructor1Count = 0;
  16. static I64 constructor2Count = 0;
  17. static I64 constructor3Count = 0;
  18. static I64 destructorCount = 0;
  19. static I64 copyCount = 0;
  20. static I64 moveCount = 0;
  21. class SAFoo
  22. {
  23. public:
  24. int m_x;
  25. SAFoo()
  26. : m_x(0)
  27. {
  28. ++constructor0Count;
  29. }
  30. SAFoo(int x)
  31. : m_x(x)
  32. {
  33. ++constructor1Count;
  34. }
  35. SAFoo(const SAFoo& b)
  36. : m_x(b.m_x)
  37. {
  38. ++constructor2Count;
  39. }
  40. SAFoo(SAFoo&& b)
  41. : m_x(b.m_x)
  42. {
  43. b.m_x = 0;
  44. ++constructor3Count;
  45. }
  46. ~SAFoo()
  47. {
  48. ++destructorCount;
  49. }
  50. SAFoo& operator=(const SAFoo& b)
  51. {
  52. m_x = b.m_x;
  53. ++copyCount;
  54. return *this;
  55. }
  56. SAFoo& operator=(SAFoo&& b)
  57. {
  58. m_x = b.m_x;
  59. b.m_x = 0;
  60. ++moveCount;
  61. return *this;
  62. }
  63. static void checkCalls()
  64. {
  65. ANKI_TEST_EXPECT_EQ(constructor0Count, 0); // default
  66. ANKI_TEST_EXPECT_GT(constructor1Count, 0);
  67. ANKI_TEST_EXPECT_EQ(constructor2Count, 0); // copy
  68. ANKI_TEST_EXPECT_GT(constructor3Count, 0); // move
  69. ANKI_TEST_EXPECT_EQ(destructorCount, constructor1Count + constructor3Count);
  70. ANKI_TEST_EXPECT_EQ(copyCount, 0); // copy
  71. ANKI_TEST_EXPECT_GEQ(moveCount, 0); // move
  72. constructor0Count = constructor1Count = constructor2Count = constructor3Count = destructorCount = copyCount =
  73. moveCount = 0;
  74. }
  75. };
  76. } // namespace
  77. } // namespace anki
  78. ANKI_TEST(Util, SparseArray)
  79. {
  80. HeapAllocator<U8> alloc(allocAligned, nullptr);
  81. // Set same key
  82. {
  83. SparseArray<PtrSize> arr;
  84. arr.emplace(alloc, 1000, 123);
  85. arr.emplace(alloc, 1000, 124);
  86. auto it = arr.find(1000);
  87. ANKI_TEST_EXPECT_EQ(*it, 124);
  88. arr.erase(alloc, it);
  89. }
  90. // Check destroy and grow
  91. {
  92. SparseArray<SAFoo> arr(64, 2);
  93. arr.emplace(alloc, 64 * 1, 123);
  94. arr.emplace(alloc, 64 * 2, 124);
  95. arr.emplace(alloc, 64 * 3, 125);
  96. ANKI_TEST_EXPECT_EQ(arr.find(64 * 1)->m_x, 123);
  97. ANKI_TEST_EXPECT_EQ(arr.find(64 * 2)->m_x, 124);
  98. ANKI_TEST_EXPECT_EQ(arr.find(64 * 3)->m_x, 125);
  99. arr.destroy(alloc);
  100. SAFoo::checkCalls();
  101. }
  102. // Do complex insertions
  103. {
  104. SparseArray<SAFoo, U32> arr(64, 3);
  105. arr.emplace(alloc, 64 * 0 - 1, 1);
  106. // Linear probing to 0
  107. arr.emplace(alloc, 64 * 1 - 1, 2);
  108. // Linear probing to 1
  109. arr.emplace(alloc, 64 * 2 - 1, 3);
  110. // Linear probing to 2
  111. arr.emplace(alloc, 1, 3);
  112. // Swap
  113. arr.emplace(alloc, 64 * 1, 3);
  114. ANKI_TEST_EXPECT_EQ(arr.getSize(), 5);
  115. arr.destroy(alloc);
  116. SAFoo::checkCalls();
  117. }
  118. // Fuzzy test
  119. {
  120. const U MAX = 10000;
  121. SparseArray<SAFoo, U32> arr;
  122. std::vector<int> numbers;
  123. srand(U32(time(nullptr)));
  124. // Insert random
  125. for(U i = 0; i < MAX; ++i)
  126. {
  127. I32 num;
  128. while(1)
  129. {
  130. num = rand();
  131. if(std::find(numbers.begin(), numbers.end(), num) == numbers.end())
  132. {
  133. // Not found
  134. ANKI_TEST_EXPECT_EQ(arr.find(num), arr.getEnd());
  135. arr.emplace(alloc, num, num);
  136. ANKI_TEST_EXPECT_EQ(arr.getSize(), i + 1);
  137. numbers.push_back(num);
  138. break;
  139. }
  140. else
  141. {
  142. // Found
  143. ANKI_TEST_EXPECT_NEQ(arr.find(num), arr.getEnd());
  144. }
  145. }
  146. arr.validate();
  147. }
  148. ANKI_TEST_EXPECT_EQ(arr.getSize(), MAX);
  149. // Remove randomly
  150. U count = MAX;
  151. while(count--)
  152. {
  153. U idx = rand() % (count + 1);
  154. int num = numbers[idx];
  155. numbers.erase(numbers.begin() + idx);
  156. auto it = arr.find(num);
  157. ANKI_TEST_EXPECT_NEQ(it, arr.getEnd());
  158. ANKI_TEST_EXPECT_EQ(it->m_x, num);
  159. arr.erase(alloc, it);
  160. arr.validate();
  161. }
  162. }
  163. // Fuzzy test #2: Do random insertions and removals
  164. {
  165. const U MAX = 10000;
  166. SparseArray<SAFoo, U64> arr;
  167. using StlMap =
  168. std::unordered_map<int, int, std::hash<int>, std::equal_to<int>, HeapAllocator<std::pair<const int, int>>>;
  169. StlMap map(10, std::hash<int>(), std::equal_to<int>(), alloc);
  170. for(U i = 0; i < MAX; ++i)
  171. {
  172. const Bool insert = (rand() & 1) || arr.getSize() == 0;
  173. if(insert)
  174. {
  175. const I32 idx = rand();
  176. if(map.find(idx) != map.end())
  177. {
  178. continue;
  179. }
  180. arr.emplace(alloc, idx, idx + 1);
  181. map[idx] = idx + 1;
  182. arr.validate();
  183. }
  184. else
  185. {
  186. const U idx = U(rand()) % map.size();
  187. auto it = std::next(std::begin(map), idx);
  188. int key = it->first;
  189. auto it2 = arr.find(key);
  190. ANKI_TEST_EXPECT_NEQ(it2, arr.getEnd());
  191. ANKI_TEST_EXPECT_EQ(it->second, it2->m_x);
  192. map.erase(it);
  193. arr.erase(alloc, it2);
  194. ANKI_TEST_EXPECT_EQ(arr.find(key), arr.getEnd());
  195. arr.validate();
  196. }
  197. // Iterate and check
  198. {
  199. StlMap bMap = map;
  200. auto it = arr.getBegin();
  201. while(it != arr.getEnd())
  202. {
  203. I32 key = it->m_x - 1;
  204. auto it2 = bMap.find(key);
  205. ANKI_TEST_EXPECT_NEQ(it2, bMap.end());
  206. bMap.erase(it2);
  207. ++it;
  208. }
  209. }
  210. }
  211. arr.destroy(alloc);
  212. // Check what the SparseArray have called
  213. SAFoo::checkCalls();
  214. }
  215. }
  216. static I64 akAllocSize = 0;
  217. static I64 akMaxAllocSize = 0;
  218. static ANKI_DONT_INLINE void* allocAlignedAk(void* userData, void* ptr, PtrSize size, PtrSize alignment)
  219. {
  220. if(ptr == nullptr)
  221. {
  222. #if ANKI_OS_LINUX
  223. akAllocSize += size;
  224. akMaxAllocSize = max(akMaxAllocSize, akAllocSize);
  225. #endif
  226. return malloc(size);
  227. }
  228. else
  229. {
  230. #if ANKI_OS_LINUX
  231. PtrSize s = malloc_usable_size(ptr);
  232. akAllocSize -= s;
  233. #endif
  234. free(ptr);
  235. return nullptr;
  236. }
  237. }
  238. static I64 stlAllocSize = 0;
  239. static I64 stlMaxAllocSize = 0;
  240. static ANKI_DONT_INLINE void* allocAlignedStl(void* userData, void* ptr, PtrSize size, PtrSize alignment)
  241. {
  242. if(ptr == nullptr)
  243. {
  244. #if ANKI_OS_LINUX
  245. stlAllocSize += size;
  246. stlMaxAllocSize = max(stlMaxAllocSize, stlAllocSize);
  247. #endif
  248. return malloc(size);
  249. }
  250. else
  251. {
  252. #if ANKI_OS_LINUX
  253. PtrSize s = malloc_usable_size(ptr);
  254. stlAllocSize -= s;
  255. #endif
  256. free(ptr);
  257. return nullptr;
  258. }
  259. }
  260. ANKI_TEST(Util, SparseArrayBench)
  261. {
  262. HeapAllocator<U8> allocAk(allocAlignedAk, nullptr);
  263. HeapAllocator<U8> allocStl(allocAlignedStl, nullptr);
  264. HeapAllocator<U8> allocTml(allocAligned, nullptr);
  265. using StlMap =
  266. std::unordered_map<int, int, std::hash<int>, std::equal_to<int>, HeapAllocator<std::pair<const int, int>>>;
  267. StlMap stdMap(10, std::hash<int>(), std::equal_to<int>(), allocStl);
  268. using AkMap = SparseArray<int, U32>;
  269. AkMap akMap(256, U32(log2(256.0f)), 0.90f);
  270. HighRezTimer timer;
  271. const U COUNT = 1024 * 1024 * 6;
  272. // Create a huge set
  273. std::vector<int> vals;
  274. {
  275. std::unordered_map<int, int> tmpMap;
  276. for(U i = 0; i < COUNT; ++i)
  277. {
  278. // Put unique keys
  279. int v;
  280. do
  281. {
  282. v = rand();
  283. } while(tmpMap.find(v) != tmpMap.end() && v != 0);
  284. tmpMap[v] = 1;
  285. vals.push_back(v);
  286. }
  287. }
  288. // Insertion
  289. {
  290. // AnkI
  291. timer.start();
  292. for(U i = 0; i < COUNT; ++i)
  293. {
  294. akMap.emplace(allocAk, vals[i], vals[i]);
  295. }
  296. timer.stop();
  297. Second akTime = timer.getElapsedTime();
  298. // STL
  299. timer.start();
  300. for(U i = 0; i < COUNT; ++i)
  301. {
  302. stdMap[vals[i]] = vals[i];
  303. }
  304. timer.stop();
  305. Second stlTime = timer.getElapsedTime();
  306. ANKI_TEST_LOGI("Inserting bench: STL %f AnKi %f | %f%%", stlTime, akTime, stlTime / akTime * 100.0);
  307. }
  308. // Search
  309. {
  310. // Search in random order
  311. std::random_shuffle(vals.begin(), vals.end());
  312. int count = 0;
  313. // Find values AnKi
  314. timer.start();
  315. for(U i = 0; i < COUNT; ++i)
  316. {
  317. auto it = akMap.find(vals[i]);
  318. count += *it;
  319. }
  320. timer.stop();
  321. Second akTime = timer.getElapsedTime();
  322. // Find values STL
  323. timer.start();
  324. for(U i = 0; i < COUNT; ++i)
  325. {
  326. count += stdMap[vals[i]];
  327. }
  328. timer.stop();
  329. Second stlTime = timer.getElapsedTime();
  330. // Print the "count" so that the compiler won't optimize it
  331. ANKI_TEST_LOGI("Find bench: STL %f AnKi %f | %f%% (r:%d)", stlTime, akTime, stlTime / akTime * 100.0, count);
  332. }
  333. // Mem usage
  334. const I64 stlMemUsage = stlMaxAllocSize + sizeof(stdMap);
  335. const I64 akMemUsage = akMaxAllocSize + sizeof(akMap);
  336. ANKI_TEST_LOGI("Max mem usage: STL %li AnKi %li | %f%% (At any given time what was the max mem usage)", stlMemUsage,
  337. akMemUsage, F64(stlMemUsage) / F32(akMemUsage) * 100.0f);
  338. // Deletes
  339. {
  340. // Remove in random order
  341. std::random_shuffle(vals.begin(), vals.end());
  342. // Random delete AnKi
  343. Second akTime = 0.0;
  344. for(U i = 0; i < vals.size(); ++i)
  345. {
  346. auto it = akMap.find(vals[i]);
  347. timer.start();
  348. akMap.erase(allocAk, it);
  349. timer.stop();
  350. akTime += timer.getElapsedTime();
  351. }
  352. // Random delete STL
  353. Second stlTime = 0.0;
  354. for(U i = 0; i < vals.size(); ++i)
  355. {
  356. auto it = stdMap.find(vals[i]);
  357. timer.start();
  358. stdMap.erase(it);
  359. timer.stop();
  360. stlTime += timer.getElapsedTime();
  361. }
  362. ANKI_TEST_LOGI("Deleting bench: STL %f AnKi %f | %f%%\n", stlTime, akTime, stlTime / akTime * 100.0);
  363. }
  364. akMap.destroy(allocAk);
  365. }