test_ds.c 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034
  1. #ifdef DS_PERF
  2. #define _CRT_SECURE_NO_WARNINGS
  3. #define _CRT_NONSTDC_NO_DEPRECATE
  4. #define _CRT_NON_CONFORMING_SWPRINTFS
  5. //#define STBDS_INTERNAL_SMALL_BUCKET // make 64-bit bucket fit both keys and hash bits
  6. //#define STBDS_SIPHASH_2_4 // performance test 1_3 against 2_4
  7. //#define STBDS_INTERNAL_BUCKET_START // don't bother offseting differently within bucket for different hash values
  8. //#define STBDS_FLUSH_CACHE (1u<<20) // do this much memory traffic to flush the cache between some benchmarking measurements
  9. #include <stdio.h>
  10. #define WIN32_LEAN_AND_MEAN
  11. #include <windows.h>
  12. #define STB_DEFINE
  13. #define STB_NO_REGISTRY
  14. #include "../stb.h"
  15. #endif
  16. #ifdef DS_TEST
  17. #define STBDS_UNIT_TESTS
  18. #define STBDS_SMALL_BUCKET
  19. #endif
  20. #ifdef DS_STATS
  21. #define STBDS_STATISTICS
  22. #endif
  23. #ifndef DS_PERF
  24. #define STBDS_ASSERT assert
  25. #include <assert.h>
  26. #endif
  27. #define STB_DS_IMPLEMENTATION
  28. #include "../stb_ds.h"
  29. size_t churn_inserts, churn_deletes;
  30. void churn(int a, int b, int count)
  31. {
  32. struct { int key,value; } *map=NULL;
  33. int i,j,n,k;
  34. for (i=0; i < a; ++i)
  35. hmput(map,i,i+1);
  36. for (n=0; n < count; ++n) {
  37. for (j=a; j < b; ++j,++i) {
  38. hmput(map,i,i+1);
  39. }
  40. assert(hmlen(map) == b);
  41. for (j=a; j < b; ++j) {
  42. k=i-j-1;
  43. k = hmdel(map,k);
  44. assert(k != 0);
  45. }
  46. assert(hmlen(map) == a);
  47. }
  48. hmfree(map);
  49. churn_inserts = i;
  50. churn_deletes = (b-a) * n;
  51. }
  52. #ifdef DS_TEST
  53. #include <stdio.h>
  54. int main(int argc, char **argv)
  55. {
  56. char *temp=NULL;
  57. stbds_unit_tests();
  58. arrins(temp, 0, 'a');
  59. arrins(temp, arrlen(temp), 'b');
  60. churn(0,100,1);
  61. churn(3,7,50000);
  62. churn(3,15,50000);
  63. churn(16, 48, 25000);
  64. churn(10, 15, 25000);
  65. churn(200,500, 5000);
  66. churn(2000,5000, 500);
  67. churn(20000,50000, 50);
  68. printf("Ok!");
  69. return 0;
  70. }
  71. #endif
  72. #ifdef DS_STATS
  73. #define MAX(a,b) ((a) > (b) ? (a) : (b))
  74. size_t max_hit_probes, max_miss_probes, total_put_probes, total_miss_probes, churn_misses;
  75. void churn_stats(int a, int b, int count)
  76. {
  77. struct { int key,value; } *map=NULL;
  78. int i,j,n,k;
  79. churn_misses = 0;
  80. for (i=0; i < a; ++i) {
  81. hmput(map,i,i+1);
  82. max_hit_probes = MAX(max_hit_probes, stbds_hash_probes);
  83. total_put_probes += stbds_hash_probes;
  84. stbds_hash_probes = 0;
  85. }
  86. for (n=0; n < count; ++n) {
  87. for (j=a; j < b; ++j,++i) {
  88. hmput(map,i,i+1);
  89. max_hit_probes = MAX(max_hit_probes, stbds_hash_probes);
  90. total_put_probes += stbds_hash_probes;
  91. stbds_hash_probes = 0;
  92. }
  93. for (j=0; j < (b-a)*10; ++j) {
  94. k=i+j;
  95. (void) hmgeti(map,k); // miss
  96. max_miss_probes = MAX(max_miss_probes, stbds_hash_probes);
  97. total_miss_probes += stbds_hash_probes;
  98. stbds_hash_probes = 0;
  99. ++churn_misses;
  100. }
  101. assert(hmlen(map) == b);
  102. for (j=a; j < b; ++j) {
  103. k=i-j-1;
  104. k = hmdel(map,k);
  105. stbds_hash_probes = 0;
  106. assert(k);
  107. }
  108. assert(hmlen(map) == a);
  109. }
  110. hmfree(map);
  111. churn_inserts = i;
  112. churn_deletes = (b-a) * n;
  113. }
  114. void reset_stats(void)
  115. {
  116. stbds_array_grow=0,
  117. stbds_hash_grow=0;
  118. stbds_hash_shrink=0;
  119. stbds_hash_rebuild=0;
  120. stbds_hash_probes=0;
  121. stbds_hash_alloc=0;
  122. stbds_rehash_probes=0;
  123. stbds_rehash_items=0;
  124. max_hit_probes = 0;
  125. max_miss_probes = 0;
  126. total_put_probes = 0;
  127. total_miss_probes = 0;
  128. }
  129. void print_churn_probe_stats(char *str)
  130. {
  131. printf("Probes: %3d max hit, %3d max miss, %4.2f avg hit, %4.2f avg miss: %s\n",
  132. (int) max_hit_probes, (int) max_miss_probes, (float) total_put_probes / churn_inserts, (float) total_miss_probes / churn_misses, str);
  133. reset_stats();
  134. }
  135. int main(int arg, char **argv)
  136. {
  137. churn_stats(0,500000,1); print_churn_probe_stats("Inserting 500000 items");
  138. churn_stats(0,500000,1); print_churn_probe_stats("Inserting 500000 items");
  139. churn_stats(0,500000,1); print_churn_probe_stats("Inserting 500000 items");
  140. churn_stats(0,500000,1); print_churn_probe_stats("Inserting 500000 items");
  141. churn_stats(49000,50000,500); print_churn_probe_stats("Deleting/Inserting 500000 items");
  142. churn_stats(49000,50000,500); print_churn_probe_stats("Deleting/Inserting 500000 items");
  143. churn_stats(49000,50000,500); print_churn_probe_stats("Deleting/Inserting 500000 items");
  144. churn_stats(49000,50000,500); print_churn_probe_stats("Deleting/Inserting 500000 items");
  145. return 0;
  146. }
  147. #endif
  148. #ifdef DS_PERF
  149. //char *strdup(const char *foo) { return 0; }
  150. //int stricmp(const char *a, const char *b) { return 0; }
  151. //int strnicmp(const char *a, const char *b, size_t n) { return 0; }
  152. unsigned __int64 t0, xsum, mn,mx,count;
  153. void begin(void)
  154. {
  155. LARGE_INTEGER m;
  156. QueryPerformanceCounter(&m);
  157. t0 = m.QuadPart;
  158. xsum = 0;
  159. count = 0;
  160. mx = 0;
  161. mn = ~(unsigned __int64) 0;
  162. }
  163. void measure(void)
  164. {
  165. unsigned __int64 t1, t;
  166. LARGE_INTEGER m;
  167. QueryPerformanceCounter(&m);
  168. t1 = m.QuadPart;
  169. t = t1-t0;
  170. if (t1 < t0)
  171. printf("ALERT: QueryPerformanceCounter was unordered!\n");
  172. if (t < mn) mn = t;
  173. if (t > mx) mx = t;
  174. xsum += t;
  175. ++count;
  176. t0 = t1;
  177. }
  178. void dont_measure(void)
  179. {
  180. LARGE_INTEGER m;
  181. QueryPerformanceCounter(&m);
  182. t0 = m.QuadPart;
  183. }
  184. double timer;
  185. double end(void)
  186. {
  187. LARGE_INTEGER m;
  188. QueryPerformanceFrequency(&m);
  189. if (count > 3) {
  190. // discard the highest and lowest
  191. xsum -= mn;
  192. xsum -= mx;
  193. count -= 2;
  194. }
  195. timer = (double) (xsum) / count / m.QuadPart * 1000;
  196. return timer;
  197. }
  198. void build(int a, int b, int count, int step)
  199. {
  200. struct { int key,value; } *map=NULL;
  201. int i,n;
  202. for (i=0; i < a; ++i) {
  203. n = i*step;
  204. hmput(map,n,i+1);
  205. }
  206. measure();
  207. churn_inserts = i;
  208. hmfree(map);
  209. dont_measure();
  210. }
  211. #ifdef STB__INCLUDE_STB_H
  212. void build_stb(int a, int b, int count, int step)
  213. {
  214. stb_idict *d = stb_idict_new_size(8);
  215. int i;
  216. for (i=0; i < a; ++i)
  217. stb_idict_add(d, i*step, i+1);
  218. measure();
  219. churn_inserts = i;
  220. stb_idict_destroy(d);
  221. dont_measure();
  222. }
  223. void multibuild_stb(int a, int b, int count, int step, int tables)
  224. {
  225. stb_idict *d[50000];
  226. int i,q;
  227. for (q=0; q < tables; ++q)
  228. d[q] = stb_idict_new_size(8);
  229. dont_measure();
  230. for (i=0; i < a; ++i)
  231. for (q=0; q < tables; ++q)
  232. stb_idict_add(d[q], i*step+q*771, i+1);
  233. measure();
  234. churn_inserts = i;
  235. for (q=0; q < tables; ++q)
  236. stb_idict_destroy(d[q]);
  237. dont_measure();
  238. }
  239. int multisearch_stb(int a, int start, int end, int step, int tables)
  240. {
  241. stb_idict *d[50000];
  242. int i,q,total=0,v;
  243. for (q=0; q < tables; ++q)
  244. d[q] = stb_idict_new_size(8);
  245. for (q=0; q < tables; ++q)
  246. for (i=0; i < a; ++i)
  247. stb_idict_add(d[q], i*step+q*771, i+1);
  248. dont_measure();
  249. for (i=start; i < end; ++i)
  250. for (q=0; q < tables; ++q)
  251. if (stb_idict_get_flag(d[q], i*step+q*771, &v))
  252. total += v;
  253. measure();
  254. churn_inserts = i;
  255. for (q=0; q < tables; ++q)
  256. stb_idict_destroy(d[q]);
  257. dont_measure();
  258. return total;
  259. }
  260. #endif
  261. int multisearch(int a, int start, int end, int step, int tables)
  262. {
  263. struct { int key,value; } *hash[50000];
  264. int i,q,total=0;
  265. for (q=0; q < tables; ++q)
  266. hash[q] = NULL;
  267. for (q=0; q < tables; ++q)
  268. for (i=0; i < a; ++i)
  269. hmput(hash[q], i*step+q*771, i+1);
  270. dont_measure();
  271. for (i=start; i < end; ++i)
  272. for (q=0; q < tables; ++q)
  273. total += hmget(hash[q], i*step+q*771);
  274. measure();
  275. churn_inserts = i;
  276. for (q=0; q < tables; ++q)
  277. hmfree(hash[q]);
  278. dont_measure();
  279. return total;
  280. }
  281. void churn_skip(unsigned int a, unsigned int b, int count)
  282. {
  283. struct { unsigned int key,value; } *map=NULL;
  284. unsigned int i,j,n,k;
  285. for (i=0; i < a; ++i)
  286. hmput(map,i,i+1);
  287. dont_measure();
  288. for (n=0; n < count; ++n) {
  289. for (j=a; j < b; ++j,++i) {
  290. hmput(map,i,i+1);
  291. }
  292. assert(hmlen(map) == b);
  293. for (j=a; j < b; ++j) {
  294. k=i-j-1;
  295. k = hmdel(map,k);
  296. assert(k != 0);
  297. }
  298. assert(hmlen(map) == a);
  299. }
  300. measure();
  301. churn_inserts = i;
  302. churn_deletes = (b-a) * n;
  303. hmfree(map);
  304. dont_measure();
  305. }
  306. typedef struct { int n[8]; } str32;
  307. void churn32(int a, int b, int count, int include_startup)
  308. {
  309. struct { str32 key; int value; } *map=NULL;
  310. int i,j,n;
  311. str32 key = { 0 };
  312. for (i=0; i < a; ++i) {
  313. key.n[0] = i;
  314. hmput(map,key,i+1);
  315. }
  316. if (!include_startup)
  317. dont_measure();
  318. for (n=0; n < count; ++n) {
  319. for (j=a; j < b; ++j,++i) {
  320. key.n[0] = i;
  321. hmput(map,key,i+1);
  322. }
  323. assert(hmlen(map) == b);
  324. for (j=a; j < b; ++j) {
  325. key.n[0] = i-j-1;
  326. hmdel(map,key);
  327. }
  328. assert(hmlen(map) == a);
  329. }
  330. measure();
  331. hmfree(map);
  332. churn_inserts = i;
  333. churn_deletes = (b-a) * n;
  334. dont_measure();
  335. }
  336. typedef struct { int n[32]; } str256;
  337. void churn256(int a, int b, int count, int include_startup)
  338. {
  339. struct { str256 key; int value; } *map=NULL;
  340. int i,j,n;
  341. str256 key = { 0 };
  342. for (i=0; i < a; ++i) {
  343. key.n[0] = i;
  344. hmput(map,key,i+1);
  345. }
  346. if (!include_startup)
  347. dont_measure();
  348. for (n=0; n < count; ++n) {
  349. for (j=a; j < b; ++j,++i) {
  350. key.n[0] = i;
  351. hmput(map,key,i+1);
  352. }
  353. assert(hmlen(map) == b);
  354. for (j=a; j < b; ++j) {
  355. key.n[0] = i-j-1;
  356. hmdel(map,key);
  357. }
  358. assert(hmlen(map) == a);
  359. }
  360. measure();
  361. hmfree(map);
  362. churn_inserts = i;
  363. churn_deletes = (b-a) * n;
  364. dont_measure();
  365. }
  366. void churn8(int a, int b, int count, int include_startup)
  367. {
  368. struct { size_t key,value; } *map=NULL;
  369. int i,j,n,k;
  370. for (i=0; i < a; ++i)
  371. hmput(map,i,i+1);
  372. if (!include_startup)
  373. dont_measure();
  374. for (n=0; n < count; ++n) {
  375. for (j=a; j < b; ++j,++i) {
  376. hmput(map,i,i+1);
  377. }
  378. assert(hmlen(map) == b);
  379. for (j=a; j < b; ++j) {
  380. k=i-j-1;
  381. k = hmdel(map,k);
  382. assert(k != 0);
  383. }
  384. assert(hmlen(map) == a);
  385. }
  386. measure();
  387. hmfree(map);
  388. churn_inserts = i;
  389. churn_deletes = (b-a) * n;
  390. dont_measure();
  391. }
  392. void multichurn4(int a, int b, int count, int include_startup, int tables)
  393. {
  394. struct { int key,value; } *map[50000];
  395. int i,j,n,k,q;
  396. for (q=0; q < tables; ++q)
  397. map[q] = NULL;
  398. dont_measure();
  399. for (i=0; i < a; ++i)
  400. for (q=0; q < tables; ++q)
  401. hmput(map[q],i,i+1);
  402. if (!include_startup)
  403. dont_measure();
  404. for (n=0; n < count; ++n) {
  405. for (j=a; j < b; ++j,++i) {
  406. for (q=0; q < tables; ++q)
  407. hmput(map[q],i,i+1);
  408. }
  409. assert(hmlen(map[0]) == b);
  410. for (j=a; j < b; ++j) {
  411. k=i-j-1;
  412. for (q=0; q < tables; ++q)
  413. k = hmdel(map[q],k);
  414. assert(k != 0);
  415. }
  416. assert(hmlen(map[0]) == a);
  417. }
  418. measure();
  419. for (q=0; q < tables; ++q)
  420. hmfree(map[q]);
  421. churn_inserts = i * tables;
  422. churn_deletes = (b-a) * n * tables;
  423. dont_measure();
  424. }
  425. struct {
  426. unsigned __int64 start;
  427. unsigned __int64 end;
  428. int table_size;
  429. } mstats[32][4000];
  430. const int first_step = 64;
  431. const int last_step = 384-48; // 32M
  432. void measure_build4(int step_log2)
  433. {
  434. double length;
  435. int i,j,k=0;
  436. int step = 1 << step_log2;
  437. unsigned __int64 t0,t1;
  438. struct { int key,value; } *map=NULL;
  439. double rdtsc_scale;
  440. begin();
  441. t0 = __rdtsc();
  442. mstats[0][0].start = __rdtsc();
  443. for (i=0; i < 256; ++i) {
  444. hmput(map,k,k+1);
  445. k += step;
  446. }
  447. mstats[0][first_step-1].end = __rdtsc();
  448. mstats[0][first_step-1].table_size = k >> step_log2;
  449. for (j=first_step; j < last_step; ++j) {
  450. for (i=0; i < (1<<(j>>4)); ++i) {
  451. hmput(map, k,k+1);
  452. k += step;
  453. }
  454. mstats[0][j].end = __rdtsc();
  455. mstats[0][j].table_size = k >> step_log2;
  456. }
  457. t1 = __rdtsc();
  458. measure();
  459. hmfree(map);
  460. length = end();
  461. rdtsc_scale = length / (t1-t0) * 1000;
  462. for (j=1; j < last_step; ++j)
  463. mstats[0][j].start = mstats[0][0].start;
  464. for (j=first_step-1; j < last_step; ++j) {
  465. printf("%12.4f,%12d,%12d,0,0,0\n", (mstats[0][j].end - mstats[0][j].start) * rdtsc_scale, mstats[0][j].table_size, mstats[0][j].table_size);
  466. }
  467. }
  468. #ifdef STBDS_FLUSH_CACHE
  469. static int cache_index;
  470. char dummy[8][STBDS_FLUSH_CACHE];
  471. int flush_cache(void)
  472. {
  473. memmove(dummy[cache_index],dummy[cache_index]+1, sizeof(dummy[cache_index])-1);
  474. cache_index = (cache_index+1)%8;
  475. return dummy[cache_index][0];
  476. }
  477. #else
  478. int flush_cache(void) { return 0; }
  479. #endif
  480. int measure_average_lookup4(int step_log2)
  481. {
  482. int total;
  483. double length;
  484. int i,j,k=0,q;
  485. int step = 1 << step_log2;
  486. unsigned __int64 t0,t1;
  487. struct { int key,value; } *map=NULL;
  488. double rdtsc_scale;
  489. begin();
  490. t0 = __rdtsc();
  491. for (i=0; i < 128; ++i) {
  492. hmput(map,k,k+1);
  493. k += step;
  494. }
  495. for (j=first_step; j <= last_step; ++j) {
  496. total += flush_cache();
  497. mstats[0][j].start = __rdtsc();
  498. for (q=i=0; i < 50000; ++i) {
  499. total += hmget(map, q); // hit
  500. if (++q == k) q = 0;
  501. }
  502. mstats[0][j].end = __rdtsc();
  503. mstats[0][j].table_size = k;
  504. total += flush_cache();
  505. mstats[1][j].start = __rdtsc();
  506. for (i=0; i < 50000; ++i) {
  507. total += hmget(map, i+k); // miss
  508. }
  509. mstats[1][j].end = __rdtsc();
  510. mstats[1][j].table_size = k;
  511. // expand table
  512. for (i=0; i < (1<<(j>>4)); ++i) {
  513. hmput(map, k,k+1);
  514. k += step;
  515. }
  516. }
  517. t1 = __rdtsc();
  518. measure();
  519. hmfree(map);
  520. length = end();
  521. rdtsc_scale = length / (t1-t0) * 1000;
  522. for (j=first_step; j <= last_step; ++j) {
  523. // time,table_size,numins,numhit,nummiss,numperflush
  524. printf("%12.4f,%12d,0,50000,0,0\n", (mstats[0][j].end - mstats[0][j].start) * rdtsc_scale, mstats[0][j].table_size);
  525. }
  526. for (j=first_step; j <= last_step; ++j) {
  527. printf("%12.4f,%12d,0,0,50000,0\n", (mstats[1][j].end - mstats[1][j].start) * rdtsc_scale, mstats[1][j].table_size);
  528. }
  529. return total;
  530. }
  531. int measure_worst_lookup4_a(int step_log2)
  532. {
  533. int total;
  534. double length;
  535. int i,j,k=0,q,worst_q,n,z,attempts;
  536. int step = 1 << step_log2;
  537. unsigned __int64 t0,t1;
  538. unsigned __int64 m0,m1,worst;
  539. struct { int key,value; } *map=NULL;
  540. double rdtsc_scale;
  541. begin();
  542. t0 = __rdtsc();
  543. memset(mstats, 0, sizeof(mstats));
  544. for (j=first_step; j <= last_step; ++j)
  545. mstats[0][j].end = mstats[1][j].end = ~(unsigned __int64) 0;
  546. for(attempts=0; attempts < 2; ++attempts) {
  547. k = 0;
  548. stbds_rand_seed(0); // force us to get the same table every time
  549. for (i=0; i < 128; ++i) {
  550. hmput(map,k,k+1);
  551. k += step;
  552. }
  553. for (j=first_step; j <= last_step; ++j) {
  554. unsigned __int64 times[32];
  555. // find the worst hit time
  556. for (z=0; z < 2; ++z) { // try the bisectioning measurement 4 times
  557. worst = 0;
  558. for (n=0; n < 10; ++n) { // test 400 keys total
  559. // find the worst time to hit 20 keys
  560. q=0;
  561. worst_q = 0;
  562. total += flush_cache();
  563. m0 = __rdtsc();
  564. for (i=0; i < 20; ++i) {
  565. total += hmget(map, q); // hit
  566. if (++q == k) q = 0;
  567. }
  568. m1 = __rdtsc();
  569. // for each n, check if this is the worst lookup we've seen
  570. if (m1 - m0 > worst) {
  571. worst = m1-m0;
  572. worst_q = q - i;
  573. if (worst_q < 0) q += k;
  574. }
  575. }
  576. // after 400 keys, take the worst 20 keys, and try each one
  577. worst = 0;
  578. q = worst_q;
  579. for (i=0; i < 20; ++i) {
  580. total += flush_cache();
  581. m0 = __rdtsc();
  582. total += hmget(map, q); // hit
  583. m1 = __rdtsc();
  584. if (m1 - m0 > worst)
  585. worst = m1-m0;
  586. if (++q == k) q = 0;
  587. }
  588. times[z] = worst;
  589. }
  590. // find the worst time in the bunch
  591. worst = 0;
  592. for (i=0; i < z; ++i)
  593. if (times[i] > worst)
  594. worst = times[i];
  595. // take the best of 'attempts', to discard outliers
  596. if (worst < mstats[0][j].end)
  597. mstats[0][j].end = worst;
  598. mstats[0][j].start = 0;
  599. mstats[0][j].table_size = k >> step_log2;
  600. // find the worst miss time
  601. for (z=0; z < 8; ++z) { // try the bisectioning measurement 8 times
  602. worst = 0;
  603. for (n=0; n < 20; ++n) { // test 400 keys total
  604. // find the worst time to hit 20 keys
  605. q=k;
  606. worst_q = 0;
  607. total += flush_cache();
  608. m0 = __rdtsc();
  609. for (i=0; i < 20; ++i) {
  610. total += hmget(map, q); // hit
  611. }
  612. m1 = __rdtsc();
  613. // for each n, check if this is the worst lookup we've seen
  614. if (m1 - m0 > worst) {
  615. worst = m1-m0;
  616. worst_q = q - i;
  617. }
  618. }
  619. // after 400 keys, take the worst 20 keys, and try each one
  620. worst = 0;
  621. q = worst_q;
  622. for (i=0; i < 20; ++i) {
  623. total += flush_cache();
  624. m0 = __rdtsc();
  625. total += hmget(map, q); // hit
  626. m1 = __rdtsc();
  627. if (m1 - m0 > worst)
  628. worst = m1-m0;
  629. }
  630. times[z] = worst;
  631. }
  632. // find the worst time in the bunch
  633. worst = 0;
  634. for (i=0; i < z; ++i)
  635. if (times[i] > worst)
  636. worst = times[i];
  637. if (worst < mstats[1][j].end)
  638. mstats[1][j].end = worst;
  639. mstats[1][j].start = 0;
  640. mstats[1][j].table_size = k >> step_log2;
  641. // expand table
  642. for (i=0; i < (1<<(j>>4)); ++i) {
  643. hmput(map, k,k+1);
  644. k += step;
  645. }
  646. }
  647. hmfree(map);
  648. }
  649. t1 = __rdtsc();
  650. measure();
  651. length = end();
  652. rdtsc_scale = length / (t1-t0) * 1000;
  653. for (j=first_step; j <= last_step; ++j) {
  654. printf("%12.4f,%12d,0,1,0,1\n", (mstats[0][j].end - mstats[0][j].start) * rdtsc_scale, mstats[0][j].table_size);
  655. }
  656. for (j=first_step; j <= last_step; ++j) {
  657. printf("%12.4f,%12d,0,0,1,1\n", (mstats[1][j].end - mstats[1][j].start) * rdtsc_scale, mstats[1][j].table_size);
  658. }
  659. return total;
  660. }
  661. int measure_worst_lookup4_b(int step_log2)
  662. {
  663. int total;
  664. double length;
  665. int i,j,k=0,q,worst_q,n,z,attempts;
  666. int step = 1 << step_log2;
  667. unsigned __int64 t0,t1;
  668. unsigned __int64 m0,m1,worst;
  669. struct { int key,value; } *map=NULL;
  670. double rdtsc_scale;
  671. begin();
  672. t0 = __rdtsc();
  673. memset(mstats, 0, sizeof(mstats));
  674. for (j=first_step; j <= last_step; ++j)
  675. mstats[0][j].end = mstats[1][j].end = ~(unsigned __int64) 0;
  676. k = 0;
  677. stbds_rand_seed(0); // force us to get the same table every time
  678. for (i=0; i < 128; ++i) {
  679. hmput(map,k,k+1);
  680. k += step;
  681. }
  682. for (j=first_step; j <= last_step; ++j) {
  683. unsigned __int64 times[32];
  684. // find the worst hit time
  685. for (z=0; z < 8; ++z) { // try this 8 times
  686. worst = 0;
  687. q=0;
  688. for (i=0; i < 5000; ++i) {
  689. total += hmget(map, q);
  690. m0 = __rdtsc();
  691. total += hmget(map, q);
  692. m1 = __rdtsc();
  693. if (m1 - m0 > worst) {
  694. worst = m1-m0;
  695. worst_q = q - i;
  696. }
  697. if (++q == k) q = 0;
  698. }
  699. // now retry with the worst one, but find the shortest time for it
  700. worst = ~(unsigned __int64) 0;
  701. for (i=0; i < 4; ++i) {
  702. total += flush_cache();
  703. m0 = __rdtsc();
  704. total += hmget(map,worst_q);
  705. m1 = __rdtsc();
  706. if (m1-m0 < worst)
  707. worst = m1-m0;
  708. }
  709. times[z] = worst;
  710. }
  711. // find the worst of those
  712. worst = 0;
  713. for (i=0; i < z; ++i)
  714. if (times[i] > worst)
  715. worst = times[i];
  716. mstats[0][j].start = 0;
  717. mstats[0][j].end = worst;
  718. mstats[0][j].table_size = k;
  719. // find the worst miss time
  720. for (z=0; z < 8; ++z) { // try this 8 times
  721. worst = 0;
  722. q=k;
  723. for (i=0; i < 5000; ++i) {
  724. total += hmget(map, q);
  725. m0 = __rdtsc();
  726. total += hmget(map, q);
  727. m1 = __rdtsc();
  728. if (m1 - m0 > worst) {
  729. worst = m1-m0;
  730. worst_q = q - i;
  731. }
  732. //printf("%6llu ", m1-m0);
  733. }
  734. // now retry with the worst one, but find the shortest time for it
  735. worst = ~(unsigned __int64) 0;
  736. for (i=0; i < 4; ++i) {
  737. total += flush_cache();
  738. m0 = __rdtsc();
  739. total += hmget(map,worst_q);
  740. m1 = __rdtsc();
  741. if (m1-m0 < worst)
  742. worst = m1-m0;
  743. }
  744. times[z] = worst;
  745. }
  746. // find the worst of those
  747. worst = 0;
  748. for (i=0; i < z; ++i)
  749. if (times[i] > worst)
  750. worst = times[i];
  751. mstats[1][j].start = 0;
  752. mstats[1][j].end = worst;
  753. mstats[1][j].table_size = k;
  754. // expand table
  755. for (i=0; i < (1<<(j>>4)); ++i) {
  756. hmput(map, k,k+1);
  757. k += step;
  758. }
  759. }
  760. hmfree(map);
  761. t1 = __rdtsc();
  762. measure();
  763. length = end();
  764. rdtsc_scale = length / (t1-t0) * 1000;
  765. for (j=first_step+1; j <= last_step; ++j) {
  766. printf("%12.4f,%12d,0,1,0,1\n", (mstats[0][j].end - mstats[0][j].start) * rdtsc_scale, mstats[0][j].table_size);
  767. }
  768. for (j=first_step+1; j <= last_step; ++j) {
  769. printf("%12.4f,%12d,0,0,1,1\n", (mstats[1][j].end - mstats[1][j].start) * rdtsc_scale, mstats[1][j].table_size);
  770. }
  771. return total;
  772. }
  773. int measure_uncached_lookup4(int step_log2)
  774. {
  775. int total;
  776. double length;
  777. int i,j,k=0,q;
  778. int step = 1 << step_log2;
  779. unsigned __int64 t0,t1;
  780. struct { int key,value; } *map=NULL;
  781. double rdtsc_scale;
  782. begin();
  783. t0 = __rdtsc();
  784. map = NULL;
  785. for (i=0; i < 128; ++i) {
  786. hmput(map,k,k+1);
  787. k += step;
  788. }
  789. for (j=first_step; j <= last_step; ++j) {
  790. mstats[0][j].start = __rdtsc();
  791. mstats[0][j].end = 0;
  792. for (q=i=0; i < 512; ++i) {
  793. if ((i & 3) == 0) {
  794. mstats[0][j].end += __rdtsc();
  795. total += flush_cache();
  796. mstats[0][j].start += __rdtsc();
  797. }
  798. total += hmget(map, q); // hit
  799. if (++q == k) q = 0;
  800. }
  801. mstats[0][j].end += __rdtsc();
  802. mstats[0][j].table_size = k;
  803. total += flush_cache();
  804. mstats[1][j].end = 0;
  805. mstats[1][j].start = __rdtsc();
  806. for (i=0; i < 512; ++i) {
  807. if ((i & 3) == 0) {
  808. mstats[1][j].end += __rdtsc();
  809. total += flush_cache();
  810. mstats[1][j].start += __rdtsc();
  811. }
  812. total += hmget(map, i+k); // miss
  813. }
  814. mstats[1][j].end += __rdtsc();
  815. mstats[1][j].table_size = k;
  816. // expand table
  817. for (i=0; i < (1<<(j>>4)); ++i) {
  818. hmput(map, k,k+1);
  819. k += step;
  820. }
  821. }
  822. hmfree(map);
  823. t1 = __rdtsc();
  824. measure();
  825. length = end();
  826. rdtsc_scale = length / (t1-t0) * 1000;
  827. for (j=first_step; j <= last_step; ++j) {
  828. printf("%12.4f,%12d,0,512,0,4\n", (mstats[0][j].end - mstats[0][j].start) * rdtsc_scale, mstats[0][j].table_size);
  829. }
  830. for (j=first_step; j <= last_step; ++j) {
  831. printf("%12.4f,%12d,0,0,512,4\n", (mstats[1][j].end - mstats[1][j].start) * rdtsc_scale, mstats[1][j].table_size);
  832. }
  833. return total;
  834. }
  835. int main(int arg, char **argv)
  836. {
  837. int n,s,w;
  838. double worst = 0;
  839. printf("# size_t=%d,", (int) sizeof(size_t));
  840. // number of cache-lines
  841. #ifdef STBDS_SMALL_BUCKET
  842. printf("cacheline=%d,", 1);
  843. #else
  844. printf("cacheline=%d,", sizeof(size_t)==8 ? 2 : 1);
  845. #endif
  846. #ifdef STBDS_FLUSH_CACHE
  847. printf("%d,", (int) stbds_log2(STBDS_FLUSH_CACHE));
  848. #else
  849. printf("0,");
  850. #endif
  851. #ifdef STBDS_BUCKET_START // don't bother offseting differently within bucket for different hash values
  852. printf("STBDS_BUCKET_START,");
  853. #else
  854. printf(",");
  855. #endif
  856. #ifdef STBDS_SIPHASH_2_4
  857. printf("STBDS_SIPHASH_2_4,");
  858. #else
  859. printf(",");
  860. #endif
  861. printf("\n");
  862. measure_worst_lookup4_b(0);
  863. //measure_worst_lookup4_a(0);
  864. measure_average_lookup4(0);
  865. measure_uncached_lookup4(0);
  866. measure_build4(0);
  867. return 0;
  868. #if 0
  869. begin(); for (n=0; n < 2000; ++n) { build_stb(2000,0,0,1); } end(); printf(" // %7.2fms : 2,000 inserts creating 2K table\n", timer);
  870. begin(); for (n=0; n < 500; ++n) { build_stb(20000,0,0,1); } end(); printf(" // %7.2fms : 20,000 inserts creating 20K table\n", timer);
  871. begin(); for (n=0; n < 100; ++n) { build_stb(200000,0,0,1); } end(); printf(" // %7.2fms : 200,000 inserts creating 200K table\n", timer);
  872. begin(); for (n=0; n < 10; ++n) { build_stb(2000000,0,0,1); } end(); printf(" // %7.2fms : 2,000,000 inserts creating 2M table\n", timer);
  873. begin(); for (n=0; n < 5; ++n) { build_stb(20000000,0,0,1); } end(); printf(" // %7.2fms : 20,000,000 inserts creating 20M table\n", timer);
  874. #endif
  875. #if 0
  876. begin(); for (n=0; n < 2000; ++n) { churn32(2000,0,0,1); } end(); printf(" // %7.2fms : 2,000 inserts creating 2K table w/ 32-byte key\n", timer);
  877. begin(); for (n=0; n < 500; ++n) { churn32(20000,0,0,1); } end(); printf(" // %7.2fms : 20,000 inserts creating 20K table w/ 32-byte key\n", timer);
  878. begin(); for (n=0; n < 100; ++n) { churn32(200000,0,0,1); } end(); printf(" // %7.2fms : 200,000 inserts creating 200K table w/ 32-byte key\n", timer);
  879. begin(); for (n=0; n < 10; ++n) { churn32(2000000,0,0,1); } end(); printf(" // %7.2fms : 2,000,000 inserts creating 2M table w/ 32-byte key\n", timer);
  880. begin(); for (n=0; n < 5; ++n) { churn32(20000000,0,0,1); } end(); printf(" // %7.2fms : 20,000,000 inserts creating 20M table w/ 32-byte key\n", timer);
  881. begin(); for (n=0; n < 2000; ++n) { churn256(2000,0,0,1); } end(); printf(" // %7.2fms : 2,000 inserts creating 2K table w/ 256-byte key\n", timer);
  882. begin(); for (n=0; n < 500; ++n) { churn256(20000,0,0,1); } end(); printf(" // %7.2fms : 20,000 inserts creating 20K table w/ 256-byte key\n", timer);
  883. begin(); for (n=0; n < 100; ++n) { churn256(200000,0,0,1); } end(); printf(" // %7.2fms : 200,000 inserts creating 200K table w/ 256-byte key\n", timer);
  884. begin(); for (n=0; n < 10; ++n) { churn256(2000000,0,0,1); } end(); printf(" // %7.2fms : 2,000,000 inserts creating 2M table w/ 256-byte key\n", timer);
  885. begin(); for (n=0; n < 5; ++n) { churn256(20000000,0,0,1); } end(); printf(" // %7.2fms : 20,000,000 inserts creating 20M table w/ 256-byte key\n", timer);
  886. #endif
  887. begin(); for (n=0; n < 20; ++n) { multisearch_stb(2000,0,2000,1,1000); } end(); printf(" // %7.2fms : 2,000,000 hits on 1,000 2K table w/ 4-byte key\n", timer);
  888. begin(); for (n=0; n < 10; ++n) { multisearch_stb(20000,0,2000,1,1000); } end(); printf(" // %7.2fms : 2,000,000 hits on 1,000 20K table w/ 4-byte key\n", timer);
  889. begin(); for (n=0; n < 6; ++n) { multisearch_stb(200000,0,2000,1,1000); } end(); printf(" // %7.2fms : 2,000,000 hits on 1,000 200K table w/ 4-byte key\n", timer);
  890. begin(); for (n=0; n < 2; ++n) { multisearch_stb(2000000,0,20000,1,100); } end(); printf(" // %7.2fms : 2,000,000 hits on 100 2M table w/ 4-byte key\n", timer);
  891. begin(); for (n=0; n < 20; ++n) { multisearch (2000,0,2000,1,1000); } end(); printf(" // %7.2fms : 2,000,000 hits on 1,000 2K table w/ 4-byte key\n", timer);
  892. begin(); for (n=0; n < 10; ++n) { multisearch (20000,0,2000,1,1000); } end(); printf(" // %7.2fms : 2,000,000 hits on 1,000 20K table w/ 4-byte key\n", timer);
  893. begin(); for (n=0; n < 6; ++n) { multisearch (200000,0,2000,1,1000); } end(); printf(" // %7.2fms : 2,000,000 hits on 1,000 200K table w/ 4-byte key\n", timer);
  894. begin(); for (n=0; n < 2; ++n) { multisearch (2000000,0,20000,1,100); } end(); printf(" // %7.2fms : 2,000,000 hits on 100 2M table w/ 4-byte key\n", timer);
  895. #if 1
  896. begin(); for (n=0; n < 2; ++n) { multibuild_stb(2000,0,0,1,10000); } end(); printf(" // %7.2fms : 20,000,000 inserts creating 10,000 2K table w/ 4-byte key\n", timer);
  897. begin(); for (n=0; n < 2; ++n) { multibuild_stb(20000,0,0,1,1000); } end(); printf(" // %7.2fms : 20,000,000 inserts creating 1,000 20K table w/ 4-byte key\n", timer);
  898. begin(); for (n=0; n < 2; ++n) { multibuild_stb(200000,0,0,1,100); } end(); printf(" // %7.2fms : 20,000,000 inserts creating 100 200K table w/ 4-byte key\n", timer);
  899. begin(); for (n=0; n < 2; ++n) { multibuild_stb(2000000,0,0,1,10); } end(); printf(" // %7.2fms : 20,000,000 inserts creating 10 2M table w/ 4-byte key\n", timer);
  900. begin(); for (n=0; n < 2; ++n) { multichurn4(2000,0,0,1,10000); } end(); printf(" // %7.2fms : 20,000,000 inserts creating 10,000 2K table w/ 4-byte key\n", timer);
  901. begin(); for (n=0; n < 2; ++n) { multichurn4(20000,0,0,1,1000); } end(); printf(" // %7.2fms : 20,000,000 inserts creating 1,000 20K table w/ 4-byte key\n", timer);
  902. begin(); for (n=0; n < 2; ++n) { multichurn4(200000,0,0,1,100); } end(); printf(" // %7.2fms : 20,000,000 inserts creating 100 200K table w/ 4-byte key\n", timer);
  903. begin(); for (n=0; n < 2; ++n) { multichurn4(2000000,0,0,1,10); } end(); printf(" // %7.2fms : 20,000,000 inserts creating 10 2M table w/ 4-byte key\n", timer);
  904. #endif
  905. begin(); for (n=0; n < 2000; ++n) { build(2000,0,0,1); } end(); printf(" // %7.2fms : 2,000 inserts creating 2K table w/ 4-byte key\n", timer);
  906. begin(); for (n=0; n < 500; ++n) { build(20000,0,0,1); } end(); printf(" // %7.2fms : 20,000 inserts creating 20K table w/ 4-byte key\n", timer);
  907. begin(); for (n=0; n < 100; ++n) { build(200000,0,0,1); } end(); printf(" // %7.2fms : 200,000 inserts creating 200K table w/ 4-byte key\n", timer);
  908. begin(); for (n=0; n < 10; ++n) { build(2000000,0,0,1); } end(); printf(" // %7.2fms : 2,000,000 inserts creating 2M table w/ 4-byte key\n", timer);
  909. begin(); for (n=0; n < 5; ++n) { build(20000000,0,0,1); } end(); printf(" // %7.2fms : 20,000,000 inserts creating 20M table w/ 4-byte key\n", timer);
  910. begin(); for (n=0; n < 2000; ++n) { churn8(2000,0,0,1); } end(); printf(" // %7.2fms : 2,000 inserts creating 2K table w/ 8-byte key\n", timer);
  911. begin(); for (n=0; n < 500; ++n) { churn8(20000,0,0,1); } end(); printf(" // %7.2fms : 20,000 inserts creating 20K table w/ 8-byte key\n", timer);
  912. begin(); for (n=0; n < 100; ++n) { churn8(200000,0,0,1); } end(); printf(" // %7.2fms : 200,000 inserts creating 200K table w/ 8-byte key\n", timer);
  913. begin(); for (n=0; n < 10; ++n) { churn8(2000000,0,0,1); } end(); printf(" // %7.2fms : 2,000,000 inserts creating 2M table w/ 8-byte key\n", timer);
  914. begin(); for (n=0; n < 5; ++n) { churn8(20000000,0,0,1); } end(); printf(" // %7.2fms : 20,000,000 inserts creating 20M table w/ 8-byte key\n", timer);
  915. begin(); for (n=0; n < 60; ++n) { churn_skip(2000,2100,5000); } end(); printf(" // %7.2fms : 500,000 inserts & deletes in 2K table\n", timer);
  916. begin(); for (n=0; n < 30; ++n) { churn_skip(20000,21000,500); } end(); printf(" // %7.2fms : 500,000 inserts & deletes in 20K table\n", timer);
  917. begin(); for (n=0; n < 15; ++n) { churn_skip(200000,201000,500); } end(); printf(" // %7.2fms : 500,000 inserts & deletes in 200K table\n", timer);
  918. begin(); for (n=0; n < 8; ++n) { churn_skip(2000000,2001000,500); } end(); printf(" // %7.2fms : 500,000 inserts & deletes in 2M table\n", timer);
  919. begin(); for (n=0; n < 5; ++n) { churn_skip(20000000,20001000,500); } end(); printf(" // %7.2fms : 500,000 inserts & deletes in 20M table\n", timer);
  920. begin(); for (n=0; n < 1; ++n) { churn_skip(200000000u,200001000u,500); } end(); printf(" // %7.2fms : 500,000 inserts & deletes in 200M table\n", timer);
  921. // even though the above measures a roughly fixed amount of work, we still have to build the table n times, hence the fewer measurements each time
  922. begin(); for (n=0; n < 60; ++n) { churn_skip(1000,3000,250); } end(); printf(" // %7.2fms : 500,000 inserts & deletes in 2K table\n", timer);
  923. begin(); for (n=0; n < 15; ++n) { churn_skip(10000,30000,25); } end(); printf(" // %7.2fms : 500,000 inserts & deletes in 20K table\n", timer);
  924. begin(); for (n=0; n < 7; ++n) { churn_skip(100000,300000,10); } end(); printf(" // %7.2fms : 2,000,000 inserts & deletes in 200K table\n", timer);
  925. begin(); for (n=0; n < 2; ++n) { churn_skip(1000000,3000000,10); } end(); printf(" // %7.2fms : 20,000,000 inserts & deletes in 2M table\n", timer);
  926. // search for bad intervals.. in practice this just seems to measure execution variance
  927. for (s = 2; s < 64; ++s) {
  928. begin(); for (n=0; n < 50; ++n) { build(200000,0,0,s); } end();
  929. if (timer > worst) {
  930. worst = timer;
  931. w = s;
  932. }
  933. }
  934. for (; s <= 1024; s *= 2) {
  935. begin(); for (n=0; n < 50; ++n) { build(200000,0,0,s); } end();
  936. if (timer > worst) {
  937. worst = timer;
  938. w = s;
  939. }
  940. }
  941. printf(" // %7.2fms(%d) : Worst time from inserting 200,000 items with spacing %d.\n", worst, w, w);
  942. return 0;
  943. }
  944. #endif