benchmark-map.cc 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. #include "hb-benchmark.hh"
  2. void RandomMap(unsigned size, hb_map_t* out, hb_set_t* key_sample) {
  3. hb_map_clear(out);
  4. unsigned sample_denom = 1;
  5. if (size > 10000)
  6. sample_denom = size / 10000;
  7. srand(size);
  8. for (unsigned i = 0; i < size; i++) {
  9. while (true) {
  10. hb_codepoint_t next = rand();
  11. if (hb_map_has (out, next)) continue;
  12. hb_codepoint_t val = rand();
  13. if (key_sample && val % sample_denom == 0)
  14. hb_set_add (key_sample, next);
  15. hb_map_set (out, next, val);
  16. break;
  17. }
  18. }
  19. }
  20. /* Insert a single value into map of varying sizes. */
  21. static void BM_MapInsert(benchmark::State& state) {
  22. unsigned map_size = state.range(0);
  23. hb_map_t* original = hb_map_create ();
  24. RandomMap(map_size, original, nullptr);
  25. assert(hb_map_get_population(original) == map_size);
  26. unsigned mask = 1;
  27. while (mask < map_size)
  28. mask <<= 1;
  29. mask--;
  30. auto needle = 0u;
  31. for (auto _ : state) {
  32. // TODO(garretrieger): create a copy of the original map.
  33. // Needs a hb_map_copy(..) in public api.
  34. hb_map_set (original, needle++ & mask, 1);
  35. }
  36. hb_map_destroy(original);
  37. }
  38. BENCHMARK(BM_MapInsert)
  39. ->Range(1 << 4, 1 << 20);
  40. /* Single value lookup on map of various sizes where the key is not present. */
  41. static void BM_MapLookupMiss(benchmark::State& state) {
  42. unsigned map_size = state.range(0);
  43. hb_map_t* original = hb_map_create ();
  44. RandomMap(map_size, original, nullptr);
  45. assert(hb_map_get_population(original) == map_size);
  46. auto needle = map_size / 2;
  47. for (auto _ : state) {
  48. benchmark::DoNotOptimize(
  49. hb_map_get (original, needle++));
  50. }
  51. hb_map_destroy(original);
  52. }
  53. BENCHMARK(BM_MapLookupMiss)
  54. ->Range(1 << 4, 1 << 20); // Map size
  55. /* Single value lookup on map of various sizes. */
  56. static void BM_MapLookupHit(benchmark::State& state) {
  57. unsigned map_size = state.range(0);
  58. hb_map_t* original = hb_map_create ();
  59. hb_set_t* key_set = hb_set_create ();
  60. RandomMap(map_size, original, key_set);
  61. assert(hb_map_get_population(original) == map_size);
  62. unsigned num_keys = hb_set_get_population (key_set);
  63. hb_codepoint_t* key_array =
  64. (hb_codepoint_t*) calloc (num_keys, sizeof(hb_codepoint_t));
  65. hb_codepoint_t cp = HB_SET_VALUE_INVALID;
  66. unsigned i = 0;
  67. while (hb_set_next (key_set, &cp))
  68. key_array[i++] = cp;
  69. i = 0;
  70. for (auto _ : state) {
  71. benchmark::DoNotOptimize(
  72. hb_map_get (original, key_array[i++ % num_keys]));
  73. }
  74. hb_set_destroy (key_set);
  75. free (key_array);
  76. hb_map_destroy(original);
  77. }
  78. BENCHMARK(BM_MapLookupHit)
  79. ->Range(1 << 4, 1 << 20); // Map size
  80. BENCHMARK_MAIN();