SparseArray.cpp 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  1. // Copyright (C) 2009-2022, 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. template<typename TIndex>
  77. class Config
  78. {
  79. public:
  80. using Index = TIndex;
  81. Index m_initialStorage = 0;
  82. U32 m_linearProbingCount = 0;
  83. F32 m_maxLodFactor = 0;
  84. Index getInitialStorageSize() const
  85. {
  86. return m_initialStorage;
  87. }
  88. U32 getLinearProbingCount() const
  89. {
  90. return m_linearProbingCount;
  91. }
  92. F32 getMaxLoadFactor() const
  93. {
  94. return m_maxLodFactor;
  95. }
  96. };
  97. } // namespace
  98. } // namespace anki
  99. ANKI_TEST(Util, SparseArray)
  100. {
  101. HeapMemoryPool pool(allocAligned, nullptr);
  102. // Set same key
  103. {
  104. SparseArray<PtrSize> arr;
  105. arr.emplace(pool, 1000, 123);
  106. arr.emplace(pool, 1000, 124);
  107. auto it = arr.find(1000);
  108. ANKI_TEST_EXPECT_EQ(*it, 124);
  109. arr.erase(pool, it);
  110. }
  111. // Check destroy and grow
  112. {
  113. SparseArray<SAFoo, Config<U32>> arr(Config<U32>{64, 2, 0.8f});
  114. arr.emplace(pool, 64 * 1, 123);
  115. arr.emplace(pool, 64 * 2, 124);
  116. arr.emplace(pool, 64 * 3, 125);
  117. ANKI_TEST_EXPECT_EQ(arr.find(64 * 1)->m_x, 123);
  118. ANKI_TEST_EXPECT_EQ(arr.find(64 * 2)->m_x, 124);
  119. ANKI_TEST_EXPECT_EQ(arr.find(64 * 3)->m_x, 125);
  120. SparseArray<SAFoo, Config<U32>> arr2(std::move(arr));
  121. arr2.destroy(pool);
  122. SAFoo::checkCalls();
  123. }
  124. // Do complex insertions
  125. {
  126. SparseArray<SAFoo, Config<U32>> arr(Config<U32>{64, 3, 0.8f});
  127. arr.emplace(pool, 64u * 0 - 1, 1);
  128. // Linear probing to 0
  129. arr.emplace(pool, 64 * 1 - 1, 2);
  130. // Linear probing to 1
  131. arr.emplace(pool, 64 * 2 - 1, 3);
  132. // Linear probing to 2
  133. arr.emplace(pool, 1, 3);
  134. // Swap
  135. arr.emplace(pool, 64 * 1, 3);
  136. ANKI_TEST_EXPECT_EQ(arr.getSize(), 5);
  137. arr.destroy(pool);
  138. SAFoo::checkCalls();
  139. }
  140. // Fuzzy test
  141. {
  142. constexpr U kMax = 10000;
  143. SparseArray<SAFoo> arr;
  144. std::vector<int> numbers;
  145. srand(U32(time(nullptr)));
  146. // Insert random
  147. for(U i = 0; i < kMax; ++i)
  148. {
  149. I32 num;
  150. while(1)
  151. {
  152. num = rand();
  153. if(std::find(numbers.begin(), numbers.end(), num) == numbers.end())
  154. {
  155. // Not found
  156. ANKI_TEST_EXPECT_EQ(arr.find(num), arr.getEnd());
  157. arr.emplace(pool, num, num);
  158. ANKI_TEST_EXPECT_EQ(arr.getSize(), i + 1);
  159. numbers.push_back(num);
  160. break;
  161. }
  162. else
  163. {
  164. // Found
  165. ANKI_TEST_EXPECT_NEQ(arr.find(num), arr.getEnd());
  166. }
  167. }
  168. arr.validate();
  169. }
  170. ANKI_TEST_EXPECT_EQ(arr.getSize(), kMax);
  171. // Remove randomly
  172. U count = kMax;
  173. while(count--)
  174. {
  175. U idx = rand() % (count + 1);
  176. int num = numbers[idx];
  177. numbers.erase(numbers.begin() + idx);
  178. auto it = arr.find(num);
  179. ANKI_TEST_EXPECT_NEQ(it, arr.getEnd());
  180. ANKI_TEST_EXPECT_EQ(it->m_x, num);
  181. arr.erase(pool, it);
  182. arr.validate();
  183. }
  184. }
  185. // Fuzzy test #2: Do random insertions and removals
  186. {
  187. constexpr U kMax = 10000;
  188. SparseArray<SAFoo, Config<U64>> arr(Config<U64>{64, 8, 0.8f});
  189. using StlMap = std::unordered_map<int, int, std::hash<int>, std::equal_to<int>>;
  190. StlMap map(10, std::hash<int>(), std::equal_to<int>());
  191. for(U i = 0; i < kMax; ++i)
  192. {
  193. const Bool insert = (rand() & 1) || arr.getSize() == 0;
  194. if(insert)
  195. {
  196. const I32 idx = rand();
  197. if(map.find(idx) != map.end())
  198. {
  199. continue;
  200. }
  201. arr.emplace(pool, idx, idx + 1);
  202. map[idx] = idx + 1;
  203. arr.validate();
  204. }
  205. else
  206. {
  207. const U idx = U(rand()) % map.size();
  208. auto it = std::next(std::begin(map), idx);
  209. int key = it->first;
  210. auto it2 = arr.find(key);
  211. ANKI_TEST_EXPECT_NEQ(it2, arr.getEnd());
  212. ANKI_TEST_EXPECT_EQ(it->second, it2->m_x);
  213. map.erase(it);
  214. arr.erase(pool, it2);
  215. ANKI_TEST_EXPECT_EQ(arr.find(key), arr.getEnd());
  216. arr.validate();
  217. }
  218. // Iterate and check
  219. {
  220. StlMap bMap = map;
  221. auto it = arr.getBegin();
  222. while(it != arr.getEnd())
  223. {
  224. I32 key = it->m_x - 1;
  225. auto it2 = bMap.find(key);
  226. ANKI_TEST_EXPECT_NEQ(it2, bMap.end());
  227. bMap.erase(it2);
  228. ++it;
  229. }
  230. }
  231. }
  232. arr.destroy(pool);
  233. // Check what the SparseArray have called
  234. SAFoo::checkCalls();
  235. }
  236. }
  237. static I64 akAllocSize = 0;
  238. static I64 akMaxAllocSize = 0;
  239. static ANKI_DONT_INLINE void* allocAlignedAk([[maybe_unused]] void* userData, void* ptr, PtrSize size,
  240. [[maybe_unused]] PtrSize alignment)
  241. {
  242. if(ptr == nullptr)
  243. {
  244. #if ANKI_OS_LINUX
  245. akAllocSize += size;
  246. akMaxAllocSize = max(akMaxAllocSize, akAllocSize);
  247. #endif
  248. return malloc(size);
  249. }
  250. else
  251. {
  252. #if ANKI_OS_LINUX
  253. PtrSize s = malloc_usable_size(ptr);
  254. akAllocSize -= s;
  255. #endif
  256. free(ptr);
  257. return nullptr;
  258. }
  259. }
  260. static I64 stlAllocSize = 0;
  261. static I64 stlMaxAllocSize = 0;
  262. static ANKI_DONT_INLINE void* allocAlignedStl([[maybe_unused]] void* userData, void* ptr, PtrSize size,
  263. [[maybe_unused]] PtrSize alignment)
  264. {
  265. if(ptr == nullptr)
  266. {
  267. #if ANKI_OS_LINUX
  268. stlAllocSize += size;
  269. stlMaxAllocSize = max(stlMaxAllocSize, stlAllocSize);
  270. #endif
  271. return malloc(size);
  272. }
  273. else
  274. {
  275. #if ANKI_OS_LINUX
  276. PtrSize s = malloc_usable_size(ptr);
  277. stlAllocSize -= s;
  278. #endif
  279. free(ptr);
  280. return nullptr;
  281. }
  282. }
  283. ANKI_TEST(Util, SparseArrayBench)
  284. {
  285. HeapMemoryPool pool(allocAlignedAk, nullptr);
  286. HeapAllocator<U8> allocStl(allocAlignedStl, nullptr);
  287. using StlMap =
  288. std::unordered_map<int, int, std::hash<int>, std::equal_to<int>, HeapAllocator<std::pair<const int, int>>>;
  289. StlMap stdMap(10, std::hash<int>(), std::equal_to<int>(), allocStl);
  290. using AkMap = SparseArray<int, Config<U32>>;
  291. AkMap akMap(Config<U32>{256, U32(log2(256.0f)), 0.9f});
  292. HighRezTimer timer;
  293. constexpr U kCount = 1024 * 1024 * 6;
  294. // Create a huge set
  295. std::vector<int> vals;
  296. {
  297. std::unordered_map<int, int> tmpMap;
  298. for(U i = 0; i < kCount; ++i)
  299. {
  300. // Put unique keys
  301. int v;
  302. do
  303. {
  304. v = int(getRandom());
  305. } while(tmpMap.find(v) != tmpMap.end() && v != 0);
  306. tmpMap[v] = 1;
  307. vals.push_back(v);
  308. }
  309. }
  310. // Insertion
  311. {
  312. // AnkI
  313. timer.start();
  314. for(U i = 0; i < kCount; ++i)
  315. {
  316. akMap.emplace(pool, vals[i], vals[i]);
  317. }
  318. timer.stop();
  319. Second akTime = timer.getElapsedTime();
  320. // STL
  321. timer.start();
  322. for(U i = 0; i < kCount; ++i)
  323. {
  324. stdMap[vals[i]] = vals[i];
  325. }
  326. timer.stop();
  327. Second stlTime = timer.getElapsedTime();
  328. ANKI_TEST_LOGI("Inserting bench: STL %f AnKi %f | %f%%", stlTime, akTime, stlTime / akTime * 100.0);
  329. }
  330. // Search
  331. {
  332. // Search in random order
  333. randomShuffle(vals.begin(), vals.end());
  334. int count = 0;
  335. // Find values AnKi
  336. timer.start();
  337. for(U i = 0; i < kCount; ++i)
  338. {
  339. auto it = akMap.find(vals[i]);
  340. count += *it;
  341. }
  342. timer.stop();
  343. Second akTime = timer.getElapsedTime();
  344. // Find values STL
  345. timer.start();
  346. for(U i = 0; i < kCount; ++i)
  347. {
  348. count += stdMap[vals[i]];
  349. }
  350. timer.stop();
  351. Second stlTime = timer.getElapsedTime();
  352. // Print the "count" so that the compiler won't optimize it
  353. ANKI_TEST_LOGI("Find bench: STL %f AnKi %f | %f%% (r:%d)", stlTime, akTime, stlTime / akTime * 100.0, count);
  354. }
  355. // Mem usage
  356. const I64 stlMemUsage = stlMaxAllocSize + sizeof(stdMap);
  357. const I64 akMemUsage = akMaxAllocSize + sizeof(akMap);
  358. ANKI_TEST_LOGI("Max mem usage: STL %li AnKi %li | %f%% (At any given time what was the max mem usage)", stlMemUsage,
  359. akMemUsage, F64(stlMemUsage) / F32(akMemUsage) * 100.0f);
  360. // Deletes
  361. {
  362. // Remove in random order
  363. randomShuffle(vals.begin(), vals.end());
  364. // Random delete AnKi
  365. Second akTime = 0.0;
  366. for(U i = 0; i < vals.size(); ++i)
  367. {
  368. auto it = akMap.find(vals[i]);
  369. timer.start();
  370. akMap.erase(pool, it);
  371. timer.stop();
  372. akTime += timer.getElapsedTime();
  373. }
  374. // Random delete STL
  375. Second stlTime = 0.0;
  376. for(U i = 0; i < vals.size(); ++i)
  377. {
  378. auto it = stdMap.find(vals[i]);
  379. timer.start();
  380. stdMap.erase(it);
  381. timer.stop();
  382. stlTime += timer.getElapsedTime();
  383. }
  384. ANKI_TEST_LOGI("Deleting bench: STL %f AnKi %f | %f%%", stlTime, akTime, stlTime / akTime * 100.0);
  385. }
  386. akMap.destroy(pool);
  387. }