VLink 2.0.0
A high-performance communication middleware
Loading...
Searching...
No Matches
fast_stream.h File Reference

A high-performance std::ostream backed by a resizable std::string buffer. More...

#include <ostream>
#include <string>
#include <string_view>
#include "./macros.h"
Include dependency graph for fast_stream.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

 High-performance std::ostream with an embedded resizable string buffer. More...

Namespaces

Detailed Description

A high-performance std::ostream backed by a resizable std::string buffer.

FastStream is a specialised output stream designed for zero-copy, low-latency log formatting inside the VLink Logger. It inherits from std::ostream, so any standard stream manipulator or operator<< overload works transparently.

Key design points:

  • Backed by an internal StringBuf that stores data in a std::string (avoids heap fragmentation for short messages via a pre-allocated capacity).
  • take_view() returns a std::string_view into the internal buffer, enabling zero-copy hand-off to the logger sink without an extra copy.
  • write_raw() bypasses std::ostream formatting and writes bytes directly into the underlying buffer, suitable for pre-formatted C-strings.
  • Default capacity is 256 bytes; the buffer grows automatically in 8 KiB increments up to any size. shrink_to_fit() can reclaim excess memory after an unusually long message.
Note
  • FastStream is not thread-safe. Each thread should own its own instance, which is the pattern used by the Logger (thread_local FastStream).
  • Calling take_view() invalidates any previously taken view after the next write or explicit reset(). Do not hold views across multiple log calls.
Example
stream << "sensor_id=" << 42 << " value=" << 3.14;
std::string_view view = stream.take_view(); // view valid until next write
write_to_sink(view);