2022-04-20 21:33:04 +02:00
|
|
|
/*
|
|
|
|
* Benchmarks for hb_map_t operations.
|
|
|
|
*/
|
|
|
|
#include "benchmark/benchmark.h"
|
|
|
|
|
2022-05-28 00:57:00 +02:00
|
|
|
#include <cassert>
|
2022-04-20 21:33:04 +02:00
|
|
|
#include <cstdlib>
|
|
|
|
#include "hb.h"
|
|
|
|
|
|
|
|
void RandomMap(unsigned size, hb_map_t* out) {
|
|
|
|
hb_map_clear(out);
|
|
|
|
|
|
|
|
srand(size);
|
|
|
|
for (unsigned i = 0; i < size; i++) {
|
|
|
|
while (true) {
|
|
|
|
hb_codepoint_t next = rand();
|
|
|
|
if (hb_map_has (out, next)) continue;
|
|
|
|
|
|
|
|
hb_map_set (out, next, rand ());
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Insert a single value into map of varying sizes. */
|
|
|
|
static void BM_MapInsert(benchmark::State& state) {
|
|
|
|
unsigned map_size = state.range(0);
|
|
|
|
|
|
|
|
hb_map_t* original = hb_map_create ();
|
|
|
|
RandomMap(map_size, original);
|
|
|
|
assert(hb_map_get_population(original) == map_size);
|
|
|
|
|
2022-04-29 21:14:41 +02:00
|
|
|
auto needle = map_size / 2;
|
|
|
|
auto v = 0;
|
2022-04-20 21:33:04 +02:00
|
|
|
for (auto _ : state) {
|
|
|
|
// TODO(garretrieger): create a copy of the original map.
|
|
|
|
// Needs a hb_map_copy(..) in public api.
|
|
|
|
|
2022-04-29 21:14:41 +02:00
|
|
|
hb_map_set (original, needle++, v++);
|
2022-04-20 21:33:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
hb_map_destroy(original);
|
|
|
|
}
|
|
|
|
BENCHMARK(BM_MapInsert)
|
2022-04-20 23:45:37 +02:00
|
|
|
->Range(1 << 4, 1 << 20);
|
2022-04-20 21:33:04 +02:00
|
|
|
|
|
|
|
/* Single value lookup on map of various sizes. */
|
|
|
|
static void BM_MapLookup(benchmark::State& state) {
|
|
|
|
unsigned map_size = state.range(0);
|
|
|
|
|
|
|
|
hb_map_t* original = hb_map_create ();
|
|
|
|
RandomMap(map_size, original);
|
|
|
|
assert(hb_map_get_population(original) == map_size);
|
|
|
|
|
2022-04-29 21:14:41 +02:00
|
|
|
auto needle = map_size / 2;
|
|
|
|
|
2022-04-20 21:33:04 +02:00
|
|
|
for (auto _ : state) {
|
|
|
|
benchmark::DoNotOptimize(
|
2022-04-29 21:14:41 +02:00
|
|
|
hb_map_get (original, needle++));
|
2022-04-20 21:33:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
hb_map_destroy(original);
|
|
|
|
}
|
|
|
|
BENCHMARK(BM_MapLookup)
|
2022-04-20 23:45:37 +02:00
|
|
|
->Range(1 << 4, 1 << 20); // Map size
|
2022-04-20 21:33:04 +02:00
|
|
|
|
|
|
|
|
|
|
|
BENCHMARK_MAIN();
|