Closed mrgreywater closed 8 years ago
see #28
Code before was something like that:
template <typename T>
class BlockPool {
public:
BlockPool() : blockSize(0) { }
BlockPool(std::size_t blockSize) {
reset(blockSize);
}
template <typename... Args>
T* construct(Args&&... args) {
if (block->size() == blockSize) {
assert(blockSize);
blocks.emplace_back();
block++;
block->reserve(blockSize);
}
block->emplace_back(std::forward<Args>(args)...);
return (T*)(&block->operator[](block->size()-1));
}
void reset(std::size_t new_size) {
blockSize = new_size;
blocks.clear();
blocks.emplace_back();
block = std::prev(blocks.end());
block->reserve(blockSize);
}
private:
typedef std::list<std::vector<T>> block_t;
std::size_t blockSize = 0;
block_t blocks;
typename block_t::iterator block;
};
But sadly it didn't perform as well as I'd hoped.
One possible addition would be to reuse allocated blocks for later polygon triangulations that use the same Earcut object, but considering the api will change in a way that this is not possible anymore (see #17), it would unnecessarily increase the size of the class.
Thanks!
Tried creating it using a
std::list<std::vector>
before, but it would require require removing the explicitly deleted move constructors of Node asstd::vector
needs it. Additionally the performance dropped about 20%.Instead it is now using
malloc/free
std::allocator<T>
with blocks of Nodes, and performance is on par (even a little faster) than boost.Before:
After