More C++ Idioms/Fast Pimpl

From Wikibooks, open books for an open world
Jump to navigation Jump to search

Intent[edit | edit source]

Increase performance of Handle Body idiom.

Also Known As[edit | edit source]

Motivation[edit | edit source]

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 | edit source]

// 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 | edit source]

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

Related Idioms[edit | edit source]

References[edit | edit source]

The Fast Pimpl Idiom