allocPool: Try to avoid false sharing.
A benchmark on a 50 objects size with false sharing mitigations gives these values (for the initArray() function): - With mitigations: 5508 microseconds - Without mitigations: 9075 microseconds
This commit is contained in:
parent
b0cbb7455b
commit
3c5c9b4a97
@ -6,6 +6,7 @@
|
||||
#include <cstddef>
|
||||
#include <cstring>
|
||||
#include <mutex>
|
||||
#include <new>
|
||||
#include <thread>
|
||||
#include <unordered_map>
|
||||
#include <vector>
|
||||
@ -19,10 +20,10 @@ template<class T>
|
||||
requires std::default_initializable<T> && resetable<T>
|
||||
class allocPool {
|
||||
public:
|
||||
explicit allocPool(size_t defaultAllocNumbers = 1000)
|
||||
: vec(defaultAllocNumbers), pivot{defaultAllocNumbers} {
|
||||
explicit allocPool(size_t allocNumbers)
|
||||
: vec(allocNumbers), pivot{allocNumbers} {
|
||||
memset(&(vec[0]), 0, sizeof(vec[0]) * vec.size());
|
||||
initArray(defaultAllocNumbers);
|
||||
initArray(allocNumbers);
|
||||
}
|
||||
|
||||
~allocPool() {
|
||||
@ -57,18 +58,19 @@ private:
|
||||
std::unordered_map<T *, size_t> positionMap;
|
||||
size_t pivot;
|
||||
|
||||
void initArray(size_t amount) {
|
||||
const auto amountOfThreads{std::thread::hardware_concurrency()};
|
||||
void initArray(size_t arrSize) {
|
||||
size_t amountOfThreads{std::thread::hardware_concurrency()};
|
||||
assert(amountOfThreads);
|
||||
const auto amountPerThread{amount / amountOfThreads};
|
||||
size_t amountPerThread{arrSize / amountOfThreads};
|
||||
size_t minObjPerThread{std::hardware_destructive_interference_size / sizeof(void *)};
|
||||
|
||||
std::vector<std::thread> threads;
|
||||
threads.reserve(amountOfThreads);
|
||||
|
||||
// Using an allocPool, we estimate that we want to allocate a lot of objects, therefore
|
||||
// the amount per thread *should* be higher than a cache line. This means we should, for
|
||||
// the most part, avoid false sharing. In the case that it isn't, then the total amount
|
||||
// should be pretty low, therefore false sharing shouldn't matter.
|
||||
// We try to avoid false sharing by defining a minimum size.
|
||||
amountPerThread = minObjPerThread > amountPerThread ? minObjPerThread : amountPerThread;
|
||||
amountOfThreads = arrSize / amountPerThread;
|
||||
|
||||
for (size_t i{}; i < amountOfThreads; i++)
|
||||
threads.emplace_back(&allocPool::initObjects, this, i * amountPerThread, amountPerThread);
|
||||
|
||||
@ -76,7 +78,7 @@ private:
|
||||
t.join();
|
||||
|
||||
// Remainder
|
||||
initObjects(amount - (amount % amountOfThreads), amount % amountOfThreads);
|
||||
initObjects(arrSize - (arrSize % amountOfThreads), arrSize % amountOfThreads);
|
||||
}
|
||||
|
||||
void initObjects(size_t startIdx, size_t amount) {
|
||||
|
Loading…
Reference in New Issue
Block a user