More C++ Idioms/Fast Pimpl
This page or section is an undeveloped draft or outline. You can help to develop the work, or you can ask for assistance in the project room. |
IntentEdit
Increase performance of Handle Body idiom.
Also Known AsEdit
MotivationEdit
Regular PIMPL idiom achieves "Compilation Firewall" by sacrificing performance. Fast PIMPL attempts to reduce the overhead of heap allocation and non-local memory access by composing the implementation object within the original interface object.
Solution and Sample CodeEdit
// Wrapper.hpp
struct Wrapper {
Wrapper();
~Wrapper();
std::aligned_storage_t<32, alignof(std::max_align_t)> storage;
struct Wrapped; // forward declaration
Wrapped* handle;
};
// Wrapper.cpp
struct Wrapper::Wrapped {
};
Wrapper::Wrapper() {
static_assert(sizeof(Wrapped) <= sizeof(this->storage) , "Object can't fit into local storage");
this->handle = new (&this->storage) Wrapped();
}
Wrapper::~Wrapper() {
handle->~Wrapped();
}
Note that handle to instance of Wrapped class is not required. To reduce memory footprint Wrapped class can be accessed by a helper function instead .
static Wrapper::Wrapped* get_wrapped(Wrapper* wrapper) {
// c++17 compatible
return std::launder(reinterpret_cast<Wrapper::Wrapped*>(&wrapper->storage));
}
Known UsesEdit
This pattern is frequently used in high performance or memory constrained environments when implementation is desired to be unseen or decoupled.
Related IdiomsEdit
- Handle Body (aka pimpl)