SparseArray.cpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  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. HeapAllocator<U8> alloc(allocAligned, nullptr);
  102. // Set same key
  103. {
  104. SparseArray<PtrSize> arr;
  105. arr.emplace(alloc, 1000, 123);
  106. arr.emplace(alloc, 1000, 124);
  107. auto it = arr.find(1000);
  108. ANKI_TEST_EXPECT_EQ(*it, 124);
  109. arr.erase(alloc, it);
  110. }
  111. // Check destroy and grow
  112. {
  113. SparseArray<SAFoo, Config<U32>> arr(Config<U32>{64, 2, 0.8f});
  114. arr.emplace(alloc, 64 * 1, 123);
  115. arr.emplace(alloc, 64 * 2, 124);
  116. arr.emplace(alloc, 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(alloc);
  122. SAFoo::checkCalls();
  123. }
  124. // Do complex insertions
  125. {
  126. SparseArray<SAFoo, Config<U32>> arr(Config<U32>{64, 3, 0.8f});
  127. arr.emplace(alloc, 64u * 0 - 1, 1);
  128. // Linear probing to 0
  129. arr.emplace(alloc, 64 * 1 - 1, 2);
  130. // Linear probing to 1
  131. arr.emplace(alloc, 64 * 2 - 1, 3);
  132. // Linear probing to 2
  133. arr.emplace(alloc, 1, 3);
  134. // Swap
  135. arr.emplace(alloc, 64 * 1, 3);
  136. ANKI_TEST_EXPECT_EQ(arr.getSize(), 5);
  137. arr.destroy(alloc);
  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(alloc, 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(alloc, 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 =
  190. std::unordered_map<int, int, std::hash<int>, std::equal_to<int>, HeapAllocator<std::pair<const int, int>>>;
  191. StlMap map(10, std::hash<int>(), std::equal_to<int>(), alloc);
  192. for(U i = 0; i < kMax; ++i)
  193. {
  194. const Bool insert = (rand() & 1) || arr.getSize() == 0;
  195. if(insert)
  196. {
  197. const I32 idx = rand();
  198. if(map.find(idx) != map.end())
  199. {
  200. continue;
  201. }
  202. arr.emplace(alloc, idx, idx + 1);
  203. map[idx] = idx + 1;
  204. arr.validate();
  205. }
  206. else
  207. {
  208. const U idx = U(rand()) % map.size();
  209. auto it = std::next(std::begin(map), idx);
  210. int key = it->first;
  211. auto it2 = arr.find(key);
  212. ANKI_TEST_EXPECT_NEQ(it2, arr.getEnd());
  213. ANKI_TEST_EXPECT_EQ(it->second, it2->m_x);
  214. map.erase(it);
  215. arr.erase(alloc, it2);
  216. ANKI_TEST_EXPECT_EQ(arr.find(key), arr.getEnd());
  217. arr.validate();
  218. }
  219. // Iterate and check
  220. {
  221. StlMap bMap = map;
  222. auto it = arr.getBegin();
  223. while(it != arr.getEnd())
  224. {
  225. I32 key = it->m_x - 1;
  226. auto it2 = bMap.find(key);
  227. ANKI_TEST_EXPECT_NEQ(it2, bMap.end());
  228. bMap.erase(it2);
  229. ++it;
  230. }
  231. }
  232. }
  233. arr.destroy(alloc);
  234. // Check what the SparseArray have called
  235. SAFoo::checkCalls();
  236. }
  237. }
  238. static I64 akAllocSize = 0;
  239. static I64 akMaxAllocSize = 0;
  240. static ANKI_DONT_INLINE void* allocAlignedAk([[maybe_unused]] void* userData, void* ptr, PtrSize size,
  241. [[maybe_unused]] PtrSize alignment)
  242. {
  243. if(ptr == nullptr)
  244. {
  245. #if ANKI_OS_LINUX
  246. akAllocSize += size;
  247. akMaxAllocSize = max(akMaxAllocSize, akAllocSize);
  248. #endif
  249. return malloc(size);
  250. }
  251. else
  252. {
  253. #if ANKI_OS_LINUX
  254. PtrSize s = malloc_usable_size(ptr);
  255. akAllocSize -= s;
  256. #endif
  257. free(ptr);
  258. return nullptr;
  259. }
  260. }
  261. static I64 stlAllocSize = 0;
  262. static I64 stlMaxAllocSize = 0;
  263. static ANKI_DONT_INLINE void* allocAlignedStl([[maybe_unused]] void* userData, void* ptr, PtrSize size,
  264. [[maybe_unused]] PtrSize alignment)
  265. {
  266. if(ptr == nullptr)
  267. {
  268. #if ANKI_OS_LINUX
  269. stlAllocSize += size;
  270. stlMaxAllocSize = max(stlMaxAllocSize, stlAllocSize);
  271. #endif
  272. return malloc(size);
  273. }
  274. else
  275. {
  276. #if ANKI_OS_LINUX
  277. PtrSize s = malloc_usable_size(ptr);
  278. stlAllocSize -= s;
  279. #endif
  280. free(ptr);
  281. return nullptr;
  282. }
  283. }
  284. ANKI_TEST(Util, SparseArrayBench)
  285. {
  286. HeapAllocator<U8> allocAk(allocAlignedAk, nullptr);
  287. HeapAllocator<U8> allocStl(allocAlignedStl, nullptr);
  288. HeapAllocator<U8> allocTml(allocAligned, nullptr);
  289. using StlMap =
  290. std::unordered_map<int, int, std::hash<int>, std::equal_to<int>, HeapAllocator<std::pair<const int, int>>>;
  291. StlMap stdMap(10, std::hash<int>(), std::equal_to<int>(), allocStl);
  292. using AkMap = SparseArray<int, Config<U32>>;
  293. AkMap akMap(Config<U32>{256, U32(log2(256.0f)), 0.9f});
  294. HighRezTimer timer;
  295. const U COUNT = 1024 * 1024 * 6;
  296. // Create a huge set
  297. std::vector<int> vals;
  298. {
  299. std::unordered_map<int, int> tmpMap;
  300. for(U i = 0; i < COUNT; ++i)
  301. {
  302. // Put unique keys
  303. int v;
  304. do
  305. {
  306. v = int(getRandom());
  307. } while(tmpMap.find(v) != tmpMap.end() && v != 0);
  308. tmpMap[v] = 1;
  309. vals.push_back(v);
  310. }
  311. }
  312. // Insertion
  313. {
  314. // AnkI
  315. timer.start();
  316. for(U i = 0; i < COUNT; ++i)
  317. {
  318. akMap.emplace(allocAk, vals[i], vals[i]);
  319. }
  320. timer.stop();
  321. Second akTime = timer.getElapsedTime();
  322. // STL
  323. timer.start();
  324. for(U i = 0; i < COUNT; ++i)
  325. {
  326. stdMap[vals[i]] = vals[i];
  327. }
  328. timer.stop();
  329. Second stlTime = timer.getElapsedTime();
  330. ANKI_TEST_LOGI("Inserting bench: STL %f AnKi %f | %f%%", stlTime, akTime, stlTime / akTime * 100.0);
  331. }
  332. // Search
  333. {
  334. // Search in random order
  335. randomShuffle(vals.begin(), vals.end());
  336. int count = 0;
  337. // Find values AnKi
  338. timer.start();
  339. for(U i = 0; i < COUNT; ++i)
  340. {
  341. auto it = akMap.find(vals[i]);
  342. count += *it;
  343. }
  344. timer.stop();
  345. Second akTime = timer.getElapsedTime();
  346. // Find values STL
  347. timer.start();
  348. for(U i = 0; i < COUNT; ++i)
  349. {
  350. count += stdMap[vals[i]];
  351. }
  352. timer.stop();
  353. Second stlTime = timer.getElapsedTime();
  354. // Print the "count" so that the compiler won't optimize it
  355. ANKI_TEST_LOGI("Find bench: STL %f AnKi %f | %f%% (r:%d)", stlTime, akTime, stlTime / akTime * 100.0, count);
  356. }
  357. // Mem usage
  358. const I64 stlMemUsage = stlMaxAllocSize + sizeof(stdMap);
  359. const I64 akMemUsage = akMaxAllocSize + sizeof(akMap);
  360. ANKI_TEST_LOGI("Max mem usage: STL %li AnKi %li | %f%% (At any given time what was the max mem usage)", stlMemUsage,
  361. akMemUsage, F64(stlMemUsage) / F32(akMemUsage) * 100.0f);
  362. // Deletes
  363. {
  364. // Remove in random order
  365. randomShuffle(vals.begin(), vals.end());
  366. // Random delete AnKi
  367. Second akTime = 0.0;
  368. for(U i = 0; i < vals.size(); ++i)
  369. {
  370. auto it = akMap.find(vals[i]);
  371. timer.start();
  372. akMap.erase(allocAk, it);
  373. timer.stop();
  374. akTime += timer.getElapsedTime();
  375. }
  376. // Random delete STL
  377. Second stlTime = 0.0;
  378. for(U i = 0; i < vals.size(); ++i)
  379. {
  380. auto it = stdMap.find(vals[i]);
  381. timer.start();
  382. stdMap.erase(it);
  383. timer.stop();
  384. stlTime += timer.getElapsedTime();
  385. }
  386. ANKI_TEST_LOGI("Deleting bench: STL %f AnKi %f | %f%%", stlTime, akTime, stlTime / akTime * 100.0);
  387. }
  388. akMap.destroy(allocAk);
  389. }