2 * Benchmarks for hb_map_t operations.
4 #include "benchmark/benchmark.h"
10 void RandomMap(unsigned size, hb_map_t* out, hb_set_t* key_sample) {
13 unsigned sample_denom = 1;
15 sample_denom = size / 10000;
18 for (unsigned i = 0; i < size; i++) {
20 hb_codepoint_t next = rand();
21 if (hb_map_has (out, next)) continue;
23 hb_codepoint_t val = rand();
24 if (key_sample && val % sample_denom == 0)
25 hb_set_add (key_sample, next);
26 hb_map_set (out, next, val);
32 /* Insert a single value into map of varying sizes. */
33 static void BM_MapInsert(benchmark::State& state) {
34 unsigned map_size = state.range(0);
36 hb_map_t* original = hb_map_create ();
37 RandomMap(map_size, original, nullptr);
38 assert(hb_map_get_population(original) == map_size);
41 while (mask < map_size)
46 for (auto _ : state) {
47 // TODO(garretrieger): create a copy of the original map.
48 // Needs a hb_map_copy(..) in public api.
50 hb_map_set (original, needle++ & mask, 1);
53 hb_map_destroy(original);
55 BENCHMARK(BM_MapInsert)
56 ->Range(1 << 4, 1 << 20);
58 /* Single value lookup on map of various sizes where the key is not present. */
59 static void BM_MapLookupMiss(benchmark::State& state) {
60 unsigned map_size = state.range(0);
62 hb_map_t* original = hb_map_create ();
63 RandomMap(map_size, original, nullptr);
64 assert(hb_map_get_population(original) == map_size);
66 auto needle = map_size / 2;
68 for (auto _ : state) {
69 benchmark::DoNotOptimize(
70 hb_map_get (original, needle++));
73 hb_map_destroy(original);
75 BENCHMARK(BM_MapLookupMiss)
76 ->Range(1 << 4, 1 << 20); // Map size
78 /* Single value lookup on map of various sizes. */
79 static void BM_MapLookupHit(benchmark::State& state) {
80 unsigned map_size = state.range(0);
82 hb_map_t* original = hb_map_create ();
83 hb_set_t* key_set = hb_set_create ();
84 RandomMap(map_size, original, key_set);
85 assert(hb_map_get_population(original) == map_size);
87 unsigned num_keys = hb_set_get_population (key_set);
88 hb_codepoint_t* key_array =
89 (hb_codepoint_t*) calloc (num_keys, sizeof(hb_codepoint_t));
91 hb_codepoint_t cp = HB_SET_VALUE_INVALID;
93 while (hb_set_next (key_set, &cp))
97 for (auto _ : state) {
98 benchmark::DoNotOptimize(
99 hb_map_get (original, key_array[i++ % num_keys]));
102 hb_set_destroy (key_set);
104 hb_map_destroy(original);
106 BENCHMARK(BM_MapLookupHit)
107 ->Range(1 << 4, 1 << 20); // Map size