HashMap.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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 <Tests/Util/Foo.h>
  7. #include <AnKi/Util/HashMap.h>
  8. #include <AnKi/Util/DynamicArray.h>
  9. #include <AnKi/Util/HighRezTimer.h>
  10. #include <unordered_map>
  11. #include <algorithm>
  12. using namespace anki;
  13. class Hasher
  14. {
  15. public:
  16. U64 operator()(int x)
  17. {
  18. return x;
  19. }
  20. };
  21. ANKI_TEST(Util, HashMap)
  22. {
  23. HeapMemoryPool pool(allocAligned, nullptr);
  24. int vals[] = {20, 15, 5, 1, 10, 0, 18, 6, 7, 11, 13, 3};
  25. U valsSize = sizeof(vals) / sizeof(vals[0]);
  26. // Simple
  27. {
  28. HashMap<int, int, Hasher> map;
  29. map.emplace(pool, 20, 1);
  30. map.emplace(pool, 21, 1);
  31. map.destroy(pool);
  32. }
  33. // Add more and iterate
  34. {
  35. HashMap<int, int, Hasher> map;
  36. for(U i = 0; i < valsSize; ++i)
  37. {
  38. map.emplace(pool, vals[i], vals[i] * 10);
  39. }
  40. U count = 0;
  41. auto it = map.getBegin();
  42. for(; it != map.getEnd(); ++it)
  43. {
  44. ++count;
  45. }
  46. ANKI_TEST_EXPECT_EQ(count, valsSize);
  47. map.destroy(pool);
  48. }
  49. // Erase
  50. {
  51. HashMap<int, int, Hasher> map;
  52. for(U i = 0; i < valsSize; ++i)
  53. {
  54. map.emplace(pool, vals[i], vals[i] * 10);
  55. }
  56. for(U i = valsSize - 1; i != 0; --i)
  57. {
  58. HashMap<int, int, Hasher>::Iterator it = map.find(vals[i]);
  59. ANKI_TEST_EXPECT_NEQ(it, map.getEnd());
  60. map.erase(pool, it);
  61. map.emplace(pool, vals[i], vals[i] * 10);
  62. }
  63. map.destroy(pool);
  64. }
  65. // Find
  66. {
  67. HashMap<int, int, Hasher> map;
  68. for(U i = 0; i < valsSize; ++i)
  69. {
  70. map.emplace(pool, vals[i], vals[i] * 10);
  71. }
  72. for(U i = valsSize - 1; i != 0; --i)
  73. {
  74. HashMap<int, int, Hasher>::Iterator it = map.find(vals[i]);
  75. ANKI_TEST_EXPECT_NEQ(it, map.getEnd());
  76. ANKI_TEST_EXPECT_EQ(*it, vals[i] * 10);
  77. }
  78. map.destroy(pool);
  79. }
  80. // Fuzzy test
  81. {
  82. const U MAX = 1000;
  83. HashMap<int, int, Hasher> akMap;
  84. std::vector<int> numbers;
  85. // Insert random
  86. for(U i = 0; i < MAX; ++i)
  87. {
  88. I32 num;
  89. while(1)
  90. {
  91. num = rand();
  92. if(std::find(numbers.begin(), numbers.end(), num) == numbers.end())
  93. {
  94. // Not found
  95. ANKI_TEST_EXPECT_EQ(akMap.find(num), akMap.getEnd());
  96. akMap.emplace(pool, num, num);
  97. numbers.push_back(num);
  98. break;
  99. }
  100. else
  101. {
  102. // Found
  103. ANKI_TEST_EXPECT_NEQ(akMap.find(num), akMap.getEnd());
  104. }
  105. }
  106. }
  107. // Remove randomly
  108. U count = MAX;
  109. while(count--)
  110. {
  111. U idx = rand() % (count + 1);
  112. int num = numbers[idx];
  113. numbers.erase(numbers.begin() + idx);
  114. auto it = akMap.find(num);
  115. akMap.erase(pool, it);
  116. }
  117. akMap.destroy(pool);
  118. }
  119. // Bench it
  120. {
  121. class Config
  122. {
  123. public:
  124. using Index = U64;
  125. static Index getInitialStorageSize()
  126. {
  127. return 128;
  128. }
  129. static U32 getLinearProbingCount()
  130. {
  131. return 32;
  132. }
  133. static F32 getMaxLoadFactor()
  134. {
  135. return 0.9f;
  136. }
  137. };
  138. using AkMap = HashMap<int, int, Hasher, Config>;
  139. AkMap akMap;
  140. using StlMap = std::unordered_map<int, int, std::hash<int>, std::equal_to<int>>;
  141. StlMap stdMap(10, std::hash<int>(), std::equal_to<int>());
  142. std::unordered_map<int, int> tmpMap;
  143. HighRezTimer timer;
  144. // Create a huge set
  145. const U32 COUNT = 1024 * 1024 * 10;
  146. DynamicArrayRaii<int> vals(&pool);
  147. vals.create(COUNT);
  148. for(U32 i = 0; i < COUNT; ++i)
  149. {
  150. // Put unique keys
  151. int v;
  152. do
  153. {
  154. v = rand();
  155. } while(tmpMap.find(v) != tmpMap.end());
  156. tmpMap[v] = 1;
  157. vals[i] = v;
  158. }
  159. // Insertion
  160. {
  161. // Put the vals AnKi
  162. timer.start();
  163. for(U32 i = 0; i < COUNT; ++i)
  164. {
  165. akMap.emplace(pool, vals[i], vals[i]);
  166. }
  167. timer.stop();
  168. Second akTime = timer.getElapsedTime();
  169. // Put the vals STL
  170. timer.start();
  171. for(U32 i = 0; i < COUNT; ++i)
  172. {
  173. stdMap[vals[i]] = vals[i];
  174. }
  175. timer.stop();
  176. Second stlTime = timer.getElapsedTime();
  177. ANKI_TEST_LOGI("Inserting bench: STL %f AnKi %f | %f%%", stlTime, akTime, stlTime / akTime * 100.0);
  178. }
  179. // Search
  180. {
  181. I64 count = 0; // To avoid compiler opts
  182. // Find values AnKi
  183. timer.start();
  184. for(U32 i = 0; i < COUNT; ++i)
  185. {
  186. auto it = akMap.find(vals[i]);
  187. count += *it;
  188. }
  189. timer.stop();
  190. Second akTime = timer.getElapsedTime();
  191. // Find values STL
  192. timer.start();
  193. for(U32 i = 0; i < COUNT; ++i)
  194. {
  195. count += stdMap[vals[i]];
  196. }
  197. timer.stop();
  198. Second stlTime = timer.getElapsedTime();
  199. ANKI_TEST_LOGI("Find bench: STL %f AnKi %f | %f%% (%ld)", stlTime, akTime, stlTime / akTime * 100.0, count);
  200. }
  201. // Delete
  202. {
  203. // Remove in random order
  204. randomShuffle(vals.begin(), vals.end());
  205. // Random delete AnKi
  206. Second akTime = 0.0;
  207. for(U32 i = 0; i < vals.getSize(); ++i)
  208. {
  209. auto it = akMap.find(vals[i]);
  210. timer.start();
  211. akMap.erase(pool, it);
  212. timer.stop();
  213. akTime += timer.getElapsedTime();
  214. }
  215. // Random delete STL
  216. Second stlTime = 0.0;
  217. for(U32 i = 0; i < vals.getSize(); ++i)
  218. {
  219. auto it = stdMap.find(vals[i]);
  220. timer.start();
  221. stdMap.erase(it);
  222. timer.stop();
  223. stlTime += timer.getElapsedTime();
  224. }
  225. ANKI_TEST_LOGI("Deleting bench: STL %f AnKi %f | %f%%", stlTime, akTime, stlTime / akTime * 100.0);
  226. }
  227. akMap.destroy(pool);
  228. }
  229. }