Resources, copies, and strings in C++

A capture device delivers a video frame. One component inspects it during a call, and a queue keeps it for later processing. Giving both functions the same parameter type would hide an important difference in how long the frame must live.

Who keeps the frame?

If the capture component owns a frame and can give it to another component, these two signatures describe different jobs. The first function can read the object while it is alive. The second takes ownership and can keep it for a worker thread.

void inspect(const Frame& frame);
void enqueue(std::unique_ptr<Frame> frame);

auto frame = std::make_unique<Frame>();
inspect(*frame);
enqueue(std::move(frame));

When the parameter to enqueue is constructed, the unique_ptr move constructor transfers the pointer and leaves the original empty. The pixels stay in place. The expression std::move(frame) enables that operation through a cast. Which data moves or gets copied depends on the type and on the overload that receives the argument.

If a device SDK recycles its buffer as soon as the callback returns, storing its address does nothing to extend its lifetime. The queue needs a copy of the pixels or a way to retain and release the SDK buffer. An RAII object can release the buffer in its destructor, including when processing exits through an exception. A raw pointer can still serve as an observer when another component controls the object’s lifetime. C++ Core Guidelines on resources and ownership.

When to choose an implementation

A template can compile the same traversal for several pixel formats and give the compiler a chance to inline small operations in the loop. Each specialization may increase the size of the binary. A virtual interface lets the application choose its decoder when it opens a camera and keeps that choice out of the rest of the capture pipeline.

The two techniques can work together. A virtual call once per frame can select an implementation that uses a specialized routine to visit the pixels. To evaluate that boundary, measure both the time spent in the loop and how often decoder selection takes place.

When a string needs more storage

Suppose the queue also keeps a short camera label. Copying that label might only copy a few bytes inside the string object. In 2010, Howard Hinnant documented that libc++’s 64-bit implementation of std::string could hold up to 22 characters before allocating. The threshold depends on the library and its representation. Hinnant’s libc++ talk at the LLVM Developers’ Meeting.

Folly documents three storage strategies for fbstring. Each makes a different trade-off when a program copies or modifies the text.

The implementation can also take advantage of jemalloc extensions when that allocator is present. To evaluate it for a service, I would first record typical string lengths and how often strings get copied and modified. A workload dominated by short labels exercises a different path from one that copies and edits long messages. Folly’s documentation of fbstring storage strategies.