Skip to content
Myr

test_ds.c

This file

Path

tests/test_ds.c

1,034 lines · 31.5 KiB · C

Last changed by

stb_ds: fix arrisn, shgetp_nullSean Barrettbefore adoption781609b

History of this file

Take it

1#ifdef DS_PERF2#define _CRT_SECURE_NO_WARNINGS3#define _CRT_NONSTDC_NO_DEPRECATE4#define _CRT_NON_CONFORMING_SWPRINTFS567//#define STBDS_INTERNAL_SMALL_BUCKET    // make 64-bit bucket fit both keys and hash bits8//#define STBDS_SIPHASH_2_4     // performance test 1_3 against 2_49//#define STBDS_INTERNAL_BUCKET_START    // don't bother offseting differently within bucket for different hash values10//#define STBDS_FLUSH_CACHE  (1u<<20) // do this much memory traffic to flush the cache between some benchmarking measurements1112#include <stdio.h>1314#define WIN32_LEAN_AND_MEAN15#include <windows.h>16#define STB_DEFINE17#define STB_NO_REGISTRY18#include "../stb.h"19#endif2021#ifdef DS_TEST22#define STBDS_UNIT_TESTS23#define STBDS_SMALL_BUCKET24#endif2526#ifdef DS_STATS27#define STBDS_STATISTICS28#endif2930#ifndef DS_PERF31#define STBDS_ASSERT assert32#include <assert.h>33#endif3435#define STB_DS_IMPLEMENTATION36#include "../stb_ds.h"3738size_t churn_inserts, churn_deletes;3940void churn(int a, int b, int count)41{42  struct { int key,value; } *map=NULL;43  int i,j,n,k;44  for (i=0; i < a; ++i)45    hmput(map,i,i+1);46  for (n=0; n < count; ++n) {47    for (j=a; j < b; ++j,++i) {48      hmput(map,i,i+1);49    }50    assert(hmlen(map) == b);51    for (j=a; j < b; ++j) {52      k=i-j-1;53      k = hmdel(map,k);54      assert(k != 0);55    }56    assert(hmlen(map) == a);57  }58  hmfree(map);59  churn_inserts = i;60  churn_deletes = (b-a) * n;61}6263#ifdef DS_TEST64#include <stdio.h>65int main(int argc, char **argv)66{67  char *temp=NULL;68  stbds_unit_tests();69  arrins(temp, 0, 'a');70  arrins(temp, arrlen(temp), 'b');71  churn(0,100,1);72  churn(3,7,50000);73  churn(3,15,50000);74  churn(16, 48, 25000);75  churn(10, 15, 25000);76  churn(200,500, 5000);77  churn(2000,5000, 500);78  churn(20000,50000, 50);79  printf("Ok!");80  return 0;81}82#endif8384#ifdef DS_STATS85#define MAX(a,b) ((a) > (b) ? (a) : (b))86size_t max_hit_probes, max_miss_probes, total_put_probes, total_miss_probes, churn_misses;87void churn_stats(int a, int b, int count)88{89  struct { int key,value; } *map=NULL;90  int i,j,n,k;91  churn_misses = 0;92  for (i=0; i < a; ++i) {93    hmput(map,i,i+1);94    max_hit_probes = MAX(max_hit_probes, stbds_hash_probes);95    total_put_probes += stbds_hash_probes;96    stbds_hash_probes = 0;97  }98    99  for (n=0; n < count; ++n) {100    for (j=a; j < b; ++j,++i) {101      hmput(map,i,i+1);102      max_hit_probes = MAX(max_hit_probes, stbds_hash_probes);103      total_put_probes += stbds_hash_probes;104      stbds_hash_probes = 0;105    }106    for (j=0; j < (b-a)*10; ++j) {107      k=i+j;108      (void) hmgeti(map,k); // miss109      max_miss_probes = MAX(max_miss_probes, stbds_hash_probes);110      total_miss_probes += stbds_hash_probes;111      stbds_hash_probes = 0;112      ++churn_misses;113    }114    assert(hmlen(map) == b);115    for (j=a; j < b; ++j) {116      k=i-j-1;117      k = hmdel(map,k);118      stbds_hash_probes = 0;119      assert(k);120    }121    assert(hmlen(map) == a);122  }123  hmfree(map);124  churn_inserts = i;125  churn_deletes = (b-a) * n;126}127128void reset_stats(void)129{130  stbds_array_grow=0, 131  stbds_hash_grow=0;132  stbds_hash_shrink=0;133  stbds_hash_rebuild=0;134  stbds_hash_probes=0;135  stbds_hash_alloc=0;136  stbds_rehash_probes=0;137  stbds_rehash_items=0;138  max_hit_probes = 0;139  max_miss_probes = 0;140  total_put_probes = 0;141  total_miss_probes = 0;142}143144void print_churn_probe_stats(char *str)145{146  printf("Probes: %3d max hit, %3d max miss, %4.2f avg hit, %4.2f avg miss: %s\n",147    (int) max_hit_probes, (int) max_miss_probes, (float) total_put_probes / churn_inserts, (float) total_miss_probes / churn_misses, str);148  reset_stats();149}150151int main(int arg, char **argv)152{153  churn_stats(0,500000,1); print_churn_probe_stats("Inserting 500000 items");154  churn_stats(0,500000,1); print_churn_probe_stats("Inserting 500000 items");155  churn_stats(0,500000,1); print_churn_probe_stats("Inserting 500000 items");156  churn_stats(0,500000,1); print_churn_probe_stats("Inserting 500000 items");157  churn_stats(49000,50000,500); print_churn_probe_stats("Deleting/Inserting 500000 items");158  churn_stats(49000,50000,500); print_churn_probe_stats("Deleting/Inserting 500000 items");159  churn_stats(49000,50000,500); print_churn_probe_stats("Deleting/Inserting 500000 items");160  churn_stats(49000,50000,500); print_churn_probe_stats("Deleting/Inserting 500000 items");161  return 0;162}163#endif164165166#ifdef DS_PERF167//char *strdup(const char *foo) { return 0; }168//int stricmp(const char *a, const char *b) { return 0; }169//int strnicmp(const char *a, const char *b, size_t n) { return 0; }170171unsigned __int64 t0, xsum, mn,mx,count;172void begin(void)173{174  LARGE_INTEGER m;175  QueryPerformanceCounter(&m);176  t0 = m.QuadPart;177  xsum = 0;178  count = 0;179  mx = 0;180  mn = ~(unsigned __int64) 0;181}182183void measure(void)184{185  unsigned __int64 t1, t;186  LARGE_INTEGER m;187  QueryPerformanceCounter(&m);188  t1 = m.QuadPart;189  t = t1-t0;190  if (t1 < t0)191    printf("ALERT: QueryPerformanceCounter was unordered!\n");192  if (t < mn) mn = t;193  if (t > mx) mx = t;194  xsum += t;195  ++count;196  t0 = t1;197}198199void dont_measure(void)200{201  LARGE_INTEGER m;202  QueryPerformanceCounter(&m);203  t0 = m.QuadPart;204}205206double timer;207double end(void)208{209  LARGE_INTEGER m;210  QueryPerformanceFrequency(&m);211212  if (count > 3) {213    // discard the highest and lowest214    xsum -= mn;215    xsum -= mx;216    count -= 2;217  }218  timer = (double) (xsum) / count / m.QuadPart * 1000;219  return timer;220}221222void build(int a, int b, int count, int step)223{224  struct { int key,value; } *map=NULL;225  int i,n;226  for (i=0; i < a; ++i) {227    n = i*step;228    hmput(map,n,i+1);229  }230  measure();231  churn_inserts = i;232  hmfree(map);233  dont_measure();234}235236#ifdef STB__INCLUDE_STB_H237void build_stb(int a, int b, int count, int step)238{239  stb_idict *d = stb_idict_new_size(8);240  int i;241  for (i=0; i < a; ++i)242    stb_idict_add(d, i*step, i+1);243  measure();244  churn_inserts = i;245  stb_idict_destroy(d);246  dont_measure();247}248249void multibuild_stb(int a, int b, int count, int step, int tables)250{251  stb_idict *d[50000];252  int i,q;253  for (q=0; q < tables; ++q)254    d[q] = stb_idict_new_size(8);255  dont_measure();256  for (i=0; i < a; ++i)257    for (q=0; q < tables; ++q)258      stb_idict_add(d[q], i*step+q*771, i+1);259  measure();260  churn_inserts = i;261  for (q=0; q < tables; ++q)262    stb_idict_destroy(d[q]);263  dont_measure();264}265266int multisearch_stb(int a, int start, int end, int step, int tables)267{268  stb_idict *d[50000];269  int i,q,total=0,v;270  for (q=0; q < tables; ++q)271    d[q] = stb_idict_new_size(8);272  for (q=0; q < tables; ++q)273    for (i=0; i < a; ++i)274      stb_idict_add(d[q], i*step+q*771, i+1);275  dont_measure();276  for (i=start; i < end; ++i)277    for (q=0; q < tables; ++q)278      if (stb_idict_get_flag(d[q], i*step+q*771, &v))279        total += v;280  measure();281  churn_inserts = i;282  for (q=0; q < tables; ++q)283    stb_idict_destroy(d[q]);284  dont_measure();285  return total;286}287#endif288289int multisearch(int a, int start, int end, int step, int tables)290{291  struct { int key,value; } *hash[50000];292  int i,q,total=0;293  for (q=0; q < tables; ++q)294    hash[q] = NULL;295  for (q=0; q < tables; ++q)296    for (i=0; i < a; ++i)297      hmput(hash[q], i*step+q*771, i+1);298  dont_measure();299  for (i=start; i < end; ++i)300    for (q=0; q < tables; ++q)301      total += hmget(hash[q], i*step+q*771);302  measure();303  churn_inserts = i;304  for (q=0; q < tables; ++q)305    hmfree(hash[q]);306  dont_measure();307  return total;308}309310void churn_skip(unsigned int a, unsigned int b, int count)311{312  struct { unsigned int key,value; } *map=NULL;313  unsigned int i,j,n,k;314  for (i=0; i < a; ++i)315    hmput(map,i,i+1);316  dont_measure();317  for (n=0; n < count; ++n) {318    for (j=a; j < b; ++j,++i) {319      hmput(map,i,i+1);320    }321    assert(hmlen(map) == b);322    for (j=a; j < b; ++j) {323      k=i-j-1;324      k = hmdel(map,k);325      assert(k != 0);326    }327    assert(hmlen(map) == a);328  }329  measure();330  churn_inserts = i;331  churn_deletes = (b-a) * n;332  hmfree(map);333  dont_measure();334}335336typedef struct { int n[8]; } str32;337void churn32(int a, int b, int count, int include_startup)338{339  struct { str32 key; int value; } *map=NULL;340  int i,j,n;341  str32 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}366367typedef struct { int n[32]; } str256;368void churn256(int a, int b, int count, int include_startup)369{370  struct { str256 key; int value; } *map=NULL;371  int i,j,n;372  str256 key = { 0 };373  for (i=0; i < a; ++i) {374    key.n[0] = i;375    hmput(map,key,i+1);376  }377  if (!include_startup)378    dont_measure();379  for (n=0; n < count; ++n) {380    for (j=a; j < b; ++j,++i) {381      key.n[0] = i;382      hmput(map,key,i+1);383    }384    assert(hmlen(map) == b);385    for (j=a; j < b; ++j) {386      key.n[0] = i-j-1;387      hmdel(map,key);388    }389    assert(hmlen(map) == a);390  }391  measure();392  hmfree(map);393  churn_inserts = i;394  churn_deletes = (b-a) * n;395  dont_measure();396}397398void churn8(int a, int b, int count, int include_startup)399{400  struct { size_t key,value; } *map=NULL;401  int i,j,n,k;402  for (i=0; i < a; ++i)403    hmput(map,i,i+1);404  if (!include_startup)405    dont_measure();406  for (n=0; n < count; ++n) {407    for (j=a; j < b; ++j,++i) {408      hmput(map,i,i+1);409    }410    assert(hmlen(map) == b);411    for (j=a; j < b; ++j) {412      k=i-j-1;413      k = hmdel(map,k);414      assert(k != 0);415    }416    assert(hmlen(map) == a);417  }418  measure();419  hmfree(map);420  churn_inserts = i;421  churn_deletes = (b-a) * n;422  dont_measure();423}424425void multichurn4(int a, int b, int count, int include_startup, int tables)426{427  struct { int key,value; } *map[50000];428  int i,j,n,k,q;429  for (q=0; q < tables; ++q)430    map[q] = NULL;431  dont_measure();432433  for (i=0; i < a; ++i)434    for (q=0; q < tables; ++q)435      hmput(map[q],i,i+1);436  if (!include_startup)437    dont_measure();438  for (n=0; n < count; ++n) {439    for (j=a; j < b; ++j,++i) {440      for (q=0; q < tables; ++q)441        hmput(map[q],i,i+1);442    }443    assert(hmlen(map[0]) == b);444    for (j=a; j < b; ++j) {445      k=i-j-1;446      for (q=0; q < tables; ++q)447        k = hmdel(map[q],k);448      assert(k != 0);449    }450    assert(hmlen(map[0]) == a);451  }452  measure();453  for (q=0; q < tables; ++q)454    hmfree(map[q]);455  churn_inserts = i * tables;456  churn_deletes = (b-a) * n * tables;457  dont_measure();458}459460struct {461  unsigned __int64 start;462  unsigned __int64 end;463  int    table_size;464} mstats[32][4000];465466const int first_step = 64;467const int last_step = 384-48; // 32M468469void measure_build4(int step_log2)470{471  double length;472  int i,j,k=0;473  int step = 1 << step_log2;474  unsigned __int64 t0,t1;475  struct { int key,value; } *map=NULL;476  double rdtsc_scale;477  begin();478  t0 = __rdtsc();479480  mstats[0][0].start = __rdtsc();481  for (i=0; i < 256; ++i) {482    hmput(map,k,k+1);483    k += step;484  }485  mstats[0][first_step-1].end = __rdtsc();486  mstats[0][first_step-1].table_size = k >> step_log2;487  for (j=first_step; j < last_step; ++j) {488    for (i=0; i < (1<<(j>>4)); ++i) {489      hmput(map, k,k+1);490      k += step;491    }492    mstats[0][j].end = __rdtsc();493    mstats[0][j].table_size = k >> step_log2;494  }495  t1 = __rdtsc();496  measure();497  hmfree(map);498  length = end();499  rdtsc_scale = length / (t1-t0) * 1000;500501  for (j=1; j < last_step; ++j)502    mstats[0][j].start = mstats[0][0].start;503  for (j=first_step-1; j < last_step; ++j) {504    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);505  }506}507508#ifdef STBDS_FLUSH_CACHE509static int cache_index;510char dummy[8][STBDS_FLUSH_CACHE];511512int flush_cache(void)513{514  memmove(dummy[cache_index],dummy[cache_index]+1, sizeof(dummy[cache_index])-1);515  cache_index = (cache_index+1)%8;516  return dummy[cache_index][0];517}518#else519int flush_cache(void) { return 0; }520#endif521522int measure_average_lookup4(int step_log2)523{524  int total;525  double length;526  int i,j,k=0,q;527  int step = 1 << step_log2;528  unsigned __int64 t0,t1;529  struct { int key,value; } *map=NULL;530  double rdtsc_scale;531  begin();532  t0 = __rdtsc();533534  for (i=0; i < 128; ++i) {535    hmput(map,k,k+1);536    k += step;537  }538  for (j=first_step; j <= last_step; ++j) {539    total += flush_cache();540    mstats[0][j].start = __rdtsc();541    for (q=i=0; i < 50000; ++i) {542       total += hmget(map, q); // hit543       if (++q == k) q = 0;544    }545    mstats[0][j].end = __rdtsc();546    mstats[0][j].table_size = k;547    total += flush_cache();548    mstats[1][j].start = __rdtsc();549    for (i=0; i < 50000; ++i) {550       total += hmget(map, i+k); // miss551    }552    mstats[1][j].end = __rdtsc();553    mstats[1][j].table_size = k;554555    // expand table556    for (i=0; i < (1<<(j>>4)); ++i) {557      hmput(map, k,k+1);558      k += step;559    }560  }561562  t1 = __rdtsc();563  measure();564  hmfree(map);565  length = end();566  rdtsc_scale = length / (t1-t0) * 1000;567568  for (j=first_step; j <= last_step; ++j) {569    // time,table_size,numins,numhit,nummiss,numperflush570    printf("%12.4f,%12d,0,50000,0,0\n", (mstats[0][j].end - mstats[0][j].start) * rdtsc_scale, mstats[0][j].table_size);571  }572  for (j=first_step; j <= last_step; ++j) {573    printf("%12.4f,%12d,0,0,50000,0\n", (mstats[1][j].end - mstats[1][j].start) * rdtsc_scale, mstats[1][j].table_size);574  }575  return total;576}577578int measure_worst_lookup4_a(int step_log2)579{580  int total;581  double length;582  int i,j,k=0,q,worst_q,n,z,attempts;583  int step = 1 << step_log2;584  unsigned __int64 t0,t1;585  unsigned __int64 m0,m1,worst;586  struct { int key,value; } *map=NULL;587  double rdtsc_scale;588  begin();589  t0 = __rdtsc();590591  memset(mstats, 0, sizeof(mstats));592  for (j=first_step; j <= last_step; ++j)593    mstats[0][j].end = mstats[1][j].end = ~(unsigned __int64) 0;594595  for(attempts=0; attempts < 2; ++attempts) {596    k = 0;597    stbds_rand_seed(0); // force us to get the same table every time598    for (i=0; i < 128; ++i) {599      hmput(map,k,k+1);600      k += step;601    }602    for (j=first_step; j <= last_step; ++j) {603      unsigned __int64 times[32];604605      // find the worst hit time606      for (z=0; z < 2; ++z) { // try the bisectioning measurement 4 times607        worst = 0;608        for (n=0; n < 10; ++n) { // test 400 keys total609          // find the worst time to hit 20 keys610          q=0;611          worst_q = 0;612          total += flush_cache();613          m0 = __rdtsc();614          for (i=0; i < 20; ++i) {615            total += hmget(map, q); // hit616            if (++q == k) q = 0;617          }618          m1 = __rdtsc();619          // for each n, check if this is the worst lookup we've seen620          if (m1 - m0 > worst) {621            worst = m1-m0;622            worst_q = q - i;623            if (worst_q < 0) q += k;624          }625        }626        // after 400 keys, take the worst 20 keys, and try each one627        worst = 0;628        q = worst_q;629        for (i=0; i < 20; ++i) {630          total += flush_cache();631          m0 = __rdtsc();632          total += hmget(map, q); // hit633          m1 = __rdtsc();634          if (m1 - m0 > worst)635            worst = m1-m0;636          if (++q == k) q = 0;637        }638        times[z] = worst;639      }640      // find the worst time in the bunch641      worst = 0;642      for (i=0; i < z; ++i)643        if (times[i] > worst)644          worst = times[i];645      // take the best of 'attempts', to discard outliers646      if (worst < mstats[0][j].end)647        mstats[0][j].end = worst;648      mstats[0][j].start = 0;649      mstats[0][j].table_size = k >> step_log2;650651      // find the worst miss time652      for (z=0; z < 8; ++z) { // try the bisectioning measurement 8 times653        worst = 0;654        for (n=0; n < 20; ++n) { // test 400 keys total655          // find the worst time to hit 20 keys656          q=k;657          worst_q = 0;658          total += flush_cache();659          m0 = __rdtsc();660          for (i=0; i < 20; ++i) {661            total += hmget(map, q); // hit662          }663          m1 = __rdtsc();664          // for each n, check if this is the worst lookup we've seen665          if (m1 - m0 > worst) {666            worst = m1-m0;667            worst_q = q - i;668          }669        }670        // after 400 keys, take the worst 20 keys, and try each one671        worst = 0;672        q = worst_q;673        for (i=0; i < 20; ++i) {674          total += flush_cache();675          m0 = __rdtsc();676          total += hmget(map, q); // hit677          m1 = __rdtsc();678          if (m1 - m0 > worst)679            worst = m1-m0;680        }681        times[z] = worst;682      }683      // find the worst time in the bunch684      worst = 0;685      for (i=0; i < z; ++i)686        if (times[i] > worst)687          worst = times[i];688      if (worst < mstats[1][j].end)689        mstats[1][j].end = worst;690      mstats[1][j].start = 0;691      mstats[1][j].table_size = k >> step_log2;692693      // expand table694      for (i=0; i < (1<<(j>>4)); ++i) {695        hmput(map, k,k+1);696        k += step;697      }698    }699    hmfree(map);700  }701702  t1 = __rdtsc();703  measure();704  length = end();705  rdtsc_scale = length / (t1-t0) * 1000;706707  for (j=first_step; j <= last_step; ++j) {708    printf("%12.4f,%12d,0,1,0,1\n", (mstats[0][j].end - mstats[0][j].start) * rdtsc_scale, mstats[0][j].table_size);709  }710  for (j=first_step; j <= last_step; ++j) {711    printf("%12.4f,%12d,0,0,1,1\n", (mstats[1][j].end - mstats[1][j].start) * rdtsc_scale, mstats[1][j].table_size);712  }713  return total;714}715716int measure_worst_lookup4_b(int step_log2)717{718  int total;719  double length;720  int i,j,k=0,q,worst_q,n,z,attempts;721  int step = 1 << step_log2;722  unsigned __int64 t0,t1;723  unsigned __int64 m0,m1,worst;724  struct { int key,value; } *map=NULL;725  double rdtsc_scale;726  begin();727  t0 = __rdtsc();728729  memset(mstats, 0, sizeof(mstats));730  for (j=first_step; j <= last_step; ++j)731    mstats[0][j].end = mstats[1][j].end = ~(unsigned __int64) 0;732733  k = 0;734  stbds_rand_seed(0); // force us to get the same table every time735  for (i=0; i < 128; ++i) {736    hmput(map,k,k+1);737    k += step;738  }739  for (j=first_step; j <= last_step; ++j) {740    unsigned __int64 times[32];741742    // find the worst hit time743    for (z=0; z < 8; ++z) { // try this 8 times744      worst = 0;745      q=0;746      for (i=0; i < 5000; ++i) {747        total += hmget(map, q);748        m0 = __rdtsc();749        total += hmget(map, q);750        m1 = __rdtsc();751        if (m1 - m0 > worst) {752          worst = m1-m0;753          worst_q = q - i;754        }755        if (++q == k) q = 0;756      }757      // now retry with the worst one, but find the shortest time for it758      worst = ~(unsigned __int64) 0;759      for (i=0; i < 4; ++i) {760        total += flush_cache();761        m0 = __rdtsc();762        total += hmget(map,worst_q);763        m1 = __rdtsc();764        if (m1-m0 < worst)765          worst = m1-m0;766      }767      times[z] = worst;768    }769770    // find the worst of those771    worst = 0;772    for (i=0; i < z; ++i)773      if (times[i] > worst)774        worst = times[i];775    mstats[0][j].start = 0;776    mstats[0][j].end = worst;777    mstats[0][j].table_size = k;778779    // find the worst miss time780    for (z=0; z < 8; ++z) { // try this 8 times781      worst = 0;782      q=k;783      for (i=0; i < 5000; ++i) {784        total += hmget(map, q);785        m0 = __rdtsc();786        total += hmget(map, q);787        m1 = __rdtsc();788        if (m1 - m0 > worst) {789          worst = m1-m0;790          worst_q = q - i;791        }792        //printf("%6llu ", m1-m0);793      }794      // now retry with the worst one, but find the shortest time for it795      worst = ~(unsigned __int64) 0;796      for (i=0; i < 4; ++i) {797        total += flush_cache();798        m0 = __rdtsc();799        total += hmget(map,worst_q);800        m1 = __rdtsc();801        if (m1-m0 < worst)802          worst = m1-m0;803      }804      times[z] = worst;805    }806807    // find the worst of those808    worst = 0;809    for (i=0; i < z; ++i)810      if (times[i] > worst)811        worst = times[i];812    mstats[1][j].start = 0;813    mstats[1][j].end = worst;814    mstats[1][j].table_size = k;815816    // expand table817    for (i=0; i < (1<<(j>>4)); ++i) {818      hmput(map, k,k+1);819      k += step;820    }821  }822  hmfree(map);823824  t1 = __rdtsc();825  measure();826  length = end();827  rdtsc_scale = length / (t1-t0) * 1000;828829  for (j=first_step+1; j <= last_step; ++j) {830    printf("%12.4f,%12d,0,1,0,1\n", (mstats[0][j].end - mstats[0][j].start) * rdtsc_scale, mstats[0][j].table_size);831  }832  for (j=first_step+1; j <= last_step; ++j) {833    printf("%12.4f,%12d,0,0,1,1\n", (mstats[1][j].end - mstats[1][j].start) * rdtsc_scale, mstats[1][j].table_size);834  }835  return total;836}837838int measure_uncached_lookup4(int step_log2)839{840  int total;841  double length;842  int i,j,k=0,q;843  int step = 1 << step_log2;844  unsigned __int64 t0,t1;845  struct { int key,value; } *map=NULL;846  double rdtsc_scale;847  begin();848  t0 = __rdtsc();849850  map = NULL;851  for (i=0; i < 128; ++i) {852    hmput(map,k,k+1);853    k += step;854  }855  for (j=first_step; j <= last_step; ++j) {856    mstats[0][j].start = __rdtsc();857    mstats[0][j].end = 0;858    for (q=i=0; i < 512; ++i) {859      if ((i & 3) == 0) {860        mstats[0][j].end += __rdtsc();861        total += flush_cache();862        mstats[0][j].start += __rdtsc();863      }864      total += hmget(map, q); // hit865      if (++q == k) q = 0;866    }867    mstats[0][j].end += __rdtsc();868    mstats[0][j].table_size = k;869    total += flush_cache();870    mstats[1][j].end = 0;871    mstats[1][j].start = __rdtsc();872    for (i=0; i < 512; ++i) {873      if ((i & 3) == 0) {874        mstats[1][j].end += __rdtsc();875        total += flush_cache();876        mstats[1][j].start += __rdtsc();877      }878      total += hmget(map, i+k); // miss879    }880    mstats[1][j].end += __rdtsc();881    mstats[1][j].table_size = k;882883    // expand table884    for (i=0; i < (1<<(j>>4)); ++i) {885      hmput(map, k,k+1);886      k += step;887    }888  }889  hmfree(map);890891  t1 = __rdtsc();892  measure();893  length = end();894  rdtsc_scale = length / (t1-t0) * 1000;895896  for (j=first_step; j <= last_step; ++j) {897    printf("%12.4f,%12d,0,512,0,4\n", (mstats[0][j].end - mstats[0][j].start) * rdtsc_scale, mstats[0][j].table_size);898  }899  for (j=first_step; j <= last_step; ++j) {900    printf("%12.4f,%12d,0,0,512,4\n", (mstats[1][j].end - mstats[1][j].start) * rdtsc_scale, mstats[1][j].table_size);901  }902  return total;903}904905906907908int main(int arg, char **argv)909{910  int n,s,w;911  double worst = 0;912913  printf("# size_t=%d,", (int) sizeof(size_t));914915// number of cache-lines916#ifdef STBDS_SMALL_BUCKET917  printf("cacheline=%d,", 1);918#else919  printf("cacheline=%d,", sizeof(size_t)==8 ? 2 : 1);920#endif921#ifdef STBDS_FLUSH_CACHE922  printf("%d,", (int) stbds_log2(STBDS_FLUSH_CACHE));923#else924  printf("0,");925#endif926#ifdef STBDS_BUCKET_START    // don't bother offseting differently within bucket for different hash values927  printf("STBDS_BUCKET_START,");928#else929  printf(",");930#endif931#ifdef STBDS_SIPHASH_2_4932  printf("STBDS_SIPHASH_2_4,");933#else934  printf(",");935#endif936  printf("\n");937938  measure_worst_lookup4_b(0);939  //measure_worst_lookup4_a(0);940  measure_average_lookup4(0);941  measure_uncached_lookup4(0);942  measure_build4(0);943  return 0;944945#if 0946  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);947  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);948  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);949  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);950  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);951#endif952953#if 0954  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);955  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);956  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);957  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);958  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);959960  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);961  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);962  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);963  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);964  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);965#endif966967  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);968  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);969  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);970  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);971972  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);973  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);974  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);975  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);976977978#if 1979  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);980  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);981  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);982  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);983984  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);985  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);986  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);987  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);988#endif989990  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);991  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);992  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);993  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);994  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);995996  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);997  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);998  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);999  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);1000  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);10011002  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);1003  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);1004  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);1005  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);1006  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);1007  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);1008  // 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 time10091010  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);1011  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);1012  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);1013  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);10141015  // search for bad intervals.. in practice this just seems to measure execution variance1016  for (s = 2; s < 64; ++s) {1017    begin(); for (n=0; n < 50; ++n) { build(200000,0,0,s); } end();1018    if (timer > worst) {1019      worst = timer;1020      w = s;1021    }1022  }1023  for (; s <= 1024; s *= 2) {1024    begin(); for (n=0; n < 50; ++n) { build(200000,0,0,s); } end();1025    if (timer > worst) {1026      worst = timer;1027      w = s;1028    }1029  }1030  printf("  // %7.2fms(%d)   : Worst time from inserting 200,000 items with spacing %d.\n", worst, w, w);10311032  return 0;1033}1034#endif