Since we are exploring the overhead of memory allocations and the ways to reduce it, the first question we must answer is how expensive a memory allocation is. After all, nobody wants to optimize something so fast that it needs no optimization. We can use Google Benchmark (or any other microbenchmark, if you prefer) to answer this question. The simplest benchmark to measure the cost of memory allocation might look like this:
void BM_malloc(benchmark::State& state) { for (auto _ : state) { void* p = malloc(64); benchmark::DoNotOptimize(p); } state.SetItemsProcessed(state.iterations());}BENCHMARK(BM_malloc_free);
The benchmark::DoNotOptimize wrapper prevents the compiler from optimizing away the unused variable. ...