More C++ Idioms/Fast Pimpl

Intent edit

Increase performance of Handle Body idiom.

Also Known As edit

Motivation edit

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 Code edit

// Wrapper.hpp
struct 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() {

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 Uses edit

This pattern is frequently used in high performance or memory constrained environments when implementation is desired to be unseen or decoupled.

Related Idioms edit

References edit

The Fast Pimpl Idiom