2018-01-02 18:12:05 +01:00
|
|
|
// Copyright (c) 2016-2017 The Bitcoin Core developers
|
2016-10-18 17:49:02 +02:00
|
|
|
// Distributed under the MIT software license, see the accompanying
|
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
|
2017-11-10 01:57:53 +01:00
|
|
|
#include <bench/bench.h>
|
2016-10-18 17:49:02 +02:00
|
|
|
|
2017-11-10 01:57:53 +01:00
|
|
|
#include <support/lockedpool.h>
|
2016-10-18 17:49:02 +02:00
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
#define ASIZE 2048
|
|
|
|
#define BITER 5000
|
|
|
|
#define MSIZE 2048
|
|
|
|
|
2016-11-10 08:00:05 +01:00
|
|
|
static void BenchLockedPool(benchmark::State& state)
|
2016-10-18 17:49:02 +02:00
|
|
|
{
|
|
|
|
void *synth_base = reinterpret_cast<void*>(0x08000000);
|
|
|
|
const size_t synth_size = 1024*1024;
|
|
|
|
Arena b(synth_base, synth_size, 16);
|
|
|
|
|
|
|
|
std::vector<void*> addr;
|
|
|
|
for (int x=0; x<ASIZE; ++x)
|
2017-06-21 21:10:00 +02:00
|
|
|
addr.push_back(nullptr);
|
2016-10-18 17:49:02 +02:00
|
|
|
uint32_t s = 0x12345678;
|
|
|
|
while (state.KeepRunning()) {
|
|
|
|
for (int x=0; x<BITER; ++x) {
|
|
|
|
int idx = s & (addr.size()-1);
|
|
|
|
if (s & 0x80000000) {
|
|
|
|
b.free(addr[idx]);
|
2017-06-21 21:10:00 +02:00
|
|
|
addr[idx] = nullptr;
|
2016-10-18 17:49:02 +02:00
|
|
|
} else if(!addr[idx]) {
|
|
|
|
addr[idx] = b.alloc((s >> 16) & (MSIZE-1));
|
|
|
|
}
|
|
|
|
bool lsb = s & 1;
|
|
|
|
s >>= 1;
|
|
|
|
if (lsb)
|
|
|
|
s ^= 0xf00f00f0; // LFSR period 0xf7ffffe0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (void *ptr: addr)
|
|
|
|
b.free(ptr);
|
|
|
|
addr.clear();
|
|
|
|
}
|
|
|
|
|
Use best-fit strategy in Arena, now O(log(n)) instead O(n)
This replaces the first-fit algorithm used in the Arena with a best-fit. According to "Dynamic Storage Allocation: A Survey and Critical Review", Wilson et. al. 1995, http://www.scs.stanford.edu/14wi-cs140/sched/readings/wilson.pdf, both startegies work well in practice.
The advantage of using best-fit is that we can switch the slow O(n) algorithm to O(log(n)) operations. Additionally, some previously O(log(n)) operations are now replaced with O(1) operations by using a hash map. The end effect is that the benchmark runs about 2.5 times faster on my machine:
old: BenchLockedPool, 5, 530, 5.25749, 0.00196938, 0.00199755, 0.00198172
new: BenchLockedPool, 5, 1300, 5.11313, 0.000781493, 0.000793314, 0.00078606
I've run all unit tests and benchmarks.
2017-12-29 11:36:11 +01:00
|
|
|
BENCHMARK(BenchLockedPool, 1300);
|