2
0

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