2024-03-01 14:21:52 -05:00
|
|
|
# allocPool - A simple & high performance object pool using modern C++
|
|
|
|
|
2024-03-01 15:19:52 -05:00
|
|
|
This is allocPool, a pool of objects in a single header file that allow you to
|
|
|
|
avoid expensive allocations during runtime. This preallocates objects in the
|
|
|
|
constructor (with threads) then offers you two functions: `getPtr()` and `returnPtr(ptr)`.
|
2024-03-01 14:21:52 -05:00
|
|
|
|
|
|
|
Using C++ concepts, we can use templates and require the class given to have a
|
|
|
|
default constructor and to have a .reset() function. It will be used to clean the
|
|
|
|
objects before giving them to another caller.
|
|
|
|
|
|
|
|
This pool uses a hashmap and a pivot to make returnPtr(ptr) extremely fast.
|
|
|
|
|
2024-03-01 15:19:52 -05:00
|
|
|
It will automatically grow when the max capacity is reached, though there will
|
2024-03-01 16:30:51 -05:00
|
|
|
be a performance penalty.
|
|
|
|
|
|
|
|
## Performance
|
|
|
|
With a simple stub class and a pool of 10000 objects, using the pool to take a pointer
|
|
|
|
and give it back takes 3 ms vs 19 ms when allocating and deallocating by hand.
|
|
|
|
```
|
|
|
|
class stub {
|
|
|
|
public:
|
|
|
|
stub() {
|
|
|
|
for (int j{}; j < 1000; j++) { i++; }
|
|
|
|
};
|
|
|
|
void reset() {}
|
|
|
|
|
|
|
|
private:
|
|
|
|
int i = 15;
|
|
|
|
};
|
|
|
|
```
|