SparseArray.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. // Copyright (C) 2009-2016, 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. ANKI_TEST(Util, SparseArray)
  11. {
  12. HeapAllocator<U8> alloc(allocAligned, nullptr);
  13. // Set same key
  14. {
  15. SparseArray<PtrSize> arr;
  16. arr.emplace(alloc, 1000, 123);
  17. arr.emplace(alloc, 1000, 124);
  18. auto it = arr.find(1000);
  19. ANKI_TEST_EXPECT_EQ(*it, 124);
  20. arr.erase(alloc, it);
  21. }
  22. // Check destroy
  23. {
  24. SparseArray<PtrSize> arr(64, 2);
  25. arr.emplace(alloc, 64 * 1, 123);
  26. arr.emplace(alloc, 64 * 2, 124);
  27. arr.emplace(alloc, 64 * 3, 125);
  28. ANKI_TEST_EXPECT_EQ(*arr.find(64 * 1), 123);
  29. ANKI_TEST_EXPECT_EQ(*arr.find(64 * 2), 124);
  30. ANKI_TEST_EXPECT_EQ(*arr.find(64 * 3), 125);
  31. arr.destroy(alloc);
  32. }
  33. // Do complex insertions
  34. {
  35. SparseArray<PtrSize, U32> arr(64, 3);
  36. arr.emplace(alloc, 64 * 0 - 1, 1);
  37. // Linear probing to 0
  38. arr.emplace(alloc, 64 * 1 - 1, 2);
  39. // Linear probing to 1
  40. arr.emplace(alloc, 64 * 2 - 1, 3);
  41. // Linear probing to 2
  42. arr.emplace(alloc, 1, 3);
  43. // Swap
  44. arr.emplace(alloc, 64 * 1, 3);
  45. ANKI_TEST_EXPECT_EQ(arr.getSize(), 5);
  46. arr.destroy(alloc);
  47. }
  48. // Fuzzy test
  49. {
  50. const U MAX = 1000;
  51. SparseArray<int, U32> arr;
  52. std::vector<int> numbers;
  53. srand(time(nullptr));
  54. // Insert random
  55. for(U i = 0; i < MAX; ++i)
  56. {
  57. U num;
  58. while(1)
  59. {
  60. num = rand();
  61. if(std::find(numbers.begin(), numbers.end(), int(num)) == numbers.end())
  62. {
  63. // Not found
  64. ANKI_TEST_EXPECT_EQ(arr.find(num), arr.getEnd());
  65. arr.emplace(alloc, num, num);
  66. ANKI_TEST_EXPECT_EQ(arr.getSize(), i + 1);
  67. numbers.push_back(num);
  68. break;
  69. }
  70. else
  71. {
  72. // Found
  73. ANKI_TEST_EXPECT_NEQ(arr.find(num), arr.getEnd());
  74. }
  75. }
  76. arr.validate();
  77. }
  78. ANKI_TEST_EXPECT_EQ(arr.getSize(), MAX);
  79. // Remove randomly
  80. U count = MAX;
  81. while(count--)
  82. {
  83. U idx = rand() % (count + 1);
  84. int num = numbers[idx];
  85. numbers.erase(numbers.begin() + idx);
  86. auto it = arr.find(num);
  87. ANKI_TEST_EXPECT_NEQ(it, arr.getEnd());
  88. ANKI_TEST_EXPECT_EQ(*it, num);
  89. arr.erase(alloc, it);
  90. arr.validate();
  91. }
  92. }
  93. // Fuzzy test #2: Do random insertions and removals
  94. {
  95. const U MAX = 10000;
  96. SparseArray<int, U32> arr;
  97. using StlMap =
  98. std::unordered_map<int, int, std::hash<int>, std::equal_to<int>, HeapAllocator<std::pair<int, int>>>;
  99. StlMap map(10, std::hash<int>(), std::equal_to<int>(), alloc);
  100. for(U i = 0; i < MAX; ++i)
  101. {
  102. const Bool insert = (rand() & 1) || arr.getSize() == 0;
  103. if(insert)
  104. {
  105. const I idx = rand();
  106. if(map.find(idx) != map.end())
  107. {
  108. continue;
  109. }
  110. arr.emplace(alloc, idx, idx);
  111. map[idx] = idx;
  112. arr.validate();
  113. }
  114. else
  115. {
  116. const U idx = U(rand()) % map.size();
  117. auto it = std::next(std::begin(map), idx);
  118. auto it2 = arr.find(it->second);
  119. ANKI_TEST_EXPECT_NEQ(it2, arr.getEnd());
  120. map.erase(it);
  121. arr.erase(alloc, it2);
  122. arr.validate();
  123. }
  124. // Iterate and check
  125. {
  126. StlMap bMap = map;
  127. auto it = arr.getBegin();
  128. while(it != arr.getEnd())
  129. {
  130. I val = *it;
  131. auto it2 = bMap.find(val);
  132. ANKI_TEST_EXPECT_NEQ(it2, bMap.end());
  133. bMap.erase(it2);
  134. ++it;
  135. }
  136. }
  137. }
  138. arr.destroy(alloc);
  139. }
  140. }
  141. static PtrSize akAllocSize = 0;
  142. static ANKI_DONT_INLINE void* allocAlignedAk(void* userData, void* ptr, PtrSize size, PtrSize alignment)
  143. {
  144. if(ptr == nullptr)
  145. {
  146. akAllocSize += size;
  147. }
  148. return allocAligned(userData, ptr, size, alignment);
  149. }
  150. static PtrSize stlAllocSize = 0;
  151. static ANKI_DONT_INLINE void* allocAlignedStl(void* userData, void* ptr, PtrSize size, PtrSize alignment)
  152. {
  153. if(ptr == nullptr)
  154. {
  155. stlAllocSize += size;
  156. }
  157. return allocAligned(userData, ptr, size, alignment);
  158. }
  159. ANKI_TEST(Util, SparseArrayBench)
  160. {
  161. HeapAllocator<U8> allocAk(allocAlignedAk, nullptr);
  162. HeapAllocator<U8> allocStl(allocAlignedStl, nullptr);
  163. HeapAllocator<U8> allocTml(allocAligned, nullptr);
  164. using StlMap = std::unordered_map<int, int, std::hash<int>, std::equal_to<int>, HeapAllocator<std::pair<int, int>>>;
  165. StlMap stdMap(10, std::hash<int>(), std::equal_to<int>(), allocStl);
  166. using AkMap = SparseArray<int, U32>;
  167. AkMap akMap(512, log2(512), 0.9f);
  168. HighRezTimer timer;
  169. const U COUNT = 1024 * 1024 * 5;
  170. // Create a huge set
  171. DynamicArrayAuto<int> vals(allocTml);
  172. {
  173. std::unordered_map<int, int> tmpMap;
  174. vals.create(COUNT);
  175. for(U i = 0; i < COUNT; ++i)
  176. {
  177. // Put unique keys
  178. int v;
  179. do
  180. {
  181. v = rand();
  182. } while(tmpMap.find(v) != tmpMap.end());
  183. tmpMap[v] = 1;
  184. vals[i] = v;
  185. }
  186. }
  187. // Insertion
  188. {
  189. // AnkI
  190. timer.start();
  191. for(U i = 0; i < COUNT; ++i)
  192. {
  193. akMap.emplace(allocAk, vals[i], vals[i]);
  194. }
  195. timer.stop();
  196. HighRezTimer::Scalar akTime = timer.getElapsedTime();
  197. // STL
  198. timer.start();
  199. for(U i = 0; i < COUNT; ++i)
  200. {
  201. stdMap[vals[i]] = vals[i];
  202. }
  203. timer.stop();
  204. HighRezTimer::Scalar stlTime = timer.getElapsedTime();
  205. ANKI_TEST_LOGI("Inserting bench: STL %f AnKi %f | %f%%", stlTime, akTime, stlTime / akTime * 100.0);
  206. }
  207. // Search
  208. {
  209. int count = 0;
  210. // Find values AnKi
  211. timer.start();
  212. for(U i = 0; i < COUNT; ++i)
  213. {
  214. auto it = akMap.find(vals[i]);
  215. count += *it;
  216. }
  217. timer.stop();
  218. HighRezTimer::Scalar akTime = timer.getElapsedTime();
  219. // Find values STL
  220. timer.start();
  221. for(U i = 0; i < COUNT; ++i)
  222. {
  223. count += stdMap[vals[i]];
  224. }
  225. timer.stop();
  226. HighRezTimer::Scalar stlTime = timer.getElapsedTime();
  227. ANKI_TEST_LOGI("Find bench: STL %f AnKi %f | %f%%", stlTime, akTime, stlTime / akTime * 100.0);
  228. }
  229. // Mem usage
  230. const PtrSize stlMemUsage = stlAllocSize + sizeof(stdMap);
  231. const PtrSize akMemUsage = akAllocSize + sizeof(akMap);
  232. ANKI_TEST_LOGI(
  233. "Mem usage: STL %llu AnKi %llu | %f%%", stlMemUsage, akMemUsage, F64(stlMemUsage) / akMemUsage * 100.0);
  234. // Deletes
  235. {
  236. const U DEL_COUNT = COUNT / 2;
  237. DynamicArrayAuto<AkMap::Iterator> delValsAk(allocTml);
  238. delValsAk.create(DEL_COUNT);
  239. DynamicArrayAuto<StlMap::iterator> delValsStl(allocTml);
  240. delValsStl.create(DEL_COUNT);
  241. std::unordered_map<int, int> tmpMap;
  242. for(U i = 0; i < DEL_COUNT; ++i)
  243. {
  244. // Put unique keys
  245. int v;
  246. do
  247. {
  248. v = vals[rand() % COUNT];
  249. } while(tmpMap.find(v) != tmpMap.end());
  250. tmpMap[v] = 1;
  251. delValsAk[i] = akMap.find(v);
  252. delValsStl[i] = stdMap.find(v);
  253. }
  254. // Random delete AnKi
  255. timer.start();
  256. for(U i = 0; i < DEL_COUNT; ++i)
  257. {
  258. akMap.erase(allocAk, delValsAk[i]);
  259. }
  260. timer.stop();
  261. HighRezTimer::Scalar akTime = timer.getElapsedTime();
  262. // Random delete STL
  263. timer.start();
  264. for(U i = 0; i < DEL_COUNT; ++i)
  265. {
  266. stdMap.erase(delValsStl[i]);
  267. }
  268. timer.stop();
  269. HighRezTimer::Scalar stlTime = timer.getElapsedTime();
  270. ANKI_TEST_LOGI("Deleting bench: STL %f AnKi %f | %f%%\n", stlTime, akTime, stlTime / akTime * 100.0);
  271. }
  272. akMap.destroy(allocAk);
  273. }